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