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