1/* 2 * Copyright (c) 2015, 2016, 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 * @bug 8136421 27 * @requires vm.jvmci 28 * @library /test/lib / 29 * @library ../common/patches 30 * @modules java.base/jdk.internal.misc 31 * @modules java.base/jdk.internal.org.objectweb.asm 32 * java.base/jdk.internal.org.objectweb.asm.tree 33 * jdk.internal.vm.ci/jdk.vm.ci.hotspot 34 * jdk.internal.vm.ci/jdk.vm.ci.code 35 * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox 36 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 37 * sun.hotspot.WhiteBox$WhiteBoxPermission 38 * @run main/othervm -Xbootclasspath/a:. 39 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 40 * -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 41 * compiler.jvmci.compilerToVM.ExecuteInstalledCodeTest 42 */ 43 44package compiler.jvmci.compilerToVM; 45 46import jdk.test.lib.Asserts; 47import jdk.test.lib.util.Pair; 48import jdk.test.lib.Utils; 49import jdk.vm.ci.code.InstalledCode; 50import jdk.vm.ci.code.InvalidInstalledCodeException; 51import jdk.vm.ci.hotspot.CompilerToVMHelper; 52import sun.hotspot.code.NMethod; 53 54import java.lang.reflect.Constructor; 55import java.lang.reflect.Modifier; 56import java.util.ArrayList; 57import java.util.List; 58 59public class ExecuteInstalledCodeTest { 60 61 public static void main(String[] args) { 62 ExecuteInstalledCodeTest test = new ExecuteInstalledCodeTest(); 63 List<CompileCodeTestCase> testCases = new ArrayList<>(); 64 testCases.addAll(CompileCodeTestCase.generate(/* bci = */ -1)); 65 testCases .stream() 66 // ignore <init> of abstract class -- 8138793 67 .filter(e -> !(e.executable instanceof Constructor 68 && Modifier.isAbstract( 69 e.executable.getDeclaringClass() 70 .getModifiers()))) 71 .forEach(test::checkSanity); 72 } 73 74 private void checkSanity(CompileCodeTestCase testCase) { 75 System.out.println(testCase); 76 // to have a clean state 77 testCase.deoptimize(); 78 Pair<Object, ? extends Throwable> reflectionResult; 79 Object[] args = Utils.getNullValues( 80 testCase.executable.getParameterTypes()); 81 reflectionResult = testCase.invoke(args); 82 NMethod nMethod = testCase.compile(); 83 if (nMethod == null) { 84 throw new Error(testCase + " : nmethod is null"); 85 } 86 InstalledCode installedCode = testCase.toInstalledCode(); 87 Object result = null; 88 Throwable expectedException = reflectionResult.second; 89 boolean gotException = true; 90 try { 91 args = addReceiver(testCase, args); 92 result = CompilerToVMHelper.executeInstalledCode( 93 args, installedCode); 94 if (testCase.executable instanceof Constructor) { 95 // <init> doesn't have return value, it changes receiver 96 result = args[0]; 97 } 98 gotException = false; 99 } catch (InvalidInstalledCodeException e) { 100 throw new AssertionError( 101 testCase + " : unexpected InvalidInstalledCodeException", e); 102 } catch (Throwable t) { 103 if (expectedException == null) { 104 throw new AssertionError(testCase 105 + " : got unexpected execption : " + t.getMessage(), t); 106 } 107 108 if (expectedException.getClass() != t.getClass()) { 109 System.err.println("exception from CompilerToVM:"); 110 t.printStackTrace(); 111 System.err.println("exception from reflection:"); 112 expectedException.printStackTrace(); 113 throw new AssertionError(String.format( 114 "%s : got unexpected different exceptions : %s != %s", 115 testCase, expectedException.getClass(), t.getClass())); 116 } 117 } 118 119 Asserts.assertEQ(reflectionResult.first, result, testCase 120 + " : different return value"); 121 if (!gotException) { 122 Asserts.assertNull(expectedException, testCase 123 + " : expected exception hasn't been thrown"); 124 } 125 } 126 127 private Object[] addReceiver(CompileCodeTestCase testCase, Object[] args) { 128 if (!Modifier.isStatic(testCase.executable.getModifiers())) { 129 // add instance as 0th arg 130 Object[] newArgs = new Object[args.length + 1]; 131 newArgs[0] = testCase.receiver; 132 System.arraycopy(args, 0, newArgs, 1, args.length); 133 args = newArgs; 134 } 135 return args; 136 } 137} 138