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