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