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