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