Skip to content

Commit d171446

Browse files
committed
8322992: Javac fails with StackOverflowError when compiling deeply nested synchronized blocks
Backport-of: 0b9350e8b619bc556f36652cde6f73211be5b85b
1 parent b6790cc commit d171446

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
@@ -1100,29 +1100,38 @@ public void visitSkip(JCSkip tree) {
11001100
}
11011101

11021102
public void visitBlock(JCBlock tree) {
1103+
/* this method is heavily invoked, as expected, for deeply nested blocks, if blocks doesn't happen to have
1104+
* patterns there will be an unnecessary tax on memory consumption every time this method is executed, for this
1105+
* reason we have created helper methods and here at a higher level we just discriminate depending on the
1106+
* presence, or not, of patterns in a given block
1107+
*/
11031108
if (tree.patternMatchingCatch != null) {
1104-
Set<JCMethodInvocation> prevInvocationsWithPatternMatchingCatch = invocationsWithPatternMatchingCatch;
1105-
ListBuffer<int[]> prevRanges = patternMatchingInvocationRanges;
1106-
State startState = code.state.dup();
1107-
try {
1108-
invocationsWithPatternMatchingCatch = tree.patternMatchingCatch.calls2Handle();
1109-
patternMatchingInvocationRanges = new ListBuffer<>();
1110-
doVisitBlock(tree);
1111-
} finally {
1112-
Chain skipCatch = code.branch(goto_);
1113-
JCCatch handler = tree.patternMatchingCatch.handler();
1114-
code.entryPoint(startState, handler.param.sym.type);
1115-
genPatternMatchingCatch(handler, env, patternMatchingInvocationRanges.toList());
1116-
code.resolve(skipCatch);
1117-
invocationsWithPatternMatchingCatch = prevInvocationsWithPatternMatchingCatch;
1118-
patternMatchingInvocationRanges = prevRanges;
1119-
}
1109+
visitBlockWithPatterns(tree);
11201110
} else {
1121-
doVisitBlock(tree);
1111+
internalVisitBlock(tree);
1112+
}
1113+
}
1114+
1115+
private void visitBlockWithPatterns(JCBlock tree) {
1116+
Set<JCMethodInvocation> prevInvocationsWithPatternMatchingCatch = invocationsWithPatternMatchingCatch;
1117+
ListBuffer<int[]> prevRanges = patternMatchingInvocationRanges;
1118+
State startState = code.state.dup();
1119+
try {
1120+
invocationsWithPatternMatchingCatch = tree.patternMatchingCatch.calls2Handle();
1121+
patternMatchingInvocationRanges = new ListBuffer<>();
1122+
internalVisitBlock(tree);
1123+
} finally {
1124+
Chain skipCatch = code.branch(goto_);
1125+
JCCatch handler = tree.patternMatchingCatch.handler();
1126+
code.entryPoint(startState, handler.param.sym.type);
1127+
genPatternMatchingCatch(handler, env, patternMatchingInvocationRanges.toList());
1128+
code.resolve(skipCatch);
1129+
invocationsWithPatternMatchingCatch = prevInvocationsWithPatternMatchingCatch;
1130+
patternMatchingInvocationRanges = prevRanges;
11221131
}
11231132
}
11241133

1125-
private void doVisitBlock(JCBlock tree) {
1134+
private void internalVisitBlock(JCBlock tree) {
11261135
int limit = code.nextreg;
11271136
Env<GenContext> localEnv = env.dup(tree, new GenContext());
11281137
genStats(tree.stats, localEnv);

0 commit comments

Comments
 (0)