43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
|
50858
|
24 |
|
|
25 |
|
43972
|
26 |
package org.graalvm.compiler.hotspot.test;
|
|
27 |
|
|
28 |
import java.lang.reflect.Method;
|
|
29 |
|
|
30 |
import org.graalvm.compiler.core.test.GraalCompilerTest;
|
46640
|
31 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
32 |
import org.graalvm.compiler.graph.Node;
|
|
33 |
import org.graalvm.compiler.nodes.Invoke;
|
|
34 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
35 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
55509
|
36 |
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
|
58299
|
37 |
import org.graalvm.compiler.test.ModuleSupport;
|
43972
|
38 |
import org.junit.BeforeClass;
|
|
39 |
import org.junit.Test;
|
44836
|
40 |
import org.objectweb.asm.ClassWriter;
|
|
41 |
import org.objectweb.asm.MethodVisitor;
|
|
42 |
import org.objectweb.asm.Opcodes;
|
43972
|
43 |
|
|
44 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
45 |
|
46344
|
46 |
public class ConstantPoolSubstitutionsTests extends GraalCompilerTest {
|
|
47 |
|
|
48 |
public ConstantPoolSubstitutionsTests() {
|
|
49 |
exportPackage(JAVA_BASE, "jdk.internal.org.objectweb.asm");
|
|
50 |
}
|
43972
|
51 |
|
|
52 |
@SuppressWarnings("try")
|
|
53 |
protected StructuredGraph test(final String snippet) {
|
|
54 |
ResolvedJavaMethod method = getMetaAccess().lookupJavaMethod(getMethod(snippet));
|
46640
|
55 |
DebugContext debug = getDebugContext();
|
|
56 |
try (DebugContext.Scope s = debug.scope("ConstantPoolSubstitutionsTests", method)) {
|
43972
|
57 |
StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
|
|
58 |
compile(graph.method(), graph);
|
|
59 |
assertNotInGraph(graph, Invoke.class);
|
46640
|
60 |
debug.dump(DebugContext.BASIC_LEVEL, graph, snippet);
|
43972
|
61 |
return graph;
|
|
62 |
} catch (Throwable e) {
|
46640
|
63 |
throw debug.handle(e);
|
43972
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) {
|
|
68 |
for (Node node : graph.getNodes()) {
|
|
69 |
if (clazz.isInstance(node)) {
|
|
70 |
fail(node.toString());
|
|
71 |
}
|
|
72 |
}
|
|
73 |
return graph;
|
|
74 |
}
|
|
75 |
|
58533
|
76 |
private static String getMiscPackage() {
|
|
77 |
if (JavaVersionUtil.JAVA_SPEC <= 8) {
|
|
78 |
return "sun.misc";
|
|
79 |
}
|
|
80 |
try {
|
|
81 |
String miscPackage = "jdk.internal.access";
|
|
82 |
Class.forName(miscPackage + ".SharedSecrets");
|
|
83 |
return miscPackage;
|
|
84 |
} catch (ClassNotFoundException e) {
|
|
85 |
try {
|
|
86 |
String miscPackage = "jdk.internal.misc";
|
|
87 |
Class.forName(miscPackage + ".SharedSecrets");
|
|
88 |
return miscPackage;
|
|
89 |
} catch (ClassNotFoundException ex) {
|
|
90 |
}
|
|
91 |
throw new AssertionError(e);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
43972
|
95 |
private static Object getConstantPoolForObject() {
|
58533
|
96 |
String miscPackage = getMiscPackage();
|
43972
|
97 |
try {
|
|
98 |
Class<?> sharedSecretsClass = Class.forName(miscPackage + ".SharedSecrets");
|
|
99 |
Class<?> javaLangAccessClass = Class.forName(miscPackage + ".JavaLangAccess");
|
|
100 |
Object jla = sharedSecretsClass.getDeclaredMethod("getJavaLangAccess").invoke(null);
|
|
101 |
return javaLangAccessClass.getDeclaredMethod("getConstantPool", Class.class).invoke(jla, Object.class);
|
|
102 |
} catch (Exception e) {
|
|
103 |
throw new AssertionError(e);
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Get the test methods from the generated class.
|
|
109 |
*/
|
|
110 |
@Override
|
|
111 |
protected Method getMethod(String methodName) {
|
|
112 |
Class<?> cl;
|
|
113 |
try {
|
|
114 |
cl = LOADER.findClass(AsmLoader.NAME);
|
|
115 |
addExports(cl);
|
|
116 |
} catch (ClassNotFoundException e) {
|
|
117 |
throw new AssertionError(e);
|
|
118 |
}
|
|
119 |
return getMethod(cl, methodName);
|
|
120 |
}
|
|
121 |
|
|
122 |
@BeforeClass
|
|
123 |
public static void beforeClass() {
|
|
124 |
addExports(AsmLoader.class);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
58299
|
128 |
* This test uses some non-public API.
|
43972
|
129 |
*/
|
|
130 |
private static void addExports(Class<?> c) {
|
58533
|
131 |
ModuleSupport.exportPackageTo(String.class, getMiscPackage(), c);
|
58299
|
132 |
ModuleSupport.exportPackageTo(String.class, "jdk.internal.reflect", c);
|
43972
|
133 |
}
|
|
134 |
|
|
135 |
@Test
|
|
136 |
public void testGetSize() {
|
|
137 |
Object cp = getConstantPoolForObject();
|
|
138 |
test("getSize", cp);
|
|
139 |
}
|
|
140 |
|
|
141 |
@Test
|
|
142 |
public void testGetIntAt() {
|
|
143 |
test("getIntAt");
|
|
144 |
}
|
|
145 |
|
|
146 |
@Test
|
|
147 |
public void testGetLongAt() {
|
|
148 |
test("getLongAt");
|
|
149 |
}
|
|
150 |
|
|
151 |
@Test
|
|
152 |
public void testGetFloatAt() {
|
|
153 |
test("getFloatAt");
|
|
154 |
}
|
|
155 |
|
|
156 |
@Test
|
|
157 |
public void testGetDoubleAt() {
|
|
158 |
test("getDoubleAt");
|
|
159 |
}
|
|
160 |
|
|
161 |
private static final String PACKAGE_NAME = ConstantPoolSubstitutionsTests.class.getPackage().getName();
|
|
162 |
private static final String PACKAGE_NAME_INTERNAL = PACKAGE_NAME.replace('.', '/');
|
|
163 |
|
|
164 |
private static AsmLoader LOADER = new AsmLoader(ConstantPoolSubstitutionsTests.class.getClassLoader());
|
|
165 |
|
|
166 |
public static class AsmLoader extends ClassLoader {
|
|
167 |
Class<?> loaded;
|
|
168 |
|
|
169 |
static final String NAME = PACKAGE_NAME + ".ConstantPoolTest";
|
|
170 |
|
|
171 |
public AsmLoader(ClassLoader parent) {
|
|
172 |
super(parent);
|
|
173 |
}
|
|
174 |
|
|
175 |
@Override
|
|
176 |
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
|
177 |
if (name.equals(NAME)) {
|
|
178 |
if (loaded != null) {
|
|
179 |
return loaded;
|
|
180 |
}
|
46344
|
181 |
byte[] bytes = Gen.generateClass();
|
43972
|
182 |
return (loaded = defineClass(name, bytes, 0, bytes.length));
|
|
183 |
} else {
|
|
184 |
return super.findClass(name);
|
|
185 |
}
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
46344
|
189 |
static class Gen implements Opcodes {
|
|
190 |
// @formatter:off
|
|
191 |
/*
|
|
192 |
static class ConstantPoolTest {
|
|
193 |
public static int getSize(Object o) {
|
|
194 |
ConstantPool cp = (ConstantPool) o;
|
|
195 |
return cp.getSize();
|
|
196 |
}
|
|
197 |
|
|
198 |
public static int getIntAt(Object o) {
|
|
199 |
ConstantPool cp = (ConstantPool) o;
|
|
200 |
return cp.getIntAt(0);
|
|
201 |
}
|
|
202 |
|
|
203 |
public static long getLongAt(Object o) {
|
|
204 |
ConstantPool cp = (ConstantPool) o;
|
|
205 |
return cp.getLongAt(0);
|
|
206 |
}
|
|
207 |
|
|
208 |
public static float getFloatAt(Object o) {
|
|
209 |
ConstantPool cp = (ConstantPool) o;
|
|
210 |
return cp.getFloatAt(0);
|
|
211 |
}
|
|
212 |
|
|
213 |
public static double getDoubleAt(Object o) {
|
|
214 |
ConstantPool cp = (ConstantPool) o;
|
|
215 |
return cp.getDoubleAt(0);
|
|
216 |
}
|
|
217 |
|
|
218 |
public static String getUTF8At(Object o) {
|
|
219 |
ConstantPool cp = (ConstantPool) o;
|
|
220 |
return cp.getUTF8At(0);
|
|
221 |
}
|
43972
|
222 |
}
|
46344
|
223 |
*/
|
|
224 |
// @formatter:on
|
43972
|
225 |
|
46344
|
226 |
static byte[] generateClass() {
|
|
227 |
|
|
228 |
ClassWriter cw = new ClassWriter(0);
|
|
229 |
MethodVisitor mv;
|
|
230 |
|
|
231 |
cw.visit(52, ACC_SUPER, PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", null, "java/lang/Object", null);
|
|
232 |
cw.visitInnerClass(PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", PACKAGE_NAME_INTERNAL + "/ConstantPoolSubstitutionsTests", "ConstantPoolTest",
|
|
233 |
ACC_STATIC);
|
55509
|
234 |
String constantPool = JavaVersionUtil.JAVA_SPEC <= 8 ? "sun/reflect/ConstantPool" : "jdk/internal/reflect/ConstantPool";
|
46344
|
235 |
|
|
236 |
mv = cw.visitMethod(0, "<init>", "()V", null, null);
|
|
237 |
mv.visitCode();
|
|
238 |
mv.visitVarInsn(ALOAD, 0);
|
|
239 |
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
|
240 |
mv.visitInsn(RETURN);
|
|
241 |
mv.visitMaxs(1, 1);
|
|
242 |
mv.visitEnd();
|
43972
|
243 |
|
46344
|
244 |
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getSize", "(Ljava/lang/Object;)I", null, null);
|
|
245 |
mv.visitCode();
|
|
246 |
mv.visitVarInsn(ALOAD, 0);
|
|
247 |
mv.visitTypeInsn(CHECKCAST, constantPool);
|
|
248 |
mv.visitVarInsn(ASTORE, 1);
|
|
249 |
mv.visitVarInsn(ALOAD, 1);
|
|
250 |
mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getSize", "()I", false);
|
|
251 |
mv.visitInsn(IRETURN);
|
|
252 |
mv.visitMaxs(1, 3);
|
|
253 |
mv.visitEnd();
|
|
254 |
|
|
255 |
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getIntAt", "(Ljava/lang/Object;)I", null, null);
|
|
256 |
mv.visitCode();
|
|
257 |
mv.visitVarInsn(ALOAD, 0);
|
|
258 |
mv.visitTypeInsn(CHECKCAST, constantPool);
|
|
259 |
mv.visitVarInsn(ASTORE, 1);
|
|
260 |
mv.visitVarInsn(ALOAD, 1);
|
|
261 |
mv.visitInsn(ICONST_0);
|
|
262 |
mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getIntAt", "(I)I", false);
|
|
263 |
mv.visitInsn(IRETURN);
|
|
264 |
mv.visitMaxs(2, 3);
|
|
265 |
mv.visitEnd();
|
|
266 |
|
|
267 |
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getLongAt", "(Ljava/lang/Object;)J", null, null);
|
|
268 |
mv.visitCode();
|
|
269 |
mv.visitVarInsn(ALOAD, 0);
|
|
270 |
mv.visitTypeInsn(CHECKCAST, constantPool);
|
|
271 |
mv.visitVarInsn(ASTORE, 1);
|
|
272 |
mv.visitVarInsn(ALOAD, 1);
|
|
273 |
mv.visitInsn(ICONST_0);
|
|
274 |
mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getLongAt", "(I)J", false);
|
|
275 |
mv.visitInsn(LRETURN);
|
|
276 |
mv.visitMaxs(2, 3);
|
|
277 |
mv.visitEnd();
|
43972
|
278 |
|
46344
|
279 |
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getFloatAt", "(Ljava/lang/Object;)F", null, null);
|
|
280 |
mv.visitCode();
|
|
281 |
mv.visitVarInsn(ALOAD, 0);
|
|
282 |
mv.visitTypeInsn(CHECKCAST, constantPool);
|
|
283 |
mv.visitVarInsn(ASTORE, 1);
|
|
284 |
mv.visitVarInsn(ALOAD, 1);
|
|
285 |
mv.visitInsn(ICONST_0);
|
|
286 |
mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getFloatAt", "(I)F", false);
|
|
287 |
mv.visitInsn(FRETURN);
|
|
288 |
mv.visitMaxs(2, 3);
|
|
289 |
mv.visitEnd();
|
43972
|
290 |
|
46344
|
291 |
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getDoubleAt", "(Ljava/lang/Object;)D", null, null);
|
|
292 |
mv.visitCode();
|
|
293 |
mv.visitVarInsn(ALOAD, 0);
|
|
294 |
mv.visitTypeInsn(CHECKCAST, constantPool);
|
|
295 |
mv.visitVarInsn(ASTORE, 1);
|
|
296 |
mv.visitVarInsn(ALOAD, 1);
|
|
297 |
mv.visitInsn(ICONST_0);
|
|
298 |
mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getDoubleAt", "(I)D", false);
|
|
299 |
mv.visitInsn(DRETURN);
|
|
300 |
mv.visitMaxs(2, 3);
|
|
301 |
mv.visitEnd();
|
43972
|
302 |
|
46344
|
303 |
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getUTF8At", "(Ljava/lang/Object;)Ljava/lang/String;", null, null);
|
|
304 |
mv.visitCode();
|
|
305 |
mv.visitVarInsn(ALOAD, 0);
|
|
306 |
mv.visitTypeInsn(CHECKCAST, constantPool);
|
|
307 |
mv.visitVarInsn(ASTORE, 1);
|
|
308 |
mv.visitVarInsn(ALOAD, 1);
|
|
309 |
mv.visitInsn(ICONST_0);
|
|
310 |
mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getUTF8At", "(I)Ljava/lang/String;", false);
|
|
311 |
mv.visitInsn(ARETURN);
|
|
312 |
mv.visitMaxs(2, 3);
|
|
313 |
mv.visitEnd();
|
|
314 |
cw.visitEnd();
|
|
315 |
|
|
316 |
return cw.toByteArray();
|
43972
|
317 |
}
|
|
318 |
}
|
|
319 |
}
|