From 04b2d44b221434d11c3526922a37bf189f75abbc Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 16 Nov 2026 15:30:36 +0000 Subject: [PATCH 1/5] -mm=own: a value captured by a closure released early is not read after `const n = new N(); { let c = () => n.v.get(); } return n.v.x;` was accepted, and read n after the inner block freed it: the closure's box owns its copies (phase 4), and a read of n after the move counted as one through its new owner (spec 29) - but the box dies with the closure value, which the `let`'s release at the block's end gave back. The reads are now checked against the closure's ends too (followClosure); one that may go anywhere else is no owner to read through. Now "'n' is used here after its value was moved", as when the closure is called. Found by the differential fuzzer (seed 920187). Co-Authored-By: Claude Opus 5.5 --- .../OwnershipInferenceReadsAfterMove.cpp | 28 +++++++++++++++++++ tslang/test/tester/CMakeLists.txt | 1 + .../own/own_err_capture_block_closure.ts | 7 +++++ 3 files changed, 36 insertions(+) create mode 100644 tslang/test/tester/own/own_err_capture_block_closure.ts diff --git a/tslang/lib/TypeScript/OwnershipInferenceReadsAfterMove.cpp b/tslang/lib/TypeScript/OwnershipInferenceReadsAfterMove.cpp index 07c699bc5..8b5b1d117 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceReadsAfterMove.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceReadsAfterMove.cpp @@ -159,6 +159,34 @@ namespace own_inference } } + // a closure's box: it lives as long as the closure value that owns it, and goes where that is + // given back (`{ let c = () => n.v.get(); } n.v` - the `let`'s release at the block's end frees + // the box, and `n` with it). A closure that may go anywhere else is no owner to read through. + for (auto &root : chain.roots) + { + auto boundOp = closureOfBox(root.first); + if (!boundOp) + { + continue; + } + + llvm::SmallVector closureUses; + llvm::SmallVector closureEnds; + auto aliased = false; + if (followClosure(boundOp.getResult(), closureUses, closureEnds, aliased) || aliased) + { + return false; + } + + for (auto *end : closureEnds) + { + if (outlives(end)) + { + return false; + } + } + } + for (auto *drop : drops) { if (dropsChain(drop, chain, nullptr, value) && outlives(drop)) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index ef61981fa..9cf399c95 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5557,6 +5557,7 @@ set(own_error_cases "own_err_weakref_after_call|:5:137: error: '[^']*' borrows the object a WeakRef refers to but is used here after it may be released or overwritten" "own_err_weakref_after_release|:4:137: error: '[^']*' borrows the object a WeakRef refers to but is used here after it may be released or overwritten" "own_err_weakref_thread|:14:15: error: -mm=own cannot make a WeakRef to a 'Node' in a program that shares what may hold one with another thread" + "own_err_capture_block_closure|:5:51: error: 'n4' is used here after its value was moved" "own_err_weakref_local_reassigned|:5:108: error: '[^']*' borrows the object a WeakRef refers to but is used here after it may be released or overwritten" "own_err_list_refill_from_param|:13:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" "own_err_list_unlink_walk|:16:9: error: '[^']*' borrows a field but is used here after it may be released or overwritten" diff --git a/tslang/test/tester/own/own_err_capture_block_closure.ts b/tslang/test/tester/own/own_err_capture_block_closure.ts new file mode 100644 index 000000000..d904e1c3b --- /dev/null +++ b/tslang/test/tester/own/own_err_capture_block_closure.ts @@ -0,0 +1,7 @@ +class V { constructor(public x: number) {} get(): number { return this.x; } } +class N { constructor(public v: V) {} } +function f2(): number { + const n4 = new N(new V(1)); + { let c5 = (): number => n4.v.get(); } return n4.v.x; +} +function main() { print(f2()); } From 5d1319a22c53fa3f805233d532b3d70ddbb211e3 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 16 Nov 2026 15:37:12 +0000 Subject: [PATCH 2/5] -mm=own: a value moved on some ways of a loop's body is released once on a break `for (...) { const n = mk(); if (stop) break; if (c) keep(h, n); }` released n twice on the break: the release on the ways that do not move n (TSLANG 81, valueUnmovedEdges) went on every edge into the blocks the move reaches - and from the move those run through the loop's latch to its header and out to its exit, where n does not live, so the break's edge into the exit got a release on top of the break's own. The blocks are now only those n's definition dominates. Found by the differential fuzzer (seed 920048; a crash under glibc's MALLOC_PERTURB_). Co-Authored-By: Claude Opus 5.5 --- .../OwnershipInferenceEdgeReleases.cpp | 7 ++- tslang/test/tester/CMakeLists.txt | 6 ++- .../tester/own/own_moved_some_paths_break.ts | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tslang/test/tester/own/own_moved_some_paths_break.ts diff --git a/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp b/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp index 07937e0a2..fd3f243f3 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp @@ -494,14 +494,17 @@ namespace own_inference } // what follows the move, while this value is the one the definition made: the blocks the move - // reaches, not through the definition again (a loop's next turn makes another) + // reaches, not through the definition again (a loop's next turn makes another), and only those + // the definition dominates - a loop's header and its exit are outside the value's life, and the + // edges into them (a `break`) release it on their own auto *moveBlock = acquire->getBlock(); llvm::SmallPtrSet after; llvm::SmallVector work(moveBlock->getSuccessors().begin(), moveBlock->getSuccessors().end()); while (!work.empty()) { auto *block = work.pop_back_val(); - if (block == definition->getBlock() || !after.insert(block).second) + if (block == definition->getBlock() || !dominance->dominates(definition->getBlock(), block) || + !after.insert(block).second) { continue; } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 9cf399c95..9bb3093cb 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4663,7 +4663,8 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_any_copies own_array_copies own_string_pushed_twice own_rest_takes own_shares_elements own_rest_keeps_declared own_interface_getter_let own_callback_assigns_capture own_param_capture_copies own_swap_local_field own_closure_keeps_param own_chain_returns_this own_callback_fewer_params - own_optional_literal_result own_merge_moves_local own_global_merge_initializer own_merge_of_constants) + own_optional_literal_result own_merge_moves_local own_global_merge_initializer own_merge_of_constants + own_moved_some_paths_break) tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") tslang_add_test(NAME test-compile-own-${own_test} COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") # The promise: no counting at all in what own emits. @@ -4704,7 +4705,8 @@ foreach(keeps_mm rc gc none) own_generic_specializations own_callback_cast own_callback_interface_slot own_equal_strings_into_any own_rest_keeps_declared own_interface_getter_let own_callback_assigns_capture own_param_capture_copies own_swap_local_field own_closure_keeps_param own_chain_returns_this own_callback_fewer_params - own_optional_literal_result own_merge_moves_local own_global_merge_initializer own_merge_of_constants) + own_optional_literal_result own_merge_moves_local own_global_merge_initializer own_merge_of_constants + own_moved_some_paths_break) tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") tslang_add_test(NAME test-compile-${keeps_mm}-${keeps_test} COMMAND test-runner -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts") endforeach() diff --git a/tslang/test/tester/own/own_moved_some_paths_break.ts b/tslang/test/tester/own/own_moved_some_paths_break.ts new file mode 100644 index 000000000..bc25186ca --- /dev/null +++ b/tslang/test/tester/own/own_moved_some_paths_break.ts @@ -0,0 +1,47 @@ +// A local moved on some ways through a loop's body and released on the others, where another way +// leaves the loop by `break`: the break releases it once - its own release - and the release on the +// ways that do not move it is not placed on the edge out of the loop as well. +class N { + constructor(public x: number, public name: string) {} +} + +class H { + n: N | null = null; +} + +@noinline +function mk(i: number): N { + return new N(i, "n" + i); +} + +function keep(h: H, p: N) { + h.n = p; +} + +function run(stopAt: number): number { + const h = new H(); + let seen: number = 0; + for (let i = 0; i < 10; i++) { + const n = mk(i); + if (i == stopAt) { + break; + } + + seen += n.x; + if (i % 2 == 1) { + keep(h, n); + } + } + + return seen + (h.n ? h.n.x * 100 : 0); +} + +function main() { + for (let round = 0; round < 200; round++) { + assert(run(4) == 0 + 1 + 2 + 3 + 300, "stops at 4"); + assert(run(7) == 21 + 500, "stops at 7"); + assert(run(100) == 45 + 900, "no stop"); + } + + print("done."); +} From 23eae8db5083212a24e3a51e64b7b73deec5ad54 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 16 Nov 2026 15:40:30 +0000 Subject: [PATCH 3/5] rc/own: a local declared in a catch or finally clause owns what it holds Under -mm=rc such a local took no count: `finally { const k = G; G = mk(5); k.items[0] }` read the object reassigning G freed (a crash; in a catch clause a wrong value from the reused block). MLIRGen left these locals unowned while a call inside an exception funclet was fragile; the example it cited (`catch (e: int) { new Res(); throw 2; }`) no longer crashes. They are owned now, released where the clause ends, on a return or a break out of it. The clause still synthesizes no cleanup TryOp of its own, so one an exception leaves the clause with leaks rather than frees. Under own the shape is refused as before ("borrows 'G' but is used here after 'G' is released or overwritten"). 00catch_finally_owned_locals_eh covers a throw, return, break, nested try and rethrow out of clauses holding owned locals (rc, own, gc, none; JIT and AOT, --di -O0 checked by hand). Found by the differential fuzzer (seed 920443). Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 7 ++- tslang/lib/TypeScript/MLIRGenVariables.cpp | 10 +-- tslang/test/tester/CMakeLists.txt | 9 +++ .../tests/00catch_finally_locals_own.ts | 63 +++++++++++++++++++ .../tests/00catch_finally_owned_locals_eh.ts | 49 +++++++++++++++ 5 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 tslang/test/tester/tests/00catch_finally_locals_own.ts create mode 100644 tslang/test/tester/tests/00catch_finally_owned_locals_eh.ts diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index b1f33a814..a34d891e7 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -694,8 +694,9 @@ class MLIRGenImpl // // Re-checked after the ToInvoke fix in Win32ExceptionPass, which retired the sibling // blockHasNestedUsing guard: this one is still needed, and the crash it avoids still has a - // cause of its own. Note it also costs ownership - localTakesOwnership consults it, so a - // heap local declared in a catch or finally clause is not owned and leaks under -mm=rc. + // cause of its own. It no longer costs ownership: a heap local declared in a catch or finally + // clause is owned (localTakesOwnership), released where the clause ends normally; with no + // synthesized TryOp there, one that an exception leaves the clause with leaks. bool blockIsInsideCatchOrFinally() { auto *block = builder.getInsertionBlock(); @@ -2387,7 +2388,7 @@ class MLIRGenImpl return genContext.ownedVars != nullptr && !variableDeclarationInfo.isGlobal && !variableDeclarationInfo.deleted && !variableDeclarationInfo.allocateInContextThis && variableDeclarationInfo.initial && variableDeclarationInfo.type && - mth.ownsHeapMemory(location, variableDeclarationInfo.type) && !blockIsInsideCatchOrFinally(); + mth.ownsHeapMemory(location, variableDeclarationInfo.type); } // `let s = 0`: no type given, and an integer literal to infer one from - integer literals are diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index f63243860..8c12932e5 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -175,10 +175,12 @@ namespace mlirgen // retaining at the declaration would read an uninitialized slot as a live reference. // Consequently a `let s: string;` assigned later never becomes an owner - the // assignment path below only fires on a slot this marked, so that stays balanced; - // - locals declared in a catch or finally clause. A release there is a call inside an - // exception funclet, and a call there is fragile independently of ownership: `catch (e: - // int) { new Res(); throw 2; }` crashes at run time with nothing of this involved. - // Rather than add a second way to reach it, those locals are not owned. + // + // A local declared in a catch or finally clause is owned like any other. It was not, while a + // call inside an exception funclet was fragile (`catch (e: int) { new Res(); throw 2; }` + // crashed, since fixed); a borrowing local there read freed memory under rc once its source + // was given a new value (a fuzzer find, seed 920443). The clause synthesizes no TryOp of its + // own (blockIsInsideCatchOrFinally), so one an exception leaves the clause with leaks. // // The test itself is localTakesOwnership, shared with the hoisting decision in // createLocalVariable so the two cannot disagree about which declarations these are. diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 9bb3093cb..a7de4c2e4 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -790,6 +790,8 @@ tslang_add_test(NAME test-compile-00-interface-union-literal COMMAND test-runner tslang_add_test(NAME test-compile-01-try-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts") tslang_add_test(NAME test-compile-00-try-finally-return COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_return.ts") tslang_add_test(NAME test-compile-00-try-finally-funclet COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_funclet.ts") +tslang_add_test(NAME test-compile-00-catch-finally-locals-own COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_finally_locals_own.ts") +tslang_add_test(NAME test-compile-00-catch-finally-owned-locals-eh COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_finally_owned_locals_eh.ts") tslang_add_test(NAME test-compile-00-try-finally-break-continue COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_break_continue.ts") tslang_add_test(NAME test-compile-00-try-catch-rethrow COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_rethrow.ts") tslang_add_test(NAME test-compile-00-try-catch-accessor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_accessor.ts") @@ -1469,6 +1471,11 @@ tslang_add_test(NAME test-jit-00-interface-union-literal COMMAND test-runner -ji tslang_add_test(NAME test-jit-01-try-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts") tslang_add_test(NAME test-jit-00-try-finally-return COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_return.ts") tslang_add_test(NAME test-jit-00-try-finally-funclet COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_funclet.ts") +tslang_add_test(NAME test-jit-00-catch-finally-locals-own COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_finally_locals_own.ts") +tslang_add_test(NAME test-jit-00-catch-finally-owned-locals-eh COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_finally_owned_locals_eh.ts") +# the same under own: locals in catch and finally clauses own what they hold (fuzzer seed 920443) +tslang_add_test(NAME test-jit-own-00-catch-finally-owned-locals-eh COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_finally_owned_locals_eh.ts") +tslang_add_test(NAME test-compile-own-00-catch-finally-owned-locals-eh COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_finally_owned_locals_eh.ts") tslang_add_test(NAME test-jit-00-try-finally-break-continue COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_break_continue.ts") tslang_add_test(NAME test-jit-00-try-catch-rethrow COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_rethrow.ts") tslang_add_test(NAME test-jit-00-try-catch-accessor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_accessor.ts") @@ -2941,6 +2948,8 @@ set(TSLANG_CORPUS 00try_finally_break_continue.ts 00try_finally_return.ts 00try_finally_funclet.ts + 00catch_finally_locals_own.ts + 00catch_finally_owned_locals_eh.ts 00try_in_catch_with_finally.ts 00try_finally.ts 00try_finally_throws.ts diff --git a/tslang/test/tester/tests/00catch_finally_locals_own.ts b/tslang/test/tester/tests/00catch_finally_locals_own.ts new file mode 100644 index 000000000..d51bf2faf --- /dev/null +++ b/tslang/test/tester/tests/00catch_finally_locals_own.ts @@ -0,0 +1,63 @@ +// A local declared in a catch or finally clause holds what it is given: it keeps that object alive +// when the place it came from is given a new value. Under rc such a local took no count, so +// reassigning the global freed the object it still read (a fuzzer find, seed 920443). +class V { + constructor(public x: number) {} +} + +class N { + items: V[]; + constructor(v: V) { + this.items = [new V(v.x)]; + } +} + +interface IY { + items: V[]; +} + +@noinline +function mk(i: number): N { + return new N(new V(i)); +} + +function churn() { + const hold: N[] = []; + for (let i = 0; i < 64; i++) { + hold.push(new N(new V(777))); + } +} + +let G: N = mk(1); + +function inFinally(): number { + try { + } finally { + const k: IY = G; + G = mk(5); + churn(); + return k.items[0].x; + } +} + +function inCatch(): number { + try { + throw 1; + } catch (e) { + const k: N = G; + G = mk(6); + churn(); + return k.items[0].x; + } +} + +function main() { + for (let i = 0; i < 100; i++) { + G = mk(1); + assert(inFinally() == 1, "finally"); + G = mk(2); + assert(inCatch() == 2, "catch"); + } + + print("done."); +} diff --git a/tslang/test/tester/tests/00catch_finally_owned_locals_eh.ts b/tslang/test/tester/tests/00catch_finally_owned_locals_eh.ts new file mode 100644 index 000000000..31bcea4de --- /dev/null +++ b/tslang/test/tester/tests/00catch_finally_owned_locals_eh.ts @@ -0,0 +1,49 @@ +// Locals declared in catch and finally clauses own what they hold: released where the clause ends, +// on a return or break out of it, and kept alive (not freed early) under every model. One that an +// exception leaves the clause with leaks: the clause has no cleanup of its own. +class R { constructor(public v: number, public s: string) {} } +@noinline function mk(i: number): R { return new R(i, "r" + i); } +function throwOutOfCatch(i: number): number { + try { + try { throw 1; } catch (e) { const k = mk(i); if (k.v >= 0) throw 2; return k.v; } + } catch (e2) { return 7; } +} +function returnFromFinally(i: number): number { + try { const a = mk(i); } finally { const k = mk(i + 1); return k.v; } +} +function breakFromFinally(n: number): number { + let s: number = 0; + for (let i = 0; i < n; i++) { + try { s += i; } finally { const k = mk(i); s += k.v; if (i == 3) break; } + } + return s; +} +function nestedTryInCatch(i: number): number { + try { throw 1; } catch (e) { + const k = mk(i); + try { const j = mk(i + 1); throw 3; } catch (e3) { const m = mk(i + 2); return k.v + m.v; } + } +} +function rethrow(i: number): number { + try { + try { throw 1; } catch (e) { const k = mk(i); throw e; } + } catch (e4) { return 9; } +} +function finallyAfterThrow(i: number): number { + let r: number = 0; + try { + try { throw 5; } finally { const k = mk(i); r = k.v; } + } catch (e5) { r += 100; } + return r; +} +function main() { + for (let i = 0; i < 300; i++) { + assert(throwOutOfCatch(i) == 7, "throwOutOfCatch"); + assert(returnFromFinally(i) == i + 1, "returnFromFinally"); + assert(breakFromFinally(10) == 12, "breakFromFinally"); + assert(nestedTryInCatch(i) == 2 * i + 2, "nestedTryInCatch"); + assert(rethrow(i) == 9, "rethrow"); + assert(finallyAfterThrow(i) == i + 100, "finallyAfterThrow"); + } + print("done."); +} From f8bf27bde66d6c44ef5e5ae4db4047a34f362f5b Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 16 Nov 2026 15:52:42 +0000 Subject: [PATCH 4/5] rc/own: a clause holding a try of its own keeps its locals unowned A clause synthesizes no cleanup TryOp, so a nested try's unwind leg - an exception out of its finally on the way back from a `return` - leaves a clause's owned local unreleased; the ownership verifier reported it on 00try_return_through_finally (`catch { let b = ...; try { return b; } finally { ... } } finally { ... }`). While such a clause is generated (clausesWithNestedTry, a counter on the generator: the hoisting decision runs from the function's context) its locals own nothing, as every clause local did before. own_err_borrow_catch_local and own_err_borrow_finally_local now report the clause's local where it is given what another only borrows, as the same local outside a clause is reported. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 29 ++++++++++++++++++- tslang/lib/TypeScript/MLIRGenStatements.cpp | 6 ++++ tslang/test/tester/CMakeLists.txt | 4 +-- .../tester/own/own_err_borrow_catch_local.ts | 4 +-- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index a34d891e7..280411127 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -682,6 +682,32 @@ class MLIRGenImpl mlir::LogicalResult mlirGenBlockWithUnwindCleanup(ts::Block blockAST, const GenContext &genContext, int skipStatements = 0); + // While a catch or finally clause whose block holds a `try` of its own is generated: its locals + // own nothing (localTakesOwnership). A clause synthesizes no cleanup TryOp, so the nested try's + // unwind leg (an exception out of its finally on the way back from a `return`) would leave one + // unreleased. A counter on the generator, not the context: the hoisting decision asks it from + // the function's context. + int clausesWithNestedTry = 0; + + // Does a block hold a `try` statement, at any depth but not in a nested function? + bool blockHasTry(ts::Block blockAST) + { + auto found = false; + ts::FilterVisitorSkipFuncsAST visitor(SyntaxKind::TryStatement, [&](TryStatement) { found = true; }); + for (auto statement : blockAST->statements) + { + // the visitor looks below the node it starts from + if (found || statement == SyntaxKind::TryStatement) + { + return true; + } + + visitor.visit(statement); + } + + return found; + } + // Whether the insertion point sits inside the catches or finally region of an enclosing // TryOp. // @@ -2388,7 +2414,8 @@ class MLIRGenImpl return genContext.ownedVars != nullptr && !variableDeclarationInfo.isGlobal && !variableDeclarationInfo.deleted && !variableDeclarationInfo.allocateInContextThis && variableDeclarationInfo.initial && variableDeclarationInfo.type && - mth.ownsHeapMemory(location, variableDeclarationInfo.type); + mth.ownsHeapMemory(location, variableDeclarationInfo.type) && + !(clausesWithNestedTry > 0 && blockIsInsideCatchOrFinally()); } // `let s = 0`: no type given, and an integer literal to infer one from - integer literals are diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index 27a940cea..36f994d29 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -1868,7 +1868,10 @@ namespace mlirgen catchGenContext.finallyGuardScope = &catchGenContext; } + auto nestedTry = blockHasTry(tryStatementAST->catchClause->block) ? 1 : 0; + clausesWithNestedTry += nestedTry; auto result = mlirGen(tryStatementAST->catchClause->block, catchGenContext); + clausesWithNestedTry -= nestedTry; EXIT_IF_FAILED(result) } @@ -1879,7 +1882,10 @@ namespace mlirgen builder.setInsertionPointToStart(&tryOp.getFinally().front()); if (tryStatementAST->finallyBlock) { + auto nestedTry = blockHasTry(tryStatementAST->finallyBlock) ? 1 : 0; + clausesWithNestedTry += nestedTry; auto result = mlirGen(tryStatementAST->finallyBlock, tryGenContext); + clausesWithNestedTry -= nestedTry; EXIT_IF_FAILED(result) // The copy of the finally that runs on the unwind leg resumes the exception past the diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index a7de4c2e4..8c5350468 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5233,8 +5233,8 @@ set(own_error_cases "own_err_borrow_and_move|:8:5: error: '[^']*' is moved here while '[^']*' borrows it" "own_err_borrow_bound_method|:20:17: error: '[^']*' borrows '[^']*' but is used here after '[^']*' is released or overwritten" "own_err_const_bound_method_reassigned|:20:11: error: '[^']*' takes a second reference" - "own_err_borrow_catch_local|:31:20: error: '[^']*' borrows '[^']*' but is used here after '[^']*' is released or overwritten" - "own_err_borrow_finally_local|:30:20: error: '[^']*' borrows '[^']*' but is used here after '[^']*' is released or overwritten" + "own_err_borrow_catch_local|:28:13: error: '[^']*' borrows '[^']*' and cannot be stored, returned or captured" + "own_err_borrow_finally_local|:27:13: error: '[^']*' borrows '[^']*' and cannot be stored, returned or captured" "own_err_borrow_derived_ref|:28:5: error: '[^']*' borrows '[^']*' but is used here after '[^']*' is released or overwritten" "own_err_const_alias_move|:12:5: error: '[^']*' borrows '[^']*' and cannot be assigned" "own_err_const_alias_field|:15:5: error: '[^']*' is moved here while '[^']*' borrows it" diff --git a/tslang/test/tester/own/own_err_borrow_catch_local.ts b/tslang/test/tester/own/own_err_borrow_catch_local.ts index 2e0e00db5..17ebcf41a 100644 --- a/tslang/test/tester/own/own_err_borrow_catch_local.ts +++ b/tslang/test/tester/own/own_err_borrow_catch_local.ts @@ -1,5 +1,5 @@ -// -mm=own, phase 2 rejects: a catch clause's locals own nothing, so `x` is one more name for what -// `b` borrows - and it is read after `a` is overwritten. (`a` is given to two `let`s, so neither +// -mm=own, phase 2 rejects: a catch clause's local owns what it holds, as any local does, so `x` is +// given what `b` only borrows - refused where it is declared. (`a` is given to two `let`s, so neither // takes its value over: each borrows it. One `let` would take it over, spec 29.) class C { v: number[] = []; From 72e35cdf7c2407d997c4d84d9e24057f0e51a245 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 16 Nov 2026 16:06:32 +0000 Subject: [PATCH 5/5] docs: spec 87 (three fuzzer finds) Co-Authored-By: Claude Opus 5.5 --- .../2026-09-24-own-memory-model-design.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index 95fc9cc96..fe8a6638e 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -6012,3 +6012,35 @@ constant data (`isConstantData`), and a global initializer's retain of constant Tests: `own_merge_moves_local`, `own_merge_of_constants`, `own_optional_literal_result`, `own_global_merge_initializer` (own, rc, gc, none). Corpus: 717 -> 724 of 890 compile under own. + +## 87. Three fuzzer finds (seeds 920048, 920187, 920443), 2026-11-16 + +The differential fuzzer's seed range 920000 found three crashes older than spec 86 (the same IR with +and without it). + +**A closure's box dies with the closure.** `const n = new N(); { let c = () => n.v.get(); } return +n.v.x;`: the box owns its copies (phase 4), and a read of `n` after the move counted as one through its +new owner (spec 29, `readsThroughOwner`), whose ends were the box variable's - none. The box goes where +the closure value is given back (the `let`'s release at the block's end), so the reads are now checked +against the closure's ends too (`followClosure`); a closure that may go anywhere else is no owner to +read through. "used after its value was moved", as when the closure is called. + +**Edge releases stay within the value's life.** `for (...) { const n = mk(); if (stop) break; if (c) +keep(h, n); }`: the release on the ways that do not move `n` (TSLANG 81, `valueUnmovedEdges`) went on +every edge into the blocks the move reaches, which ran through the loop's latch to its header and exit, +where `n` does not live; the break's edge into the exit got a release on top of its own. The blocks are +now those `n`'s definition dominates. + +**Locals of catch and finally clauses own what they hold** (rc and own). MLIRGen left them unowned +while a call in an exception funclet was fragile (since fixed); under rc such a local read freed memory +once its source was given a new value (`finally { const k = G; G = mk(5); k.items[0] }`). They are owned +now, released where the clause ends, on a return or a break. A clause synthesizes no cleanup TryOp, so +one an exception leaves the clause with leaks; and a clause that holds a `try` of its own keeps its +locals unowned (`clausesWithNestedTry`), since that try's unwind leg (an exception out of its finally on +the way back from a `return`) would leave one unreleased - the ownership verifier sees that leg. Under +own a clause local is decided as any local: `own_err_borrow_catch_local` / `_finally_local` now report +it where it is given what another only borrows. + +Tests: `own_err_capture_block_closure`, `own_moved_some_paths_break`, `00catch_finally_locals_own` (rc, +gc, none; refused under own), `00catch_finally_owned_locals_eh` (every model). Fuzz seeds 920000 and +921000 x500: no failure.