1 /*
   2  * Copyright (c) 2015, 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" : "jdk.internal.misc";
  79         try {
  80             Class<?> sharedSecretsClass = Class.forName(miscPackage + ".SharedSecrets");
  81             Class<?> javaLangAccessClass = Class.forName(miscPackage + ".JavaLangAccess");
  82             Object jla = sharedSecretsClass.getDeclaredMethod("getJavaLangAccess").invoke(null);
  83             return javaLangAccessClass.getDeclaredMethod("getConstantPool", Class.class).invoke(jla, Object.class);
  84         } catch (Exception e) {
  85             throw new AssertionError(e);
  86         }
  87     }
  88 
  89     /**
  90      * Get the test methods from the generated class.
  91      */
  92     @Override
  93     protected Method getMethod(String methodName) {
  94         Class<?> cl;
  95         try {
  96             cl = LOADER.findClass(AsmLoader.NAME);
  97             addExports(cl);
  98         } catch (ClassNotFoundException e) {
  99             throw new AssertionError(e);
 100         }
 101         return getMethod(cl, methodName);
 102     }
 103 
 104     @BeforeClass
 105     public static void beforeClass() {
 106         addExports(AsmLoader.class);
 107     }
 108 
 109     /**
 110      * This test uses some API hidden by the JDK9 module system.
 111      */
 112     private static void addExports(Class<?> c) {
 113         if (!Java8OrEarlier) {
 114             Object javaBaseModule = JLModule.fromClass(String.class);
 115             Object cModule = JLModule.fromClass(c);
 116             uncheckedAddExports(javaBaseModule, "jdk.internal.reflect", cModule);
 117             uncheckedAddExports(javaBaseModule, "jdk.internal.misc", cModule);
 118         }
 119     }
 120 
 121     @Test
 122     public void testGetSize() {
 123         Object cp = getConstantPoolForObject();
 124         test("getSize", cp);
 125     }
 126 
 127     @Test
 128     public void testGetIntAt() {
 129         test("getIntAt");
 130     }
 131 
 132     @Test
 133     public void testGetLongAt() {
 134         test("getLongAt");
 135     }
 136 
 137     @Test
 138     public void testGetFloatAt() {
 139         test("getFloatAt");
 140     }
 141 
 142     @Test
 143     public void testGetDoubleAt() {
 144         test("getDoubleAt");
 145     }
 146 
 147     private static final String PACKAGE_NAME = ConstantPoolSubstitutionsTests.class.getPackage().getName();
 148     private static final String PACKAGE_NAME_INTERNAL = PACKAGE_NAME.replace('.', '/');
 149 
 150     private static AsmLoader LOADER = new AsmLoader(ConstantPoolSubstitutionsTests.class.getClassLoader());
 151 
 152     public static class AsmLoader extends ClassLoader {
 153         Class<?> loaded;
 154 
 155         static final String NAME = PACKAGE_NAME + ".ConstantPoolTest";
 156 
 157         public AsmLoader(ClassLoader parent) {
 158             super(parent);
 159         }
 160 
 161         @Override
 162         protected Class<?> findClass(String name) throws ClassNotFoundException {
 163             if (name.equals(NAME)) {
 164                 if (loaded != null) {
 165                     return loaded;
 166                 }
 167                 byte[] bytes = Gen.generateClass();
 168                 return (loaded = defineClass(name, bytes, 0, bytes.length));
 169             } else {
 170                 return super.findClass(name);
 171             }
 172         }
 173     }
 174 
 175     static class Gen implements Opcodes {
 176         // @formatter:off
 177         /*
 178         static class ConstantPoolTest {
 179             public static int getSize(Object o) {
 180                 ConstantPool cp = (ConstantPool) o;
 181                 return cp.getSize();
 182             }
 183 
 184             public static int getIntAt(Object o) {
 185                 ConstantPool cp = (ConstantPool) o;
 186                 return cp.getIntAt(0);
 187             }
 188 
 189             public static long getLongAt(Object o) {
 190                 ConstantPool cp = (ConstantPool) o;
 191                 return cp.getLongAt(0);
 192             }
 193 
 194             public static float getFloatAt(Object o) {
 195                 ConstantPool cp = (ConstantPool) o;
 196                 return cp.getFloatAt(0);
 197             }
 198 
 199             public static double getDoubleAt(Object o) {
 200                 ConstantPool cp = (ConstantPool) o;
 201                 return cp.getDoubleAt(0);
 202             }
 203 
 204             public static String getUTF8At(Object o) {
 205                 ConstantPool cp = (ConstantPool) o;
 206                 return cp.getUTF8At(0);
 207             }
 208         }
 209         */
 210         // @formatter:on
 211 
 212         static byte[] generateClass() {
 213 
 214             ClassWriter cw = new ClassWriter(0);
 215             MethodVisitor mv;
 216 
 217             cw.visit(52, ACC_SUPER, PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", null, "java/lang/Object", null);
 218             cw.visitInnerClass(PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", PACKAGE_NAME_INTERNAL + "/ConstantPoolSubstitutionsTests", "ConstantPoolTest",
 219                             ACC_STATIC);
 220             String constantPool = Java8OrEarlier ? "sun/reflect/ConstantPool" : "jdk/internal/reflect/ConstantPool";
 221 
 222             mv = cw.visitMethod(0, "<init>", "()V", null, null);
 223             mv.visitCode();
 224             mv.visitVarInsn(ALOAD, 0);
 225             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 226             mv.visitInsn(RETURN);
 227             mv.visitMaxs(1, 1);
 228             mv.visitEnd();
 229 
 230             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getSize", "(Ljava/lang/Object;)I", null, null);
 231             mv.visitCode();
 232             mv.visitVarInsn(ALOAD, 0);
 233             mv.visitTypeInsn(CHECKCAST, constantPool);
 234             mv.visitVarInsn(ASTORE, 1);
 235             mv.visitVarInsn(ALOAD, 1);
 236             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getSize", "()I", false);
 237             mv.visitInsn(IRETURN);
 238             mv.visitMaxs(1, 3);
 239             mv.visitEnd();
 240 
 241             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getIntAt", "(Ljava/lang/Object;)I", null, null);
 242             mv.visitCode();
 243             mv.visitVarInsn(ALOAD, 0);
 244             mv.visitTypeInsn(CHECKCAST, constantPool);
 245             mv.visitVarInsn(ASTORE, 1);
 246             mv.visitVarInsn(ALOAD, 1);
 247             mv.visitInsn(ICONST_0);
 248             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getIntAt", "(I)I", false);
 249             mv.visitInsn(IRETURN);
 250             mv.visitMaxs(2, 3);
 251             mv.visitEnd();
 252 
 253             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getLongAt", "(Ljava/lang/Object;)J", null, null);
 254             mv.visitCode();
 255             mv.visitVarInsn(ALOAD, 0);
 256             mv.visitTypeInsn(CHECKCAST, constantPool);
 257             mv.visitVarInsn(ASTORE, 1);
 258             mv.visitVarInsn(ALOAD, 1);
 259             mv.visitInsn(ICONST_0);
 260             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getLongAt", "(I)J", false);
 261             mv.visitInsn(LRETURN);
 262             mv.visitMaxs(2, 3);
 263             mv.visitEnd();
 264 
 265             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getFloatAt", "(Ljava/lang/Object;)F", null, null);
 266             mv.visitCode();
 267             mv.visitVarInsn(ALOAD, 0);
 268             mv.visitTypeInsn(CHECKCAST, constantPool);
 269             mv.visitVarInsn(ASTORE, 1);
 270             mv.visitVarInsn(ALOAD, 1);
 271             mv.visitInsn(ICONST_0);
 272             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getFloatAt", "(I)F", false);
 273             mv.visitInsn(FRETURN);
 274             mv.visitMaxs(2, 3);
 275             mv.visitEnd();
 276 
 277             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getDoubleAt", "(Ljava/lang/Object;)D", null, null);
 278             mv.visitCode();
 279             mv.visitVarInsn(ALOAD, 0);
 280             mv.visitTypeInsn(CHECKCAST, constantPool);
 281             mv.visitVarInsn(ASTORE, 1);
 282             mv.visitVarInsn(ALOAD, 1);
 283             mv.visitInsn(ICONST_0);
 284             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getDoubleAt", "(I)D", false);
 285             mv.visitInsn(DRETURN);
 286             mv.visitMaxs(2, 3);
 287             mv.visitEnd();
 288 
 289             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getUTF8At", "(Ljava/lang/Object;)Ljava/lang/String;", null, null);
 290             mv.visitCode();
 291             mv.visitVarInsn(ALOAD, 0);
 292             mv.visitTypeInsn(CHECKCAST, constantPool);
 293             mv.visitVarInsn(ASTORE, 1);
 294             mv.visitVarInsn(ALOAD, 1);
 295             mv.visitInsn(ICONST_0);
 296             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getUTF8At", "(I)Ljava/lang/String;", false);
 297             mv.visitInsn(ARETURN);
 298             mv.visitMaxs(2, 3);
 299             mv.visitEnd();
 300             cw.visitEnd();
 301 
 302             return cw.toByteArray();
 303         }
 304     }
 305 }