Skip to content

own/rc: three fuzzer finds - closure box lifetime, a break's double release, catch/finally locals - #874

Open
ASDAlexander77 wants to merge 5 commits into
mainfrom
own-fuzz-920
Open

ASDAlexander77 wants to merge 5 commits into
mainfrom
own-fuzz-920

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes the three crashes the differential fuzzer found in seed range 920000 (reported in #873). All three were older than #873: the IR was identical with and without it.

1. A value captured by a closure released early (own, seed 920187)

const n = new N(new V(1));
{ let c = () => n.v.get(); }   // c never called
return n.v.x;                  // read n after the block freed it
  • Cause: the closure's box owns its copies (phase 4). A read of n after the move counted as a read through its new owner (spec 29, readsThroughOwner), whose lifetime ends were those of the box variable, and it has none. But the box dies with the closure value, which the let released at the end of the block.
  • Fix: the reads are now checked against the closure's ends too (followClosure), and a closure that may go anywhere else is not treated as an owner.
  • Result: "'n' is used here after its value was moved", as already reported when the closure is called. Test: own_err_capture_block_closure.

2. A value moved on some paths of a loop body, released twice on break (own, seed 920048)

for (...) { const n = mk(i); if (stop) break; if (c) keep(h, n); }
  • Cause: the release on the paths that don't move n (TSLANG 81, valueUnmovedEdges) was placed on every edge into the blocks reachable after the move. From the move, those include the loop's latch, header and exit, where n does not live. So the break edge into the exit got a release on top of the break's own release.
  • Fix: only blocks that n's definition dominates are counted.
  • Result: the break releases n once. Test: own_moved_some_paths_break. Before the fix it crashed on Windows and under Linux's poisoning allocator.

3. Locals of catch and finally clauses own what they hold (rc and own, seed 920443)

finally { const k: IY = G; G = mk(5); churn(); k.items[0].x }  // rc: crash; in a catch: a wrong value
  • Cause: MLIRGen left such locals without an owner, because a call inside an exception funclet used to be fragile. The example it cited (catch (e: int) { new Res(); throw 2; }) no longer crashes.
  • Fix: these locals are now owned, and released where the clause ends, including on a return or a break out of it.
  • Limits:
    • A clause still has no cleanup of its own, so if an exception leaves the clause, the local leaks rather than being freed.
    • A clause that contains a try of its own keeps its locals unowned (clausesWithNestedTry). The ownership verifier sees that try's unwind path and would report the leak; it did, on 00try_return_through_finally.
  • Under own such a local is decided like any other local. own_err_borrow_catch_local and own_err_borrow_finally_local are still refused, but now at the declaration ("borrows ... and cannot be stored"), as the same local is outside a clause.
  • Tests:
    • 00catch_finally_locals_own: rc, gc and none; refused under own.
    • 00catch_finally_owned_locals_eh: a throw, return, break, nested try and rethrow out of clauses holding owned locals, under every model, JIT and AOT. I also checked it by hand with --di and --opt_level=0.

Validation

Check Result
Windows ctest 7644/7644
Linux full ctest (MALLOC_PERTURB_=165, tcache off) 7623/7623
Differential fuzzer, seed 920000 ×500 923 programs, 0 failures (was 3)
Differential fuzzer, seed 921000 ×500 917 programs, 0 failures
Default library under own, gc, none and rc everything passes; the only tests not run are those skipped by design

Spec section 87 records the three rules.

🤖 Generated with Claude Code

ASDAlexander77 and others added 5 commits November 16, 2026 15:52
`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 <noreply@anthropic.com>
… 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant