5109N/A/*
5109N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5109N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5109N/A *
5109N/A * This code is free software; you can redistribute it and/or modify it
5109N/A * under the terms of the GNU General Public License version 2 only, as
5109N/A * published by the Free Software Foundation. Oracle designates this
5109N/A * particular file as subject to the "Classpath" exception as provided
5109N/A * by Oracle in the LICENSE file that accompanied this code.
5109N/A *
5109N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5109N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5109N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5109N/A * version 2 for more details (a copy is included in the LICENSE file that
5109N/A * accompanied this code).
5109N/A *
5109N/A * You should have received a copy of the GNU General Public License version
5109N/A * 2 along with this work; if not, write to the Free Software Foundation,
5109N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5109N/A *
5109N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5109N/A * or visit www.oracle.com if you need additional information or have any
5109N/A * questions.
5109N/A */
5109N/A
5109N/A/* @test
5109N/A * @summary test access checking by java.lang.invoke.MethodHandles.Lookup
5109N/A * @library ../../../..
5109N/A * @build test.java.lang.invoke.AccessControlTest
5109N/A * @build test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote
5109N/A * @run junit/othervm test.java.lang.invoke.AccessControlTest
5109N/A */
5109N/A
5109N/Apackage test.java.lang.invoke;
5109N/A
5109N/Aimport java.lang.invoke.*;
5109N/Aimport java.lang.reflect.*;
5109N/Aimport java.util.*;
5109N/Aimport org.junit.*;
5109N/A
5109N/Aimport static java.lang.invoke.MethodHandles.*;
5109N/Aimport static java.lang.invoke.MethodHandles.Lookup.*;
5109N/Aimport static java.lang.invoke.MethodType.*;
5109N/Aimport static org.junit.Assert.*;
5109N/Aimport test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote;
5109N/A
5109N/A
5109N/A/**
5109N/A * Test many combinations of Lookup access and cross-class lookupStatic.
5109N/A * @author jrose
5109N/A */
5109N/Apublic class AccessControlTest {
5109N/A static final Class<?> THIS_CLASS = AccessControlTest.class;
5109N/A // How much output?
5109N/A static int verbosity = 0;
5109N/A static {
5109N/A String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
5109N/A if (vstr == null)
5109N/A vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
5109N/A if (vstr != null) verbosity = Integer.parseInt(vstr);
5109N/A }
5109N/A
5109N/A private class LookupCase implements Comparable<LookupCase> {
5109N/A final Lookup lookup;
5109N/A final Class<?> lookupClass;
5109N/A final int lookupModes;
5109N/A public LookupCase(Lookup lookup) {
5109N/A this.lookup = lookup;
5109N/A this.lookupClass = lookup.lookupClass();
5109N/A this.lookupModes = lookup.lookupModes();
5109N/A assert(lookupString().equals(lookup.toString()));
5109N/A numberOf(lookupClass().getClassLoader()); // assign CL#
5109N/A }
5109N/A public LookupCase(Class<?> lookupClass, int lookupModes) {
5109N/A this.lookup = null;
5109N/A this.lookupClass = lookupClass;
5109N/A this.lookupModes = lookupModes;
5109N/A numberOf(lookupClass().getClassLoader()); // assign CL#
5109N/A }
5109N/A
5109N/A public final Class<?> lookupClass() { return lookupClass; }
5109N/A public final int lookupModes() { return lookupModes; }
5109N/A
5109N/A public Lookup lookup() { lookup.getClass(); return lookup; }
5109N/A
5109N/A @Override
5109N/A public int compareTo(LookupCase that) {
5109N/A Class<?> c1 = this.lookupClass();
5109N/A Class<?> c2 = that.lookupClass();
5109N/A if (c1 != c2) {
5109N/A int cmp = c1.getName().compareTo(c2.getName());
5109N/A if (cmp != 0) return cmp;
5109N/A cmp = numberOf(c1.getClassLoader()) - numberOf(c2.getClassLoader());
5109N/A assert(cmp != 0);
5109N/A return cmp;
5109N/A }
5109N/A return -(this.lookupModes() - that.lookupModes());
5109N/A }
5109N/A
5109N/A @Override
5109N/A public boolean equals(Object that) {
5109N/A return (that instanceof LookupCase && equals((LookupCase)that));
5109N/A }
5109N/A public boolean equals(LookupCase that) {
5109N/A return (this.lookupClass() == that.lookupClass() &&
5109N/A this.lookupModes() == that.lookupModes());
5109N/A }
5109N/A
5109N/A @Override
5109N/A public int hashCode() {
5109N/A return lookupClass().hashCode() + (lookupModes() * 31);
5109N/A }
5109N/A
5109N/A /** Simulate all assertions in the spec. for Lookup.toString. */
5109N/A private String lookupString() {
5109N/A String name = lookupClass.getName();
5109N/A String suffix = "";
5109N/A if (lookupModes == 0)
5109N/A suffix = "/noaccess";
5109N/A else if (lookupModes == PUBLIC)
5109N/A suffix = "/public";
5109N/A else if (lookupModes == (PUBLIC|PACKAGE))
5109N/A suffix = "/package";
5109N/A else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE))
5109N/A suffix = "/private";
5109N/A else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE|PROTECTED))
5109N/A suffix = "";
5109N/A else
5109N/A suffix = "/#"+Integer.toHexString(lookupModes);
5109N/A return name+suffix;
5109N/A }
5109N/A
5109N/A /** Simulate all assertions from the spec. for Lookup.in:
5109N/A * <hr/>
5109N/A * Creates a lookup on the specified new lookup class.
5109N/A * [A1] The resulting object will report the specified
5109N/A * class as its own {@link #lookupClass lookupClass}.
5109N/A * <p>
5109N/A * [A2] However, the resulting {@code Lookup} object is guaranteed
5109N/A * to have no more access capabilities than the original.
5109N/A * In particular, access capabilities can be lost as follows:<ul>
5109N/A * <li>[A3] If the new lookup class differs from the old one,
5109N/A * protected members will not be accessible by virtue of inheritance.
5109N/A * (Protected members may continue to be accessible because of package sharing.)
5109N/A * <li>[A4] If the new lookup class is in a different package
5109N/A * than the old one, protected and default (package) members will not be accessible.
5109N/A * <li>[A5] If the new lookup class is not within the same package member
5109N/A * as the old one, private members will not be accessible.
5109N/A * <li>[A6] If the new lookup class is not accessible to the old lookup class,
5109N/A * using the original access modes,
5109N/A * then no members, not even public members, will be accessible.
5109N/A * [A7] (In all other cases, public members will continue to be accessible.)
5109N/A * </ul>
5109N/A * Other than the above cases, the new lookup will have the same
5109N/A * access capabilities as the original. [A8]
5109N/A * <hr/>
5109N/A */
5109N/A public LookupCase in(Class<?> c2) {
5109N/A Class<?> c1 = lookupClass();
5109N/A int m1 = lookupModes();
5109N/A int changed = 0;
5109N/A boolean samePackage = (c1.getClassLoader() == c2.getClassLoader() &&
5109N/A packagePrefix(c1).equals(packagePrefix(c2)));
5109N/A boolean sameTopLevel = (topLevelClass(c1) == topLevelClass(c2));
5109N/A boolean sameClass = (c1 == c2);
5109N/A assert(samePackage || !sameTopLevel);
5109N/A assert(sameTopLevel || !sameClass);
5109N/A boolean accessible = sameClass; // [A6]
5109N/A if ((m1 & PACKAGE) != 0) accessible |= samePackage;
5109N/A if ((m1 & PUBLIC ) != 0) accessible |= (c2.getModifiers() & PUBLIC) != 0;
5109N/A if (!accessible) {
5109N/A // Different package and no access to c2; lose all access.
5109N/A changed |= (PUBLIC|PACKAGE|PRIVATE|PROTECTED); // [A6]
5109N/A }
5109N/A if (!samePackage) {
5109N/A // Different package; lose PACKAGE and lower access.
5109N/A changed |= (PACKAGE|PRIVATE|PROTECTED); // [A4]
5109N/A }
5109N/A if (!sameTopLevel) {
5109N/A // Different top-level class. Lose PRIVATE and lower access.
5109N/A changed |= (PRIVATE|PROTECTED); // [A5]
5109N/A }
5109N/A if (!sameClass) {
5109N/A changed |= (PROTECTED); // [A3]
5109N/A } else {
5109N/A assert(changed == 0); // [A8] (no deprivation if same class)
5109N/A }
5109N/A if (accessible) assert((changed & PUBLIC) == 0); // [A7]
5109N/A int m2 = m1 & ~changed;
5109N/A LookupCase l2 = new LookupCase(c2, m2);
5109N/A assert(l2.lookupClass() == c2); // [A1]
5109N/A assert((m1 | m2) == m1); // [A2] (no elevation of access)
5109N/A return l2;
5109N/A }
5109N/A
5109N/A @Override
5109N/A public String toString() {
5109N/A String s = lookupClass().getSimpleName();
5109N/A String lstr = lookupString();
5109N/A int sl = lstr.indexOf('/');
5109N/A if (sl >= 0) s += lstr.substring(sl);
5109N/A ClassLoader cld = lookupClass().getClassLoader();
5109N/A if (cld != THIS_LOADER) s += "/loader#"+numberOf(cld);
5109N/A return s;
5109N/A }
5109N/A
5109N/A /** Predict the success or failure of accessing this method. */
5109N/A public boolean willAccess(Method m) {
5109N/A Class<?> c1 = lookupClass();
5109N/A Class<?> c2 = m.getDeclaringClass();
5109N/A LookupCase lc = this.in(c2);
5109N/A int m1 = lc.lookupModes();
5109N/A int m2 = fixMods(m.getModifiers());
5109N/A // privacy is strictly enforced on lookups
5109N/A if (c1 != c2) m1 &= ~PRIVATE;
5109N/A // protected access is sometimes allowed
5109N/A if ((m2 & PROTECTED) != 0) {
5109N/A int prev = m2;
5109N/A m2 |= PACKAGE; // it acts like a package method also
5109N/A if ((lookupModes() & PROTECTED) != 0 &&
5109N/A c2.isAssignableFrom(c1))
5109N/A m2 |= PUBLIC; // from a subclass, it acts like a public method also
5109N/A }
5109N/A if (verbosity >= 2)
5109N/A System.out.println(this+" willAccess "+lc+" m1="+m1+" m2="+m2+" => "+((m2 & m1) != 0));
5109N/A return (m2 & m1) != 0;
5109N/A }
5109N/A }
5109N/A
5109N/A private static Class<?> topLevelClass(Class<?> cls) {
5109N/A Class<?> c = cls;
5109N/A for (Class<?> ec; (ec = c.getEnclosingClass()) != null; )
5109N/A c = ec;
5109N/A assert(c.getEnclosingClass() == null);
5109N/A assert(c == cls || cls.getEnclosingClass() != null);
5109N/A return c;
5109N/A }
5109N/A
5109N/A private static String packagePrefix(Class<?> c) {
5109N/A while (c.isArray()) c = c.getComponentType();
5109N/A String s = c.getName();
5109N/A assert(s.indexOf('/') < 0);
5109N/A return s.substring(0, s.lastIndexOf('.')+1);
5109N/A }
5109N/A
5109N/A
5109N/A private final TreeSet<LookupCase> CASES = new TreeSet<>();
5109N/A private final TreeMap<LookupCase,TreeSet<LookupCase>> CASE_EDGES = new TreeMap<>();
5109N/A private final ArrayList<ClassLoader> LOADERS = new ArrayList<>();
5109N/A private final ClassLoader THIS_LOADER = this.getClass().getClassLoader();
5109N/A { if (THIS_LOADER != null) LOADERS.add(THIS_LOADER); } // #1
5109N/A
5109N/A private LookupCase lookupCase(String name) {
5109N/A for (LookupCase lc : CASES) {
5109N/A if (lc.toString().equals(name))
5109N/A return lc;
5109N/A }
5109N/A throw new AssertionError(name);
5109N/A }
5109N/A
5109N/A private int numberOf(ClassLoader cl) {
5109N/A if (cl == null) return 0;
5109N/A int i = LOADERS.indexOf(cl);
5109N/A if (i < 0) {
5109N/A i = LOADERS.size();
5109N/A LOADERS.add(cl);
5109N/A }
5109N/A return i+1;
5109N/A }
5109N/A
5109N/A private void addLookupEdge(LookupCase l1, Class<?> c2, LookupCase l2) {
5109N/A TreeSet<LookupCase> edges = CASE_EDGES.get(l2);
5109N/A if (edges == null) CASE_EDGES.put(l2, edges = new TreeSet<>());
5109N/A if (edges.add(l1)) {
5109N/A Class<?> c1 = l1.lookupClass();
5109N/A assert(l2.lookupClass() == c2); // [A1]
5109N/A int m1 = l1.lookupModes();
5109N/A int m2 = l2.lookupModes();
5109N/A assert((m1 | m2) == m1); // [A2] (no elevation of access)
5109N/A LookupCase expect = l1.in(c2);
5109N/A if (!expect.equals(l2))
5109N/A System.out.println("*** expect "+l1+" => "+expect+" but got "+l2);
5109N/A assertEquals(expect, l2);
5109N/A }
5109N/A }
5109N/A
5109N/A private void makeCases(Lookup[] originalLookups) {
5109N/A // make initial set of lookup test cases
5109N/A CASES.clear(); LOADERS.clear(); CASE_EDGES.clear();
5109N/A ArrayList<Class<?>> classes = new ArrayList<>();
5109N/A for (Lookup l : originalLookups) {
5109N/A CASES.add(new LookupCase(l));
5109N/A classes.remove(l.lookupClass()); // no dups please
5109N/A classes.add(l.lookupClass());
5109N/A }
5109N/A System.out.println("loaders = "+LOADERS);
5109N/A int rounds = 0;
5109N/A for (int lastCount = -1; lastCount != CASES.size(); ) {
5109N/A lastCount = CASES.size(); // if CASES grow in the loop we go round again
5109N/A for (LookupCase lc1 : CASES.toArray(new LookupCase[0])) {
5109N/A for (Class<?> c2 : classes) {
5109N/A LookupCase lc2 = new LookupCase(lc1.lookup().in(c2));
5109N/A addLookupEdge(lc1, c2, lc2);
5109N/A CASES.add(lc2);
5109N/A }
5109N/A }
5109N/A rounds++;
5109N/A }
5109N/A System.out.println("filled in "+CASES.size()+" cases from "+originalLookups.length+" original cases in "+rounds+" rounds");
5109N/A if (false) {
5109N/A System.out.println("CASES: {");
5109N/A for (LookupCase lc : CASES) {
5109N/A System.out.println(lc);
5109N/A Set<LookupCase> edges = CASE_EDGES.get(lc);
5109N/A if (edges != null)
5109N/A for (LookupCase prev : edges) {
5109N/A System.out.println("\t"+prev);
5109N/A }
5109N/A }
5109N/A System.out.println("}");
5109N/A }
5109N/A }
5109N/A
5109N/A @Test public void test() {
5109N/A makeCases(lookups());
5109N/A if (verbosity > 0) {
5109N/A verbosity += 9;
5109N/A Method pro_in_self = targetMethod(THIS_CLASS, PROTECTED, methodType(void.class));
5109N/A testOneAccess(lookupCase("AccessControlTest/public"), pro_in_self, "find");
5109N/A testOneAccess(lookupCase("Remote_subclass/public"), pro_in_self, "find");
5109N/A testOneAccess(lookupCase("Remote_subclass"), pro_in_self, "find");
5109N/A verbosity -= 9;
5109N/A }
5109N/A Set<Class<?>> targetClassesDone = new HashSet<>();
5109N/A for (LookupCase targetCase : CASES) {
5109N/A Class<?> targetClass = targetCase.lookupClass();
5109N/A if (!targetClassesDone.add(targetClass)) continue; // already saw this one
5109N/A String targetPlace = placeName(targetClass);
5109N/A if (targetPlace == null) continue; // Object, String, not a target
5109N/A for (int targetAccess : ACCESS_CASES) {
5109N/A MethodType methodType = methodType(void.class);
5109N/A Method method = targetMethod(targetClass, targetAccess, methodType);
5109N/A // Try to access target method from various contexts.
5109N/A for (LookupCase sourceCase : CASES) {
5109N/A testOneAccess(sourceCase, method, "find");
5109N/A testOneAccess(sourceCase, method, "unreflect");
5109N/A }
5109N/A }
5109N/A }
5109N/A System.out.println("tested "+testCount+" access scenarios; "+testCountFails+" accesses were denied");
5109N/A }
5109N/A
5109N/A private int testCount, testCountFails;
5109N/A
5109N/A private void testOneAccess(LookupCase sourceCase, Method method, String kind) {
5109N/A Class<?> targetClass = method.getDeclaringClass();
5109N/A String methodName = method.getName();
5109N/A MethodType methodType = methodType(method.getReturnType(), method.getParameterTypes());
5109N/A boolean willAccess = sourceCase.willAccess(method);
5109N/A boolean didAccess = false;
5109N/A ReflectiveOperationException accessError = null;
5109N/A try {
5109N/A switch (kind) {
5109N/A case "find":
5109N/A if ((method.getModifiers() & Modifier.STATIC) != 0)
5109N/A sourceCase.lookup().findStatic(targetClass, methodName, methodType);
5109N/A else
5109N/A sourceCase.lookup().findVirtual(targetClass, methodName, methodType);
5109N/A break;
5109N/A case "unreflect":
5109N/A sourceCase.lookup().unreflect(method);
5109N/A break;
5109N/A default:
5109N/A throw new AssertionError(kind);
5109N/A }
5109N/A didAccess = true;
5109N/A } catch (ReflectiveOperationException ex) {
5109N/A accessError = ex;
5109N/A }
5109N/A if (willAccess != didAccess) {
5109N/A System.out.println(sourceCase+" => "+targetClass.getSimpleName()+"."+methodName+methodType);
5109N/A System.out.println("fail on "+method+" ex="+accessError);
5109N/A assertEquals(willAccess, didAccess);
5109N/A }
5109N/A testCount++;
5109N/A if (!didAccess) testCountFails++;
5109N/A }
5109N/A
5109N/A static Method targetMethod(Class<?> targetClass, int targetAccess, MethodType methodType) {
5109N/A String methodName = accessName(targetAccess)+placeName(targetClass);
5109N/A if (verbosity >= 2)
5109N/A System.out.println(targetClass.getSimpleName()+"."+methodName+methodType);
5109N/A try {
5109N/A Method method = targetClass.getDeclaredMethod(methodName, methodType.parameterArray());
5109N/A assertEquals(method.getReturnType(), methodType.returnType());
5109N/A int haveMods = method.getModifiers();
5109N/A assert(Modifier.isStatic(haveMods));
5109N/A assert(targetAccess == fixMods(haveMods));
5109N/A return method;
5109N/A } catch (NoSuchMethodException ex) {
5109N/A throw new AssertionError(methodName, ex);
5109N/A }
5109N/A }
5109N/A
5109N/A static String placeName(Class<?> cls) {
5109N/A // return "self", "sibling", "nestmate", etc.
5109N/A if (cls == AccessControlTest.class) return "self";
5109N/A String cln = cls.getSimpleName();
5109N/A int under = cln.lastIndexOf('_');
5109N/A if (under < 0) return null;
5109N/A return cln.substring(under+1);
5109N/A }
5109N/A static String accessName(int acc) {
5109N/A switch (acc) {
5109N/A case PUBLIC: return "pub_in_";
5109N/A case PROTECTED: return "pro_in_";
5109N/A case PACKAGE: return "pkg_in_";
5109N/A case PRIVATE: return "pri_in_";
5109N/A }
5109N/A assert(false);
5109N/A return "?";
5109N/A }
5109N/A private static final int[] ACCESS_CASES = {
5109N/A PUBLIC, PACKAGE, PRIVATE, PROTECTED
5109N/A };
5109N/A /** Return one of the ACCESS_CASES. */
5109N/A static int fixMods(int mods) {
5109N/A mods &= (PUBLIC|PRIVATE|PROTECTED);
5109N/A switch (mods) {
5109N/A case PUBLIC: case PRIVATE: case PROTECTED: return mods;
5109N/A case 0: return PACKAGE;
5109N/A }
5109N/A throw new AssertionError(mods);
5109N/A }
5109N/A
5109N/A static Lookup[] lookups() {
5109N/A ArrayList<Lookup> tem = new ArrayList<>();
5109N/A Collections.addAll(tem,
5109N/A AccessControlTest.lookup_in_self(),
5109N/A Inner_nestmate.lookup_in_nestmate(),
5109N/A AccessControlTest_sibling.lookup_in_sibling());
5109N/A if (true) {
5109N/A Collections.addAll(tem,Acquaintance_remote.lookups());
5109N/A } else {
5109N/A try {
5109N/A Class<?> remc = Class.forName("test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote");
5109N/A Lookup[] remls = (Lookup[]) remc.getMethod("lookups").invoke(null);
5109N/A Collections.addAll(tem, remls);
5109N/A } catch (ReflectiveOperationException ex) {
5109N/A throw new LinkageError("reflection failed", ex);
5109N/A }
5109N/A }
5109N/A tem.add(publicLookup());
5109N/A tem.add(publicLookup().in(String.class));
5109N/A tem.add(publicLookup().in(List.class));
5109N/A return tem.toArray(new Lookup[0]);
5109N/A }
5109N/A
5109N/A static Lookup lookup_in_self() {
5109N/A return MethodHandles.lookup();
5109N/A }
5109N/A static public void pub_in_self() { }
5109N/A static protected void pro_in_self() { }
5109N/A static /*package*/ void pkg_in_self() { }
5109N/A static private void pri_in_self() { }
5109N/A
5109N/A static class Inner_nestmate {
5109N/A static Lookup lookup_in_nestmate() {
5109N/A return MethodHandles.lookup();
5109N/A }
5109N/A static public void pub_in_nestmate() { }
5109N/A static protected void pro_in_nestmate() { }
5109N/A static /*package*/ void pkg_in_nestmate() { }
5109N/A static private void pri_in_nestmate() { }
5109N/A }
5109N/A}
5109N/Aclass AccessControlTest_sibling {
5109N/A static Lookup lookup_in_sibling() {
5109N/A return MethodHandles.lookup();
5109N/A }
5109N/A static public void pub_in_sibling() { }
5109N/A static protected void pro_in_sibling() { }
5109N/A static /*package*/ void pkg_in_sibling() { }
5109N/A static private void pri_in_sibling() { }
5109N/A}
5109N/A
5109N/A// This guy tests access from outside the package:
5109N/A/*
5109N/Apackage test.java.lang.invoke.AccessControlTest_subpkg;
5109N/Apublic class Acquaintance_remote {
5109N/A public static Lookup[] lookups() { ...
5109N/A }
5109N/A ...
5109N/A}
5109N/A*/