1 /* 2 * Copyright (c) 2014, 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 8030976 8059226 27 * @library /test/lib / 28 * @modules java.base/jdk.internal.org.objectweb.asm 29 * java.base/jdk.internal.misc 30 * java.compiler 31 * java.management 32 * jdk.jvmstat/sun.jvmstat.monitor 33 * 34 * @build sun.hotspot.WhiteBox 35 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 36 * sun.hotspot.WhiteBox$WhiteBoxPermission 37 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 38 * -XX:+WhiteBoxAPI -XX:+LogCompilation 39 * -XX:CompileCommand=compileonly,UnstableIfExecutable.test 40 * -XX:LogFile=always_taken_not_fired.xml 41 * compiler.uncommontrap.TestUnstableIfTrap ALWAYS_TAKEN false 42 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 43 * -XX:+WhiteBoxAPI -XX:+LogCompilation 44 * -XX:CompileCommand=compileonly,UnstableIfExecutable.test 45 * -XX:LogFile=always_taken_fired.xml 46 * compiler.uncommontrap.TestUnstableIfTrap ALWAYS_TAKEN true 47 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 48 * -XX:+WhiteBoxAPI -XX:+LogCompilation 49 * -XX:CompileCommand=compileonly,UnstableIfExecutable.test 50 * -XX:LogFile=never_taken_not_fired.xml 51 * compiler.uncommontrap.TestUnstableIfTrap NEVER_TAKEN false 52 * @run main/othervm -Xbatch -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 53 * -XX:+WhiteBoxAPI -XX:+LogCompilation 54 * -XX:CompileCommand=compileonly,UnstableIfExecutable.test 55 * -XX:LogFile=never_taken_fired.xml 56 * compiler.uncommontrap.TestUnstableIfTrap NEVER_TAKEN true 57 * @run driver compiler.testlibrary.uncommontrap.Verifier always_taken_not_fired.xml 58 * always_taken_fired.xml 59 * never_taken_not_fired.xml 60 * never_taken_fired.xml 61 */ 62 63 package compiler.uncommontrap; 64 65 import compiler.testlibrary.uncommontrap.Verifier; 66 import jdk.internal.org.objectweb.asm.ClassVisitor; 67 import jdk.internal.org.objectweb.asm.ClassWriter; 68 import jdk.internal.org.objectweb.asm.Label; 69 import jdk.internal.org.objectweb.asm.MethodVisitor; 70 import jdk.test.lib.ByteCodeLoader; 71 import jdk.test.lib.Platform; 72 import sun.hotspot.WhiteBox; 73 74 import java.io.File; 75 import java.io.FileWriter; 76 import java.io.IOException; 77 import java.lang.reflect.Method; 78 import java.util.Properties; 79 80 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT; 81 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC; 82 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_STATIC; 83 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_VOLATILE; 84 import static jdk.internal.org.objectweb.asm.Opcodes.GETSTATIC; 85 import static jdk.internal.org.objectweb.asm.Opcodes.GOTO; 86 import static jdk.internal.org.objectweb.asm.Opcodes.IADD; 87 import static jdk.internal.org.objectweb.asm.Opcodes.ICONST_1; 88 import static jdk.internal.org.objectweb.asm.Opcodes.IFEQ; 89 import static jdk.internal.org.objectweb.asm.Opcodes.ILOAD; 90 import static jdk.internal.org.objectweb.asm.Opcodes.ISUB; 91 import static jdk.internal.org.objectweb.asm.Opcodes.RETURN; 92 93 public class TestUnstableIfTrap { 94 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 95 private static final String CLASS_NAME = "UnstableIfExecutable"; 96 private static final String METHOD_NAME = "test"; 97 private static final String FIELD_NAME = "field"; 98 private static final int ITERATIONS = 1_000_000; 99 // There is no dependency on particular class file version, so it could be 100 // set to any version (if you're updating this test for Java 42). 101 private static final int CLASS_FILE_VERSION = 49; 102 private static final int MAX_TIER = 4; 103 // This test aimed to verify that uncommon trap with reason "unstable_if" 104 // is emitted when method that contain control-flow divergence such that 105 // one of two branches is never taken (and other one is taken always). 106 // C2 will made a decision whether or not the branch was ever taken 107 // depending on method's profile. 108 // If profile was collected for a few method's invocations, then C2 will not 109 // trust in branches' probabilities and the tested trap won't be emitted. 110 // In fact, a method has to be invoked at least 40 time at the day when this 111 // comment was written (see Parse::dynamic_branch_prediction for an actual 112 // value). It would be to implementation dependent to use "40" as 113 // a threshold value in the test, so in order to improve test's robustness 114 // the threshold value is 1000: if the tested method was compiled by C2 115 // before it was invoked 1000 times, then we won't verify that trap was 116 // emitted and fired. 117 private static final int MIN_INVOCATIONS_BEFORE_C2_COMPILATION = 1000; 118 /** 119 * Description of test case parameters and uncommon trap that will 120 * be emitted during tested method compilation. 121 */ 122 private static enum TestCaseName { 123 ALWAYS_TAKEN(false, "taken always"), 124 NEVER_TAKEN(true, "taken never"); 125 TestCaseName(boolean predicate, String comment) { 126 this.predicate = predicate; 127 this.comment = comment; 128 } 129 130 public final boolean predicate; 131 public final String name = "unstable_if"; 132 public final String comment; 133 } 134 135 public static void main(String args[]) { 136 if (args.length != 2) { 137 throw new Error("Expected two arguments: test case name and a " 138 + "boolean determining if uncommon trap should be fired."); 139 } 140 test(TestCaseName.valueOf(args[0]), Boolean.valueOf(args[1])); 141 } 142 143 private static void test(TestCaseName testCase, boolean shouldBeFired) { 144 Method testMethod; 145 Label unstableIfLocation = new Label(); 146 boolean shouldBeEmitted; 147 boolean compiledToEarly = false; 148 149 try { 150 Class testClass = ByteCodeLoader.load(CLASS_NAME, 151 generateTest(unstableIfLocation)); 152 testMethod = testClass.getDeclaredMethod(METHOD_NAME, 153 boolean.class); 154 for (int i = 0; i < ITERATIONS; i++) { 155 testMethod.invoke(null, testCase.predicate); 156 if (i < MIN_INVOCATIONS_BEFORE_C2_COMPILATION 157 && isMethodCompiledByC2(testMethod)) { 158 compiledToEarly = true; 159 // There is no sense in further invocations: we already 160 // decided to avoid verification. 161 break; 162 } 163 } 164 // We're checking that trap should be emitted (i.e. it was compiled 165 // by C2) before the trap is fired, because otherwise the nmethod 166 // will be deoptimized and isMethodCompiledByC2 will return false. 167 shouldBeEmitted = isMethodCompiledByC2(testMethod) 168 && !compiledToEarly; 169 if (shouldBeFired) { 170 testMethod.invoke(null, !testCase.predicate); 171 } 172 } catch (ReflectiveOperationException e) { 173 throw new Error("Test case should be generated, loaded and executed" 174 + " without any issues.", e); 175 } 176 177 shouldBeFired &= shouldBeEmitted; 178 179 Properties properties = new Properties(); 180 properties.setProperty(Verifier.VERIFICATION_SHOULD_BE_SKIPPED, 181 Boolean.toString(compiledToEarly)); 182 properties.setProperty(Verifier.UNCOMMON_TRAP_SHOULD_EMITTED, 183 Boolean.toString(shouldBeEmitted)); 184 properties.setProperty(Verifier.UNCOMMON_TRAP_SHOULD_FIRED, 185 Boolean.toString(shouldBeFired)); 186 properties.setProperty(Verifier.UNCOMMON_TRAP_NAME, testCase.name); 187 properties.setProperty(Verifier.UNCOMMON_TRAP_COMMENT, 188 testCase.comment); 189 properties.setProperty(Verifier.UNCOMMON_TRAP_BCI, 190 Integer.toString(unstableIfLocation.getOffset())); 191 192 properties.list(System.out); 193 194 File f = new File(WB.getStringVMFlag("LogFile") + 195 Verifier.PROPERTIES_FILE_SUFFIX); 196 try (FileWriter wr = new FileWriter(f)) { 197 properties.store(wr, ""); 198 } catch (IOException e) { 199 throw new Error("Unable to store test properties.", e); 200 } 201 } 202 203 private static boolean isMethodCompiledByC2(Method m) { 204 boolean isTiered = WB.getBooleanVMFlag("TieredCompilation"); 205 boolean isMethodCompiled = WB.isMethodCompiled(m); 206 boolean isMethodCompiledAtMaxTier 207 = WB.getMethodCompilationLevel(m) == MAX_TIER; 208 209 return Platform.isServer() && isMethodCompiled 210 && (!isTiered || isMethodCompiledAtMaxTier); 211 } 212 213 /** 214 * Generates class with name {@code CLASS_NAME}, which will contain a 215 * static method {@code METHOD_NAME}: 216 * 217 * <pre>{@code 218 * public abstract class UnstableIfExecutable { 219 * private static int field = 0; 220 * 221 * public static void test(boolean alwaysTrue) { 222 * if (alwaysTrue) { 223 * field++; 224 * } else { 225 * field--; 226 * } 227 * } 228 * } 229 * }</pre> 230 * 231 * @return generated bytecode. 232 */ 233 private static byte[] generateTest(Label unstableIfLocation) { 234 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 235 236 cw.visit(CLASS_FILE_VERSION, ACC_PUBLIC | ACC_ABSTRACT, CLASS_NAME, 237 null, "java/lang/Object", null); 238 239 cw.visitField(ACC_PUBLIC | ACC_STATIC | ACC_VOLATILE, FIELD_NAME, 240 "I", null, Integer.valueOf(0)); 241 242 generateTestMethod(cw, unstableIfLocation); 243 244 return cw.toByteArray(); 245 } 246 247 private static void generateTestMethod(ClassVisitor cv, 248 Label unstableIfLocation) { 249 MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, METHOD_NAME, 250 "(Z)V", null, null); 251 mv.visitCode(); 252 253 Label end = new Label(); 254 Label falseBranch = new Label(); 255 256 // push "field" field's value and 1 to stack 257 mv.visitFieldInsn(GETSTATIC, CLASS_NAME, FIELD_NAME, "I"); 258 mv.visitInsn(ICONST_1); 259 // load argument's value 260 mv.visitVarInsn(ILOAD, 0); // alwaysTrue 261 // here is our unstable if 262 mv.visitLabel(unstableIfLocation); 263 mv.visitJumpInsn(IFEQ, falseBranch); 264 // increment on "true" 265 mv.visitInsn(IADD); 266 mv.visitJumpInsn(GOTO, end); 267 // decrement on "false" 268 mv.visitLabel(falseBranch); 269 mv.visitInsn(ISUB); 270 mv.visitLabel(end); 271 // bye bye 272 mv.visitInsn(RETURN); 273 274 mv.visitMaxs(0, 0); 275 mv.visitEnd(); 276 } 277 } 278