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