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