|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | + * @test |
| 26 | + * @requires vm.jvmci |
| 27 | + * @summary Test CONSTANT_Dynamic resolution by HotSpotConstantPool. |
| 28 | + * @modules java.base/jdk.internal.org.objectweb.asm |
| 29 | + * jdk.internal.vm.ci/jdk.vm.ci.hotspot:+open |
| 30 | + * jdk.internal.vm.ci/jdk.vm.ci.runtime |
| 31 | + * jdk.internal.vm.ci/jdk.vm.ci.meta |
| 32 | + * @run testng/othervm |
| 33 | + * -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:-UseJVMCICompiler |
| 34 | + * jdk.vm.ci.hotspot.test.TestDynamicConstant |
| 35 | + */ |
| 36 | + |
| 37 | +package jdk.vm.ci.hotspot.test; |
| 38 | + |
| 39 | +import java.io.File; |
| 40 | +import java.io.IOException; |
| 41 | +import java.lang.invoke.ConstantBootstraps; |
| 42 | +import java.lang.invoke.MethodHandles; |
| 43 | +import java.lang.reflect.Method; |
| 44 | +import java.nio.file.Files; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +import org.testng.Assert; |
| 48 | +import org.testng.annotations.Test; |
| 49 | + |
| 50 | +import jdk.internal.org.objectweb.asm.ClassWriter; |
| 51 | +import jdk.internal.org.objectweb.asm.ConstantDynamic; |
| 52 | +import jdk.internal.org.objectweb.asm.Handle; |
| 53 | +import jdk.internal.org.objectweb.asm.MethodVisitor; |
| 54 | +import jdk.internal.org.objectweb.asm.Opcodes; |
| 55 | +import jdk.internal.org.objectweb.asm.Type; |
| 56 | +import jdk.vm.ci.hotspot.HotSpotObjectConstant; |
| 57 | +import jdk.vm.ci.meta.ConstantPool; |
| 58 | +import jdk.vm.ci.meta.MetaAccessProvider; |
| 59 | +import jdk.vm.ci.meta.PrimitiveConstant; |
| 60 | +import jdk.vm.ci.meta.ResolvedJavaMethod; |
| 61 | +import jdk.vm.ci.runtime.JVMCI; |
| 62 | + |
| 63 | +/** |
| 64 | + * Tests support for Dynamic constants. |
| 65 | + * |
| 66 | + * @see "https://openjdk.java.net/jeps/309" |
| 67 | + * @see "https://bugs.openjdk.java.net/browse/JDK-8177279" |
| 68 | + */ |
| 69 | +public class TestDynamicConstant implements Opcodes { |
| 70 | + |
| 71 | + private static final int PUBLIC_STATIC = ACC_PUBLIC | ACC_STATIC; |
| 72 | + |
| 73 | + static final String testClassInternalName = Type.getInternalName(TestDynamicConstant.class); |
| 74 | + static final String constantBootstrapsClassInternalName = Type.getInternalName(ConstantBootstraps.class); |
| 75 | + |
| 76 | + enum CondyType { |
| 77 | + /** |
| 78 | + * Condy whose bootstrap method is one of the {@code TestDynamicConstant.get<type>BSM()} |
| 79 | + * methods. |
| 80 | + */ |
| 81 | + CALL_DIRECT_BSM, |
| 82 | + |
| 83 | + /** |
| 84 | + * Condy whose bootstrap method is {@link ConstantBootstraps#invoke} that invokes one of the |
| 85 | + * {@code TestDynamicConstant.get<type>()} methods. |
| 86 | + */ |
| 87 | + CALL_INDIRECT_BSM, |
| 88 | + |
| 89 | + /** |
| 90 | + * Condy whose bootstrap method is {@link ConstantBootstraps#invoke} that invokes one of the |
| 91 | + * {@code TestDynamicConstant.get<type>(<type> p1, <type> p2)} methods with args that are |
| 92 | + * condys themselves. |
| 93 | + */ |
| 94 | + CALL_INDIRECT_WITH_ARGS_BSM |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Generates a class with a static {@code run} method that returns a value loaded from |
| 99 | + * CONSTANT_Dynamic constant pool entry. |
| 100 | + */ |
| 101 | + static class TestGenerator { |
| 102 | + |
| 103 | + /** |
| 104 | + * Type of value returned by the generated {@code run} method. |
| 105 | + */ |
| 106 | + final Type type; |
| 107 | + |
| 108 | + /** |
| 109 | + * Type of condy used to produce the returned value. |
| 110 | + */ |
| 111 | + final CondyType condyType; |
| 112 | + |
| 113 | + /** |
| 114 | + * Base name of the static {@code TestDynamicConstant.get<type>} method(s) invoked from |
| 115 | + * condys in the generated class. |
| 116 | + */ |
| 117 | + final String getter; |
| 118 | + |
| 119 | + /** |
| 120 | + * Name of the generated class. |
| 121 | + */ |
| 122 | + final String className; |
| 123 | + |
| 124 | + TestGenerator(Class<?> type, CondyType condyType) { |
| 125 | + String typeName = type.getSimpleName(); |
| 126 | + this.type = Type.getType(type); |
| 127 | + this.condyType = condyType; |
| 128 | + this.getter = "get" + typeName.substring(0, 1).toUpperCase() + typeName.substring(1); |
| 129 | + this.className = TestDynamicConstant.class.getName() + "$" + typeName + '_' + condyType; |
| 130 | + } |
| 131 | + |
| 132 | + Class<?> generateClass() throws ClassNotFoundException { |
| 133 | + TestCL cl = new TestCL(getClass().getClassLoader()); |
| 134 | + return cl.findClass(className); |
| 135 | + } |
| 136 | + |
| 137 | + byte[] generateClassfile() { |
| 138 | + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); |
| 139 | + cw.visit(V16, ACC_SUPER | ACC_PUBLIC, className.replace('.', '/'), null, "java/lang/Object", null); |
| 140 | + |
| 141 | + // @formatter:off |
| 142 | + // Object ConstantBootstraps.invoke(MethodHandles.Lookup lookup, String name, Class<?> type, MethodHandle handle, Object... args) |
| 143 | + // @formatter:on |
| 144 | + String invokeSig = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;"; |
| 145 | + Handle invokeHandle = new Handle(H_INVOKESTATIC, constantBootstrapsClassInternalName, "invoke", invokeSig, false); |
| 146 | + |
| 147 | + String desc = type.getDescriptor(); |
| 148 | + if (condyType == CondyType.CALL_DIRECT_BSM) { |
| 149 | + // Example: int TestDynamicConstant.getIntBSM(MethodHandles.Lookup l, String name, |
| 150 | + // Class<?> type) |
| 151 | + String sig = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)" + desc; |
| 152 | + Handle handle = new Handle(H_INVOKESTATIC, testClassInternalName, getter + "BSM", sig, false); |
| 153 | + |
| 154 | + ConstantDynamic condy = new ConstantDynamic("const", desc, handle); |
| 155 | + MethodVisitor run = cw.visitMethod(PUBLIC_STATIC, "run", "()" + desc, null, null); |
| 156 | + run.visitLdcInsn(condy); |
| 157 | + run.visitInsn(type.getOpcode(IRETURN)); |
| 158 | + run.visitMaxs(0, 0); |
| 159 | + run.visitEnd(); |
| 160 | + } else if (condyType == CondyType.CALL_INDIRECT_BSM) { |
| 161 | + // Example: int TestDynamicConstant.getInt() |
| 162 | + Handle handle = new Handle(H_INVOKESTATIC, testClassInternalName, getter, "()" + desc, false); |
| 163 | + |
| 164 | + ConstantDynamic condy = new ConstantDynamic("const", desc, invokeHandle, handle); |
| 165 | + MethodVisitor run = cw.visitMethod(PUBLIC_STATIC, "run", "()" + desc, null, null); |
| 166 | + run.visitLdcInsn(condy); |
| 167 | + run.visitInsn(type.getOpcode(IRETURN)); |
| 168 | + run.visitMaxs(0, 0); |
| 169 | + run.visitEnd(); |
| 170 | + } else { |
| 171 | + assert condyType == CondyType.CALL_INDIRECT_WITH_ARGS_BSM; |
| 172 | + // Example: int TestDynamicConstant.getInt() |
| 173 | + Handle handle1 = new Handle(H_INVOKESTATIC, testClassInternalName, getter, "()" + desc, false); |
| 174 | + |
| 175 | + // Example: int TestDynamicConstant.getInt(int v1, int v2) |
| 176 | + Handle handle2 = new Handle(H_INVOKESTATIC, testClassInternalName, getter, "(" + desc + desc + ")" + desc, false); |
| 177 | + |
| 178 | + ConstantDynamic condy1 = new ConstantDynamic("const1", desc, invokeHandle, handle1); |
| 179 | + ConstantDynamic condy2 = new ConstantDynamic("const2", desc, invokeHandle, handle2, condy1, condy1); |
| 180 | + |
| 181 | + MethodVisitor run = cw.visitMethod(PUBLIC_STATIC, "run", "()" + desc, null, null); |
| 182 | + run.visitLdcInsn(condy2); |
| 183 | + run.visitInsn(type.getOpcode(IRETURN)); |
| 184 | + run.visitMaxs(0, 0); |
| 185 | + run.visitEnd(); |
| 186 | + } |
| 187 | + cw.visitEnd(); |
| 188 | + return cw.toByteArray(); |
| 189 | + } |
| 190 | + |
| 191 | + private final class TestCL extends ClassLoader { |
| 192 | + String saveClassfilesDir = System.getProperty("save.classfiles.dir"); |
| 193 | + |
| 194 | + private TestCL(ClassLoader parent) { |
| 195 | + super(parent); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + protected Class<?> findClass(String name) throws ClassNotFoundException { |
| 200 | + if (name.equals(className)) { |
| 201 | + byte[] classfileBytes = generateClassfile(); |
| 202 | + if (saveClassfilesDir != null) { |
| 203 | + try { |
| 204 | + File classfile = new File(saveClassfilesDir, name.replace('.', File.separatorChar) + ".class"); |
| 205 | + File classfileDir = classfile.getParentFile(); |
| 206 | + classfileDir.mkdirs(); |
| 207 | + Files.write(classfile.toPath(), classfileBytes); |
| 208 | + System.out.println("Wrote: " + classfile.getAbsolutePath()); |
| 209 | + } catch (IOException cause) { |
| 210 | + Assert.fail("Error saving class file for " + name, cause); |
| 211 | + } |
| 212 | + } |
| 213 | + return defineClass(name, classfileBytes, 0, classfileBytes.length); |
| 214 | + } else { |
| 215 | + return super.findClass(name); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + @SuppressWarnings("try") |
| 222 | + @Test |
| 223 | + public void test() throws Throwable { |
| 224 | + MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); |
| 225 | + Class<?>[] types = { |
| 226 | + boolean.class, |
| 227 | + byte.class, |
| 228 | + short.class, |
| 229 | + char.class, |
| 230 | + int.class, |
| 231 | + float.class, |
| 232 | + long.class, |
| 233 | + double.class, |
| 234 | + String.class, |
| 235 | + List.class |
| 236 | + }; |
| 237 | + for (Class<?> type : types) { |
| 238 | + for (CondyType condyType : CondyType.values()) { |
| 239 | + TestGenerator e = new TestGenerator(type, condyType); |
| 240 | + Class<?> testClass = e.generateClass(); |
| 241 | + Method m = testClass.getDeclaredMethod("run"); |
| 242 | + ResolvedJavaMethod run = metaAccess.lookupJavaMethod(m); |
| 243 | + ConstantPool cp = run.getConstantPool(); |
| 244 | + Method getTagAt = cp.getClass().getDeclaredMethod("getTagAt", int.class); |
| 245 | + getTagAt.setAccessible(true); |
| 246 | + Object lastConstant = null; |
| 247 | + for (int cpi = 1; cpi < cp.length(); cpi++) { |
| 248 | + String tag = String.valueOf(getTagAt.invoke(cp, cpi)); |
| 249 | + if (tag.equals("Dynamic")) { |
| 250 | + lastConstant = cp.lookupConstant(cpi); |
| 251 | + } |
| 252 | + } |
| 253 | + Assert.assertTrue(lastConstant != null, "No Dynamic entries in constant pool of " + testClass.getName()); |
| 254 | + |
| 255 | + // Execute code to resolve condy by execution and compare |
| 256 | + // with condy resolved via ConstantPool |
| 257 | + Object expect = m.invoke(null); |
| 258 | + Object actual; |
| 259 | + if (lastConstant instanceof PrimitiveConstant) { |
| 260 | + actual = ((PrimitiveConstant) lastConstant).asBoxedPrimitive(); |
| 261 | + } else { |
| 262 | + actual = ((HotSpotObjectConstant) lastConstant).asObject(type); |
| 263 | + } |
| 264 | + Assert.assertEquals(actual, expect, m + ":"); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + // @formatter:off |
| 270 | + @SuppressWarnings("unused") public static boolean getBooleanBSM(MethodHandles.Lookup l, String name, Class<?> type) { return true; } |
| 271 | + @SuppressWarnings("unused") public static char getCharBSM (MethodHandles.Lookup l, String name, Class<?> type) { return '*'; } |
| 272 | + @SuppressWarnings("unused") public static short getShortBSM (MethodHandles.Lookup l, String name, Class<?> type) { return Short.MAX_VALUE; } |
| 273 | + @SuppressWarnings("unused") public static byte getByteBSM (MethodHandles.Lookup l, String name, Class<?> type) { return Byte.MAX_VALUE; } |
| 274 | + @SuppressWarnings("unused") public static int getIntBSM (MethodHandles.Lookup l, String name, Class<?> type) { return Integer.MAX_VALUE; } |
| 275 | + @SuppressWarnings("unused") public static float getFloatBSM (MethodHandles.Lookup l, String name, Class<?> type) { return Float.MAX_VALUE; } |
| 276 | + @SuppressWarnings("unused") public static long getLongBSM (MethodHandles.Lookup l, String name, Class<?> type) { return Long.MAX_VALUE; } |
| 277 | + @SuppressWarnings("unused") public static double getDoubleBSM (MethodHandles.Lookup l, String name, Class<?> type) { return Double.MAX_VALUE; } |
| 278 | + @SuppressWarnings("unused") public static String getStringBSM (MethodHandles.Lookup l, String name, Class<?> type) { return "a string"; } |
| 279 | + @SuppressWarnings("unused") public static List<?> getListBSM (MethodHandles.Lookup l, String name, Class<?> type) { return List.of("element"); } |
| 280 | + |
| 281 | + |
| 282 | + public static boolean getBoolean() { return true; } |
| 283 | + public static char getChar () { return '*'; } |
| 284 | + public static short getShort () { return Short.MAX_VALUE; } |
| 285 | + public static byte getByte () { return Byte.MAX_VALUE; } |
| 286 | + public static int getInt () { return Integer.MAX_VALUE; } |
| 287 | + public static float getFloat () { return Float.MAX_VALUE; } |
| 288 | + public static long getLong () { return Long.MAX_VALUE; } |
| 289 | + public static double getDouble () { return Double.MAX_VALUE; } |
| 290 | + public static String getString () { return "a string"; } |
| 291 | + public static List<?> getList () { return List.of("element"); } |
| 292 | + |
| 293 | + public static boolean getBoolean(boolean v1, boolean v2) { return v1 || v2; } |
| 294 | + public static char getChar (char v1, char v2) { return (char)(v1 ^ v2); } |
| 295 | + public static short getShort (short v1, short v2) { return (short)(v1 ^ v2); } |
| 296 | + public static byte getByte (byte v1, byte v2) { return (byte)(v1 ^ v2); } |
| 297 | + public static int getInt (int v1, int v2) { return v1 ^ v2; } |
| 298 | + public static float getFloat (float v1, float v2) { return v1 * v2; } |
| 299 | + public static long getLong (long v1, long v2) { return v1 ^ v2; } |
| 300 | + public static double getDouble (double v1, double v2) { return v1 * v2; } |
| 301 | + public static String getString (String v1, String v2) { return v1 + v2; } |
| 302 | + public static List<?> getList (List<?> v1, List<?> v2) { return List.of(v1, v2); } |
| 303 | + // @formatter:on |
| 304 | +} |
0 commit comments