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 * 36 * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper 37 * @build compiler.jvmci.compilerToVM.AllocateCompileIdTest 38 * @build sun.hotspot.WhiteBox 39 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 40 * sun.hotspot.WhiteBox$WhiteBoxPermission 41 * @run main/othervm -Xbootclasspath/a:. 42 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 43 * -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 44 * -XX:-BackgroundCompilation 45 * compiler.jvmci.compilerToVM.AllocateCompileIdTest 46 */ 47 48 package compiler.jvmci.compilerToVM; 49 50 import compiler.jvmci.common.CTVMUtilities; 51 import jdk.test.lib.Asserts; 52 import jdk.test.lib.Pair; 53 import jdk.test.lib.Utils; 54 import jdk.vm.ci.hotspot.CompilerToVMHelper; 55 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 56 import sun.hotspot.code.NMethod; 57 58 import java.lang.reflect.Executable; 59 import java.lang.reflect.Method; 60 import java.util.ArrayList; 61 import java.util.HashSet; 62 import java.util.List; 63 import java.util.stream.Collectors; 64 import java.util.stream.Stream; 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 }