1 /*
   2  * Copyright (c) 2015, 2019, 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.serviceprovider.JavaVersionUtil.JAVA_SPEC;
  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.ModuleSupport;
  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 non-public API.
 113      */
 114     private static void addExports(Class<?> c) {
 115         String packageName = JAVA_SPEC <= 11 ? "jdk.internal.misc" : "jdk.internal.access";
 116         ModuleSupport.exportPackageTo(String.class, packageName, c);
 117         ModuleSupport.exportPackageTo(String.class, "jdk.internal.reflect", c);
 118     }
 119 
 120     @Test
 121     public void testGetSize() {
 122         Object cp = getConstantPoolForObject();
 123         test("getSize", cp);
 124     }
 125 
 126     @Test
 127     public void testGetIntAt() {
 128         test("getIntAt");
 129     }
 130 
 131     @Test
 132     public void testGetLongAt() {
 133         test("getLongAt");
 134     }
 135 
 136     @Test
 137     public void testGetFloatAt() {
 138         test("getFloatAt");
 139     }
 140 
 141     @Test
 142     public void testGetDoubleAt() {
 143         test("getDoubleAt");
 144     }
 145 
 146     private static final String PACKAGE_NAME = ConstantPoolSubstitutionsTests.class.getPackage().getName();
 147     private static final String PACKAGE_NAME_INTERNAL = PACKAGE_NAME.replace('.', '/');
 148 
 149     private static AsmLoader LOADER = new AsmLoader(ConstantPoolSubstitutionsTests.class.getClassLoader());
 150 
 151     public static class AsmLoader extends ClassLoader {
 152         Class<?> loaded;
 153 
 154         static final String NAME = PACKAGE_NAME + ".ConstantPoolTest";
 155 
 156         public AsmLoader(ClassLoader parent) {
 157             super(parent);
 158         }
 159 
 160         @Override
 161         protected Class<?> findClass(String name) throws ClassNotFoundException {
 162             if (name.equals(NAME)) {
 163                 if (loaded != null) {
 164                     return loaded;
 165                 }
 166                 byte[] bytes = Gen.generateClass();
 167                 return (loaded = defineClass(name, bytes, 0, bytes.length));
 168             } else {
 169                 return super.findClass(name);
 170             }
 171         }
 172     }
 173 
 174     static class Gen implements Opcodes {
 175         // @formatter:off
 176         /*
 177         static class ConstantPoolTest {
 178             public static int getSize(Object o) {
 179                 ConstantPool cp = (ConstantPool) o;
 180                 return cp.getSize();
 181             }
 182 
 183             public static int getIntAt(Object o) {
 184                 ConstantPool cp = (ConstantPool) o;
 185                 return cp.getIntAt(0);
 186             }
 187 
 188             public static long getLongAt(Object o) {
 189                 ConstantPool cp = (ConstantPool) o;
 190                 return cp.getLongAt(0);
 191             }
 192 
 193             public static float getFloatAt(Object o) {
 194                 ConstantPool cp = (ConstantPool) o;
 195                 return cp.getFloatAt(0);
 196             }
 197 
 198             public static double getDoubleAt(Object o) {
 199                 ConstantPool cp = (ConstantPool) o;
 200                 return cp.getDoubleAt(0);
 201             }
 202 
 203             public static String getUTF8At(Object o) {
 204                 ConstantPool cp = (ConstantPool) o;
 205                 return cp.getUTF8At(0);
 206             }
 207         }
 208         */
 209         // @formatter:on
 210 
 211         static byte[] generateClass() {
 212 
 213             ClassWriter cw = new ClassWriter(0);
 214             MethodVisitor mv;
 215 
 216             cw.visit(52, ACC_SUPER, PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", null, "java/lang/Object", null);
 217             cw.visitInnerClass(PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", PACKAGE_NAME_INTERNAL + "/ConstantPoolSubstitutionsTests", "ConstantPoolTest",
 218                             ACC_STATIC);
 219             String constantPool = JavaVersionUtil.JAVA_SPEC <= 8 ? "sun/reflect/ConstantPool" : "jdk/internal/reflect/ConstantPool";
 220 
 221             mv = cw.visitMethod(0, "<init>", "()V", null, null);
 222             mv.visitCode();
 223             mv.visitVarInsn(ALOAD, 0);
 224             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 225             mv.visitInsn(RETURN);
 226             mv.visitMaxs(1, 1);
 227             mv.visitEnd();
 228 
 229             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getSize", "(Ljava/lang/Object;)I", null, null);
 230             mv.visitCode();
 231             mv.visitVarInsn(ALOAD, 0);
 232             mv.visitTypeInsn(CHECKCAST, constantPool);
 233             mv.visitVarInsn(ASTORE, 1);
 234             mv.visitVarInsn(ALOAD, 1);
 235             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getSize", "()I", false);
 236             mv.visitInsn(IRETURN);
 237             mv.visitMaxs(1, 3);
 238             mv.visitEnd();
 239 
 240             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getIntAt", "(Ljava/lang/Object;)I", null, null);
 241             mv.visitCode();
 242             mv.visitVarInsn(ALOAD, 0);
 243             mv.visitTypeInsn(CHECKCAST, constantPool);
 244             mv.visitVarInsn(ASTORE, 1);
 245             mv.visitVarInsn(ALOAD, 1);
 246             mv.visitInsn(ICONST_0);
 247             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getIntAt", "(I)I", false);
 248             mv.visitInsn(IRETURN);
 249             mv.visitMaxs(2, 3);
 250             mv.visitEnd();
 251 
 252             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getLongAt", "(Ljava/lang/Object;)J", null, null);
 253             mv.visitCode();
 254             mv.visitVarInsn(ALOAD, 0);
 255             mv.visitTypeInsn(CHECKCAST, constantPool);
 256             mv.visitVarInsn(ASTORE, 1);
 257             mv.visitVarInsn(ALOAD, 1);
 258             mv.visitInsn(ICONST_0);
 259             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getLongAt", "(I)J", false);
 260             mv.visitInsn(LRETURN);
 261             mv.visitMaxs(2, 3);
 262             mv.visitEnd();
 263 
 264             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getFloatAt", "(Ljava/lang/Object;)F", null, null);
 265             mv.visitCode();
 266             mv.visitVarInsn(ALOAD, 0);
 267             mv.visitTypeInsn(CHECKCAST, constantPool);
 268             mv.visitVarInsn(ASTORE, 1);
 269             mv.visitVarInsn(ALOAD, 1);
 270             mv.visitInsn(ICONST_0);
 271             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getFloatAt", "(I)F", false);
 272             mv.visitInsn(FRETURN);
 273             mv.visitMaxs(2, 3);
 274             mv.visitEnd();
 275 
 276             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getDoubleAt", "(Ljava/lang/Object;)D", null, null);
 277             mv.visitCode();
 278             mv.visitVarInsn(ALOAD, 0);
 279             mv.visitTypeInsn(CHECKCAST, constantPool);
 280             mv.visitVarInsn(ASTORE, 1);
 281             mv.visitVarInsn(ALOAD, 1);
 282             mv.visitInsn(ICONST_0);
 283             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getDoubleAt", "(I)D", false);
 284             mv.visitInsn(DRETURN);
 285             mv.visitMaxs(2, 3);
 286             mv.visitEnd();
 287 
 288             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getUTF8At", "(Ljava/lang/Object;)Ljava/lang/String;", null, null);
 289             mv.visitCode();
 290             mv.visitVarInsn(ALOAD, 0);
 291             mv.visitTypeInsn(CHECKCAST, constantPool);
 292             mv.visitVarInsn(ASTORE, 1);
 293             mv.visitVarInsn(ALOAD, 1);
 294             mv.visitInsn(ICONST_0);
 295             mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getUTF8At", "(I)Ljava/lang/String;", false);
 296             mv.visitInsn(ARETURN);
 297             mv.visitMaxs(2, 3);
 298             mv.visitEnd();
 299             cw.visitEnd();
 300 
 301             return cw.toByteArray();
 302         }
 303     }
 304 }