ExecuteInstalledCodeTest.java revision 9287:40bd4478a362
11541Srgrimespackage compiler.jvmci.compilerToVM;
21541Srgrimes
31541Srgrimesimport jdk.vm.ci.code.InstalledCode;
41541Srgrimesimport jdk.vm.ci.code.InvalidInstalledCodeException;
51541Srgrimesimport jdk.vm.ci.hotspot.CompilerToVMHelper;
61541Srgrimesimport jdk.test.lib.Asserts;
71541Srgrimesimport jdk.test.lib.Utils;
81541Srgrimesimport jdk.test.lib.Pair;
91541Srgrimesimport sun.hotspot.code.NMethod;
101541Srgrimes
111541Srgrimesimport java.lang.reflect.Constructor;
121541Srgrimesimport java.lang.reflect.Executable;
131541Srgrimesimport java.lang.reflect.InvocationTargetException;
141541Srgrimesimport java.lang.reflect.Method;
151541Srgrimesimport java.lang.reflect.Modifier;
161541Srgrimesimport java.util.ArrayList;
171541Srgrimesimport java.util.HashMap;
181541Srgrimesimport java.util.List;
191541Srgrimesimport java.util.Map;
201541Srgrimes
211541Srgrimes/*
221541Srgrimes * @test
231541Srgrimes * @bug 8136421
241541Srgrimes * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
251541Srgrimes * @library /testlibrary /../../test/lib /
261541Srgrimes * @compile ../common/CompilerToVMHelper.java
271541Srgrimes * @build sun.hotspot.WhiteBox
281541Srgrimes *        compiler.jvmci.compilerToVM.ExecuteInstalledCodeTest
291541Srgrimes * @run main ClassFileInstaller sun.hotspot.WhiteBox
301541Srgrimes *                              sun.hotspot.WhiteBox$WhiteBoxPermission
311541Srgrimes *                              jdk.vm.ci.hotspot.CompilerToVMHelper
321541Srgrimes * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
331541Srgrimes *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
341541Srgrimes *      compiler.jvmci.compilerToVM.ExecuteInstalledCodeTest
351541Srgrimes */
361541Srgrimes
371541Srgrimespublic class ExecuteInstalledCodeTest {
381541Srgrimes
3950477Speter    public static void main(String[] args) {
401541Srgrimes        ExecuteInstalledCodeTest test = new ExecuteInstalledCodeTest();
411541Srgrimes        List<CompileCodeTestCase> testCases = new ArrayList<>();
422165Spaul        testCases.addAll(CompileCodeTestCase.generate(/* bci = */ -1));
432165Spaul        testCases .stream()
442165Spaul                // ignore <init> of abstract class -- 8138793
4555205Speter                .filter(e -> !(e.executable instanceof Constructor
4617649Speter                        && Modifier.isAbstract(
4717649Speter                                e.executable.getDeclaringClass()
4892719Salfred                                        .getModifiers())))
4917649Speter                .forEach(test::checkSanity);
5013545Sjulian    }
511541Srgrimes
521541Srgrimes    private void checkSanity(CompileCodeTestCase testCase) {
531541Srgrimes        System.out.println(testCase);
541541Srgrimes        // to have a clean state
551541Srgrimes        testCase.deoptimize();
561541Srgrimes        Pair<Object, ? extends Throwable> reflectionResult;
571541Srgrimes        Object[] args = Utils.getNullValues(
581541Srgrimes                testCase.executable.getParameterTypes());
591541Srgrimes        reflectionResult = testCase.invoke(args);
601541Srgrimes        NMethod nMethod = testCase.compile();
611541Srgrimes        if (nMethod == null) {
621541Srgrimes            throw new Error(testCase + " : nmethod is null");
631541Srgrimes        }
641541Srgrimes        InstalledCode installedCode = testCase.toInstalledCode();
651541Srgrimes        Object result = null;
661541Srgrimes        Throwable expectedException = reflectionResult.second;
671541Srgrimes        boolean gotException = true;
681541Srgrimes        try {
691541Srgrimes            args = addReceiver(testCase, args);
701541Srgrimes            result = CompilerToVMHelper.executeInstalledCode(
711541Srgrimes                    args, installedCode);
721541Srgrimes            if (testCase.executable instanceof Constructor) {
731541Srgrimes                // <init> doesn't have return value, it changes receiver
741541Srgrimes                result = args[0];
751541Srgrimes            }
761541Srgrimes            gotException = false;
771541Srgrimes        } catch (InvalidInstalledCodeException e) {
781541Srgrimes            throw new AssertionError(
791541Srgrimes                    testCase + " : unexpected InvalidInstalledCodeException", e);
801541Srgrimes        } catch (Throwable t) {
811541Srgrimes            if (expectedException == null) {
821541Srgrimes                throw new AssertionError(testCase
831541Srgrimes                        + " : got unexpected execption : " + t.getMessage(), t);
841541Srgrimes            }
851541Srgrimes
861541Srgrimes            if (expectedException.getClass() != t.getClass()) {
8796755Strhodes                System.err.println("exception from CompilerToVM:");
881541Srgrimes                t.printStackTrace();
891541Srgrimes                System.err.println("exception from reflection:");
901541Srgrimes                expectedException.printStackTrace();
911541Srgrimes                throw new AssertionError(String.format(
921541Srgrimes                        "%s : got unexpected different exceptions : %s != %s",
931541Srgrimes                        testCase, expectedException.getClass(), t.getClass()));
941541Srgrimes            }
951541Srgrimes        }
961541Srgrimes
971541Srgrimes        Asserts.assertEQ(reflectionResult.first, result, testCase
981541Srgrimes                + " : different return value");
991541Srgrimes        if (!gotException) {
1001541Srgrimes            Asserts.assertNull(expectedException, testCase
1011541Srgrimes                    + " : expected exception hasn't been thrown");
1021541Srgrimes        }
1031541Srgrimes    }
1041541Srgrimes
1051541Srgrimes    private Object[] addReceiver(CompileCodeTestCase testCase, Object[] args) {
1061541Srgrimes        if (!Modifier.isStatic(testCase.executable.getModifiers())) {
1071541Srgrimes            // add instance as 0th arg
1081541Srgrimes            Object[] newArgs = new Object[args.length + 1];
1091541Srgrimes            newArgs[0] = testCase.receiver;
1101541Srgrimes            System.arraycopy(args, 0, newArgs, 1, args.length);
11189642Skeramida            args = newArgs;
1121541Srgrimes        }
1131541Srgrimes        return args;
1141541Srgrimes    }
1151541Srgrimes}
1161541Srgrimes