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 (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.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 import jdk.test.lib.Asserts; 51 import jdk.test.lib.Pair; 52 import jdk.test.lib.Utils; 53 import jdk.vm.ci.hotspot.CompilerToVMHelper; 54 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 55 import sun.hotspot.code.NMethod; 56 57 import java.lang.reflect.Executable; 58 import java.lang.reflect.Method; 59 import java.util.ArrayList; 60 import java.util.HashSet; 61 import java.util.List; 62 import java.util.stream.Collectors; 63 import java.util.stream.Stream; 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 }