jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package sun.invoke.util; jaroslav@1646: jaroslav@1646: import java.lang.reflect.Modifier; jaroslav@1646: import static java.lang.reflect.Modifier.*; jaroslav@1646: import sun.reflect.Reflection; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * This class centralizes information about the JVM's linkage access control. jaroslav@1646: * @author jrose jaroslav@1646: */ jaroslav@1646: public class VerifyAccess { jaroslav@1646: jaroslav@1646: private VerifyAccess() { } // cannot instantiate jaroslav@1646: jaroslav@1646: private static final int PACKAGE_ONLY = 0; jaroslav@1646: private static final int PACKAGE_ALLOWED = java.lang.invoke.MethodHandles.Lookup.PACKAGE; jaroslav@1646: private static final int PROTECTED_OR_PACKAGE_ALLOWED = (PACKAGE_ALLOWED|PROTECTED); jaroslav@1646: private static final int ALL_ACCESS_MODES = (PUBLIC|PRIVATE|PROTECTED|PACKAGE_ONLY); jaroslav@1646: private static final boolean ALLOW_NESTMATE_ACCESS = false; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Evaluate the JVM linkage rules for access to the given method jaroslav@1646: * on behalf of a caller class which proposes to perform the access. jaroslav@1646: * Return true if the caller class has privileges to invoke a method jaroslav@1646: * or access a field with the given properties. jaroslav@1646: * This requires an accessibility check of the referencing class, jaroslav@1646: * plus an accessibility check of the member within the class, jaroslav@1646: * which depends on the member's modifier flags. jaroslav@1646: *

jaroslav@1646: * The relevant properties include the defining class ({@code defc}) jaroslav@1646: * of the member, and its modifier flags ({@code mods}). jaroslav@1646: * Also relevant is the class used to make the initial symbolic reference jaroslav@1646: * to the member ({@code refc}). If this latter class is not distinguished, jaroslav@1646: * the defining class should be passed for both arguments ({@code defc == refc}). jaroslav@1646: *

JVM Specification, 5.4.4 "Access Control"

jaroslav@1646: * A field or method R is accessible to a class or interface D if jaroslav@1646: * and only if any of the following conditions is true: jaroslav@1646: * This discussion of access control omits a related restriction jaroslav@1646: * on the target of a protected field access or method invocation jaroslav@1646: * (the target must be of class D or a subtype of D). That jaroslav@1646: * requirement is checked as part of the verification process jaroslav@1646: * (5.4.1); it is not part of link-time access control. jaroslav@1646: * @param refc the class used in the symbolic reference to the proposed member jaroslav@1646: * @param defc the class in which the proposed member is actually defined jaroslav@1646: * @param mods modifier flags for the proposed member jaroslav@1646: * @param lookupClass the class for which the access check is being made jaroslav@1646: * @return true iff the the accessing class can access such a member jaroslav@1646: */ jaroslav@1646: public static boolean isMemberAccessible(Class refc, // symbolic ref class jaroslav@1646: Class defc, // actual def class jaroslav@1646: int mods, // actual member mods jaroslav@1646: Class lookupClass, jaroslav@1646: int allowedModes) { jaroslav@1646: if (allowedModes == 0) return false; jaroslav@1646: assert((allowedModes & PUBLIC) != 0 && jaroslav@1646: (allowedModes & ~(ALL_ACCESS_MODES|PACKAGE_ALLOWED)) == 0); jaroslav@1646: // The symbolic reference class (refc) must always be fully verified. jaroslav@1646: if (!isClassAccessible(refc, lookupClass, allowedModes)) { jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: // Usually refc and defc are the same, but verify defc also in case they differ. jaroslav@1646: if (defc == lookupClass && jaroslav@1646: (allowedModes & PRIVATE) != 0) jaroslav@1646: return true; // easy check; all self-access is OK jaroslav@1646: switch (mods & ALL_ACCESS_MODES) { jaroslav@1646: case PUBLIC: jaroslav@1646: return true; // already checked above jaroslav@1646: case PROTECTED: jaroslav@1646: if ((allowedModes & PROTECTED_OR_PACKAGE_ALLOWED) != 0 && jaroslav@1646: isSamePackage(defc, lookupClass)) jaroslav@1646: return true; jaroslav@1646: if ((allowedModes & PROTECTED) == 0) jaroslav@1646: return false; jaroslav@1646: if ((mods & STATIC) != 0 && jaroslav@1646: !isRelatedClass(refc, lookupClass)) jaroslav@1646: return false; jaroslav@1646: if ((allowedModes & PROTECTED) != 0 && jaroslav@1646: isSuperClass(defc, lookupClass)) jaroslav@1646: return true; jaroslav@1646: return false; jaroslav@1646: case PACKAGE_ONLY: // That is, zero. Unmarked member is package-only access. jaroslav@1646: return ((allowedModes & PACKAGE_ALLOWED) != 0 && jaroslav@1646: isSamePackage(defc, lookupClass)); jaroslav@1646: case PRIVATE: jaroslav@1646: // Loosened rules for privates follows access rules for inner classes. jaroslav@1646: return (ALLOW_NESTMATE_ACCESS && jaroslav@1646: (allowedModes & PRIVATE) != 0 && jaroslav@1646: isSamePackageMember(defc, lookupClass)); jaroslav@1646: default: jaroslav@1646: throw new IllegalArgumentException("bad modifiers: "+Modifier.toString(mods)); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: static boolean isRelatedClass(Class refc, Class lookupClass) { jaroslav@1646: return (refc == lookupClass || jaroslav@1646: refc.isAssignableFrom(lookupClass) || jaroslav@1646: lookupClass.isAssignableFrom(refc)); jaroslav@1646: } jaroslav@1646: jaroslav@1646: static boolean isSuperClass(Class defc, Class lookupClass) { jaroslav@1646: return defc.isAssignableFrom(lookupClass); jaroslav@1646: } jaroslav@1646: jaroslav@1646: static int getClassModifiers(Class c) { jaroslav@1646: // This would return the mask stored by javac for the source-level modifiers. jaroslav@1646: // return c.getModifiers(); jaroslav@1646: // But what we need for JVM access checks are the actual bits from the class header. jaroslav@1646: // ...But arrays and primitives are synthesized with their own odd flags: jaroslav@1646: if (c.isArray() || c.isPrimitive()) jaroslav@1646: return c.getModifiers(); jaroslav@1646: return Reflection.getClassAccessFlags(c); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Evaluate the JVM linkage rules for access to the given class on behalf of caller. jaroslav@1646: *

JVM Specification, 5.4.4 "Access Control"

jaroslav@1646: * A class or interface C is accessible to a class or interface D jaroslav@1646: * if and only if either of the following conditions are true: jaroslav@1646: * @param refc the symbolic reference class to which access is being checked (C) jaroslav@1646: * @param lookupClass the class performing the lookup (D) jaroslav@1646: */ jaroslav@1646: public static boolean isClassAccessible(Class refc, Class lookupClass, jaroslav@1646: int allowedModes) { jaroslav@1646: if (allowedModes == 0) return false; jaroslav@1646: assert((allowedModes & PUBLIC) != 0 && jaroslav@1646: (allowedModes & ~(ALL_ACCESS_MODES|PACKAGE_ALLOWED)) == 0); jaroslav@1646: int mods = getClassModifiers(refc); jaroslav@1646: if (isPublic(mods)) jaroslav@1646: return true; jaroslav@1646: if ((allowedModes & PACKAGE_ALLOWED) != 0 && jaroslav@1646: isSamePackage(lookupClass, refc)) jaroslav@1646: return true; jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Decide if the given method type, attributed to a member or symbolic jaroslav@1646: * reference of a given reference class, is really visible to that class. jaroslav@1646: * @param type the supposed type of a member or symbolic reference of refc jaroslav@1646: * @param refc the class attempting to make the reference jaroslav@1646: */ jaroslav@1646: public static boolean isTypeVisible(Class type, Class refc) { jaroslav@1646: if (type == refc) return true; // easy check jaroslav@1646: while (type.isArray()) type = type.getComponentType(); jaroslav@1646: if (type.isPrimitive() || type == Object.class) return true; jaroslav@1646: ClassLoader parent = type.getClassLoader(); jaroslav@1646: if (parent == null) return true; jaroslav@1646: ClassLoader child = refc.getClassLoader(); jaroslav@1646: if (child == null) return false; jaroslav@1646: if (parent == child || loadersAreRelated(parent, child, true)) jaroslav@1646: return true; jaroslav@1646: // Do it the hard way: Look up the type name from the refc loader. jaroslav@1646: try { jaroslav@1646: Class res = child.loadClass(type.getName()); jaroslav@1646: return (type == res); jaroslav@1646: } catch (ClassNotFoundException ex) { jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Decide if the given method type, attributed to a member or symbolic jaroslav@1646: * reference of a given reference class, is really visible to that class. jaroslav@1646: * @param type the supposed type of a member or symbolic reference of refc jaroslav@1646: * @param refc the class attempting to make the reference jaroslav@1646: */ jaroslav@1646: public static boolean isTypeVisible(java.lang.invoke.MethodType type, Class refc) { jaroslav@1646: for (int n = -1, max = type.parameterCount(); n < max; n++) { jaroslav@1646: Class ptype = (n < 0 ? type.returnType() : type.parameterType(n)); jaroslav@1646: if (!isTypeVisible(ptype, refc)) jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: return true; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Test if two classes have the same class loader and package qualifier. jaroslav@1646: * @param class1 a class jaroslav@1646: * @param class2 another class jaroslav@1646: * @return whether they are in the same package jaroslav@1646: */ jaroslav@1646: public static boolean isSamePackage(Class class1, Class class2) { jaroslav@1646: assert(!class1.isArray() && !class2.isArray()); jaroslav@1646: if (class1 == class2) jaroslav@1646: return true; jaroslav@1646: if (class1.getClassLoader() != class2.getClassLoader()) jaroslav@1646: return false; jaroslav@1646: String name1 = class1.getName(), name2 = class2.getName(); jaroslav@1646: int dot = name1.lastIndexOf('.'); jaroslav@1646: if (dot != name2.lastIndexOf('.')) jaroslav@1646: return false; jaroslav@1646: for (int i = 0; i < dot; i++) { jaroslav@1646: if (name1.charAt(i) != name2.charAt(i)) jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: return true; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** Return the package name for this class. jaroslav@1646: */ jaroslav@1646: public static String getPackageName(Class cls) { jaroslav@1646: assert(!cls.isArray()); jaroslav@1646: String name = cls.getName(); jaroslav@1646: int dot = name.lastIndexOf('.'); jaroslav@1646: if (dot < 0) return ""; jaroslav@1646: return name.substring(0, dot); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Test if two classes are defined as part of the same package member (top-level class). jaroslav@1646: * If this is true, they can share private access with each other. jaroslav@1646: * @param class1 a class jaroslav@1646: * @param class2 another class jaroslav@1646: * @return whether they are identical or nested together jaroslav@1646: */ jaroslav@1646: public static boolean isSamePackageMember(Class class1, Class class2) { jaroslav@1646: if (class1 == class2) jaroslav@1646: return true; jaroslav@1646: if (!isSamePackage(class1, class2)) jaroslav@1646: return false; jaroslav@1646: if (getOutermostEnclosingClass(class1) != getOutermostEnclosingClass(class2)) jaroslav@1646: return false; jaroslav@1646: return true; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static Class getOutermostEnclosingClass(Class c) { jaroslav@1646: Class pkgmem = c; jaroslav@1646: for (Class enc = c; (enc = enc.getEnclosingClass()) != null; ) jaroslav@1646: pkgmem = enc; jaroslav@1646: return pkgmem; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static boolean loadersAreRelated(ClassLoader loader1, ClassLoader loader2, jaroslav@1646: boolean loader1MustBeParent) { jaroslav@1646: if (loader1 == loader2 || loader1 == null jaroslav@1646: || (loader2 == null && !loader1MustBeParent)) { jaroslav@1646: return true; jaroslav@1646: } jaroslav@1646: for (ClassLoader scan2 = loader2; jaroslav@1646: scan2 != null; scan2 = scan2.getParent()) { jaroslav@1646: if (scan2 == loader1) return true; jaroslav@1646: } jaroslav@1646: if (loader1MustBeParent) return false; jaroslav@1646: // see if loader2 is a parent of loader1: jaroslav@1646: for (ClassLoader scan1 = loader1; jaroslav@1646: scan1 != null; scan1 = scan1.getParent()) { jaroslav@1646: if (scan1 == loader2) return true; jaroslav@1646: } jaroslav@1646: return false; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Is the class loader of parentClass identical to, or an ancestor of, jaroslav@1646: * the class loader of childClass? jaroslav@1646: * @param parentClass a class jaroslav@1646: * @param childClass another class, which may be a descendent of the first class jaroslav@1646: * @return whether parentClass precedes or equals childClass in class loader order jaroslav@1646: */ jaroslav@1646: public static boolean classLoaderIsAncestor(Class parentClass, Class childClass) { jaroslav@1646: return loadersAreRelated(parentClass.getClassLoader(), childClass.getClassLoader(), true); jaroslav@1646: } jaroslav@1646: }