From 43c13a8cd4e52a24d4894382d4ad117749308609 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 23 Sep 2026 14:37:32 +0100 Subject: [PATCH] Fix try/catch inside a catch clause whose try has a finally With a finally, throws in a catch clause stay inside the catch funclet (they invoke into the finally, which ends the catch). A throw inside a try nested in that catch unwinds to the nested try's own pad instead - a top-level pad reached from inside a catchpad, invalid funclet nesting - so even a matching nested catch crashed at run time. Such a throw now ends the outer catch first, as it already did without a finally. An exception the nested catch does not take also skipped the outer finally. A try in its parent's catch clause now chains its mismatch to the parent's finally (new finallyBlockOf / inParentTryCatch side tables), on Windows and on the Itanium path alike. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/TypeScriptPassContext.h | 7 ++ tslang/lib/TypeScript/LowerToAffineLoops.cpp | 33 +++++- tslang/test/tester/CMakeLists.txt | 3 + .../tests/00try_in_catch_with_finally.ts | 106 ++++++++++++++++++ 4 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 tslang/test/tester/tests/00try_in_catch_with_finally.ts diff --git a/tslang/include/TypeScript/TypeScriptPassContext.h b/tslang/include/TypeScript/TypeScriptPassContext.h index e5c02fbfa..e97d51f21 100644 --- a/tslang/include/TypeScript/TypeScriptPassContext.h +++ b/tslang/include/TypeScript/TypeScriptPassContext.h @@ -126,6 +126,8 @@ struct TSContext cleanup.beginRun(); parentTryOp.beginRun(); inParentTryBody.beginRun(); + inParentTryCatch.beginRun(); + finallyBlockOf.beginRun(); landingBlockOf.beginRun(); leavesCatch.beginRun(); } @@ -143,6 +145,11 @@ struct TSContext // parent's to catch - as opposed to the parent's catch or finally, where unwinding to the // parent's landing pad would loop back into the handler it is already running. OpSideTable inParentTryBody{"inParentTryBody"}; + // Whether a try sits in its parentTryOp's catch clause. An exception leaving it is not the + // parent's to catch, but it still has to run the parent's finally (finallyBlockOf). + OpSideTable inParentTryCatch{"inParentTryCatch"}; + // The block a try's exceptional path enters its finally through, for a try that has one. + OpSideTable finallyBlockOf{"finallyBlockOf"}; OpSideTable landingBlockOf{"landingBlockOf"}; // Throws that sit inside a catch clause and therefore have to end the active catch before // they leave it. `return`, `break` and `continue` carry the same meaning in `unwind`, but diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index 3d87fe935..d7461fd13 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -1298,6 +1298,16 @@ struct TryOpLowering : public TsPattern mlir::Block *parentTryOpLandingPad = parentTryOp ? tsContext->landingBlockOf.lookup(parentTryOp) : nullptr; auto parentCatchesThis = parentTryOpLandingPad && tsContext->inParentTryBody.lookup(tryOp.getOperation()); + // Where an exception this try does not catch has to go next within the function: the + // parent's landing pad for a try in the parent's body, the parent's finally for one in + // the parent's catch clause (whose exceptions the parent does not catch, but whose + // finally still has to run), and nowhere - the caller - otherwise. + mlir::Block *enclosingHandler = parentCatchesThis ? parentTryOpLandingPad : nullptr; + if (!enclosingHandler && parentTryOp && tsContext->inParentTryCatch.lookup(tryOp.getOperation())) + { + enclosingHandler = tsContext->finallyBlockOf.lookup(parentTryOp); + } + MLIRRTTIHelperVC rttih(rewriter, module, tsContext->compileOptions); auto i8PtrTy = mth.getOpaqueType(); @@ -1335,17 +1345,21 @@ struct TryOpLowering : public TsPattern // set TryOp -> child TryOp auto inBody = false; + auto inCatch = false; auto visitorTryOps = [&](Operation *op) { if (auto childTryOp = dyn_cast_or_null(op)) { tsContext->parentTryOp[op] = tryOp.getOperation(); tsContext->inParentTryBody[op] = inBody; + tsContext->inParentTryCatch[op] = inCatch; } }; inBody = true; tryOp.getBody().walk(visitorTryOps); inBody = false; + inCatch = true; tryOp.getCatches().walk(visitorTryOps); + inCatch = false; tryOp.getFinally().walk(visitorTryOps); mlir::SmallVector returns; @@ -1454,7 +1468,14 @@ struct TryOpLowering : public TsPattern // turns this throw into an invoke into the finally block, and the finally // is what ends the catch - ending it here as well runs it twice and // breaks the unwind (51exceptions.ts is the case that proves it). - if (!finallyHasOps) + // + // Except for a throw inside a try nested in this catch: that one unwinds to + // the nested try's own pad, not to the finally. Left inside this catch's + // funclet, the nested pad would be a catchswitch `within none` reached from + // inside a catchpad - invalid funclet nesting, and a crash at run time. So + // it ends the catch first, exactly as it does when there is no finally. + auto inNestedTry = op->getParentOfType() != nullptr; + if (!finallyHasOps || inNestedTry) { tsContext->leavesCatch[op] = true; } @@ -1503,6 +1524,7 @@ struct TryOpLowering : public TsPattern { tsContext->parentTryOp[newOp] = parent; tsContext->inParentTryBody[newOp] = tsContext->inParentTryBody.lookup(oldOp); + tsContext->inParentTryCatch[newOp] = tsContext->inParentTryCatch.lookup(oldOp); } } }; @@ -1513,6 +1535,7 @@ struct TryOpLowering : public TsPattern finallyMapping); propagateTsContextEntries(finallyMapping); finallyBlock = beforeFinallyBlock->getNextNode(); + tsContext->finallyBlockOf[tryOp.getOperation()] = finallyBlock; finallyBlockLast = continuation->getPrevNode(); // add clone for 'return' @@ -1659,7 +1682,7 @@ struct TryOpLowering : public TsPattern // where the exception belongs (the same catch-all-and-rethrow the nested finally and // cleanup cases here already use). linuxHasCleanups adds the clause on its own. auto linuxTypedCatchChains = !tsContext->compileOptions.isWindows && catchHasOps && rttih.hasType() && - !linuxHasCleanups && (parentCatchesThis || finallyHasOps); + !linuxHasCleanups && (enclosingHandler || finallyHasOps); mlir::Value catchAll; if (parentTryOpLandingPad && finallyHasOps || linuxHasCleanups && rttih.hasType() || @@ -1759,7 +1782,7 @@ struct TryOpLowering : public TsPattern // // Only a try in its parent's body has an edge to add (parentCatchesThis): one in the // parent's catch or finally is already inside the handler that pad leads to. - auto windowsNeedsParentEdge = tsContext->compileOptions.isWindows && (parentCatchesThis || finallyHasOps); + auto windowsNeedsParentEdge = tsContext->compileOptions.isWindows && (enclosingHandler || finallyHasOps); if (rttih.hasType() && (!tsContext->compileOptions.isWindows || windowsNeedsParentEdge)) { if (linuxHasCleanups) @@ -1940,9 +1963,9 @@ struct TryOpLowering : public TsPattern { tsContext->unwind[rethrowOp] = finallyBlock; } - else if (parentCatchesThis) + else if (enclosingHandler) { - tsContext->unwind[rethrowOp] = parentTryOpLandingPad; + tsContext->unwind[rethrowOp] = enclosingHandler; } } else if (parentTryOpLandingPad) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index bf5dd6d65..fab86becc 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -388,6 +388,7 @@ tslang_add_test(NAME test-compile-00-try-using-catch COMMAND test-runner "${PROJ tslang_add_test(NAME test-compile-00-throw-in-catch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_in_catch.ts") tslang_add_test(NAME test-compile-00-throw-inlined COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_inlined.ts") tslang_add_test(NAME test-compile-00-try-catch-sequence-and-nesting COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_sequence_and_nesting.ts") +tslang_add_test(NAME test-compile-00-try-in-catch-with-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_in_catch_with_finally.ts") tslang_add_test(NAME test-compile-00-throw-number-untyped-catch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts") tslang_add_test(NAME test-compile-00-catch-untyped-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_untyped_value.ts") tslang_add_test(NAME test-compile-00-throw-bool-bigint COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_bool_bigint.ts") @@ -814,6 +815,7 @@ tslang_add_test(NAME test-jit-00-owned-locals COMMAND test-runner -jit "${PROJEC tslang_add_test(NAME test-jit-00-throw-in-catch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_in_catch.ts") tslang_add_test(NAME test-jit-00-throw-inlined COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_inlined.ts") tslang_add_test(NAME test-jit-00-try-catch-sequence-and-nesting COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_sequence_and_nesting.ts") +tslang_add_test(NAME test-jit-00-try-in-catch-with-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_in_catch_with_finally.ts") tslang_add_test(NAME test-jit-00-throw-number-untyped-catch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts") tslang_add_test(NAME test-jit-00-catch-untyped-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_untyped_value.ts") tslang_add_test(NAME test-jit-00-throw-bool-bigint COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_bool_bigint.ts") @@ -1721,6 +1723,7 @@ set(TSLANG_CORPUS 00try_catch.ts 00try_finally_break_continue.ts 00try_finally_return.ts + 00try_in_catch_with_finally.ts 00try_finally.ts 00try_using_catch.ts 00tuple_named.ts diff --git a/tslang/test/tester/tests/00try_in_catch_with_finally.ts b/tslang/test/tester/tests/00try_in_catch_with_finally.ts new file mode 100644 index 000000000..2c1da933d --- /dev/null +++ b/tslang/test/tester/tests/00try_in_catch_with_finally.ts @@ -0,0 +1,106 @@ +// A try/catch written inside a catch clause whose own try also has a finally. +// +// Without the finally this already worked (00nested_catch.ts): a throw inside the nested try +// ends the outer catch ahead of itself, so the nested try runs outside the outer catch's funclet. +// With a finally, throws in the catch clause are left inside the funclet instead, because they +// invoke into the finally and the finally is what ends the catch. A throw inside the *nested* +// try, though, unwinds to the nested try's own pad, and that pad is emitted at the top level - +// so an invoke inside the catch funclet unwound to a pad outside it. That is invalid funclet +// nesting, and even a nested catch that matched crashed the program. Such a throw now ends the +// outer catch first, as it does without a finally. +// +// Also: an exception the nested catch does not take has to run the outer finally before it +// leaves the function. The nested try is in the outer try's catch clause, so its exceptions are +// not the outer try's to catch, but the outer finally still owes them a run. + +let trail = 0; + +function step(n: number) { + trail = trail * 10 + n; +} + +function nestedMatches() { + try { + throw 1; + } catch (e: int) { + step(1); + try { + throw 2; + } catch (x: int) { + step(9); + } + step(8); + } finally { + step(2); + } +} + +function nestedMismatchRunsOuterFinally() { + try { + throw 1; + } catch (e: int) { + step(1); + try { + throw "s"; + } catch (x: int) { + step(9); + } + step(8); + } finally { + step(2); + } +} + +function bothFinallysRunInnermostFirst() { + try { + throw 1; + } catch (e: int) { + step(1); + try { + throw "s"; + } catch (x: int) { + step(9); + } finally { + step(5); + } + step(8); + } finally { + step(2); + } +} + +function throwAfterNestedTry() { + try { + throw 1; + } catch (e: int) { + step(1); + try { + throw 2; + } catch (x: int) { + step(9); + } + throw "t"; + } finally { + step(2); + } +} + +function run(f: () => void) { + trail = 0; + try { + f(); + } catch (e: string) { + step(3); + } + + return trail; +} + +function main() { + assert(run(nestedMatches) == 1982, "a matching nested catch inside a catch with a finally"); + assert(run(nestedMismatchRunsOuterFinally) == 123, "a nested mismatch runs the outer finally, then reaches the caller"); + assert(run(bothFinallysRunInnermostFirst) == 1523, "both finally blocks run, innermost first"); + assert(run(throwAfterNestedTry) == 1923, "a throw after a nested try still runs the finally"); + + print("done."); +}