Skip to content

Commit 559826c

Browse files
committed
8332474: Tighten up ToolBox' JavacTask to not silently accept javac crash as a failure
Reviewed-by: vromero
1 parent eec0e15 commit 559826c

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java

+1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSy
330330
} else {
331331
sym = syms.enterModule(name);
332332
if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) {
333+
decl.sym = syms.errModule;
333334
log.error(decl.pos(), Errors.DuplicateModule(sym));
334335
return;
335336
}

test/langtools/tools/lib/toolbox/AbstractTask.java

+51-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.EnumMap;
3636
import java.util.HashMap;
3737
import java.util.Map;
38+
import java.util.function.BiConsumer;
3839
import static toolbox.ToolBox.lineSeparator;
3940

4041
/**
@@ -50,7 +51,9 @@ abstract class AbstractTask<T extends AbstractTask<T>> implements Task {
5051
private final Map<OutputKind, String> redirects = new EnumMap<>(OutputKind.class);
5152
private final Map<String, String> envVars = new HashMap<>();
5253
private Expect expect = Expect.SUCCESS;
53-
int expectedExitCode = 0;
54+
//validator for exit codes, first parameter is the exit code
55+
//the second the test name:
56+
private BiConsumer<Integer, String> exitCodeValidator = null;
5457

5558
/**
5659
* Create a task that will execute in the specified mode.
@@ -67,7 +70,7 @@ protected AbstractTask(ToolBox tb, Mode mode) {
6770
* @return the result of calling {@code run()}
6871
*/
6972
public Result run(Expect expect) {
70-
expect(expect, Integer.MIN_VALUE);
73+
expect(expect, (_, _) -> {});
7174
return run();
7275
}
7376

@@ -83,17 +86,56 @@ public Result run(Expect expect, int exitCode) {
8386
return run();
8487
}
8588

89+
/**
90+
* Sets the expected outcome of the task and calls {@code run()}.
91+
* @param expect the expected outcome
92+
* @param exitCodeValidator an exit code validator. The first parameter will
93+
* be the actual exit code, the second test name,
94+
* should throw TaskError if the exit code is not
95+
* as expected. Only used if the expected outcome
96+
* is {@code FAIL}
97+
* @return the result of calling {@code run()}
98+
*/
99+
public Result run(Expect expect,
100+
BiConsumer<Integer, String> exitCodeValidator) {
101+
expect(expect, exitCodeValidator);
102+
return run();
103+
}
104+
105+
/**
106+
* Sets the expected outcome and expected exit code of the task.
107+
* The exit code will not be checked if the outcome is
108+
* {@code Expect.SUCCESS} or if the exit code is set to
109+
* {@code Integer.MIN_VALUE}.
110+
* @param expect the expected outcome
111+
* @param expectedExitCode the expected exit code
112+
*/
113+
protected void expect(Expect expect, int expectedExitCode) {
114+
expect(expect, (exitCode, testName) -> {
115+
if (expectedExitCode != Integer.MIN_VALUE &&
116+
exitCode != expectedExitCode) {
117+
throw new TaskError("Task " + testName + "failed with unexpected exit code "
118+
+ exitCode + ", expected " + expectedExitCode);
119+
}
120+
});
121+
}
122+
86123
/**
87124
* Sets the expected outcome and expected exit code of the task.
88125
* The exit code will not be checked if the outcome is
89126
* {@code Expect.SUCCESS} or if the exit code is set to
90127
* {@code Integer.MIN_VALUE}.
91128
* @param expect the expected outcome
92-
* @param exitCode the expected exit code
129+
* @param exitCodeValidator an exit code validator. The first parameter will
130+
* be the actual exit code, the second test name,
131+
* should throw TaskError if the exit code is not
132+
* as expected. Only used if the expected outcome
133+
* is {@code FAIL}
93134
*/
94-
protected void expect(Expect expect, int exitCode) {
135+
protected void expect(Expect expect,
136+
BiConsumer<Integer, String> exitCodeValidator) {
95137
this.expect = expect;
96-
this.expectedExitCode = exitCode;
138+
this.exitCodeValidator = exitCodeValidator;
97139
}
98140

99141
/**
@@ -119,11 +161,11 @@ protected Result checkExit(Result result) throws TaskError {
119161
throw new TaskError("Task " + name() + " succeeded unexpectedly");
120162
}
121163

122-
if (expectedExitCode != Integer.MIN_VALUE
123-
&& result.exitCode != expectedExitCode) {
164+
try {
165+
exitCodeValidator.accept(result.exitCode, name());
166+
} catch (Throwable t) {
124167
result.writeAll();
125-
throw new TaskError("Task " + name() + "failed with unexpected exit code "
126-
+ result.exitCode + ", expected " + expectedExitCode);
168+
throw t;
127169
}
128170
break;
129171
}

test/langtools/tools/lib/toolbox/JavacTask.java

+20
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@ public String name() {
314314
return "javac";
315315
}
316316

317+
@Override
318+
public Result run(Expect expect) {
319+
int expectedExitCode = expect == Expect.SUCCESS ? 0 : 1;
320+
321+
return run(expect, (exitCode, testName) -> {
322+
if (exitCode == 4) {
323+
throw new TaskError("Task " + testName + " failed due to a javac crash "
324+
+ "(exit code 4)");
325+
}
326+
});
327+
}
328+
329+
@Override
330+
public Result run(Expect expect, int exitCode) {
331+
if (exitCode == 4) {
332+
throw new IllegalArgumentException("Disallowed exit code: 4");
333+
}
334+
return super.run(expect, exitCode);
335+
}
336+
317337
/**
318338
* Calls the compiler with the arguments as currently configured.
319339
* @return a Result object indicating the outcome of the compilation

0 commit comments

Comments
 (0)