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