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