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