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