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