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