1 /*
   2  * Copyright (c) 2016, 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 DefineAnon
  26  * @bug 8058575
  27  * @library /testlibrary
  28  * @modules java.base/jdk.internal.org.objectweb.asm
  29  *          java.management
  30  * @compile -XDignore.symbol.file=true DefineAnon.java
  31  * @run main/othervm p1.DefineAnon
  32  */
  33 
  34 package p1;
  35 
  36 import jdk.internal.org.objectweb.asm.ClassWriter;
  37 import jdk.internal.org.objectweb.asm.MethodVisitor;
  38 import jdk.internal.org.objectweb.asm.Opcodes;
  39 import sun.misc.Unsafe;
  40 
  41 
  42 class T {
  43     static           protected void test0() { System.out.println("test0 (public)"); }
  44     static           protected void test1() { System.out.println("test1 (protected)"); }
  45     static /*package-private*/ void test2() { System.out.println("test2 (package)"); }
  46     static             private void test3() { System.out.println("test3 (private)"); }
  47 }
  48 
  49 public class DefineAnon {
  50 
  51     private static Unsafe getUnsafe() {
  52         try {
  53             java.lang.reflect.Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
  54             singleoneInstanceField.setAccessible(true);
  55             return (Unsafe) singleoneInstanceField.get(null);
  56         } catch (Throwable ex) {
  57             throw new RuntimeException("Was unable to get Unsafe instance.");
  58         }
  59     }
  60 
  61     static Unsafe UNSAFE = DefineAnon.getUnsafe();
  62 
  63     static Class<?> getAnonClass(Class<?> hostClass, final String className) {
  64         final String superName = "java/lang/Object";
  65         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
  66         cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null);
  67 
  68         MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "test", "()V", null, null);
  69         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test0", "()V", false);
  70         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test1", "()V", false);
  71         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test2", "()V", false);
  72         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test3", "()V", false);
  73         mv.visitInsn(Opcodes.RETURN);
  74         mv.visitMaxs(0, 0);
  75         mv.visitEnd();
  76 
  77         final byte[] classBytes = cw.toByteArray();
  78         Class<?> invokerClass = UNSAFE.defineAnonymousClass(hostClass, classBytes, new Object[0]);
  79         UNSAFE.ensureClassInitialized(invokerClass);
  80         return invokerClass;
  81     }
  82 
  83     public static void main(String[] args) throws Throwable {
  84         Throwable fail = null;
  85 
  86         // Anonymous class has the privileges of its host class, so test[0123] should all work.
  87         System.out.println("Injecting from the same package (p1):");
  88         Class<?> p1cls = getAnonClass(T.class, "p1/AnonClass");
  89         try {
  90             p1cls.getMethod("test").invoke(null);
  91         } catch (Throwable ex) {
  92             ex.printStackTrace();
  93             fail = ex;  // throw this to make test fail, since subtest failed
  94         }
  95 
  96         // Anonymous class has different package name from host class.  Should throw
  97         // IllegalArgumentException.
  98         System.out.println("Injecting from the wrong package (p2):");
  99         try {
 100             Class<?> p2cls = getAnonClass(DefineAnon.class, "p2/AnonClass");
 101             p2cls.getMethod("test").invoke(null);
 102             System.out.println("Failed, did not get expected IllegalArgumentException");
 103         } catch (java.lang.IllegalArgumentException e) {
 104             if (e.getMessage().contains("Host class p1/DefineAnon and anonymous class p2/AnonClass")) {
 105                 System.out.println("Got expected IllegalArgumentException: " + e.getMessage());
 106             } else {
 107                 throw new RuntimeException("Unexpected message: " + e.getMessage());
 108             }
 109         } catch (Throwable ex) {
 110             ex.printStackTrace();
 111             fail = ex;  // throw this to make test fail, since subtest failed
 112         }
 113 
 114         // Inject a class in the unnamed package into p1.T.  It should be able
 115         // to access all methods in p1.T.
 116         System.out.println("Injecting unnamed package into correct host class:");
 117         try {
 118             Class<?> p3cls = getAnonClass(T.class, "AnonClass");
 119             p3cls.getMethod("test").invoke(null);
 120         } catch (Throwable ex) {
 121             ex.printStackTrace();
 122             fail = ex;  // throw this to make test fail, since subtest failed
 123         }
 124 
 125         // Try using an array class as the host class.  This should throw IllegalArgumentException.
 126         try {
 127             Class<?> p3cls = getAnonClass(String[].class, "AnonClass");
 128             throw new RuntimeException("Expected IllegalArgumentException not thrown");
 129         } catch (IllegalArgumentException ex) {
 130         }
 131 
 132         if (fail != null) throw fail;  // make test fail, since subtest failed
 133     }
 134 }