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  * @test
  26  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  28  * @library /testlibrary /../../test/lib /
  29  * @compile ../common/CompilerToVMHelper.java
  30  * @build sun.hotspot.WhiteBox
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  *                              jdk.vm.ci.hotspot.CompilerToVMHelper
  34  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  35  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  36  *      -XX:-BackgroundCompilation
  37  *      compiler.jvmci.compilerToVM.AllocateCompileIdTest
  38  */
  39 
  40 package compiler.jvmci.compilerToVM;
  41 
  42 import compiler.jvmci.common.CTVMUtilities;
  43 
  44 import java.lang.reflect.Executable;
  45 import java.lang.reflect.Method;
  46 import java.util.ArrayList;
  47 import java.util.HashMap;
  48 import java.util.List;
  49 import java.util.Map;
  50 import java.util.HashSet;
  51 
  52 import compiler.jvmci.common.testcases.TestCase;
  53 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  54 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  55 import jdk.test.lib.Asserts;
  56 import jdk.test.lib.Pair;
  57 import jdk.test.lib.Utils;
  58 import sun.hotspot.WhiteBox;
  59 import sun.hotspot.code.NMethod;
  60 
  61 public class AllocateCompileIdTest {
  62 
  63     private final HashSet<Integer> ids = new HashSet<>();
  64 
  65     public static void main(String[] args) {
  66         AllocateCompileIdTest test = new AllocateCompileIdTest();
  67         createTestCasesCorrectBci().forEach(test::runSanityCorrectTest);
  68         createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest);
  69     }
  70 
  71 
  72     private static List<CompileCodeTestCase> createTestCasesCorrectBci() {
  73         List<CompileCodeTestCase> result = new ArrayList<>();
  74         try {
  75             Class<?> aClass = DummyClass.class;
  76             Method method = aClass.getMethod("withLoop");
  77             result.add(new CompileCodeTestCase(method, 17));
  78             result.add(new CompileCodeTestCase(method, -1));
  79         } catch (NoSuchMethodException e) {
  80             throw new Error("TEST BUG : " + e, e);
  81         }
  82         return result;
  83     }
  84 
  85 
  86     private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>>
  87             createTestCasesIncorrectBci() {
  88         List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result
  89                 = new ArrayList<>();
  90 
  91         try {
  92             Class<?> aClass = DummyClass.class;
  93             Method method = aClass.getMethod("dummyInstanceFunction");
  94             // greater than bytecode.length
  95             int[] bcis = new int[] {30, 50, 200};
  96             for (int bci : bcis) {
  97                 result.add(new Pair<>(new CompileCodeTestCase(method, bci),
  98                         IllegalArgumentException.class));
  99             }
 100             bcis = new int[] {-4, -50, -200};
 101             for (int bci : bcis) {
 102                 result.add(new Pair<>(new CompileCodeTestCase(method, bci),
 103                         IllegalArgumentException.class));
 104             }
 105         } catch (NoSuchMethodException e) {
 106             throw new Error("TEST BUG : " + e.getMessage(), e);
 107         }
 108         return result;
 109     }
 110 
 111     private void runSanityCorrectTest(CompileCodeTestCase testCase) {
 112         System.out.println(testCase);
 113         Executable aMethod = testCase.executable;
 114         int bci = testCase.bci;
 115         HotSpotResolvedJavaMethodImpl method = CTVMUtilities
 116                 .getResolvedMethod(aMethod);
 117         int wbCompileID = getWBCompileID(testCase);
 118         int id = CompilerToVMHelper.allocateCompileId(method, bci);
 119         Asserts.assertNE(id, 0, testCase + " : zero compile id");
 120 
 121         if (wbCompileID > 0) {
 122             Asserts.assertGT(id, wbCompileID, testCase
 123                     + " : allocated 'compile id' not  greater than existed");
 124             if (!ids.add(wbCompileID)) {
 125                 throw new AssertionError(String.format(
 126                         "%s : vm compilation allocated existed id -- %d",
 127                         testCase, id));
 128             }
 129         }
 130         if (!ids.add(id)) {
 131             throw new AssertionError(String.format(
 132                     "%s : allocateCompileId returned existed id %d",
 133                     testCase, id));
 134         }
 135     }
 136 
 137     private void runSanityIncorrectTest(
 138             Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) {
 139         System.out.println(testCase);
 140         Class<? extends Throwable> exception = testCase.second;
 141         Executable aMethod = testCase.first.executable;
 142         int bci = testCase.first.bci;
 143         HotSpotResolvedJavaMethodImpl method = CTVMUtilities
 144                 .getResolvedMethod(aMethod);
 145         Utils.runAndCheckException(
 146                 () -> CompilerToVMHelper.allocateCompileId(method, bci),
 147                 exception);
 148     }
 149 
 150     private int getWBCompileID(CompileCodeTestCase testCase) {
 151         NMethod nm = testCase.deoptimizeAndCompile();
 152         if (nm == null) {
 153             throw new Error("[TEST BUG] cannot compile method " + testCase);
 154         }
 155         return nm.compile_id;
 156     }
 157 }