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