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 /*
  26  * @test
  27  * @bug 8136421
  28  * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
  29  * @library /testlibrary /test/lib /
  30  * @library ../common/patches
  31  * @modules java.base/jdk.internal.misc
  32  * @modules java.base/jdk.internal.org.objectweb.asm
  33  *          java.base/jdk.internal.org.objectweb.asm.tree
  34  *          jdk.vm.ci/jdk.vm.ci.hotspot
  35  *          jdk.vm.ci/jdk.vm.ci.code
  36  *          jdk.vm.ci/jdk.vm.ci.runtime
  37  *
  38  * @ignore 8139700
  39  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  40  * @build compiler.jvmci.compilerToVM.InvalidateInstalledCodeTest
  41  * @build sun.hotspot.WhiteBox
  42  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  43  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  44  * @run main/othervm -Xbootclasspath/a:.
  45  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  46  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  47  *                   compiler.jvmci.compilerToVM.InvalidateInstalledCodeTest
  48  */
  49 
  50 package compiler.jvmci.compilerToVM;
  51 
  52 import compiler.jvmci.common.CTVMUtilities;
  53 import jdk.test.lib.Asserts;
  54 import jdk.test.lib.Utils;
  55 import jdk.vm.ci.code.CodeCacheProvider;
  56 import jdk.vm.ci.code.CompilationResult;
  57 import jdk.vm.ci.code.InstalledCode;
  58 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  59 import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
  60 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  61 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  62 import sun.hotspot.code.NMethod;
  63 
  64 import java.util.List;
  65 
  66 public class InvalidateInstalledCodeTest {
  67     private static final CodeCacheProvider CACHE_PROVIDER
  68             = HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend()
  69                     .getCodeCache();
  70 
  71     public static void main(String[] args) {
  72         InvalidateInstalledCodeTest test
  73                 = new InvalidateInstalledCodeTest();
  74         List<CompileCodeTestCase> testCases
  75                 = CompileCodeTestCase.generate(/* bci = */ 0);
  76         testCases.addAll(CompileCodeTestCase.generate(/* bci = */ -1));
  77         testCases.forEach(test::check);
  78         test.checkNull();
  79     }
  80 
  81     private void checkNull() {
  82         Utils.runAndCheckException(
  83                 () -> CompilerToVMHelper.invalidateInstalledCode(null),
  84                 NullPointerException.class);
  85     }
  86 
  87     private void check(CompileCodeTestCase testCase) {
  88         System.out.println(testCase);
  89         HotSpotResolvedJavaMethod javaMethod
  90                 = CTVMUtilities.getResolvedMethod(testCase.executable);
  91         HotSpotCompilationRequest compRequest = new HotSpotCompilationRequest(
  92                 javaMethod, testCase.bci, /* jvmciEnv = */ 0L);
  93         String name = testCase.executable.getName();
  94         CompilationResult compResult = new CompilationResult(name);
  95         // to pass sanity check of default -1
  96         compResult.setTotalFrameSize(0);
  97         compResult.close();
  98         InstalledCode installedCode = CACHE_PROVIDER.installCode(
  99                 compRequest, compResult,
 100                 new InstalledCode(name), /* speculationLog = */ null,
 101                 /* isDefault = */ false);
 102         Asserts.assertTrue(installedCode.isValid(), testCase
 103                 + " : code is invalid even before invalidation");
 104 
 105         NMethod beforeInvalidation = testCase.toNMethod();
 106         if (beforeInvalidation != null) {
 107             throw new Error("TESTBUG : " + testCase + " : nmethod isn't found");
 108         }
 109         // run twice to verify how it works if method is already invalidated
 110         for (int i = 0; i < 2; ++i) {
 111             CompilerToVMHelper.invalidateInstalledCode(installedCode);
 112             Asserts.assertFalse(installedCode.isValid(), testCase
 113                             + " : code is valid after invalidation, i = " + i);
 114             NMethod afterInvalidation = testCase.toNMethod();
 115             if (afterInvalidation != null) {
 116                 System.err.println("before: " + beforeInvalidation);
 117                 System.err.println("after: " + afterInvalidation);
 118                 throw new AssertionError(testCase
 119                         + " : method hasn't been invalidated, i = " + i);
 120             }
 121         }
 122     }
 123 }