1 /* 2 * Copyright (c) 2021, 2025, 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 package compiler.lib.ir_framework.test; 25 26 import compiler.lib.ir_framework.*; 27 import compiler.lib.ir_framework.Compiler; 28 import compiler.lib.ir_framework.shared.*; 29 import jdk.test.lib.Platform; 30 import jdk.test.lib.Utils; 31 import jdk.test.whitebox.WhiteBox; 32 33 import java.io.PrintWriter; 34 import java.io.StringWriter; 35 import java.lang.annotation.Annotation; 36 import java.lang.reflect.*; 37 import java.util.*; 38 import java.util.stream.Collectors; 39 import java.util.stream.Stream; 40 41 import static compiler.lib.ir_framework.shared.TestFrameworkSocket.PRINT_TIMES_TAG; 42 43 /** 44 * This class' main method is called from {@link TestFramework} and represents the so-called "test VM". The class is 45 * the heart of the framework and is responsible for executing all the specified tests in the test class. It uses the 46 * Whitebox API and reflection to achieve this task. 47 */ 48 public class TestVM { 49 private static final WhiteBox WHITE_BOX; 50 51 static { 52 try { 53 WHITE_BOX = WhiteBox.getWhiteBox(); 54 } catch (UnsatisfiedLinkError e) { 55 System.err.println(System.lineSeparator() + """ 56 ########################################################## 57 - Did you call a test-related interface method from 58 TestFramework in main() of your test? Make sure to 59 only call setup/run methods and no checks or 60 assertions from main() of your test! 61 - Are you rerunning the test VM (TestVM class) 62 directly after a JTreg run? Make sure to start it 63 from within JTwork/scratch and with the flag 64 -DReproduce=true! 65 ########################################################## 66 """); 67 throw e; 68 } 69 } 70 71 /** 72 * The default number of warm-up iterations used to warm up a {@link Test} annotated test method. 73 * Use {@code -DWarmup=XY} to specify a different default value. An individual warm-up can also be 74 * set by specifying a {@link Warmup} iteration for a test. 75 */ 76 public static final int WARMUP_ITERATIONS = Integer.parseInt(System.getProperty("Warmup", "2000")); 77 78 private static final boolean ALLOW_METHOD_NOT_COMPILABLE = Boolean.getBoolean("AllowNotCompilable"); 79 private static final boolean TIERED_COMPILATION = (Boolean)WHITE_BOX.getVMFlag("TieredCompilation"); 80 private static final CompLevel TIERED_COMPILATION_STOP_AT_LEVEL; 81 private static final boolean CLIENT_VM = Platform.isClient(); 82 83 static { 84 CompLevel level = CompLevel.forValue(((Long)WHITE_BOX.getVMFlag("TieredStopAtLevel")).intValue()); 85 if (CLIENT_VM && level == CompLevel.C2) { 86 // No C2 available, use C1 level without profiling. 87 level = CompLevel.C1_SIMPLE; 88 } 89 TIERED_COMPILATION_STOP_AT_LEVEL = level; 90 } 91 public static final boolean TEST_C1 = (TIERED_COMPILATION && TIERED_COMPILATION_STOP_AT_LEVEL.getValue() < CompLevel.C2.getValue()) || CLIENT_VM; 92 93 static final boolean XCOMP = Platform.isComp(); 94 static final boolean VERBOSE = Boolean.getBoolean("Verbose"); 95 private static final boolean PRINT_TIMES = Boolean.getBoolean("PrintTimes"); 96 public static final boolean USE_COMPILER = WHITE_BOX.getBooleanVMFlag("UseCompiler"); 97 static final boolean EXCLUDE_RANDOM = Boolean.getBoolean("ExcludeRandom"); 98 private static final String TESTLIST = System.getProperty("Test", ""); 99 private static final String EXCLUDELIST = System.getProperty("Exclude", ""); 100 private static final boolean DUMP_REPLAY = Boolean.getBoolean("DumpReplay"); 101 private static final boolean GC_AFTER = Boolean.getBoolean("GCAfter"); 102 private static final boolean SHUFFLE_TESTS = Boolean.parseBoolean(System.getProperty("ShuffleTests", "true")); 103 // Use separate flag as VERIFY_IR could have been set by user but due to other flags it was disabled by flag VM. 104 private static final boolean PRINT_VALID_IR_RULES = Boolean.getBoolean("ShouldDoIRVerification"); 105 protected static final long PER_METHOD_TRAP_LIMIT = (Long)WHITE_BOX.getVMFlag("PerMethodTrapLimit"); 106 protected static final boolean PROFILE_INTERPRETER = (Boolean)WHITE_BOX.getVMFlag("ProfileInterpreter"); 107 private static final boolean FLIP_C1_C2 = Boolean.getBoolean("FlipC1C2"); 108 private static final boolean IGNORE_COMPILER_CONTROLS = Boolean.getBoolean("IgnoreCompilerControls"); 109 110 private final HashMap<Method, DeclaredTest> declaredTests = new HashMap<>(); 111 private final List<AbstractTest> allTests = new ArrayList<>(); 112 private final HashMap<String, Method> testMethodMap = new HashMap<>(); 113 private final HashMap<String, Method> setupMethodMap = new HashMap<>(); 114 private final List<String> excludeList; 115 private final List<String> testList; 116 private Set<Class<?>> helperClasses = null; // Helper classes that contain framework annotations to be processed. 117 private final IREncodingPrinter irMatchRulePrinter; 118 private final Class<?> testClass; 119 private final Map<Executable, CompLevel> forceCompileMap = new HashMap<>(); 120 121 private TestVM(Class<?> testClass) { 122 TestRun.check(testClass != null, "Test class cannot be null"); 123 this.testClass = testClass; 124 this.testList = createTestFilterList(TESTLIST, testClass); 125 this.excludeList = createTestFilterList(EXCLUDELIST, testClass); 126 127 if (PRINT_VALID_IR_RULES) { 128 irMatchRulePrinter = new IREncodingPrinter(); 129 } else { 130 irMatchRulePrinter = null; 131 } 132 } 133 134 /** 135 * Parse "test1,test2,test3" into a list. 136 */ 137 private static List<String> createTestFilterList(String list, Class<?> testClass) { 138 List<String> filterList = null; 139 if (!list.isEmpty()) { 140 String classPrefix = testClass.getSimpleName() + "."; 141 filterList = new ArrayList<>(Arrays.asList(list.split(","))); 142 for (int i = filterList.size() - 1; i >= 0; i--) { 143 String test = filterList.get(i); 144 if (test.indexOf(".") > 0) { 145 if (test.startsWith(classPrefix)) { 146 test = test.substring(classPrefix.length()); 147 filterList.set(i, test); 148 } else { 149 filterList.remove(i); 150 } 151 } 152 } 153 } 154 return filterList; 155 } 156 157 /** 158 * Main entry point of the test VM. 159 */ 160 public static void main(String[] args) { 161 try { 162 String testClassName = args[0]; 163 System.out.println("TestVM main() called - about to run tests in class " + testClassName); 164 Class<?> testClass = getClassObject(testClassName, "test"); 165 166 TestVM framework = new TestVM(testClass); 167 framework.addHelperClasses(args); 168 framework.start(); 169 } finally { 170 TestFrameworkSocket.closeClientSocket(); 171 } 172 } 173 174 protected static Class<?> getClassObject(String className, String classType) { 175 try { 176 return Class.forName(className); 177 } catch (Exception e) { 178 throw new TestRunException("Could not find " + classType + " class", e); 179 } 180 } 181 182 /** 183 * Set up all helper classes and verify they are specified correctly. 184 */ 185 private void addHelperClasses(String[] args) { 186 Class<?>[] helperClassesList = getHelperClasses(args); 187 if (helperClassesList != null) { 188 TestRun.check(Arrays.stream(helperClassesList).noneMatch(Objects::isNull), "A Helper class cannot be null"); 189 this.helperClasses = new HashSet<>(); 190 191 for (Class<?> helperClass : helperClassesList) { 192 if (Arrays.stream(testClass.getDeclaredClasses()).anyMatch(c -> c == helperClass)) { 193 // Nested class of test class is automatically treated as helper class 194 TestFormat.failNoThrow("Nested " + helperClass + " inside test " + testClass + " is implicitly" 195 + " treated as helper class and does not need to be specified as such."); 196 continue; 197 } 198 TestRun.check(!this.helperClasses.contains(helperClass), "Cannot add the same class twice: " + helperClass); 199 this.helperClasses.add(helperClass); 200 } 201 } 202 } 203 204 private static Class<?>[] getHelperClasses(String[] args) { 205 if (args.length == 1) { 206 return null; 207 } 208 Class<?>[] helperClasses = new Class<?>[args.length - 1]; // First argument is test class 209 for (int i = 1; i < args.length; i++) { 210 String helperClassName = args[i]; 211 helperClasses[i - 1] = getClassObject(helperClassName, "helper"); 212 } 213 return helperClasses; 214 } 215 216 private void checkHelperClass(Class<?> clazz) { 217 checkAnnotationsInClass(clazz, "helper"); 218 for (Class<?> c : clazz.getDeclaredClasses()) { 219 checkAnnotationsInClass(c, "nested (and helper)"); 220 } 221 } 222 223 private void checkAnnotationsInClass(Class<?> c, String clazzType) { 224 Method[] methods = c.getDeclaredMethods(); 225 for (Method m : methods) { 226 TestFormat.checkNoThrow(getAnnotation(m, Test.class) == null, 227 "Cannot use @Test annotation in " + clazzType + " " + c + " at " + m); 228 TestFormat.checkNoThrow(getAnnotation(m, Run.class) == null, 229 "Cannot use @Run annotation in " + clazzType + " " + c + " at " + m); 230 TestFormat.checkNoThrow(getAnnotation(m, Check.class) == null, 231 "Cannot use @Check annotation in " + clazzType + " " + c + " at " + m); 232 TestFormat.checkNoThrow(getAnnotation(m, Setup.class) == null, 233 "Cannot use @Setup annotation in " + clazzType + " " + c + " at " + m); 234 } 235 } 236 237 /** 238 * Only called by internal tests testing the framework itself. Accessed by reflection. Not exposed to normal users. 239 */ 240 private static void runTestsOnSameVM(Class<?> testClass) { 241 if (testClass == null) { 242 StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); 243 testClass = walker.getCallerClass(); 244 } 245 TestVM framework = new TestVM(testClass); 246 framework.start(); 247 } 248 249 /** 250 * Once everything is initialized and set up, start collecting tests and executing them afterwards. 251 */ 252 private void start() { 253 setupTests(); 254 checkForcedCompilationsCompleted(); 255 runTests(); 256 } 257 258 private void setupTests() { 259 for (Class<?> clazz : testClass.getDeclaredClasses()) { 260 checkAnnotationsInClass(clazz, "inner"); 261 } 262 if (DUMP_REPLAY) { 263 addReplay(); 264 } 265 266 // Collect the @Setup methods so we can reference them 267 // from the test methods 268 collectSetupMethods(); 269 270 // Make sure to first setup test methods and make them non-inlineable and only then process compile commands. 271 setupDeclaredTests(); 272 processControlAnnotations(testClass); 273 processHelperClasses(); 274 setupCheckedAndCustomRunTests(); 275 276 // All remaining tests are simple base tests without check or specific way to run them. 277 addBaseTests(); 278 if (PRINT_VALID_IR_RULES) { 279 irMatchRulePrinter.emit(); 280 VMInfoPrinter.emit(); 281 } 282 TestFormat.throwIfAnyFailures(); 283 declaredTests.clear(); 284 testMethodMap.clear(); 285 } 286 287 private void addBaseTests() { 288 declaredTests.forEach((m, test) -> { 289 if (test.getAttachedMethod() == null) { 290 try { 291 Arguments argumentsAnno = getAnnotation(m, Arguments.class); 292 TestFormat.check(argumentsAnno != null || m.getParameterCount() == 0, "Missing @Arguments annotation to define arguments of " + m); 293 BaseTest baseTest = new BaseTest(test, shouldExcludeTest(m.getName())); 294 allTests.add(baseTest); 295 if (PRINT_VALID_IR_RULES) { 296 irMatchRulePrinter.emitRuleEncoding(m, baseTest.isSkipped()); 297 } 298 } catch (TestFormatException e) { 299 // Failure logged. Continue and report later. 300 } 301 } 302 }); 303 } 304 305 /** 306 * Check if user wants to exclude this test by checking the -DTest and -DExclude lists. 307 */ 308 private boolean shouldExcludeTest(String testName) { 309 boolean hasTestList = testList != null; 310 boolean hasExcludeList = excludeList != null; 311 if (hasTestList) { 312 return !testList.contains(testName) || (hasExcludeList && excludeList.contains(testName)); 313 } else if (hasExcludeList) { 314 return excludeList.contains(testName); 315 } 316 return false; 317 } 318 319 /** 320 * Generate replay compilation files. 321 */ 322 private void addReplay() { 323 String directive = "[{ match: \"*.*\", DumpReplay: true }]"; 324 TestFramework.check(WHITE_BOX.addCompilerDirective(directive) == 1, "Failed to add DUMP_REPLAY directive"); 325 } 326 327 private void processControlAnnotations(Class<?> clazz) { 328 if (IGNORE_COMPILER_CONTROLS) { 329 return; 330 } 331 // Also apply compile commands to all inner classes of 'clazz'. 332 ArrayList<Class<?>> classes = new ArrayList<>(Arrays.asList(clazz.getDeclaredClasses())); 333 classes.add(clazz); 334 for (Class<?> c : classes) { 335 applyClassAnnotations(c); 336 List<Executable> executables = new ArrayList<>(Arrays.asList(c.getDeclaredMethods())); 337 Collections.addAll(executables, c.getDeclaredConstructors()); 338 for (Executable ex : executables) { 339 checkClassAnnotations(ex); 340 try { 341 applyIndependentCompilationCommands(ex); 342 } catch (TestFormatException e) { 343 // Failure logged. Continue and report later. 344 } 345 } 346 347 // Only force compilation now because above annotations affect inlining 348 for (Executable ex : executables) { 349 try { 350 applyForceCompileCommand(ex); 351 } catch (TestFormatException e) { 352 // Failure logged. Continue and report later. 353 } 354 } 355 } 356 } 357 358 private void applyClassAnnotations(Class<?> c) { 359 ForceCompileClassInitializer anno = getAnnotation(c, ForceCompileClassInitializer.class); 360 if (anno == null) { 361 return; 362 } 363 364 // Compile class initializer 365 CompLevel level = anno.value(); 366 if (level == CompLevel.SKIP || level == CompLevel.WAIT_FOR_COMPILATION) { 367 TestFormat.failNoThrow("Cannot define compilation level SKIP or WAIT_FOR_COMPILATION in " + 368 "@ForceCompileClassInitializer at " + c); 369 return; 370 } 371 level = restrictCompLevel(anno.value()); 372 if (level != CompLevel.SKIP) { 373 // Make sure class is initialized to avoid compilation bailout of <clinit> 374 getClassObject(c.getName(), "nested"); // calls Class.forName() to initialize 'c' 375 TestFormat.checkNoThrow(WHITE_BOX.enqueueInitializerForCompilation(c, level.getValue()), 376 "Failed to enqueue <clinit> of " + c + " for compilation. Did you specify " 377 + "@ForceCompileClassInitializer without providing a static class initialization? " 378 + "Make sure to provide any form of static initialization or remove the annotation. " 379 + "For debugging purposes, -DIgnoreCompilerControls=true can be used to temporarly " 380 + "ignore @ForceCompileClassInitializer annotations."); 381 } 382 } 383 384 private void checkClassAnnotations(Executable ex) { 385 TestFormat.checkNoThrow(getAnnotation(ex, ForceCompileClassInitializer.class) == null, 386 "@ForceCompileClassInitializer only allowed at classes but not at method " + ex); 387 } 388 389 /** 390 * Exclude a method from compilation with a compiler randomly. Return the compiler for which the method was made 391 * not compilable. 392 */ 393 public static Compiler excludeRandomly(Executable ex) { 394 Compiler compiler = switch (Utils.getRandomInstance().nextInt() % 3) { 395 case 1 -> Compiler.C1; 396 case 2 -> Compiler.C2; 397 default -> Compiler.ANY; 398 }; 399 WHITE_BOX.makeMethodNotCompilable(ex, compiler.getValue(), false); 400 WHITE_BOX.makeMethodNotCompilable(ex, compiler.getValue(), true); 401 System.out.println("Excluding from " + compiler.name() + " compilation: " + ex); 402 return compiler; 403 } 404 405 private void applyIndependentCompilationCommands(Executable ex) { 406 ForceInline forceInlineAnno = getAnnotation(ex, ForceInline.class); 407 DontInline dontInlineAnno = getAnnotation(ex, DontInline.class); 408 ForceCompile forceCompileAnno = getAnnotation(ex, ForceCompile.class); 409 DontCompile dontCompileAnno = getAnnotation(ex, DontCompile.class); 410 checkCompilationCommandAnnotations(ex, forceInlineAnno, dontInlineAnno, forceCompileAnno, dontCompileAnno); 411 // First handle inline annotations 412 if (dontInlineAnno != null) { 413 WHITE_BOX.testSetDontInlineMethod(ex, true); 414 } else if (forceInlineAnno != null) { 415 WHITE_BOX.testSetForceInlineMethod(ex, true); 416 } 417 if (dontCompileAnno != null) { 418 dontCompileWithCompiler(ex, dontCompileAnno.value()); 419 } 420 if (EXCLUDE_RANDOM && getAnnotation(ex, Test.class) == null && forceCompileAnno == null && dontCompileAnno == null) { 421 // Randomly exclude helper methods from compilation 422 if (Utils.getRandomInstance().nextBoolean()) { 423 excludeRandomly(ex); 424 } 425 } 426 } 427 428 private void checkCompilationCommandAnnotations(Executable ex, ForceInline forceInlineAnno, DontInline dontInlineAnno, ForceCompile forceCompileAnno, DontCompile dontCompileAnno) { 429 Test testAnno = getAnnotation(ex, Test.class); 430 Run runAnno = getAnnotation(ex, Run.class); 431 Check checkAnno = getAnnotation(ex, Check.class); 432 TestFormat.check((testAnno == null && runAnno == null && checkAnno == null) || Stream.of(forceCompileAnno, dontCompileAnno, forceInlineAnno, dontInlineAnno).noneMatch(Objects::nonNull), 433 "Cannot use explicit compile command annotations (@ForceInline, @DontInline, " + 434 "@ForceCompile or @DontCompile) together with @Test, @Check or @Run: " + ex + ". Use compLevel in @Test for fine tuning."); 435 if (Stream.of(forceInlineAnno, dontCompileAnno, dontInlineAnno).filter(Objects::nonNull).count() > 1) { 436 // Failure 437 TestFormat.check(dontCompileAnno == null || dontInlineAnno == null, 438 "@DontInline is implicitely done with @DontCompile annotation at " + ex); 439 TestFormat.fail("Cannot mix @ForceInline, @DontInline and @DontCompile at the same time at " + ex); 440 } 441 TestFormat.check(forceInlineAnno == null || dontInlineAnno == null, "Cannot have @ForceInline and @DontInline at the same time at " + ex); 442 if (forceCompileAnno != null && dontCompileAnno != null) { 443 CompLevel forceCompileLevel = forceCompileAnno.value(); 444 Compiler dontCompileCompiler = dontCompileAnno.value(); 445 TestFormat.check(dontCompileCompiler != Compiler.ANY, 446 "Cannot have @DontCompile(Compiler.ANY) and @ForceCompile at the same time at " + ex); 447 TestFormat.check(forceCompileLevel != CompLevel.ANY, 448 "Cannot have @ForceCompile(CompLevel.ANY) and @DontCompile at the same time at " + ex); 449 TestFormat.check(forceCompileLevel.isNotCompilationLevelOfCompiler(dontCompileCompiler), 450 "Overlapping compilation level and compiler with @ForceCompile and @DontCompile at " + ex); 451 } 452 } 453 454 /** 455 * Exlude the method from compilation and make sure it is not inlined. 456 */ 457 private void dontCompileAndDontInlineMethod(Method m) { 458 if (!IGNORE_COMPILER_CONTROLS) { 459 WHITE_BOX.makeMethodNotCompilable(m, CompLevel.ANY.getValue(), true); 460 WHITE_BOX.makeMethodNotCompilable(m, CompLevel.ANY.getValue(), false); 461 WHITE_BOX.testSetDontInlineMethod(m, true); 462 } 463 } 464 465 private void dontCompileWithCompiler(Executable ex, Compiler compiler) { 466 if (VERBOSE) { 467 System.out.println("dontCompileWithCompiler " + ex + " , compiler = " + compiler.name()); 468 } 469 WHITE_BOX.makeMethodNotCompilable(ex, compiler.getValue(), true); 470 WHITE_BOX.makeMethodNotCompilable(ex, compiler.getValue(), false); 471 if (compiler == Compiler.ANY) { 472 WHITE_BOX.testSetDontInlineMethod(ex, true); 473 } 474 } 475 476 private void applyForceCompileCommand(Executable ex) { 477 ForceCompile forceCompileAnno = getAnnotation(ex, ForceCompile.class); 478 if (forceCompileAnno != null) { 479 CompLevel compLevel = forceCompileAnno.value(); 480 TestFormat.check(compLevel != CompLevel.SKIP && compLevel != CompLevel.WAIT_FOR_COMPILATION, 481 "Cannot define compilation level SKIP or WAIT_FOR_COMPILATION in @ForceCompile at " + ex); 482 compLevel = restrictCompLevel(forceCompileAnno.value()); 483 if (FLIP_C1_C2) { 484 compLevel = compLevel.flipCompLevel(); 485 compLevel = restrictCompLevel(compLevel.flipCompLevel()); 486 } 487 if (EXCLUDE_RANDOM) { 488 compLevel = compLevel.excludeCompilationRandomly(ex); 489 } 490 if (compLevel != CompLevel.SKIP) { 491 enqueueForCompilation(ex, compLevel); 492 forceCompileMap.put(ex, compLevel); 493 } 494 } 495 } 496 497 static void enqueueForCompilation(Executable ex, CompLevel requestedCompLevel) { 498 if (TestVM.VERBOSE) { 499 System.out.println("enqueueForCompilation " + ex + ", level = " + requestedCompLevel); 500 } 501 CompLevel compLevel = restrictCompLevel(requestedCompLevel); 502 if (compLevel != CompLevel.SKIP) { 503 WHITE_BOX.enqueueMethodForCompilation(ex, compLevel.getValue()); 504 } else { 505 System.out.println("Skipped compilation on level " + requestedCompLevel + " due to VM flags not allowing it."); 506 } 507 } 508 509 510 /** 511 * Collect all @Setup annotated methods and add them to setupMethodMap, for convenience to reference later from 512 * tests with @Arguments(setup = "setupMethodName"). 513 */ 514 private void collectSetupMethods() { 515 for (Method m : testClass.getDeclaredMethods()) { 516 Setup setupAnnotation = getAnnotation(m, Setup.class); 517 if (setupAnnotation != null) { 518 addSetupMethod(m); 519 } 520 } 521 } 522 523 private void addSetupMethod(Method m) { 524 TestFormat.checkNoThrow(getAnnotation(m, Test.class) == null, 525 "@Setup method cannot have @Test annotation: " + m); 526 TestFormat.checkNoThrow(getAnnotation(m, Check.class) == null, 527 "@Setup method cannot have @Check annotation: " + m); 528 TestFormat.checkNoThrow(getAnnotation(m, Arguments.class) == null, 529 "@Setup method cannot have @Arguments annotation: " + m); 530 TestFormat.checkNoThrow(getAnnotation(m, Run.class) == null, 531 "@Setup method cannot have @Run annotation: " + m); 532 Method mOverloaded = setupMethodMap.put(m.getName(), m); 533 TestFormat.checkNoThrow(mOverloaded == null, 534 "@Setup method cannot be overloaded: " + mOverloaded + " with " + m); 535 m.setAccessible(true); 536 } 537 538 /** 539 * Setup @Test annotated method an add them to the declaredTests map to have a convenient way of accessing them 540 * once setting up a framework test (base checked, or custom run test). 541 */ 542 private void setupDeclaredTests() { 543 for (Method m : testClass.getDeclaredMethods()) { 544 Test testAnno = getAnnotation(m, Test.class); 545 try { 546 if (testAnno != null) { 547 addDeclaredTest(m); 548 } else { 549 TestFormat.checkNoThrow(!m.isAnnotationPresent(IR.class) && !m.isAnnotationPresent(IRs.class), 550 "Found @IR annotation on non-@Test method " + m); 551 TestFormat.checkNoThrow(!m.isAnnotationPresent(Warmup.class) || getAnnotation(m, Run.class) != null, 552 "Found @Warmup annotation on non-@Test or non-@Run method " + m); 553 } 554 } catch (TestFormatException e) { 555 // Failure logged. Continue and report later. 556 } 557 } 558 TestFormat.checkNoThrow(!declaredTests.isEmpty(), "Did not specify any @Test methods in " + testClass); 559 } 560 561 private void addDeclaredTest(Method m) { 562 Test testAnno = getAnnotation(m, Test.class); 563 checkTestAnnotations(m, testAnno); 564 Warmup warmup = getAnnotation(m, Warmup.class); 565 int warmupIterations = WARMUP_ITERATIONS; 566 if (warmup != null) { 567 warmupIterations = warmup.value(); 568 TestFormat.checkNoThrow(warmupIterations >= 0, "Cannot have negative value for @Warmup at " + m); 569 } 570 571 if (!IGNORE_COMPILER_CONTROLS) { 572 // Don't inline test methods by default. Do not apply this when -DIgnoreCompilerControls=true is set. 573 WHITE_BOX.testSetDontInlineMethod(m, true); 574 } 575 CompLevel compLevel = restrictCompLevel(testAnno.compLevel()); 576 if (FLIP_C1_C2) { 577 compLevel = compLevel.flipCompLevel(); 578 compLevel = restrictCompLevel(compLevel.flipCompLevel()); 579 } 580 if (EXCLUDE_RANDOM) { 581 compLevel = compLevel.excludeCompilationRandomly(m); 582 } 583 boolean allowNotCompilable = testAnno.allowNotCompilable() || ALLOW_METHOD_NOT_COMPILABLE; 584 ArgumentsProvider argumentsProvider = ArgumentsProviderBuilder.build(m, setupMethodMap); 585 DeclaredTest test = new DeclaredTest(m, argumentsProvider, compLevel, warmupIterations, allowNotCompilable); 586 declaredTests.put(m, test); 587 testMethodMap.put(m.getName(), m); 588 } 589 590 private void checkTestAnnotations(Method m, Test testAnno) { 591 TestFormat.check(!testMethodMap.containsKey(m.getName()), 592 "Cannot overload two @Test methods: " + m + ", " + testMethodMap.get(m.getName())); 593 TestFormat.check(testAnno != null, m + " must be a method with a @Test annotation"); 594 595 Check checkAnno = getAnnotation(m, Check.class); 596 Run runAnno = getAnnotation(m, Run.class); 597 TestFormat.check(checkAnno == null && runAnno == null, 598 m + " has invalid @Check or @Run annotation while @Test annotation is present."); 599 600 TestFormat.checkNoThrow(Arrays.stream(m.getParameterTypes()).noneMatch(AbstractInfo.class::isAssignableFrom), 601 "Cannot " + AbstractInfo.class + " or any of its subclasses as parameter type at " + 602 "@Test method " + m); 603 604 TestFormat.checkNoThrow(!AbstractInfo.class.isAssignableFrom(m.getReturnType()), 605 "Cannot " + AbstractInfo.class + " or any of its subclasses as return type at " + 606 "@Test method " + m); 607 } 608 609 610 /** 611 * Get the appropriate level as permitted by the test scenario and VM flags. 612 */ 613 private static CompLevel restrictCompLevel(CompLevel compLevel) { 614 if (!USE_COMPILER) { 615 return CompLevel.SKIP; 616 } 617 if (compLevel == CompLevel.ANY) { 618 // Use highest available compilation level by default (usually C2). 619 compLevel = TIERED_COMPILATION_STOP_AT_LEVEL; 620 } 621 if (TEST_C1 && compLevel == CompLevel.C2) { 622 return CompLevel.SKIP; 623 } 624 if ((!TIERED_COMPILATION && !CLIENT_VM) && compLevel.getValue() < CompLevel.C2.getValue()) { 625 return CompLevel.SKIP; 626 } 627 if ((TIERED_COMPILATION || CLIENT_VM) && compLevel.getValue() > TIERED_COMPILATION_STOP_AT_LEVEL.getValue()) { 628 return CompLevel.SKIP; 629 } 630 return compLevel; 631 } 632 633 /** 634 * Verify that the helper classes do not contain illegal framework annotations and then apply the actions as 635 * specified by the different helper class annotations. 636 */ 637 private void processHelperClasses() { 638 if (helperClasses != null) { 639 for (Class<?> helperClass : helperClasses) { 640 // Process the helper classes and apply the explicit compile commands 641 TestFormat.checkNoThrow(helperClass != testClass, 642 "Cannot specify test " + testClass + " as helper class, too."); 643 checkHelperClass(helperClass); 644 processControlAnnotations(helperClass); 645 } 646 } 647 } 648 649 /** 650 * First set up checked (with @Check) and custom run tests (with @Run). All remaining unmatched/unused @Test methods 651 * are treated as base tests and set up as such later. 652 */ 653 private void setupCheckedAndCustomRunTests() { 654 for (Method m : testClass.getDeclaredMethods()) { 655 Check checkAnno = getAnnotation(m, Check.class); 656 Run runAnno = getAnnotation(m, Run.class); 657 Arguments argumentsAnno = getAnnotation(m, Arguments.class); 658 try { 659 TestFormat.check(argumentsAnno == null || (checkAnno == null && runAnno == null), 660 "Cannot have @Argument annotation in combination with @Run or @Check at " + m); 661 if (checkAnno != null) { 662 addCheckedTest(m, checkAnno, runAnno); 663 } else if (runAnno != null) { 664 addCustomRunTest(m, runAnno); 665 } 666 } catch (TestFormatException e) { 667 // Failure logged. Continue and report later. 668 } 669 } 670 } 671 672 /** 673 * Set up a checked test by first verifying the correct format of the @Test and @Check method and then adding it 674 * to the allTests list which keeps track of all framework tests that are eventually executed. 675 */ 676 private void addCheckedTest(Method m, Check checkAnno, Run runAnno) { 677 Method testMethod = testMethodMap.get(checkAnno.test()); 678 DeclaredTest test = declaredTests.get(testMethod); 679 checkCheckedTest(m, checkAnno, runAnno, testMethod, test); 680 test.setAttachedMethod(m); 681 TestFormat.check(getAnnotation(testMethod, Arguments.class) != null || testMethod.getParameterCount() == 0, 682 "Missing @Arguments annotation to define arguments of " + testMethod + " required by " 683 + "checked test " + m); 684 CheckedTest.Parameter parameter = getCheckedTestParameter(m, testMethod); 685 dontCompileAndDontInlineMethod(m); 686 CheckedTest checkedTest = new CheckedTest(test, m, checkAnno, parameter, shouldExcludeTest(testMethod.getName())); 687 allTests.add(checkedTest); 688 if (PRINT_VALID_IR_RULES) { 689 // Only need to emit IR verification information if IR verification is actually performed. 690 irMatchRulePrinter.emitRuleEncoding(testMethod, checkedTest.isSkipped()); 691 } 692 } 693 694 private void checkCheckedTest(Method m, Check checkAnno, Run runAnno, Method testMethod, DeclaredTest test) { 695 TestFormat.check(runAnno == null, m + " has invalid @Run annotation while @Check annotation is present."); 696 TestFormat.check(testMethod != null, "Did not find associated test method \"" + m.getDeclaringClass().getName() 697 + "." + checkAnno.test() + "\" for @Check at " + m); 698 TestFormat.check(test != null, "Missing @Test annotation for associated test method " + testMethod + " for @Check at " + m); 699 Method attachedMethod = test.getAttachedMethod(); 700 TestFormat.check(attachedMethod == null, 701 "Cannot use @Test " + testMethod + " for more than one @Run or one @Check method. Found: " + m + ", " + attachedMethod); 702 } 703 704 /** 705 * Only allow parameters as specified in {@link Check}. 706 */ 707 private CheckedTest.Parameter getCheckedTestParameter(Method m, Method testMethod) { 708 boolean firstParameterTestInfo = m.getParameterCount() > 0 && m.getParameterTypes()[0].equals(TestInfo.class); 709 boolean secondParameterTestInfo = m.getParameterCount() > 1 && m.getParameterTypes()[1].equals(TestInfo.class); 710 CheckedTest.Parameter parameter = null; 711 Class<?> testReturnType = testMethod.getReturnType(); 712 switch (m.getParameterCount()) { 713 case 0 -> parameter = CheckedTest.Parameter.NONE; 714 case 1 -> { 715 TestFormat.checkNoThrow(firstParameterTestInfo || m.getParameterTypes()[0] == testReturnType, 716 "Single-parameter version of @Check method " + m + " must match return type of @Test " + testMethod); 717 parameter = firstParameterTestInfo ? CheckedTest.Parameter.TEST_INFO_ONLY : CheckedTest.Parameter.RETURN_ONLY; 718 } 719 case 2 -> { 720 TestFormat.checkNoThrow(m.getParameterTypes()[0] == testReturnType && secondParameterTestInfo, 721 "Two-parameter version of @Check method " + m + " must provide as first parameter the same" 722 + " return type as @Test method " + testMethod + " and as second parameter an object of " + TestInfo.class); 723 parameter = CheckedTest.Parameter.BOTH; 724 } 725 default -> TestFormat.failNoThrow("@Check method " + m + " must provide either a none, single or two-parameter variant."); 726 } 727 return parameter; 728 } 729 730 /** 731 * Set up a custom run test by first verifying the correct format of the @Test and @Run method and then adding it 732 * to the allTests list which keeps track of all framework tests that are eventually executed. 733 */ 734 private void addCustomRunTest(Method m, Run runAnno) { 735 checkRunMethod(m, runAnno); 736 List<DeclaredTest> tests = new ArrayList<>(); 737 boolean shouldExcludeTest = true; 738 for (String testName : runAnno.test()) { 739 try { 740 Method testMethod = testMethodMap.get(testName); 741 DeclaredTest test = declaredTests.get(testMethod); 742 checkCustomRunTest(m, testName, testMethod, test, runAnno.mode()); 743 test.setAttachedMethod(m); 744 tests.add(test); 745 // Only exclude custom run test if all test methods excluded 746 shouldExcludeTest &= shouldExcludeTest(testMethod.getName()); 747 } catch (TestFormatException e) { 748 // Logged, continue. 749 } 750 } 751 if (tests.isEmpty()) { 752 return; // There was a format violation. Return. 753 } 754 dontCompileAndDontInlineMethod(m); 755 CustomRunTest customRunTest = new CustomRunTest(m, getAnnotation(m, Warmup.class), runAnno, tests, shouldExcludeTest); 756 allTests.add(customRunTest); 757 if (PRINT_VALID_IR_RULES) { 758 tests.forEach(test -> irMatchRulePrinter.emitRuleEncoding(test.getTestMethod(), customRunTest.isSkipped())); 759 } 760 } 761 762 /** 763 * Only allow parameters as specified in {@link Run}. 764 */ 765 private void checkCustomRunTest(Method m, String testName, Method testMethod, DeclaredTest test, RunMode runMode) { 766 TestFormat.check(testMethod != null, "Did not find associated @Test method \"" + m.getDeclaringClass().getName() 767 + "." + testName + "\" specified in @Run at " + m); 768 TestFormat.check(test != null, 769 "Missing @Test annotation for associated test method " + testName + " for @Run at " + m); 770 Method attachedMethod = test.getAttachedMethod(); 771 TestFormat.check(attachedMethod == null, 772 "Cannot use @Test " + testMethod + " for more than one @Run/@Check method. Found: " 773 + m + ", " + attachedMethod); 774 Arguments argumentsAnno = getAnnotation(testMethod, Arguments.class); 775 TestFormat.check(argumentsAnno == null, 776 "Cannot use @Arguments at test method " + testMethod + " in combination with @Run method " + m); 777 Warmup warmupAnno = getAnnotation(testMethod, Warmup.class); 778 TestFormat.checkNoThrow(warmupAnno == null, 779 "Cannot set @Warmup at @Test method " + testMethod + " when used with its @Run method " 780 + m + ". Use @Warmup at @Run method instead."); 781 Test testAnno = getAnnotation(testMethod, Test.class); 782 TestFormat.checkNoThrow(runMode != RunMode.STANDALONE || testAnno.compLevel() == CompLevel.ANY, 783 "Setting explicit compilation level for @Test method " + testMethod + " has no effect " 784 + "when used with STANDALONE @Run method " + m); 785 } 786 787 private void checkRunMethod(Method m, Run runAnno) { 788 TestFormat.check(runAnno.test().length > 0, "@Run method " + m + " must specify at least one test method"); 789 TestFormat.checkNoThrow(m.getParameterCount() == 0 || (m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(RunInfo.class)), 790 "@Run method " + m + " must specify either no parameter or exactly one " + RunInfo.class + " parameter."); 791 Warmup warmupAnno = getAnnotation(m, Warmup.class); 792 TestFormat.checkNoThrow(warmupAnno == null || runAnno.mode() != RunMode.STANDALONE, 793 "Cannot set @Warmup at @Run method " + m + " when used with RunMode.STANDALONE. The @Run method is only invoked once."); 794 } 795 796 private static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> c) { 797 T[] annos = element.getAnnotationsByType(c); 798 TestFormat.check(annos.length < 2, element + " has duplicated annotations"); 799 return Arrays.stream(annos).findFirst().orElse(null); 800 } 801 802 /** 803 * Ensure that all compilations that were enforced (added to compilation queue) by framework annotations are 804 * completed. Wait if necessary for a short amount of time for their completion. 805 */ 806 private void checkForcedCompilationsCompleted() { 807 if (forceCompileMap.isEmpty()) { 808 return; 809 } 810 final long started = System.currentTimeMillis(); 811 long elapsed; 812 do { 813 forceCompileMap.entrySet().removeIf(entry -> WHITE_BOX.getMethodCompilationLevel(entry.getKey()) == entry.getValue().getValue()); 814 if (forceCompileMap.isEmpty()) { 815 // All @ForceCompile methods are compiled at the requested level. 816 return; 817 } 818 // Retry again if not yet compiled. 819 forceCompileMap.forEach(TestVM::enqueueForCompilation); 820 elapsed = System.currentTimeMillis() - started; 821 } while (elapsed < 5000); 822 StringBuilder builder = new StringBuilder(); 823 forceCompileMap.forEach((key, value) -> builder.append("- ").append(key).append(" at CompLevel.").append(value) 824 .append(System.lineSeparator())); 825 throw new TestRunException("Could not force compile the following @ForceCompile methods:" 826 + System.lineSeparator() + builder.toString()); 827 } 828 829 /** 830 * Once all framework tests are collected, they are run in this method. 831 */ 832 private void runTests() { 833 TreeMap<Long, String> durations = (PRINT_TIMES || VERBOSE) ? new TreeMap<>() : null; 834 long startTime = System.nanoTime(); 835 List<AbstractTest> testList; 836 boolean testFilterPresent = testFilterPresent(); 837 if (testFilterPresent) { 838 // Only run the specified tests by the user filters -DTest and/or -DExclude. 839 testList = allTests.stream().filter(test -> !test.isSkipped()).collect(Collectors.toList()); 840 if (testList.isEmpty()) { 841 // Throw an exception to inform the user about an empty specified test set with -DTest and/or -DExclude 842 throw new NoTestsRunException(); 843 } 844 } else { 845 testList = allTests; 846 } 847 848 if (SHUFFLE_TESTS) { 849 // Execute tests in random order (execution sequence affects profiling). This is done by default. 850 Collections.shuffle(testList, Utils.getRandomInstance()); 851 } 852 StringBuilder builder = new StringBuilder(); 853 int failures = 0; 854 855 // Execute all tests and keep track of each exception that is thrown. These are then reported once all tests 856 // are executing. This prevents a premature exit without running all tests. 857 for (AbstractTest test : testList) { 858 if (VERBOSE) { 859 System.out.println("Run " + test.toString()); 860 } 861 if (testFilterPresent) { 862 TestFrameworkSocket.write("Run " + test.toString(), TestFrameworkSocket.TESTLIST_TAG, true); 863 } 864 try { 865 test.run(); 866 } catch (TestRunException e) { 867 StringWriter sw = new StringWriter(); 868 PrintWriter pw = new PrintWriter(sw); 869 e.printStackTrace(pw); 870 builder.append(test.toString()).append(":").append(System.lineSeparator()).append(sw.toString()) 871 .append(System.lineSeparator()).append(System.lineSeparator()); 872 failures++; 873 } 874 if (PRINT_TIMES || VERBOSE) { 875 long endTime = System.nanoTime(); 876 long duration = (endTime - startTime); 877 durations.put(duration, test.getName()); 878 if (VERBOSE) { 879 System.out.println("Done " + test.getName() + ": " + duration + " ns = " + (duration / 1000000) + " ms"); 880 } 881 } 882 if (GC_AFTER) { 883 System.out.println("doing GC"); 884 WHITE_BOX.fullGC(); 885 } 886 } 887 888 // Print execution times 889 if (VERBOSE || PRINT_TIMES) { 890 TestFrameworkSocket.write("Test execution times:", PRINT_TIMES_TAG, true); 891 for (Map.Entry<Long, String> entry : durations.entrySet()) { 892 TestFrameworkSocket.write(String.format("%-25s%15d ns%n", entry.getValue() + ":", entry.getKey()), 893 PRINT_TIMES_TAG, true); 894 } 895 } 896 897 if (failures > 0) { 898 // Finally, report all occurred exceptions in a nice format. 899 String msg = System.lineSeparator() + System.lineSeparator() + "Test Failures (" + failures + ")" 900 + System.lineSeparator() + "----------------" + "-".repeat(String.valueOf(failures).length()); 901 throw new TestRunException(msg + System.lineSeparator() + builder.toString()); 902 } 903 } 904 905 private boolean testFilterPresent() { 906 return testList != null || excludeList != null; 907 } 908 909 enum TriState { 910 Maybe, 911 Yes, 912 No 913 } 914 915 public static void compile(Method m, CompLevel compLevel) { 916 TestRun.check(compLevel != CompLevel.SKIP && compLevel != CompLevel.WAIT_FOR_COMPILATION, 917 "Invalid compilation request with level " + compLevel); 918 enqueueForCompilation(m, compLevel); 919 } 920 921 public static void deoptimize(Method m) { 922 WHITE_BOX.deoptimizeMethod(m); 923 } 924 925 public static boolean isCompiled(Method m) { 926 return compiledAtLevel(m, CompLevel.ANY) == TriState.Yes; 927 } 928 929 public static boolean isC1Compiled(Method m) { 930 return compiledByC1(m) == TriState.Yes; 931 } 932 933 public static boolean isC2Compiled(Method m) { 934 return compiledByC2(m) == TriState.Yes; 935 } 936 937 public static boolean isCompiledAtLevel(Method m, CompLevel compLevel) { 938 return compiledAtLevel(m, compLevel) == TriState.Yes; 939 } 940 941 public static void assertDeoptimizedByC1(Method m) { 942 if (notUnstableDeoptAssertion(m, CompLevel.C1_SIMPLE)) { 943 TestRun.check(compiledByC1(m) != TriState.Yes || PER_METHOD_TRAP_LIMIT == 0 || !PROFILE_INTERPRETER, 944 m + " should have been deoptimized by C1"); 945 } 946 } 947 948 public static void assertDeoptimizedByC2(Method m) { 949 if (notUnstableDeoptAssertion(m, CompLevel.C2)) { 950 TestRun.check(compiledByC2(m) != TriState.Yes || PER_METHOD_TRAP_LIMIT == 0 || !PROFILE_INTERPRETER, 951 m + " should have been deoptimized by C2"); 952 } 953 } 954 955 /** 956 * Some VM flags could make the deopt assertions unstable. 957 */ 958 private static boolean notUnstableDeoptAssertion(Method m, CompLevel level) { 959 return (USE_COMPILER && !XCOMP && !IGNORE_COMPILER_CONTROLS && !TEST_C1 && 960 (!EXCLUDE_RANDOM || WHITE_BOX.isMethodCompilable(m, level.getValue(), false))); 961 } 962 963 public static void assertCompiledByC1(Method m) { 964 TestRun.check(compiledByC1(m) != TriState.No, m + " should have been C1 compiled"); 965 } 966 967 public static void assertCompiledByC2(Method m) { 968 TestRun.check(compiledByC2(m) != TriState.No, m + " should have been C2 compiled"); 969 } 970 971 public static void assertCompiledAtLevel(Method m, CompLevel level) { 972 TestRun.check(compiledAtLevel(m, level) != TriState.No, m + " should have been compiled at level " + level.name()); 973 } 974 975 public static void assertNotCompiled(Method m) { 976 TestRun.check(!isC1Compiled(m), m + " should not have been compiled by C1"); 977 TestRun.check(!isC2Compiled(m), m + " should not have been compiled by C2"); 978 } 979 980 public static void assertCompiled(Method m) { 981 TestRun.check(compiledByC1(m) != TriState.No || compiledByC2(m) != TriState.No, m + " should have been compiled"); 982 } 983 984 private static TriState compiledByC1(Method m) { 985 TriState triState = compiledAtLevel(m, CompLevel.C1_SIMPLE); 986 if (triState != TriState.No) { 987 return triState; 988 } 989 triState = compiledAtLevel(m, CompLevel.C1_LIMITED_PROFILE); 990 if (triState != TriState.No) { 991 return triState; 992 } 993 triState = compiledAtLevel(m, CompLevel.C1_FULL_PROFILE); 994 return triState; 995 } 996 997 private static TriState compiledByC2(Method m) { 998 return compiledAtLevel(m, CompLevel.C2); 999 } 1000 1001 private static TriState compiledAtLevel(Method m, CompLevel level) { 1002 if (WHITE_BOX.isMethodCompiled(m, false)) { 1003 switch (level) { 1004 case C1_SIMPLE, C1_LIMITED_PROFILE, C1_FULL_PROFILE, C2 -> { 1005 if (WHITE_BOX.getMethodCompilationLevel(m, false) == level.getValue()) { 1006 return TriState.Yes; 1007 } 1008 } 1009 case ANY -> { 1010 return TriState.Yes; 1011 } 1012 default -> throw new TestRunException("compiledAtLevel() should not be called with " + level); 1013 } 1014 } 1015 if (!USE_COMPILER || XCOMP || TEST_C1 || IGNORE_COMPILER_CONTROLS || FLIP_C1_C2 || 1016 (EXCLUDE_RANDOM && !WHITE_BOX.isMethodCompilable(m, level.getValue(), false))) { 1017 return TriState.Maybe; 1018 } 1019 return TriState.No; 1020 } 1021 }