1 /*
   2  * Copyright (c) 2015, 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 (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library / /testlibrary
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.org.objectweb.asm
  31  *          java.base/jdk.internal.org.objectweb.asm.tree
  32  *          jdk.vm.ci/jdk.vm.ci.hotspot
  33  *          jdk.vm.ci/jdk.vm.ci.code
  34  *          jdk.vm.ci/jdk.vm.ci.meta
  35  *          jdk.vm.ci/jdk.vm.ci.runtime
  36  * @ignore 8144964
  37  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  38  * @build compiler.jvmci.common.JVMCIHelpers
  39  *     compiler.jvmci.events.JvmciNotifyInstallEventTest
  40  * @run main jdk.test.lib.FileInstaller ../common/services/ ./META-INF/services/
  41  * @run main jdk.test.lib.FileInstaller ./JvmciNotifyInstallEventTest.config
  42  *     ./META-INF/services/jdk.vm.ci.hotspot.HotSpotVMEventListener
  43  * @run main ClassFileInstaller
  44  *     compiler.jvmci.common.JVMCIHelpers$EmptyHotspotCompiler
  45  *     compiler.jvmci.common.JVMCIHelpers$EmptyCompilerFactory
  46  *     compiler.jvmci.events.JvmciNotifyInstallEventTest
  47  *     compiler.jvmci.common.CTVMUtilities
  48  *     compiler.jvmci.common.testcases.SimpleClass
  49  *     jdk.test.lib.Asserts
  50  *     jdk.test.lib.Utils
  51  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  52  *     -Djvmci.compiler=EmptyCompiler -Xbootclasspath/a:. -Xmixed
  53  *     -XX:+UseJVMCICompiler -XX:-BootstrapJVMCI
  54  *     -Dcompiler.jvmci.events.JvmciNotifyInstallEventTest.failoninit=false
  55  *     compiler.jvmci.events.JvmciNotifyInstallEventTest
  56  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  57  *     -Djvmci.compiler=EmptyCompiler -Xbootclasspath/a:. -Xmixed
  58  *     -XX:+UseJVMCICompiler -XX:-BootstrapJVMCI -XX:JVMCINMethodSizeLimit=0
  59  *     -Dcompiler.jvmci.events.JvmciNotifyInstallEventTest.failoninit=false
  60  *     compiler.jvmci.events.JvmciNotifyInstallEventTest
  61  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-EnableJVMCI
  62  *     -Djvmci.compiler=EmptyCompiler -Xbootclasspath/a:. -Xmixed
  63  *     -Dcompiler.jvmci.events.JvmciNotifyInstallEventTest.failoninit=true
  64  *     compiler.jvmci.events.JvmciNotifyInstallEventTest
  65  */
  66 
  67 package compiler.jvmci.events;
  68 
  69 import compiler.jvmci.common.CTVMUtilities;
  70 import compiler.jvmci.common.testcases.SimpleClass;
  71 import jdk.test.lib.Asserts;
  72 import java.lang.reflect.Method;
  73 import jdk.test.lib.Utils;
  74 import jdk.vm.ci.hotspot.HotSpotVMEventListener;
  75 import jdk.vm.ci.code.CompiledCode;
  76 import jdk.vm.ci.code.InstalledCode;
  77 import jdk.vm.ci.code.site.DataPatch;
  78 import jdk.vm.ci.code.site.Site;
  79 import jdk.vm.ci.meta.Assumptions.Assumption;
  80 import jdk.vm.ci.meta.ResolvedJavaMethod;
  81 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  82 import jdk.vm.ci.hotspot.HotSpotCompiledCode;
  83 import jdk.vm.ci.hotspot.HotSpotCompiledCode.Comment;
  84 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  85 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  86 
  87 public class JvmciNotifyInstallEventTest implements HotSpotVMEventListener {
  88     private static final String METHOD_NAME = "testMethod";
  89     private static final boolean FAIL_ON_INIT = !Boolean.getBoolean(
  90             "compiler.jvmci.events.JvmciNotifyInstallEventTest.failoninit");
  91     private static volatile int gotInstallNotification = 0;
  92 
  93     public static void main(String args[]) {
  94         new JvmciNotifyInstallEventTest().runTest();
  95     }
  96 
  97     private void runTest() {
  98         if (gotInstallNotification != 0) {
  99             throw new Error("Got install notification before test actions");
 100         }
 101         HotSpotCodeCacheProvider codeCache;
 102         try {
 103             codeCache = (HotSpotCodeCacheProvider) HotSpotJVMCIRuntime.runtime()
 104                     .getHostJVMCIBackend().getCodeCache();
 105         } catch (InternalError ie) {
 106             if (FAIL_ON_INIT) {
 107                 throw new AssertionError(
 108                         "Got unexpected InternalError trying to get code cache",
 109                         ie);
 110             }
 111             // passed
 112             return;
 113         }
 114         Asserts.assertTrue(FAIL_ON_INIT,
 115                     "Haven't caught InternalError in negative case");
 116         Method testMethod;
 117         try {
 118             testMethod = SimpleClass.class.getDeclaredMethod(METHOD_NAME);
 119         } catch (NoSuchMethodException e) {
 120             throw new Error("TEST BUG: Can't find " + METHOD_NAME, e);
 121         }
 122         HotSpotResolvedJavaMethod method = CTVMUtilities
 123                 .getResolvedMethod(SimpleClass.class, testMethod);
 124         HotSpotCompiledCode compiledCode = new HotSpotCompiledCode(METHOD_NAME,
 125                 new byte[0], 0, new Site[0], new Assumption[0],
 126                 new ResolvedJavaMethod[]{method}, new Comment[0], new byte[0],
 127                 16, new DataPatch[0], false, 0, null);
 128         codeCache.installCode(method, compiledCode, /* installedCode = */ null,
 129                 /* speculationLog = */ null, /* isDefault = */ false);
 130         Asserts.assertEQ(gotInstallNotification, 1,
 131                 "Got unexpected event count after 1st install attempt");
 132         // since "empty" compilation result is ok, a second attempt should be ok
 133         codeCache.installCode(method, compiledCode, /* installedCode = */ null,
 134                 /* speculationLog = */ null, /* isDefault = */ false);
 135         Asserts.assertEQ(gotInstallNotification, 2,
 136                 "Got unexpected event count after 2nd install attempt");
 137         // and an incorrect cases
 138         Utils.runAndCheckException(() -> {
 139             codeCache.installCode(method, null, null, null, true);
 140         }, NullPointerException.class);
 141         Asserts.assertEQ(gotInstallNotification, 2,
 142                 "Got unexpected event count after 3rd install attempt");
 143         Utils.runAndCheckException(() -> {
 144             codeCache.installCode(null, null, null, null, true);
 145         }, NullPointerException.class);
 146         Asserts.assertEQ(gotInstallNotification, 2,
 147                 "Got unexpected event count after 4th install attempt");
 148     }
 149 
 150     @Override
 151     public void notifyInstall(HotSpotCodeCacheProvider hotSpotCodeCacheProvider,
 152             InstalledCode installedCode, CompiledCode compiledCode) {
 153         gotInstallNotification++;
 154     }
 155 }