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