diff options
author | jfrijters <jfrijters> | 2013-08-12 11:55:55 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2013-08-12 11:55:55 +0400 |
commit | 5befcded8aa819499e9033140f628c306d436e5f (patch) | |
tree | b220f4fca974842f1835c33a5defa82992ec5606 /openjdk/java | |
parent | a800686029a45aad051c4a59d6d00633de8e7c79 (diff) |
Switched from @HasCallerID to @CallerSensitive and merged CallerSenstive related 7u40 changes.
Diffstat (limited to 'openjdk/java')
-rw-r--r-- | openjdk/java/io/ObjectStreamClass.java | 20 | ||||
-rw-r--r-- | openjdk/java/lang/Class.java | 181 | ||||
-rw-r--r-- | openjdk/java/lang/ClassLoader.java | 57 | ||||
-rw-r--r-- | openjdk/java/lang/System.java | 11 | ||||
-rw-r--r-- | openjdk/java/lang/Thread.java | 37 | ||||
-rw-r--r-- | openjdk/java/lang/invoke/MethodHandles.java | 14 | ||||
-rw-r--r-- | openjdk/java/lang/reflect/Constructor.java | 6 | ||||
-rw-r--r-- | openjdk/java/lang/reflect/Field.java | 186 | ||||
-rw-r--r-- | openjdk/java/lang/reflect/Method.java | 6 | ||||
-rw-r--r-- | openjdk/java/security/AccessController.java | 48 | ||||
-rw-r--r-- | openjdk/java/sql/DriverManager.java | 78 | ||||
-rw-r--r-- | openjdk/java/util/ResourceBundle.java | 29 |
12 files changed, 410 insertions, 263 deletions
diff --git a/openjdk/java/io/ObjectStreamClass.java b/openjdk/java/io/ObjectStreamClass.java index b448ab10..97489b1e 100644 --- a/openjdk/java/io/ObjectStreamClass.java +++ b/openjdk/java/io/ObjectStreamClass.java @@ -50,7 +50,10 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import sun.misc.Unsafe; +import sun.reflect.CallerSensitive; +import sun.reflect.Reflection; import sun.reflect.ReflectionFactory; +import sun.reflect.misc.ReflectUtil; /** * Serialization's descriptor for classes. It contains the name and @@ -262,7 +265,17 @@ public class ObjectStreamClass implements Serializable { * * @return the <code>Class</code> instance that this descriptor represents */ + @CallerSensitive public Class<?> forClass() { + if (cl == null) { + return null; + } + if (System.getSecurityManager() != null) { + Class<?> caller = Reflection.getCallerClass(); + if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(), cl.getClassLoader())) { + ReflectUtil.checkPackageAccess(cl); + } + } return cl; } @@ -1156,7 +1169,14 @@ public class ObjectStreamClass implements Serializable { end = end.getSuperclass(); } + HashSet<String> oscNames = new HashSet<>(3); + for (ObjectStreamClass d = this; d != null; d = d.superDesc) { + if (oscNames.contains(d.name)) { + throw new InvalidClassException("Circular reference."); + } else { + oscNames.add(d.name); + } // search up inheritance hierarchy for class with matching name String searchName = (d.cl != null) ? d.cl.getName() : d.name; diff --git a/openjdk/java/lang/Class.java b/openjdk/java/lang/Class.java index 20f35534..bbc1d241 100644 --- a/openjdk/java/lang/Class.java +++ b/openjdk/java/lang/Class.java @@ -53,6 +53,7 @@ import java.util.Set; import java.util.Map; import java.util.HashMap; import sun.misc.Unsafe; +import sun.reflect.CallerSensitive; import sun.reflect.ConstantPool; import sun.reflect.Reflection; import sun.reflect.ReflectionFactory; @@ -65,7 +66,9 @@ import sun.reflect.generics.repository.ConstructorRepository; import sun.reflect.generics.scope.ClassScope; import sun.security.util.SecurityConstants; import java.lang.annotation.Annotation; -import sun.reflect.annotation.AnnotationType; +import java.lang.reflect.Proxy; +import sun.reflect.annotation.*; +import sun.reflect.misc.ReflectUtil; import cli.System.Runtime.Serialization.IObjectReference; import cli.System.Runtime.Serialization.SerializationException; import cli.System.Runtime.Serialization.SerializationInfo; @@ -281,10 +284,11 @@ public final * by this method fails * @exception ClassNotFoundException if the class cannot be located */ - @ikvm.internal.HasCallerID + @CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException { - return forName0(className, true, ClassLoader.getCallerClassLoader()); + return forName0(className, true, + ClassLoader.getClassLoader(Reflection.getCallerClass())); } @@ -348,7 +352,7 @@ public final * @see java.lang.ClassLoader * @since 1.2 */ - @ikvm.internal.HasCallerID + @CallerSensitive public static Class<?> forName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException @@ -356,7 +360,7 @@ public final if (loader == null) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { - ClassLoader ccl = ClassLoader.getCallerClassLoader(); + ClassLoader ccl = ClassLoader.getClassLoader(Reflection.getCallerClass()); if (ccl != null) { sm.checkPermission( SecurityConstants.GET_CLASSLOADER_PERMISSION); @@ -418,19 +422,14 @@ public final * </ul> * */ - @ikvm.internal.HasCallerID + @CallerSensitive public T newInstance() throws InstantiationException, IllegalAccessException { if (System.getSecurityManager() != null) { - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); } - return newInstance0(ikvm.internal.CallerID.getCallerID()); - } - private T newInstance0(ikvm.internal.CallerID callerID) - throws InstantiationException, IllegalAccessException - { // NOTE: the following code may not be strictly correct under // the current Java memory model. @@ -464,7 +463,7 @@ public final // Security check (same as in java.lang.reflect.Constructor) int modifiers = tmpConstructor.getModifiers(); if (!Reflection.quickCheckMemberAccess(this, modifiers)) { - Class<?> caller = callerID.getCallerClass(); + Class<?> caller = Reflection.getCallerClass(); if (newInstanceCallerCache != caller) { Reflection.ensureMemberAccess(caller, this, null, modifiers); newInstanceCallerCache = caller; @@ -705,17 +704,14 @@ public final * @see SecurityManager#checkPermission * @see java.lang.RuntimePermission */ - @ikvm.internal.HasCallerID + @CallerSensitive public ClassLoader getClassLoader() { ClassLoader cl = getClassLoader0(); if (cl == null) return null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { - ClassLoader ccl = ClassLoader.getCallerClassLoader(); - if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) { - sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); - } + ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass()); } return cl; } @@ -996,6 +992,7 @@ public final * that class is a local or anonymous class; otherwise {@code null}. * @since 1.5 */ + @CallerSensitive public Method getEnclosingMethod() { EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); @@ -1017,13 +1014,22 @@ public final for(int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]); + // Perform access check + Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); + // be very careful not to change the stack depth of this + // checkMemberAccess call for security reasons + // see java.lang.SecurityManager.checkMemberAccess + // + // Note that we need to do this on the enclosing class + enclosingCandidate.checkMemberAccess(Member.DECLARED, + Reflection.getCallerClass(), true); /* * Loop over all declared methods; match method name, * number of and type of parameters, *and* return * type. Matching return type is also necessary * because of covariant returns, etc. */ - for(Method m: enclosingInfo.getEnclosingClass().getDeclaredMethods()) { + for(Method m: enclosingCandidate.getDeclaredMethods()) { if (m.getName().equals(enclosingInfo.getName()) ) { Class<?>[] candidateParamClasses = m.getParameterTypes(); if (candidateParamClasses.length == parameterClasses.length) { @@ -1124,6 +1130,7 @@ public final * that class is a local or anonymous class; otherwise {@code null}. * @since 1.5 */ + @CallerSensitive public Constructor<?> getEnclosingConstructor() { EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); @@ -1144,11 +1151,20 @@ public final for(int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]); + // Perform access check + Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass(); + // be very careful not to change the stack depth of this + // checkMemberAccess call for security reasons + // see java.lang.SecurityManager.checkMemberAccess + // + // Note that we need to do this on the enclosing class + enclosingCandidate.checkMemberAccess(Member.DECLARED, + Reflection.getCallerClass(), true); /* * Loop over all declared constructors; match number * of and type of parameters. */ - for(Constructor<?> c: enclosingInfo.getEnclosingClass().getDeclaredConstructors()) { + for(Constructor<?> c: enclosingCandidate.getDeclaredConstructors()) { Class<?>[] candidateParamClasses = c.getParameterTypes(); if (candidateParamClasses.length == parameterClasses.length) { boolean matches = true; @@ -1190,6 +1206,7 @@ public final * @return the immediately enclosing class of the underlying class * @since 1.5 */ + @CallerSensitive public Class<?> getEnclosingClass() { // There are five kinds of classes (or interfaces): // a) Top level classes @@ -1203,18 +1220,24 @@ public final // attribute if and only if it is a local class or an // anonymous class. EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo(); + Class<?> enclosingCandidate; if (enclosingInfo == null) { // This is a top level or a nested class or an inner class (a, b, or c) - return getDeclaringClass(); + enclosingCandidate = getDeclaringClass(); } else { Class<?> enclosingClass = enclosingInfo.getEnclosingClass(); // This is a local class or an anonymous class (d or e) if (enclosingClass == this || enclosingClass == null) throw new InternalError("Malformed enclosing method information"); else - return enclosingClass; + enclosingCandidate = enclosingClass; } + + if (enclosingCandidate != null) + enclosingCandidate.checkPackageAccess( + ClassLoader.getClassLoader(Reflection.getCallerClass()), true); + return enclosingCandidate; } /** @@ -1397,12 +1420,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Class<?>[] getClasses() { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); // Privileged so this implementation can look at DECLARED classes, // something the caller might not have privilege to do. The code here @@ -1473,12 +1496,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Field[] getFields() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); return copyFields(privateGetPublicFields(null)); } @@ -1525,12 +1548,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Method[] getMethods() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); return copyMethods(privateGetPublicMethods()); } @@ -1575,12 +1598,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Constructor<?>[] getConstructors() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); return copyConstructors(privateGetDeclaredConstructors(true)); } @@ -1634,13 +1657,13 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Field getField(String name) throws NoSuchFieldException, SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); Field field = getField0(name); if (field == null) { throw new NoSuchFieldException(name); @@ -1720,13 +1743,13 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); Method method = getMethod0(name, parameterTypes); if (method == null) { throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); @@ -1775,13 +1798,13 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true); return getConstructor0(parameterTypes, Member.PUBLIC); } @@ -1819,12 +1842,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Class<?>[] getDeclaredClasses() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false); return getDeclaredClasses0(); } @@ -1864,12 +1887,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Field[] getDeclaredFields() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); return copyFields(privateGetDeclaredFields(false)); } @@ -1913,12 +1936,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Method[] getDeclaredMethods() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); return copyMethods(privateGetDeclaredMethods(false)); } @@ -1959,12 +1982,12 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Constructor<?>[] getDeclaredConstructors() throws SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); return copyConstructors(privateGetDeclaredConstructors(false)); } @@ -2003,13 +2026,13 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); Field field = searchFields(privateGetDeclaredFields(false), name); if (field == null) { throw new NoSuchFieldException(name); @@ -2059,13 +2082,13 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); if (method == null) { throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes)); @@ -2110,13 +2133,13 @@ public final * * @since JDK1.1 */ - @ikvm.internal.HasCallerID + @CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException { // be very careful not to change the stack depth of this // checkMemberAccess call for security reasons // see java.lang.SecurityManager.checkMemberAccess - checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader()); + checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true); return getConstructor0(parameterTypes, Member.DECLARED); } @@ -2274,31 +2297,69 @@ public final */ static native Class getPrimitiveClass(String name); + private static boolean isCheckMemberAccessOverridden(SecurityManager smgr) { + if (smgr.getClass() == SecurityManager.class) return false; + + Class<?>[] paramTypes = new Class<?>[] {Class.class, int.class}; + return smgr.getClass().getMethod0("checkMemberAccess", paramTypes). + getDeclaringClass() != SecurityManager.class; + } + /* * Check if client is allowed to access members. If access is denied, * throw a SecurityException. * - * Be very careful not to change the stack depth of this checkMemberAccess - * call for security reasons. - * See java.lang.SecurityManager.checkMemberAccess. + * This method also enforces package access. * * <p> Default policy: allow all clients access with normal Java access * control. */ - private void checkMemberAccess(int which, ClassLoader ccl) { - SecurityManager s = System.getSecurityManager(); + private void checkMemberAccess(int which, Class<?> caller, boolean checkProxyInterfaces) { + final SecurityManager s = System.getSecurityManager(); if (s != null) { - s.checkMemberAccess(this, which); - ClassLoader cl = getClassLoader0(); - if ((ccl != null) && (ccl != cl) && - ((cl == null) || !cl.isAncestor(ccl))) { + final ClassLoader ccl = ClassLoader.getClassLoader(caller); + final ClassLoader cl = getClassLoader0(); + if (!isCheckMemberAccessOverridden(s)) { + // Inlined SecurityManager.checkMemberAccess + if (which != Member.PUBLIC) { + if (ccl != cl) { + s.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); + } + } + } else { + // Don't refactor; otherwise break the stack depth for + // checkMemberAccess of subclasses of SecurityManager as specified. + s.checkMemberAccess(this, which); + } + this.checkPackageAccess(ccl, checkProxyInterfaces); + } + } + + /* + * Checks if a client loaded in ClassLoader ccl is allowed to access this + * class under the current package access policy. If access is denied, + * throw a SecurityException. + */ + private void checkPackageAccess(final ClassLoader ccl, boolean checkProxyInterfaces) { + final SecurityManager s = System.getSecurityManager(); + if (s != null) { + final ClassLoader cl = getClassLoader0(); + if (ReflectUtil.needsPackageAccessCheck(ccl, cl)) { String name = this.getName(); int i = name.lastIndexOf('.'); if (i != -1) { - s.checkPackageAccess(name.substring(0, i)); + // skip the package access check on a proxy class in default proxy package + String pkg = name.substring(0, i); + if (!Proxy.isProxyClass(this) || ReflectUtil.isNonPublicProxyClass(this)) { + s.checkPackageAccess(pkg); + } } } + // check package access on the proxy interfaces + if (checkProxyInterfaces && Proxy.isProxyClass(this)) { + ReflectUtil.checkProxyPackageAccess(ccl, this.getInterfaces()); + } } } diff --git a/openjdk/java/lang/ClassLoader.java b/openjdk/java/lang/ClassLoader.java index 8162ba81..57c1ee3e 100644 --- a/openjdk/java/lang/ClassLoader.java +++ b/openjdk/java/lang/ClassLoader.java @@ -56,6 +56,7 @@ import sun.misc.CompoundEnumeration; import sun.misc.Resource; import sun.misc.URLClassPath; import sun.misc.VM; +import sun.reflect.CallerSensitive; import sun.reflect.Reflection; import sun.security.util.SecurityConstants; @@ -1256,9 +1257,11 @@ public abstract class ClassLoader { * * @since 1.7 */ - @ikvm.internal.HasCallerID + @CallerSensitive protected static boolean registerAsParallelCapable() { - return ParallelLoaders.register(ikvm.internal.CallerID.getCallerID().getCallerClass()); + Class<? extends ClassLoader> callerClass = + Reflection.getCallerClass().asSubclass(ClassLoader.class); + return ParallelLoaders.register(callerClass); } /** @@ -1402,15 +1405,13 @@ public abstract class ClassLoader { * * @since 1.2 */ + @CallerSensitive public final ClassLoader getParent() { if (parent == null) return null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { - ClassLoader ccl = getCallerClassLoader(); - if (ccl != null && !isAncestor(ccl)) { - sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); - } + checkClassLoaderPermission(parent, Reflection.getCallerClass()); } return parent; } @@ -1470,7 +1471,7 @@ public abstract class ClassLoader { * * @revised 1.4 */ - @ikvm.internal.HasCallerID + @CallerSensitive public static ClassLoader getSystemClassLoader() { initSystemClassLoader(); if (scl == null) { @@ -1478,10 +1479,7 @@ public abstract class ClassLoader { } SecurityManager sm = System.getSecurityManager(); if (sm != null) { - ClassLoader ccl = getCallerClassLoader(); - if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) { - sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); - } + checkClassLoaderPermission(scl, Reflection.getCallerClass()); } return scl; } @@ -1529,13 +1527,25 @@ public abstract class ClassLoader { return false; } - // Returns the invoker's class loader, or null if none. - // NOTE: This must always be invoked when there is exactly one intervening - // frame from the core libraries on the stack between this method's - // invocation and the desired invoker. - static ClassLoader getCallerClassLoader() { - // NOTE use of more generic Reflection.getCallerClass() - Class caller = Reflection.getCallerClass(3); + // Tests if class loader access requires "getClassLoader" permission + // check. A class loader 'from' can access class loader 'to' if + // class loader 'from' is same as class loader 'to' or an ancestor + // of 'to'. The class loader in a system domain can access + // any class loader. + private static boolean needsClassLoaderPermissionCheck(ClassLoader from, + ClassLoader to) + { + if (from == to) + return false; + + if (from == null) + return false; + + return !to.isAncestor(from); + } + + // Returns the class's class loader, or null if none. + static ClassLoader getClassLoader(Class<?> caller) { // This can be null if the VM is requesting it if (caller == null) { return null; @@ -1544,6 +1554,17 @@ public abstract class ClassLoader { return caller.getClassLoader0(); } + static void checkClassLoaderPermission(ClassLoader cl, Class<?> caller) { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + // caller can be null if the VM is requesting it + ClassLoader ccl = getClassLoader(caller); + if (needsClassLoaderPermissionCheck(ccl, cl)) { + sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); + } + } + } + // The class loader for the system // @GuardedBy("ClassLoader.class") private static ClassLoader scl; diff --git a/openjdk/java/lang/System.java b/openjdk/java/lang/System.java index e6f4d3cc..e74187b9 100644 --- a/openjdk/java/lang/System.java +++ b/openjdk/java/lang/System.java @@ -33,7 +33,7 @@ import java.security.PrivilegedAction; import java.security.AllPermission; import java.nio.channels.Channel; import java.nio.channels.spi.SelectorProvider; - +import sun.reflect.CallerSensitive; import sun.reflect.Reflection; import sun.security.util.SecurityConstants; @@ -1105,9 +1105,9 @@ public final class System { * @see java.lang.Runtime#load(java.lang.String) * @see java.lang.SecurityManager#checkLink(java.lang.String) */ - @ikvm.internal.HasCallerID + @CallerSensitive public static void load(String filename) { - Runtime.getRuntime().load0(Reflection.getCallerClass(2), filename); + Runtime.getRuntime().load0(Reflection.getCallerClass(), filename); } /** @@ -1131,9 +1131,9 @@ public final class System { * @see java.lang.Runtime#loadLibrary(java.lang.String) * @see java.lang.SecurityManager#checkLink(java.lang.String) */ - @ikvm.internal.HasCallerID + @CallerSensitive public static void loadLibrary(String libname) { - Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(2), libname); + Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(), libname); } /** @@ -1160,7 +1160,6 @@ public final class System { return "lib" + libname + ".so"; } } - /* returns the class of the caller. */ static Class<?> getCallerClass() { // NOTE use of more generic Reflection.getCallerClass() diff --git a/openjdk/java/lang/Thread.java b/openjdk/java/lang/Thread.java index 3de50ff7..e8f57d06 100644 --- a/openjdk/java/lang/Thread.java +++ b/openjdk/java/lang/Thread.java @@ -37,6 +37,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.locks.LockSupport; import sun.nio.ch.Interruptible; +import sun.reflect.CallerSensitive; +import sun.reflect.Reflection; import sun.security.util.SecurityConstants; @@ -169,7 +171,7 @@ class Thread implements Runnable { private cli.System.Threading.Thread nativeThread; private Throwable stillborn; private boolean running; // used only for coordination with stop0(), is never set to false - private boolean interruptPending; + private volatile boolean interruptPending; private volatile boolean nativeInterruptPending; private volatile boolean interruptableWait; private boolean timedWait; @@ -1228,7 +1230,12 @@ class Thread implements Runnable { * @revised 6.0 */ public static boolean interrupted() { - return currentThread().isInterrupted(true); + Thread current = currentThread(); + if (!current.interruptPending) { + return false; + } + current.interruptPending = false; + return true; } /** @@ -1245,22 +1252,7 @@ class Thread implements Runnable { * @revised 6.0 */ public boolean isInterrupted() { - return isInterrupted(false); - } - - /** - * Tests if some Thread has been interrupted. The interrupted state - * is reset or not based on the value of ClearInterrupted that is - * passed. - */ - private boolean isInterrupted(boolean ClearInterrupted) { - synchronized (lock) { - boolean b = interruptPending; - if (ClearInterrupted) { - interruptPending = false; - } - return b; - } + return interruptPending; } /** @@ -1723,19 +1715,18 @@ class Thread implements Runnable { * * @since 1.2 */ + @CallerSensitive public ClassLoader getContextClassLoader() { if (contextClassLoader == ClassLoader.DUMMY) { contextClassLoader = ClassLoader.getSystemClassLoader(); } if (contextClassLoader == null) return null; + SecurityManager sm = System.getSecurityManager(); if (sm != null) { - ClassLoader ccl = ClassLoader.getCallerClassLoader(); - if (ccl != null && ccl != contextClassLoader && - !contextClassLoader.isAncestor(ccl)) { - sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); - } + ClassLoader.checkClassLoaderPermission(contextClassLoader, + Reflection.getCallerClass()); } return contextClassLoader; } diff --git a/openjdk/java/lang/invoke/MethodHandles.java b/openjdk/java/lang/invoke/MethodHandles.java index 710d6ab0..6f0db408 100644 --- a/openjdk/java/lang/invoke/MethodHandles.java +++ b/openjdk/java/lang/invoke/MethodHandles.java @@ -33,11 +33,10 @@ import sun.invoke.util.Wrapper; import java.util.List; import java.util.ArrayList; import java.util.Arrays; +import sun.reflect.CallerSensitive; import sun.reflect.Reflection; import static java.lang.invoke.MethodHandleStatics.*; import static java.lang.invoke.MethodHandleNatives.Constants.*; -import ikvm.internal.CallerID; -import ikvm.internal.HasCallerID; /** * This class consists exclusively of static methods that operate on or return @@ -68,9 +67,9 @@ public class MethodHandles { * This lookup object is a <em>capability</em> which may be delegated to trusted agents. * Do not store it in place where untrusted code can access it. */ - @HasCallerID + @CallerSensitive public static Lookup lookup() { - return new Lookup(CallerID.getCallerID()); + return new Lookup(Reflection.getCallerClass()); } /** @@ -412,14 +411,9 @@ public class MethodHandles { * Also, don't make it private, lest javac interpose * an access$N method. */ - Lookup(CallerID caller) { - this(caller.getCallerClass(), ALL_MODES); - // make sure we haven't accidentally picked up a privileged class: - checkUnprivilegedlookupClass(lookupClass); - } - Lookup(Class<?> lookupClass) { this(lookupClass, ALL_MODES); + checkUnprivilegedlookupClass(lookupClass); } private Lookup(Class<?> lookupClass, int allowedModes) { diff --git a/openjdk/java/lang/reflect/Constructor.java b/openjdk/java/lang/reflect/Constructor.java index e62e8772..b13d30a5 100644 --- a/openjdk/java/lang/reflect/Constructor.java +++ b/openjdk/java/lang/reflect/Constructor.java @@ -25,6 +25,7 @@ package java.lang.reflect; +import sun.reflect.CallerSensitive; import sun.reflect.ConstructorAccessor; import sun.reflect.Reflection; import sun.reflect.generics.repository.ConstructorRepository; @@ -501,15 +502,14 @@ public final * @exception ExceptionInInitializerError if the initialization provoked * by this method fails. */ - @ikvm.internal.HasCallerID + @CallerSensitive public T newInstance(Object ... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { - Class<?> caller = Reflection.getCallerClass(2); - + Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, null, modifiers); } } diff --git a/openjdk/java/lang/reflect/Field.java b/openjdk/java/lang/reflect/Field.java index c796d20a..b2cb46b3 100644 --- a/openjdk/java/lang/reflect/Field.java +++ b/openjdk/java/lang/reflect/Field.java @@ -25,6 +25,7 @@ package java.lang.reflect; +import sun.reflect.CallerSensitive; import sun.reflect.FieldAccessor; import sun.reflect.Reflection; import sun.reflect.generics.repository.FieldRepository; @@ -34,7 +35,6 @@ import sun.reflect.generics.scope.ClassScope; import java.lang.annotation.Annotation; import java.util.Map; import sun.reflect.annotation.AnnotationParser; -import ikvm.internal.CallerID; /** @@ -365,11 +365,16 @@ class Field extends AccessibleObject implements Member { * @exception ExceptionInInitializerError if the initialization provoked * by this method fails. */ - @ikvm.internal.HasCallerID + @CallerSensitive public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).get(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).get(obj); } /** @@ -394,11 +399,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getBoolean(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getBoolean(obj); } /** @@ -423,11 +433,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public byte getByte(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getByte(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getByte(obj); } /** @@ -454,11 +469,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getChar(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getChar(obj); } /** @@ -485,11 +505,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public short getShort(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getShort(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getShort(obj); } /** @@ -516,11 +541,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getInt(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getInt(obj); } /** @@ -547,11 +577,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public long getLong(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getLong(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getLong(obj); } /** @@ -578,11 +613,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getFloat(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getFloat(obj); } /** @@ -609,11 +649,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#get */ - @ikvm.internal.HasCallerID + @CallerSensitive public double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException { - return getFieldAccessor(obj, CallerID.getCallerID()).getDouble(obj); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + return getFieldAccessor(obj).getDouble(obj); } /** @@ -682,11 +727,16 @@ class Field extends AccessibleObject implements Member { * @exception ExceptionInInitializerError if the initialization provoked * by this method fails. */ - @ikvm.internal.HasCallerID + @CallerSensitive public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).set(obj, value); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).set(obj, value); } /** @@ -713,11 +763,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setBoolean(Object obj, boolean z) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setBoolean(obj, z); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setBoolean(obj, z); } /** @@ -744,11 +799,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setByte(Object obj, byte b) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setByte(obj, b); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setByte(obj, b); } /** @@ -775,11 +835,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setChar(Object obj, char c) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setChar(obj, c); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setChar(obj, c); } /** @@ -806,11 +871,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setShort(Object obj, short s) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setShort(obj, s); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setShort(obj, s); } /** @@ -837,11 +907,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setInt(obj, i); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setInt(obj, i); } /** @@ -868,11 +943,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setLong(Object obj, long l) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setLong(obj, l); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setLong(obj, l); } /** @@ -899,11 +979,16 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setFloat(Object obj, float f) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setFloat(obj, f); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setFloat(obj, f); } /** @@ -930,21 +1015,25 @@ class Field extends AccessibleObject implements Member { * by this method fails. * @see Field#set */ - @ikvm.internal.HasCallerID + @CallerSensitive public void setDouble(Object obj, double d) throws IllegalArgumentException, IllegalAccessException { - getFieldAccessor(obj, CallerID.getCallerID()).setDouble(obj, d); + if (!override) { + if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { + checkAccess(Reflection.getCallerClass(), clazz, obj, modifiers); + } + } + getFieldAccessor(obj).setDouble(obj, d); } - // Convenience routine which performs security checks - private FieldAccessor getFieldAccessor(Object obj, CallerID callerID) + // security check is done before calling this method + private FieldAccessor getFieldAccessor(Object obj) throws IllegalAccessException { - doSecurityCheck(obj, callerID); boolean ov = override; - FieldAccessor a = (ov)? overrideFieldAccessor : fieldAccessor; - return (a != null)? a : acquireFieldAccessor(ov); + FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor; + return (a != null) ? a : acquireFieldAccessor(ov); } // NOTE that there is no synchronization used here. It is correct @@ -989,19 +1078,6 @@ class Field extends AccessibleObject implements Member { } } - // NOTE: be very careful if you change the stack depth of this - // routine. The depth of the "getCallerClass" call is hardwired so - // that the compiler can have an easier time if this gets inlined. - private void doSecurityCheck(Object obj, CallerID callerID) throws IllegalAccessException { - if (!override) { - if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { - Class<?> caller = callerID.getCallerClass(); - - checkAccess(caller, clazz, obj, modifiers); - } - } - } - /* * Utility routine to paper over array type names */ diff --git a/openjdk/java/lang/reflect/Method.java b/openjdk/java/lang/reflect/Method.java index b1548d6b..dce789a4 100644 --- a/openjdk/java/lang/reflect/Method.java +++ b/openjdk/java/lang/reflect/Method.java @@ -26,6 +26,7 @@ package java.lang.reflect; import ikvm.internal.CallerID; +import sun.reflect.CallerSensitive; import sun.reflect.MethodAccessor; import sun.reflect.Reflection; import sun.reflect.generics.repository.MethodRepository; @@ -578,15 +579,14 @@ public final * @exception ExceptionInInitializerError if the initialization * provoked by this method fails. */ - @ikvm.internal.HasCallerID + @CallerSensitive public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { - Class<?> caller = CallerID.getCallerID().getCallerClass(); - + Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } diff --git a/openjdk/java/security/AccessController.java b/openjdk/java/security/AccessController.java index 60b7722a..19d4af96 100644 --- a/openjdk/java/security/AccessController.java +++ b/openjdk/java/security/AccessController.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -28,6 +28,8 @@ package java.security; import ikvm.internal.CallerID; import sun.misc.Unsafe; import sun.security.util.Debug; +import sun.reflect.CallerSensitive; +import sun.reflect.Reflection; /** * <p> The AccessController class is used for access control operations @@ -317,7 +319,7 @@ public final class AccessController { * @see java.security.DomainCombiner */ - @ikvm.internal.HasCallerID + @CallerSensitive public static <T> T doPrivileged(PrivilegedAction<T> action) { return (T)doPrivileged(action, null, CallerID.getCallerID()); } @@ -344,14 +346,14 @@ public final class AccessController { * * @since 1.6 */ + @CallerSensitive public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) { - - DomainCombiner dc = null; AccessControlContext acc = getStackAccessControlContext(); - if (acc == null || (dc = acc.getAssignedCombiner()) == null) { + if (acc == null) { return AccessController.doPrivileged(action); } - return AccessController.doPrivileged(action, preserveCombiner(dc)); + DomainCombiner dc = acc.getAssignedCombiner(); + return AccessController.doPrivileged(action, preserveCombiner(dc, Reflection.getCallerClass())); } @@ -382,7 +384,7 @@ public final class AccessController { * @see #doPrivileged(PrivilegedAction) * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext) */ - @ikvm.internal.HasCallerID + @CallerSensitive public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context) { return (T)doPrivileged(action, context, CallerID.getCallerID()); @@ -412,7 +414,7 @@ public final class AccessController { * @see #doPrivilegedWithCombiner(PrivilegedExceptionAction) * @see java.security.DomainCombiner */ - @ikvm.internal.HasCallerID + @CallerSensitive public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException { @@ -445,41 +447,39 @@ public final class AccessController { * * @since 1.6 */ + @CallerSensitive public static <T> T doPrivilegedWithCombiner (PrivilegedExceptionAction<T> action) throws PrivilegedActionException { - DomainCombiner dc = null; AccessControlContext acc = getStackAccessControlContext(); - if (acc == null || (dc = acc.getAssignedCombiner()) == null) { + if (acc == null) { return AccessController.doPrivileged(action); } - return AccessController.doPrivileged(action, preserveCombiner(dc)); + DomainCombiner dc = acc.getAssignedCombiner(); + return AccessController.doPrivileged(action, preserveCombiner(dc, Reflection.getCallerClass())); } /** * preserve the combiner across the doPrivileged call */ - private static AccessControlContext preserveCombiner - (DomainCombiner combiner) { - - /** - * callerClass[0] = Reflection.getCallerClass - * callerClass[1] = AccessController.preserveCombiner - * callerClass[2] = AccessController.doPrivileged - * callerClass[3] = caller - */ - final Class callerClass = sun.reflect.Reflection.getCallerClass(3); + private static AccessControlContext preserveCombiner(DomainCombiner combiner, + final Class<?> caller) { ProtectionDomain callerPd = doPrivileged (new PrivilegedAction<ProtectionDomain>() { public ProtectionDomain run() { - return callerClass.getProtectionDomain(); + return caller.getProtectionDomain(); } }); // perform 'combine' on the caller of doPrivileged, // even if the caller is from the bootclasspath ProtectionDomain[] pds = new ProtectionDomain[] {callerPd}; - return new AccessControlContext(combiner.combine(pds, null), combiner); + if (combiner == null) { + return new AccessControlContext(pds); + } else { + return new AccessControlContext(combiner.combine(pds, null), + combiner); + } } @@ -512,7 +512,7 @@ public final class AccessController { * @see #doPrivileged(PrivilegedAction) * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext) */ - @ikvm.internal.HasCallerID + @CallerSensitive public static <T> T doPrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context) diff --git a/openjdk/java/sql/DriverManager.java b/openjdk/java/sql/DriverManager.java index 6c35058d..8698345c 100644 --- a/openjdk/java/sql/DriverManager.java +++ b/openjdk/java/sql/DriverManager.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,13 +25,13 @@ package java.sql; -import ikvm.internal.CallerID; import java.util.Iterator; import java.util.ServiceLoader; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.concurrent.CopyOnWriteArrayList; - +import sun.reflect.CallerSensitive; +import sun.reflect.Reflection; /** * <P>The basic service for managing a set of JDBC drivers.<br> @@ -181,15 +181,10 @@ public class DriverManager { * @return a Connection to the URL * @exception SQLException if a database access error occurs */ - @ikvm.internal.HasCallerID + @CallerSensitive public static Connection getConnection(String url, java.util.Properties info) throws SQLException { - - // Gets the classloader of the code that called this method, may - // be null. - ClassLoader callerCL = CallerID.getCallerID().getCallerClassLoader(); - - return (getConnection(url, info, callerCL)); + return (getConnection(url, info, Reflection.getCallerClass())); } /** @@ -205,15 +200,11 @@ public class DriverManager { * @return a connection to the URL * @exception SQLException if a database access error occurs */ - @ikvm.internal.HasCallerID + @CallerSensitive public static Connection getConnection(String url, String user, String password) throws SQLException { java.util.Properties info = new java.util.Properties(); - // Gets the classloader of the code that called this method, may - // be null. - ClassLoader callerCL = CallerID.getCallerID().getCallerClassLoader(); - if (user != null) { info.put("user", user); } @@ -221,7 +212,7 @@ public class DriverManager { info.put("password", password); } - return (getConnection(url, info, callerCL)); + return (getConnection(url, info, Reflection.getCallerClass())); } /** @@ -234,17 +225,12 @@ public class DriverManager { * @return a connection to the URL * @exception SQLException if a database access error occurs */ - @ikvm.internal.HasCallerID + @CallerSensitive public static Connection getConnection(String url) throws SQLException { java.util.Properties info = new java.util.Properties(); - - // Gets the classloader of the code that called this method, may - // be null. - ClassLoader callerCL = CallerID.getCallerID().getCallerClassLoader(); - - return (getConnection(url, info, callerCL)); + return (getConnection(url, info, Reflection.getCallerClass())); } /** @@ -258,22 +244,20 @@ public class DriverManager { * that can connect to the given URL * @exception SQLException if a database access error occurs */ - @ikvm.internal.HasCallerID + @CallerSensitive public static Driver getDriver(String url) throws SQLException { println("DriverManager.getDriver(\"" + url + "\")"); - // Gets the classloader of the code that called this method, may - // be null. - ClassLoader callerCL = CallerID.getCallerID().getCallerClassLoader(); + Class<?> callerClass = Reflection.getCallerClass(); // Walk through the loaded registeredDrivers attempting to locate someone // who understands the given URL. for (DriverInfo aDriver : registeredDrivers) { // If the caller does not have permission to load the driver then // skip it. - if(isDriverAllowed(aDriver.driver, callerCL)) { + if(isDriverAllowed(aDriver.driver, callerClass)) { try { if(aDriver.driver.acceptsURL(url)) { // Success! @@ -287,6 +271,7 @@ public class DriverManager { } else { println(" skipping: " + aDriver.driver.getClass().getName()); } + } println("getDriver: no suitable driver"); @@ -326,21 +311,18 @@ public class DriverManager { * @param driver the JDBC Driver to drop * @exception SQLException if a database access error occurs */ - @ikvm.internal.HasCallerID + @CallerSensitive public static synchronized void deregisterDriver(Driver driver) throws SQLException { if (driver == null) { return; } - // Gets the classloader of the code that called this method, - // may be null. - ClassLoader callerCL = CallerID.getCallerID().getCallerClassLoader(); println("DriverManager.deregisterDriver: " + driver); DriverInfo aDriver = new DriverInfo(driver); if(registeredDrivers.contains(aDriver)) { - if (isDriverAllowed(driver, callerCL)) { + if (isDriverAllowed(driver, Reflection.getCallerClass())) { registeredDrivers.remove(aDriver); } else { // If the caller does not have permission to load the driver then @@ -361,25 +343,22 @@ public class DriverManager { * * @return the list of JDBC Drivers loaded by the caller's class loader */ - @ikvm.internal.HasCallerID + @CallerSensitive public static java.util.Enumeration<Driver> getDrivers() { java.util.Vector<Driver> result = new java.util.Vector<Driver>(); - // Gets the classloader of the code that called this method, may - // be null. - ClassLoader callerCL = CallerID.getCallerID().getCallerClassLoader(); + Class<?> callerClass = Reflection.getCallerClass(); // Walk through the loaded registeredDrivers. for(DriverInfo aDriver : registeredDrivers) { // If the caller does not have permission to load the driver then // skip it. - if(isDriverAllowed(aDriver.driver, callerCL)) { + if(isDriverAllowed(aDriver.driver, callerClass)) { result.addElement(aDriver.driver); } else { println(" skipping: " + aDriver.getClass().getName()); } } - return (result.elements()); } @@ -471,6 +450,11 @@ public class DriverManager { // Indicates whether the class object that would be created if the code calling // DriverManager is accessible. + private static boolean isDriverAllowed(Driver driver, Class<?> caller) { + ClassLoader callerCL = caller != null ? caller.getClassLoader() : null; + return isDriverAllowed(driver, callerCL); + } + private static boolean isDriverAllowed(Driver driver, ClassLoader classLoader) { boolean result = false; if(driver != null) { @@ -523,7 +507,7 @@ public class DriverManager { */ try{ while(driversIterator.hasNext()) { - println(" Loading done by the java.util.ServiceLoader : "+driversIterator.next()); + driversIterator.next(); } } catch(Throwable t) { // Do nothing @@ -553,18 +537,19 @@ public class DriverManager { // Worker method called by the public getConnection() methods. private static Connection getConnection( - String url, java.util.Properties info, ClassLoader callerCL) throws SQLException { + String url, java.util.Properties info, Class<?> caller) throws SQLException { /* * When callerCl is null, we should check the application's * (which is invoking this class indirectly) * classloader, so that the JDBC driver class outside rt.jar * can be loaded from here. */ - synchronized(DriverManager.class) { - // synchronize loading of the correct classloader. - if(callerCL == null) { - callerCL = Thread.currentThread().getContextClassLoader(); - } + ClassLoader callerCL = caller != null ? caller.getClassLoader() : null; + synchronized (DriverManager.class) { + // synchronize loading of the correct classloader. + if (callerCL == null) { + callerCL = Thread.currentThread().getContextClassLoader(); + } } if(url == null) { @@ -610,7 +595,6 @@ public class DriverManager { println("getConnection: no suitable driver found for "+ url); throw new SQLException("No suitable driver found for "+ url, "08001"); } - } /* diff --git a/openjdk/java/util/ResourceBundle.java b/openjdk/java/util/ResourceBundle.java index b572875e..98ee3c51 100644 --- a/openjdk/java/util/ResourceBundle.java +++ b/openjdk/java/util/ResourceBundle.java @@ -55,6 +55,8 @@ import java.security.PrivilegedExceptionAction; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.jar.JarEntry; + +import sun.reflect.CallerSensitive; import sun.reflect.Reflection; import sun.util.locale.BaseLocale; import sun.util.locale.LocaleObjectCache; @@ -412,11 +414,10 @@ public abstract class ResourceBundle { /* * Automatic determination of the ClassLoader to be used to load - * resources on behalf of the client. N.B. The client is getLoader's - * caller's caller. + * resources on behalf of the client. */ - private static ClassLoader getLoader(Class c) { - ClassLoader cl = (c == null) ? null : c.getClassLoader(); + private static ClassLoader getLoader(Class<?> caller) { + ClassLoader cl = caller == null ? null : caller.getClassLoader(); if (cl == null) { // When the caller's loader is the boot class loader, cl is null // here. In that case, ClassLoader.getSystemClassLoader() may @@ -714,12 +715,12 @@ public abstract class ResourceBundle { * if no resource bundle for the specified base name can be found * @return a resource bundle for the given base name and the default locale */ - @ikvm.internal.HasCallerID + @CallerSensitive public static final ResourceBundle getBundle(String baseName) { return getBundleImpl(baseName, Locale.getDefault(), /* must determine loader here, else we break stack invariant */ - getLoader(Reflection.getCallerClass(2)), + getLoader(Reflection.getCallerClass()), Control.INSTANCE); } @@ -757,12 +758,12 @@ public abstract class ResourceBundle { * needed. * @since 1.6 */ - @ikvm.internal.HasCallerID + @CallerSensitive public static final ResourceBundle getBundle(String baseName, Control control) { return getBundleImpl(baseName, Locale.getDefault(), /* must determine loader here, else we break stack invariant */ - getLoader(Reflection.getCallerClass(2)), + getLoader(Reflection.getCallerClass()), control); } @@ -787,13 +788,13 @@ public abstract class ResourceBundle { * if no resource bundle for the specified base name can be found * @return a resource bundle for the given base name and locale */ - @ikvm.internal.HasCallerID + @CallerSensitive public static final ResourceBundle getBundle(String baseName, Locale locale) { return getBundleImpl(baseName, locale, /* must determine loader here, else we break stack invariant */ - getLoader(Reflection.getCallerClass(2)), + getLoader(Reflection.getCallerClass()), Control.INSTANCE); } @@ -834,12 +835,12 @@ public abstract class ResourceBundle { * needed. * @since 1.6 */ - @ikvm.internal.HasCallerID + @CallerSensitive public static final ResourceBundle getBundle(String baseName, Locale targetLocale, Control control) { return getBundleImpl(baseName, targetLocale, /* must determine loader here, else we break stack invariant */ - getLoader(Reflection.getCallerClass(2)), + getLoader(Reflection.getCallerClass()), control); } @@ -1675,9 +1676,9 @@ public abstract class ResourceBundle { * @since 1.6 * @see ResourceBundle.Control#getTimeToLive(String,Locale) */ - @ikvm.internal.HasCallerID + @CallerSensitive public static final void clearCache() { - clearCache(getLoader(Reflection.getCallerClass(2))); + clearCache(getLoader(Reflection.getCallerClass())); } /** |