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