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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
34 changes: 31 additions & 3 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TryStatement> 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.
//
Expand All @@ -694,8 +720,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();
Expand Down Expand Up @@ -2387,7 +2414,8 @@ 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) &&
!(clausesWithNestedTry > 0 && blockIsInsideCatchOrFinally());
}

// `let s = 0`: no type given, and an integer literal to infer one from - integer literals are
Expand Down
6 changes: 6 additions & 0 deletions tslang/lib/TypeScript/MLIRGenStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions tslang/lib/TypeScript/MLIRGenVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::Block *, 16> after;
llvm::SmallVector<mlir::Block *> 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;
}
Expand Down
28 changes: 28 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferenceReadsAfterMove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::Operation *> closureUses;
llvm::SmallVector<mlir::Operation *> 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))
Expand Down
20 changes: 16 additions & 4 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4663,7 +4672,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.
Expand Down Expand Up @@ -4704,7 +4714,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()
Expand Down Expand Up @@ -5222,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"
Expand Down Expand Up @@ -5557,6 +5568,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"
Expand Down
4 changes: 2 additions & 2 deletions tslang/test/tester/own/own_err_borrow_catch_local.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];
Expand Down
7 changes: 7 additions & 0 deletions tslang/test/tester/own/own_err_capture_block_closure.ts
Original file line number Diff line number Diff line change
@@ -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()); }
47 changes: 47 additions & 0 deletions tslang/test/tester/own/own_moved_some_paths_break.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
Loading
Loading