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