1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8022718
  27  * @summary Runtime accessibility checking: protected class, if extended, should be accessible from another package
  28  * @modules java.base/jdk.internal.org.objectweb.asm
  29  * @compile -XDignore.symbol.file BogoLoader.java MethodInvoker.java Test.java anotherpkg/MethodSupplierOuter.java
  30  * @run main/othervm Test
  31  */
  32 
  33 import java.lang.reflect.InvocationTargetException;
  34 import java.lang.reflect.Method;
  35 import java.util.HashMap;
  36 import java.util.HashSet;
  37 import jdk.internal.org.objectweb.asm.ClassWriter;
  38 import jdk.internal.org.objectweb.asm.MethodVisitor;
  39 import jdk.internal.org.objectweb.asm.ClassVisitor;
  40 import jdk.internal.org.objectweb.asm.Opcodes;
  41 
  42 interface MyFunctionalInterface {
  43 
  44     void invokeMethodReference();
  45 }
  46 
  47 class MakeProtected implements BogoLoader.VisitorMaker, Opcodes {
  48 
  49     final boolean whenVisitInner;
  50 
  51     MakeProtected(boolean when_visit_inner) {
  52         super();
  53         whenVisitInner = when_visit_inner;
  54     }
  55 
  56     public ClassVisitor make(ClassVisitor cv) {
  57         return new ClassVisitor(Opcodes.ASM7, cv) {
  58 
  59             @Override
  60             public void visitInnerClass(String name, String outerName,
  61                     String innerName, int access) {
  62                 if (whenVisitInner) {
  63                     int access_ = (ACC_PROTECTED | access) & ~(ACC_PRIVATE | ACC_PUBLIC);
  64                     System.out.println("visitInnerClass: name = " + name
  65                             + ", outerName = " + outerName
  66                             + ", innerName = " + innerName
  67                             + ", access original = 0x" + Integer.toHexString(access)
  68                             + ", access modified to 0x" + Integer.toHexString(access_));
  69                     access = access_;
  70                 }
  71                 super.visitInnerClass(name, outerName, innerName, access);
  72             }
  73         };
  74     }
  75 };
  76 
  77 public class Test {
  78 
  79     public static void main(String argv[]) throws Exception, Throwable {
  80         BogoLoader.VisitorMaker makeProtectedNop = new MakeProtected(false);
  81         BogoLoader.VisitorMaker makeProtectedMod = new MakeProtected(true);
  82 
  83         int errors = 0;
  84         errors += tryModifiedInvocation(makeProtectedNop);
  85         errors += tryModifiedInvocation(makeProtectedMod);
  86 
  87         if (errors > 0) {
  88             throw new Error("FAIL; there were errors");
  89         }
  90     }
  91 
  92     private static int tryModifiedInvocation(BogoLoader.VisitorMaker makeProtected)
  93             throws Throwable, ClassNotFoundException {
  94         HashMap<String, BogoLoader.VisitorMaker> replace
  95                 = new HashMap<String, BogoLoader.VisitorMaker>();
  96         replace.put("anotherpkg.MethodSupplierOuter$MethodSupplier", makeProtected);
  97         HashSet<String> in_bogus = new HashSet<String>();
  98         in_bogus.add("MethodInvoker");
  99         in_bogus.add("MyFunctionalInterface");
 100         in_bogus.add("anotherpkg.MethodSupplierOuter"); // seems to be never loaded
 101         in_bogus.add("anotherpkg.MethodSupplierOuter$MethodSupplier");
 102 
 103         BogoLoader bl = new BogoLoader(in_bogus, replace);
 104         try {
 105             Class<?> isw = bl.loadClass("MethodInvoker");
 106             Method meth = isw.getMethod("invoke");
 107             Object result = meth.invoke(null);
 108         } catch (Throwable th) {
 109             System.out.flush();
 110             Thread.sleep(250); // Let Netbeans get its I/O sorted out.
 111             th.printStackTrace();
 112             System.err.flush();
 113             Thread.sleep(250); // Let Netbeans get its I/O sorted out.
 114             return 1;
 115         }
 116         return 0;
 117     }
 118 }