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