Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tslang/include/TypeScript/TypeScriptPassContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ struct TSContext
cleanup.beginRun();
parentTryOp.beginRun();
inParentTryBody.beginRun();
inParentTryCatch.beginRun();
finallyBlockOf.beginRun();
landingBlockOf.beginRun();
leavesCatch.beginRun();
}
Expand All @@ -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<bool> 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<bool> inParentTryCatch{"inParentTryCatch"};
// The block a try's exceptional path enters its finally through, for a try that has one.
OpSideTable<mlir::Block *> finallyBlockOf{"finallyBlockOf"};
OpSideTable<mlir::Block *> 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
Expand Down
33 changes: 28 additions & 5 deletions tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,16 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
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();

Expand Down Expand Up @@ -1335,17 +1345,21 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>

// set TryOp -> child TryOp
auto inBody = false;
auto inCatch = false;
auto visitorTryOps = [&](Operation *op) {
if (auto childTryOp = dyn_cast_or_null<mlir_ts::TryOp>(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<Operation *> returns;
Expand Down Expand Up @@ -1454,7 +1468,14 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
// 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<mlir_ts::TryOp>() != nullptr;
if (!finallyHasOps || inNestedTry)
{
tsContext->leavesCatch[op] = true;
}
Expand Down Expand Up @@ -1503,6 +1524,7 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
{
tsContext->parentTryOp[newOp] = parent;
tsContext->inParentTryBody[newOp] = tsContext->inParentTryBody.lookup(oldOp);
tsContext->inParentTryCatch[newOp] = tsContext->inParentTryCatch.lookup(oldOp);
}
}
};
Expand All @@ -1513,6 +1535,7 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
finallyMapping);
propagateTsContextEntries(finallyMapping);
finallyBlock = beforeFinallyBlock->getNextNode();
tsContext->finallyBlockOf[tryOp.getOperation()] = finallyBlock;
finallyBlockLast = continuation->getPrevNode();

// add clone for 'return'
Expand Down Expand Up @@ -1659,7 +1682,7 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
// 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() ||
Expand Down Expand Up @@ -1759,7 +1782,7 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
//
// 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)
Expand Down Expand Up @@ -1940,9 +1963,9 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
{
tsContext->unwind[rethrowOp] = finallyBlock;
}
else if (parentCatchesThis)
else if (enclosingHandler)
{
tsContext->unwind[rethrowOp] = parentTryOpLandingPad;
tsContext->unwind[rethrowOp] = enclosingHandler;
}
}
else if (parentTryOpLandingPad)
Expand Down
3 changes: 3 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
106 changes: 106 additions & 0 deletions tslang/test/tester/tests/00try_in_catch_with_finally.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
Loading