Skip to content

Commit 0b9350e

Browse files
author
Vicente Romero
committed
8322992: Javac fails with StackOverflowError when compiling deeply nested synchronized blocks
Reviewed-by: jlahoda
1 parent 20be5e0 commit 0b9350e

File tree

2 files changed

+1214
-18
lines changed

2 files changed

+1214
-18
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,29 +1074,38 @@ public void visitSkip(JCSkip tree) {
10741074
}
10751075

10761076
public void visitBlock(JCBlock tree) {
1077+
/* this method is heavily invoked, as expected, for deeply nested blocks, if blocks doesn't happen to have
1078+
* patterns there will be an unnecessary tax on memory consumption every time this method is executed, for this
1079+
* reason we have created helper methods and here at a higher level we just discriminate depending on the
1080+
* presence, or not, of patterns in a given block
1081+
*/
10771082
if (tree.patternMatchingCatch != null) {
1078-
Set<JCMethodInvocation> prevInvocationsWithPatternMatchingCatch = invocationsWithPatternMatchingCatch;
1079-
ListBuffer<int[]> prevRanges = patternMatchingInvocationRanges;
1080-
State startState = code.state.dup();
1081-
try {
1082-
invocationsWithPatternMatchingCatch = tree.patternMatchingCatch.calls2Handle();
1083-
patternMatchingInvocationRanges = new ListBuffer<>();
1084-
doVisitBlock(tree);
1085-
} finally {
1086-
Chain skipCatch = code.branch(goto_);
1087-
JCCatch handler = tree.patternMatchingCatch.handler();
1088-
code.entryPoint(startState, handler.param.sym.type);
1089-
genPatternMatchingCatch(handler, env, patternMatchingInvocationRanges.toList());
1090-
code.resolve(skipCatch);
1091-
invocationsWithPatternMatchingCatch = prevInvocationsWithPatternMatchingCatch;
1092-
patternMatchingInvocationRanges = prevRanges;
1093-
}
1083+
visitBlockWithPatterns(tree);
10941084
} else {
1095-
doVisitBlock(tree);
1085+
internalVisitBlock(tree);
1086+
}
1087+
}
1088+
1089+
private void visitBlockWithPatterns(JCBlock tree) {
1090+
Set<JCMethodInvocation> prevInvocationsWithPatternMatchingCatch = invocationsWithPatternMatchingCatch;
1091+
ListBuffer<int[]> prevRanges = patternMatchingInvocationRanges;
1092+
State startState = code.state.dup();
1093+
try {
1094+
invocationsWithPatternMatchingCatch = tree.patternMatchingCatch.calls2Handle();
1095+
patternMatchingInvocationRanges = new ListBuffer<>();
1096+
internalVisitBlock(tree);
1097+
} finally {
1098+
Chain skipCatch = code.branch(goto_);
1099+
JCCatch handler = tree.patternMatchingCatch.handler();
1100+
code.entryPoint(startState, handler.param.sym.type);
1101+
genPatternMatchingCatch(handler, env, patternMatchingInvocationRanges.toList());
1102+
code.resolve(skipCatch);
1103+
invocationsWithPatternMatchingCatch = prevInvocationsWithPatternMatchingCatch;
1104+
patternMatchingInvocationRanges = prevRanges;
10961105
}
10971106
}
10981107

1099-
private void doVisitBlock(JCBlock tree) {
1108+
private void internalVisitBlock(JCBlock tree) {
11001109
int limit = code.nextreg;
11011110
Env<GenContext> localEnv = env.dup(tree, new GenContext());
11021111
genStats(tree.stats, localEnv);

0 commit comments

Comments
 (0)