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