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