author | iignatyev |
Fri, 14 Sep 2018 14:02:57 -0700 | |
changeset 51754 | 594919232b8f |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
1 |
/* |
40193 | 2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
4 |
* |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
8 |
* |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
13 |
* accompanied this code). |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
14 |
* |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
18 |
* |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
21 |
* questions. |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
22 |
*/ |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
23 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
24 |
/* @test |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
25 |
* @bug 8046903 |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
26 |
* @summary VM anonymous class members can't be statically invocable |
36966 | 27 |
* @modules java.base/jdk.internal.misc java.base/jdk.internal.org.objectweb.asm |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
28 |
* @run junit test.java.lang.invoke.VMAnonymousClass |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
29 |
*/ |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
30 |
package test.java.lang.invoke; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
31 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
32 |
import java.lang.invoke.MethodHandle; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
33 |
import java.lang.invoke.MethodHandles; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
34 |
import java.lang.invoke.MethodType; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
35 |
import org.junit.Test; |
36966 | 36 |
import jdk.internal.misc.Unsafe; |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
37 |
import jdk.internal.org.objectweb.asm.*; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
38 |
import static jdk.internal.org.objectweb.asm.Opcodes.*; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
39 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
40 |
public class VMAnonymousClass { |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
41 |
public static void main(String[] args) throws Throwable { |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
42 |
VMAnonymousClass test = new VMAnonymousClass(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
43 |
test.testJavaLang(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
44 |
test.testJavaUtil(); |
36966 | 45 |
test.testJdkInternalMisc(); |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
46 |
test.testJavaLangInvoke(); |
40193 | 47 |
test.testProhibitedJavaPkg(); |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
48 |
System.out.println("TEST PASSED"); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
49 |
} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
50 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
51 |
// Test VM anonymous classes from different packages |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
52 |
// (see j.l.i.InvokerBytecodeGenerator::isStaticallyInvocable). |
36966 | 53 |
@Test public void testJavaLang() throws Throwable { test("java/lang"); } |
54 |
@Test public void testJavaUtil() throws Throwable { test("java/util"); } |
|
55 |
@Test public void testJdkInternalMisc() throws Throwable { test("jdk/internal/misc"); } |
|
56 |
@Test public void testJavaLangInvoke() throws Throwable { test("java/lang/invoke"); } |
|
40193 | 57 |
@Test public void testProhibitedJavaPkg() throws Throwable { |
58 |
try { |
|
40964
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
59 |
test("java/prohibited"); |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
60 |
} catch (IllegalArgumentException e) { |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
61 |
return; |
40193 | 62 |
} |
63 |
throw new RuntimeException("Expected SecurityException"); |
|
64 |
} |
|
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
65 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40964
diff
changeset
|
66 |
private static Unsafe unsafe = Unsafe.getUnsafe(); |
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
67 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
68 |
private static void test(String pkg) throws Throwable { |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
69 |
byte[] bytes = dumpClass(pkg); |
40193 | 70 |
Class host_class; |
71 |
if (pkg.equals("java/prohibited")) { |
|
72 |
VMAnonymousClass sampleclass = new VMAnonymousClass(); |
|
73 |
host_class = (Class)sampleclass.getClass(); |
|
40964
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
74 |
} else if (pkg.equals("java/lang")) { |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
75 |
host_class = Object.class; |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
76 |
} else if (pkg.equals("java/util")) { |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
77 |
host_class = java.util.ArrayList.class; |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
78 |
} else if (pkg.equals("jdk/internal/misc")) { |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
79 |
host_class = jdk.internal.misc.Signal.class; |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
80 |
} else if (pkg.equals("java/lang/invoke")) { |
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
81 |
host_class = java.lang.invoke.CallSite.class; |
40193 | 82 |
} else { |
40964
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40193
diff
changeset
|
83 |
throw new RuntimeException("Unexpected pkg: " + pkg); |
40193 | 84 |
} |
85 |
// Define VM anonymous class |
|
86 |
Class anonClass = unsafe.defineAnonymousClass(host_class, bytes, null); |
|
24974
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
87 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
88 |
MethodType t = MethodType.methodType(Object.class, int.class); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
89 |
MethodHandle target = MethodHandles.lookup().findStatic(anonClass, "get", t); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
90 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
91 |
// Wrap target into LF (convert) to get "target" referenced from LF |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
92 |
MethodHandle wrappedMH = target.asType(MethodType.methodType(Object.class, Integer.class)); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
93 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
94 |
// Invoke enough times to provoke LF compilation to bytecode. |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
95 |
for (int i = 0; i<100; i++) { |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
96 |
Object r = wrappedMH.invokeExact((Integer)1); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
97 |
} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
98 |
} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
99 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
100 |
/* |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
101 |
* Constructs bytecode for the following class: |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
102 |
* public class pkg.MyClass { |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
103 |
* MyClass() {} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
104 |
* public Object get(int i) { return null; } |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
105 |
* } |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
106 |
*/ |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
107 |
public static byte[] dumpClass(String pkg) { |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
108 |
ClassWriter cw = new ClassWriter(0); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
109 |
MethodVisitor mv; |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
110 |
|
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
111 |
cw.visit(52, ACC_SUPER | ACC_PUBLIC, pkg+"/MyClass", null, "java/lang/Object", null); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
112 |
{ |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
113 |
mv = cw.visitMethod(0, "<init>", "()V", null, null); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
114 |
mv.visitCode(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
115 |
mv.visitVarInsn(ALOAD, 0); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
116 |
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
117 |
mv.visitInsn(RETURN); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
118 |
mv.visitMaxs(1, 1); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
119 |
mv.visitEnd(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
120 |
} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
121 |
{ |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
122 |
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "get", "(I)Ljava/lang/Object;", null, null); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
123 |
mv.visitCode(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
124 |
mv.visitInsn(ACONST_NULL); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
125 |
mv.visitInsn(ARETURN); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
126 |
mv.visitMaxs(1, 1); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
127 |
mv.visitEnd(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
128 |
} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
129 |
cw.visitEnd(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
130 |
return cw.toByteArray(); |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
131 |
} |
e4a51c336a52
8046903: VM anonymous class members can't be statically invocable
vlivanov
parents:
diff
changeset
|
132 |
} |