Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package invokecustom; |
| 5 | |
Rico Wind | c0016d1 | 2017-10-26 13:39:32 +0200 | [diff] [blame] | 6 | |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 7 | import java.io.IOException; |
Rico Wind | c0016d1 | 2017-10-26 13:39:32 +0200 | [diff] [blame] | 8 | import java.io.InputStream; |
| 9 | import java.io.OutputStream; |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 10 | import java.lang.invoke.CallSite; |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 11 | import java.lang.invoke.MethodHandles; |
| 12 | import java.lang.invoke.MethodType; |
Rico Wind | c0016d1 | 2017-10-26 13:39:32 +0200 | [diff] [blame] | 13 | import java.nio.file.Files; |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 14 | import java.nio.file.Path; |
| 15 | import java.nio.file.Paths; |
| 16 | import org.objectweb.asm.ClassReader; |
| 17 | import org.objectweb.asm.ClassVisitor; |
| 18 | import org.objectweb.asm.ClassWriter; |
| 19 | import org.objectweb.asm.Handle; |
| 20 | import org.objectweb.asm.MethodVisitor; |
| 21 | import org.objectweb.asm.Opcodes; |
| 22 | import org.objectweb.asm.Type; |
| 23 | |
| 24 | public class TestGenerator { |
| 25 | |
| 26 | private final Path classNamePath; |
| 27 | |
| 28 | public static void main(String[] args) throws IOException { |
| 29 | assert args.length == 1; |
| 30 | TestGenerator testGenerator = new TestGenerator(Paths.get(args[0], |
| 31 | TestGenerator.class.getPackage().getName(), InvokeCustom.class.getSimpleName() + ".class")); |
| 32 | testGenerator.generateTests(); |
| 33 | } |
| 34 | |
| 35 | public TestGenerator(Path classNamePath) { |
| 36 | this.classNamePath = classNamePath; |
| 37 | } |
| 38 | |
| 39 | private void generateTests() throws IOException { |
Rico Wind | c0016d1 | 2017-10-26 13:39:32 +0200 | [diff] [blame] | 40 | try (InputStream inputStream = Files.newInputStream(classNamePath)) { |
| 41 | ClassReader cr = new ClassReader(inputStream); |
| 42 | ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); |
| 43 | cr.accept( |
clementbera | 6c91c65 | 2019-04-15 17:28:42 +0200 | [diff] [blame] | 44 | new ClassVisitor(Opcodes.ASM7, cw) { |
Rico Wind | c0016d1 | 2017-10-26 13:39:32 +0200 | [diff] [blame] | 45 | @Override |
| 46 | public void visitEnd() { |
| 47 | generateMethodTest1(cw); |
| 48 | generateMethodTest2(cw); |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 49 | generateMethodTest3(cw); |
| 50 | generateMethodTest4(cw); |
Rico Wind | c0016d1 | 2017-10-26 13:39:32 +0200 | [diff] [blame] | 51 | generateMethodMain(cw); |
| 52 | super.visitEnd(); |
| 53 | } |
| 54 | }, 0); |
| 55 | try (OutputStream output = Files.newOutputStream(classNamePath)) { |
| 56 | output.write(cw.toByteArray()); |
| 57 | } |
| 58 | } |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* Generate main method that only call all test methods. */ |
| 62 | private void generateMethodMain(ClassVisitor cv) { |
| 63 | MethodVisitor mv = cv.visitMethod( |
| 64 | Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); |
| 65 | mv.visitMethodInsn( |
| 66 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test1", "()V", false); |
Mikaël Peltier | cfd6dac | 2017-10-10 13:45:55 +0200 | [diff] [blame] | 67 | mv.visitMethodInsn( |
| 68 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test2", "()V", false); |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 69 | mv.visitMethodInsn( |
| 70 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test3", "()V", false); |
| 71 | mv.visitMethodInsn( |
| 72 | Opcodes.INVOKESTATIC, Type.getInternalName(InvokeCustom.class), "test4", "()V", false); |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 73 | mv.visitInsn(Opcodes.RETURN); |
| 74 | mv.visitMaxs(-1, -1); |
| 75 | } |
| 76 | |
| 77 | /** |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 78 | * Generate test with an invokedynamic, a static bootstrap method without extra args and |
| 79 | * args to the target method. |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 80 | */ |
| 81 | private void generateMethodTest1(ClassVisitor cv) { |
| 82 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test1", "()V", |
| 83 | null, null); |
| 84 | MethodType mt = MethodType.methodType( |
| 85 | CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class); |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 86 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 87 | "bsmLookupStatic", mt.toMethodDescriptorString(), false); |
| 88 | mv.visitLdcInsn(new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
| 89 | "targetMethodTest1", "()V", false)); |
| 90 | mv.visitLdcInsn(new Handle(Opcodes.H_GETSTATIC, Type.getInternalName(InvokeCustom.class), |
| 91 | "staticField1", "Ljava/lang/String;", false)); |
| 92 | mv.visitInvokeDynamicInsn("targetMethodTest2", |
| 93 | "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)V", |
| 94 | bootstrap); |
| 95 | mv.visitInsn(Opcodes.RETURN); |
| 96 | mv.visitMaxs(-1, -1); |
| 97 | } |
Mikaël Peltier | cfd6dac | 2017-10-10 13:45:55 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 100 | * Generate test with an invokedynamic, a static bootstrap method without extra args and |
| 101 | * args to the target method. |
Mikaël Peltier | cfd6dac | 2017-10-10 13:45:55 +0200 | [diff] [blame] | 102 | */ |
| 103 | private void generateMethodTest2(ClassVisitor cv) { |
| 104 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test2", "()V", |
| 105 | null, null); |
| 106 | MethodType mt = MethodType.methodType( |
| 107 | CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class); |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 108 | Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class), |
Mikaël Peltier | cfd6dac | 2017-10-10 13:45:55 +0200 | [diff] [blame] | 109 | "bsmLookupStatic", mt.toMethodDescriptorString(), false); |
| 110 | mv.visitLdcInsn(Type.getMethodType("(ZBSCIFJDLjava/lang/String;)Ljava/lang/Object;")); |
| 111 | mv.visitInvokeDynamicInsn("targetMethodTest3", "(Ljava/lang/invoke/MethodType;)V", |
| 112 | bootstrap); |
| 113 | mv.visitInsn(Opcodes.RETURN); |
| 114 | mv.visitMaxs(-1, -1); |
| 115 | } |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Generate test with a const method handle pointing to the middle of a class hierarchy. |
| 119 | * Call a static method with the method handle which will do a MethodHandle.invoke call on |
| 120 | * a sub class instance. |
| 121 | * |
| 122 | * Tests that the const method handle is rewritten when renaming. Also tests that the |
| 123 | * middle class does not disappear (for instance via class merging). |
| 124 | */ |
| 125 | private void generateMethodTest3(ClassVisitor cv) { |
| 126 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test3", "()V", |
| 127 | null, null); |
| 128 | MethodType mt = MethodType.methodType(ReturnType.class, ArgumentType.class); |
| 129 | Handle invokeWithArg = new Handle( |
| 130 | Opcodes.H_INVOKEVIRTUAL, |
| 131 | Type.getInternalName(Middle.class), |
| 132 | "targetMethodTest4", |
| 133 | mt.toMethodDescriptorString(), |
| 134 | false); |
| 135 | mv.visitLdcInsn(invokeWithArg); |
| 136 | mv.visitMethodInsn( |
| 137 | Opcodes.INVOKESTATIC, |
| 138 | Type.getInternalName(InvokeCustom.class), |
| 139 | "doInvokeSubWithArg", |
| 140 | "(Ljava/lang/invoke/MethodHandle;)V", |
| 141 | false); |
| 142 | mv.visitInsn(Opcodes.RETURN); |
| 143 | mv.visitMaxs(-1, -1); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Generate test with a const method handle pointing to a class which inherits the method from |
| 148 | * the super class. Call a static method with the method handle which will do a |
| 149 | * MethodHandle.invokeExact. |
| 150 | */ |
| 151 | private void generateMethodTest4(ClassVisitor cv) { |
| 152 | MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test4", "()V", |
| 153 | null, null); |
| 154 | MethodType mt = MethodType.methodType(ReturnType.class, ArgumentType.class); |
| 155 | Handle invokeExactWithArg = new Handle( |
| 156 | Opcodes.H_INVOKEVIRTUAL, |
| 157 | Type.getInternalName(Impl.class), |
| 158 | "targetMethodTest5", |
| 159 | mt.toMethodDescriptorString(), |
| 160 | false); |
| 161 | mv.visitLdcInsn(invokeExactWithArg); |
| 162 | mv.visitMethodInsn( |
| 163 | Opcodes.INVOKESTATIC, |
| 164 | Type.getInternalName(InvokeCustom.class), |
| 165 | "doInvokeExactImplWithArg", |
| 166 | "(Ljava/lang/invoke/MethodHandle;)V", |
| 167 | false); |
| 168 | mv.visitInsn(Opcodes.RETURN); |
| 169 | mv.visitMaxs(-1, -1); |
| 170 | } |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 171 | } |