Skip to content

Commit 9804538

Browse files
committedDec 27, 2022
8274911: testlibrary_tests/ir_framework/tests/TestIRMatching.java fails with "java.lang.RuntimeException: Should have thrown exception"
Backport-of: f62346066869b681d1cc9f63775393b11a48722a
1 parent 554f17b commit 9804538

File tree

4 files changed

+90
-73
lines changed

4 files changed

+90
-73
lines changed
 

‎test/hotspot/jtreg/compiler/lib/ir_framework/driver/IRMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ private void reportFailuresIfAny() {
495495
// Do not throw an exception in this case (i.e. bailout).
496496
String compilations = compilationsBuilder.toString();
497497
if (!compilations.contains(SAFEPOINT_WHILE_PRINTING_MESSAGE)) {
498-
throw new IRViolationException(failuresBuilder.toString(), compilationsBuilder.toString());
498+
throw new IRViolationException(failuresBuilder.toString(), compilations);
499499
} else {
500500
System.out.println("Found " + SAFEPOINT_WHILE_PRINTING_MESSAGE + ", bail out of IR matching");
501501
}

‎test/hotspot/jtreg/testlibrary_tests/ir_framework/tests/TestIRMatching.java

+77-69
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131

3232
import java.io.ByteArrayOutputStream;
3333
import java.io.PrintStream;
34-
import java.util.ArrayList;
35-
import java.util.Arrays;
36-
import java.util.List;
34+
import java.util.*;
3735
import java.util.regex.Matcher;
3836
import java.util.regex.Pattern;
3937

@@ -51,22 +49,32 @@
5149

5250
public class TestIRMatching {
5351

54-
private static final List<Exception> exceptions = new ArrayList<>();
52+
private static final Map<Exception, String> exceptions = new LinkedHashMap<>();
53+
private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
54+
private static final ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
55+
private static final PrintStream ps = new PrintStream(baos);
56+
private static final PrintStream psErr = new PrintStream(baosErr);
57+
private static final PrintStream oldOut = System.out;
58+
private static final PrintStream oldErr = System.err;
5559

5660
private static void addException(Exception e) {
57-
System.out.println(TestFramework.getLastTestVMOutput());
58-
exceptions.add(e);
61+
System.out.flush();
62+
System.err.flush();
63+
exceptions.put(e, baos.toString() + System.lineSeparator() + baosErr.toString());
5964
}
6065

6166
public static void main(String[] args) {
62-
runFailOnTestsArgs(BadFailOnConstraint.create(AndOr1.class, "test1(int)", 1, "CallStaticJava"), "-XX:TLABRefillWasteFraction=50", "-XX:+UsePerfData", "-XX:+UseTLAB");
63-
runFailOnTestsArgs(BadFailOnConstraint.create(AndOr1.class, "test2()", 1, "CallStaticJava"), "-XX:TLABRefillWasteFraction=50", "-XX:-UsePerfData", "-XX:+UseTLAB");
64-
67+
// Redirect System.out and System.err to reduce noise.
68+
System.setOut(ps);
69+
System.setErr(psErr);
6570
runWithArguments(AndOr1.class, "-XX:TLABRefillWasteFraction=52", "-XX:+UsePerfData", "-XX:+UseTLAB");
6671
runWithArguments(CountComparisons.class, "-XX:TLABRefillWasteFraction=50");
6772
runWithArguments(GoodCount.class, "-XX:TLABRefillWasteFraction=50");
6873
runWithArguments(MultipleFailOnGood.class, "-XX:TLABRefillWasteFraction=50");
6974

75+
runCheck(new String[] {"-XX:TLABRefillWasteFraction=50", "-XX:+UsePerfData", "-XX:+UseTLAB"}, BadFailOnConstraint.create(AndOr1.class, "test1(int)", 1, "CallStaticJava"));
76+
runCheck(new String[] {"-XX:TLABRefillWasteFraction=50", "-XX:-UsePerfData", "-XX:+UseTLAB"}, BadFailOnConstraint.create(AndOr1.class, "test2()", 1, "CallStaticJava"));
77+
7078
String[] allocMatches = { "MyClass", "wrapper for: _new_instance_Java" };
7179
runCheck(BadFailOnConstraint.create(MultipleFailOnBad.class, "fail1()", 1, 1, "Store"),
7280
BadFailOnConstraint.create(MultipleFailOnBad.class, "fail1()", 1, 3, "Store"),
@@ -223,55 +231,49 @@ public static void main(String[] args) {
223231
: BadFailOnConstraint.create(CheckCastArray.class, "arrayCopy(java.lang.Object[],java.lang.Class)", 1, "checkcast_arraycopy")
224232
);
225233

226-
// Redirect stdout to stream and then check if we find required IR encoding read from socket.
227-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
228-
PrintStream ps = new PrintStream(baos);
229-
PrintStream old = System.out;
230-
System.setOut(ps);
231-
232234
try {
233235
runWithArgumentsFail(CompilationOutputOfFails.class);
234-
Utils.shouldHaveThrownException();
236+
Utils.shouldHaveThrownException(baos.toString());
235237
} catch (IRViolationException e) {
236238
try {
237-
boolean failed = false;
239+
StringBuilder failures = new StringBuilder();
238240
System.out.flush();
239241
String output = baos.toString();
240242
baos.reset();
241243
Pattern pattern = Pattern.compile(">>> Compilation.*both\\d.*\\RPrintIdeal:(?:(?!PrintOpto|>>> Compilation)[\\S\\s])+PrintOptoAssembly");
242244
Matcher matcher = pattern.matcher(output);
243245
long bothCount = matcher.results().count();
244246
if (bothCount != 7L) {
245-
exceptions.add(new RuntimeException("Could not find all both() methods, expected 7 but found " + bothCount));
246-
failed = true;
247+
failures.append("- Could not find all both() methods, expected 7 but found ").append(bothCount).append(System.lineSeparator());
247248
}
248249
pattern = Pattern.compile(">>> Compilation.*ideal\\d.*\\RPrintIdeal:(?:(?!>>> Compilation)[\\S\\s])+");
249250
matcher = pattern.matcher(output);
250251
int count = 0;
251252
while (matcher.find()) {
252253
String match = matcher.group();
253-
Asserts.assertFalse(match.contains("PrintOptoAssembly"), "Cannot contain opto assembly: " + output);
254+
if (match.contains("PrintOptoAssembly")) {
255+
failures.append("Cannot contain opto assembly: ").append(System.lineSeparator()).append(match);
256+
}
254257
count++;
255258
}
256259
if (count != 7) {
257-
exceptions.add(new RuntimeException("Could not find all ideal() methods, expected 7 but found " + count));
258-
failed = true;
260+
failures.append("- Could not find all ideal() methods, expected 7 but found ").append(count).append(System.lineSeparator());
259261
}
260262
pattern = Pattern.compile(">>> Compilation.*opto\\d.*\\RPrintOptoAssembly:(?:(?!>>> Compilation)[\\S\\s])+");
261263
matcher = pattern.matcher(output);
262264
count = 0;
263265
while (matcher.find()) {
264266
String match = matcher.group();
265-
Asserts.assertFalse(match.contains("PrintIdeal"), "Cannot contain opto assembly: " + output);
267+
if (match.contains("PrintIdeal")) {
268+
failures.append("Cannot contain print assembly: ").append(System.lineSeparator()).append(match);
269+
}
266270
count++;
267271
}
268272
if (count != 7) {
269-
exceptions.add(new RuntimeException("Could not find all opto() methods, expected 7 but found " + count));
270-
failed = true;
273+
failures.append("- Could not find all opto() methods, expected 7 but found ").append(count).append(System.lineSeparator());
271274
}
272-
if (failed) {
273-
System.err.println(TestFramework.getLastTestVMOutput());
274-
System.err.println(output);
275+
if (!failures.isEmpty()) {
276+
addException(new RuntimeException(failures.toString()));
275277
}
276278
} catch (Exception e1) {
277279
addException(e1);
@@ -283,52 +285,80 @@ public static void main(String[] args) {
283285
runWithArguments(FlagComparisons.class, "-XX:TLABRefillWasteFraction=50");
284286
System.out.flush();
285287
String output = baos.toString();
286-
baos.reset();
287288
findIrIds(output, "testMatchAllIf50", 0, 21);
288289
findIrIds(output, "testMatchNoneIf50", -1, -1);
289290

290291
runWithArguments(FlagComparisons.class, "-XX:TLABRefillWasteFraction=49");
291292
System.out.flush();
292293
output = baos.toString();
293-
baos.reset();
294294
findIrIds(output, "testMatchAllIf50", 4, 6, 13, 18);
295295
findIrIds(output, "testMatchNoneIf50", 0, 3, 8, 10, 17, 22);
296296

297297
runWithArguments(FlagComparisons.class, "-XX:TLABRefillWasteFraction=51");
298298
System.out.flush();
299299
output = baos.toString();
300-
baos.reset();
301300
findIrIds(output, "testMatchAllIf50", 7, 12, 19, 21);
302301
findIrIds(output, "testMatchNoneIf50", 4, 7, 11, 16, 20, 22);
303-
System.setOut(old);
302+
System.setOut(oldOut);
303+
System.setErr(oldErr);
304304

305305
if (!exceptions.isEmpty()) {
306-
System.err.println("TestIRMatching failed with one or more exceptions:");
307-
for (Exception e : exceptions) {
308-
System.err.println(e.getMessage());
306+
System.err.println("TestIRMatching failed with " + exceptions.size() + " exception(s):");
307+
int i = 1;
308+
System.err.println("************************");
309+
for (Map.Entry<Exception, String> entry : exceptions.entrySet()) {
310+
System.err.println("***** Exception " + String.format("%02d", i++) +" *****");
311+
System.err.println("************************");
312+
313+
Exception e = entry.getKey();
309314
e.printStackTrace(System.err);
310-
System.err.println("---------");
315+
System.err.println();
316+
System.err.println("===== OUTPUT ======");
317+
System.err.println(entry.getValue());
318+
System.err.println("MESSAGE: " + e.getMessage());
319+
System.err.println("************************");
311320
}
312-
throw new RuntimeException("TestIRMatching failed with one or more exceptions - check stderr and stdout");
321+
i = 1;
322+
System.err.println("====================================");
323+
System.err.println("********************");
324+
System.err.println("***** OVERVIEW *****");
325+
System.err.println("********************");
326+
for (Map.Entry<Exception, String> entry : exceptions.entrySet()) {
327+
Exception e = entry.getKey();
328+
System.err.print((i++) + ") ");
329+
entry.getKey().printStackTrace(System.err);
330+
System.err.println("********************");
331+
}
332+
throw new RuntimeException("TestIRMatching failed with " + exceptions.size() + " exception(s) - check stderr and stdout");
313333
}
314334
}
315335

336+
private static void runFramework(TestFramework framework) {
337+
baos.reset();
338+
baosErr.reset();
339+
framework.start();
340+
}
341+
316342
private static void runWithArguments(Class<?> clazz, String... args) {
317343
try {
318-
new TestFramework(clazz).addFlags(args).start();
344+
runFramework(new TestFramework(clazz).addFlags(args));
319345
} catch (Exception e) {
320346
addException(e);
321347
}
322348
}
323349

324350
private static void runWithArgumentsFail(Class<?> clazz, String... args) {
325-
new TestFramework(clazz).addFlags(args).start();
351+
runFramework(new TestFramework(clazz).addFlags(args));
326352
}
327353

328354
private static void runCheck(String[] args , Constraint... constraints) {
329355
try {
330-
new TestFramework(constraints[0].getKlass()).addFlags(args).start(); // All constraints have the same class.
331-
Utils.shouldHaveThrownException();
356+
TestFramework framework = new TestFramework(constraints[0].getKlass()); // All constraints have the same class.
357+
if (args != null) {
358+
framework.addFlags(args);
359+
}
360+
runFramework(framework);
361+
Utils.shouldHaveThrownException(baos.toString());
332362
} catch (IRViolationException e) {
333363
checkConstraints(e, constraints);
334364
} catch (Exception e) {
@@ -337,14 +367,7 @@ private static void runCheck(String[] args , Constraint... constraints) {
337367
}
338368

339369
private static void runCheck(Constraint... constraints) {
340-
try {
341-
TestFramework.run(constraints[0].getKlass()); // All constraints have the same class.
342-
Utils.shouldHaveThrownException();
343-
} catch (IRViolationException e) {
344-
checkConstraints(e, constraints);
345-
} catch (Exception e) {
346-
addException(e);
347-
}
370+
runCheck(null, constraints);
348371
}
349372

350373
private static void checkConstraints(IRViolationException e, Constraint[] constraints) {
@@ -354,25 +377,9 @@ private static void checkConstraints(IRViolationException e, Constraint[] constr
354377
constraint.checkConstraint(e);
355378
}
356379
} catch (Exception e1) {
357-
System.out.println(TestFramework.getLastTestVMOutput());
380+
System.out.println(e.getCompilations());
358381
System.out.println(message);
359-
exceptions.add(e1);
360-
}
361-
}
362-
363-
// Single constraint
364-
private static void runFailOnTestsArgs(Constraint constraint, String... args) {
365-
try {
366-
new TestFramework(constraint.getKlass()).addFlags(args).start(); // All constraints have the same class.
367-
Utils.shouldHaveThrownException();
368-
} catch (IRViolationException e) {
369-
try {
370-
constraint.checkConstraint(e);
371-
} catch (Exception e1) {
372-
addException(e);
373-
}
374-
} catch (Exception e) {
375-
addException(e);
382+
addException(e1);
376383
}
377384
}
378385

@@ -387,8 +394,9 @@ public static void findIrIds(String output, String method, int... numbers) {
387394
builder.append(j);
388395
}
389396
}
390-
Asserts.assertTrue(output.contains(builder.toString()), "Could not find encoding: \"" + builder.toString()
391-
+ System.lineSeparator());
397+
if (!output.contains(builder.toString())) {
398+
addException(new RuntimeException("Could not find encoding: \"" + builder.toString() + System.lineSeparator()));
399+
}
392400
}
393401
}
394402

‎test/hotspot/jtreg/testlibrary_tests/ir_framework/tests/TestRunTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import compiler.lib.ir_framework.shared.TestRunException;
2929
import jdk.test.lib.Asserts;
3030

31+
import java.io.ByteArrayOutputStream;
32+
import java.io.PrintStream;
3133
import java.util.Arrays;
3234

3335
/*
@@ -41,15 +43,22 @@
4143
public class TestRunTests {
4244

4345
public static void main(String[] args) {
46+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
47+
PrintStream ps = new PrintStream(baos);
48+
PrintStream oldOut = System.out;
49+
System.setOut(ps);
50+
4451
TestFramework.run();
4552
try {
4653
TestFramework.run(BadStandalone.class);
47-
Utils.shouldHaveThrownException();
54+
Utils.shouldHaveThrownException(baos.toString());
4855
} catch (IRViolationException e) {
56+
System.setOut(oldOut);
4957
String[] matches = { "test(int)", "test2(int)", "Failed IR Rules (2)"};
5058
Arrays.stream(matches).forEach(m -> Asserts.assertTrue(e.getExceptionInfo().contains(m)));
5159
Asserts.assertEQ(e.getExceptionInfo().split("STANDALONE mode", -1).length - 1, 2);
5260
}
61+
System.setOut(oldOut);
5362
new TestFramework(SkipCompilation.class).addFlags("-XX:-UseCompiler").start();
5463
new TestFramework(SkipCompilation.class).addFlags("-Xint").start();
5564
new TestFramework(SkipC2Compilation.class).addFlags("-XX:TieredStopAtLevel=1").start();

‎test/hotspot/jtreg/testlibrary_tests/ir_framework/tests/Utils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import java.util.Arrays;
3232

3333
public class Utils {
34-
public static void shouldHaveThrownException() {
34+
public static void shouldHaveThrownException(String s) {
3535
// Do not throw an exception if we hit a safepoint while printing which could possibly let the IR matching fail.
3636
// This happens very rarely. If there is a problem with the test, then we will catch that on the next test invocation.
37-
if (!TestVMProcess.getLastTestVMOutput().contains(IRMatcher.SAFEPOINT_WHILE_PRINTING_MESSAGE)) {
37+
if (!s.contains(IRMatcher.SAFEPOINT_WHILE_PRINTING_MESSAGE)) {
3838
Asserts.fail("Should have thrown exception");
3939
}
4040
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 27, 2022

@openjdk-notifier[bot]
Please sign in to comment.