1 /*
2  * Copyright (c) 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 // This is copied from DefineAnon to test Symbol Refcounting for the package prepended name.
25 
26 package p1;
27 
28 import jdk.internal.org.objectweb.asm.ClassWriter;
29 import jdk.internal.org.objectweb.asm.MethodVisitor;
30 import jdk.internal.org.objectweb.asm.Opcodes;
31 import jdk.internal.misc.Unsafe;
32 
33 
34 class T {
test0()35     static           protected void test0() { System.out.println("test0 (public)"); }
test1()36     static           protected void test1() { System.out.println("test1 (protected)"); }
test2()37     static /*package-private*/ void test2() { System.out.println("test2 (package)"); }
test3()38     static             private void test3() { System.out.println("test3 (private)"); }
39 }
40 
41 public class AnonSymbolLeak {
42 
43     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
44 
getAnonClass(Class<?> hostClass, final String className)45     static Class<?> getAnonClass(Class<?> hostClass, final String className) {
46         final String superName = "java/lang/Object";
47         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
48         cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null);
49 
50         MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "test", "()V", null, null);
51         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test0", "()V", false);
52         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test1", "()V", false);
53         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test2", "()V", false);
54         mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test3", "()V", false);
55         mv.visitInsn(Opcodes.RETURN);
56         mv.visitMaxs(0, 0);
57         mv.visitEnd();
58 
59         final byte[] classBytes = cw.toByteArray();
60         Class<?> invokerClass = UNSAFE.defineAnonymousClass(hostClass, classBytes, new Object[0]);
61         UNSAFE.ensureClassInitialized(invokerClass);
62         return invokerClass;
63     }
64 
test()65     public static void test() throws Throwable {
66         // AnonClass is injected into package p1.
67         System.out.println("Injecting from the same package (p1):");
68         Class<?> p1cls = getAnonClass(T.class, "AnonClass");
69         p1cls.getMethod("test").invoke(null);
70     }
71 
main(java.lang.String[] unused)72     public static void main(java.lang.String[] unused) throws Throwable {
73         test();
74     }
75 }
76