1 /* 2 * Copyright (c) 2012, 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 package vm.compiler.coverage.parentheses.share; 24 25 import java.lang.invoke.MethodHandles; 26 import java.lang.invoke.MethodHandles.Lookup; 27 import java.lang.reflect.Field; 28 import jdk.internal.org.objectweb.asm.ClassWriter; 29 import jdk.internal.org.objectweb.asm.MethodVisitor; 30 31 import static jdk.internal.org.objectweb.asm.Opcodes.*; 32 33 34 import java.lang.reflect.Method; 35 import java.util.List; 36 37 /** 38 * This class convert instructions sequence to java class file, load it to same JVM and then execute. 39 * This class uses hidden classes for class loading 40 */ 41 public class HotspotInstructionsExecutor implements InstructionsExecutor { 42 43 private static final Object[] NO_CP_PATCHES = new Object[0]; 44 45 private int stackSize; 46 HotspotInstructionsExecutor(int stackSize)47 public HotspotInstructionsExecutor(int stackSize) { 48 this.stackSize = stackSize; 49 } 50 51 @Override execute(List<Instruction> instructions)52 public int execute(List<Instruction> instructions) throws ReflectiveOperationException { 53 Class execClass = generateClass(instructions); 54 Method execMethod = execClass.getMethod("exec"); 55 return (Integer) execMethod.invoke(null); 56 } 57 generateClass(List<Instruction> instructions)58 private Class generateClass(List<Instruction> instructions) throws ReflectiveOperationException { 59 // Needs to be in the same package as the lookup class. 60 String classNameForASM = "vm/compiler/coverage/parentheses/share/ExecClass"; 61 62 ClassWriter cw = new ClassWriter(0); 63 cw.visit(V1_1, ACC_PUBLIC, classNameForASM, null, "java/lang/Object", null); 64 65 // creates a MethodWriter for the 'main' method 66 MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "exec", "()I", null, null); 67 68 for (Instruction instruction : instructions) { 69 mw.visitInsn(instruction.opCode); 70 } 71 72 mw.visitInsn(IRETURN); 73 mw.visitMaxs(stackSize, 2); 74 mw.visitEnd(); 75 76 Lookup lookup = MethodHandles.lookup(); 77 Class<?> hc = lookup.defineHiddenClass(cw.toByteArray(), false).lookupClass(); 78 return hc; 79 } 80 } 81