Skip to content

Generators: a resumed try owns its landing pad, in the frame it is running in - #9

Merged
blacktrs merged 2 commits into
mainfrom
generator-throw
Jul 29, 2026
Merged

blacktrs merged 2 commits into
mainfrom
generator-throw

Conversation

@blacktrs

@blacktrs blacktrs commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

A resumed generator ran its try with a jmp_buf captured in a stack frame that no longer existed, and with the consumer's jump depth. Two bugs fell out of that, and the loud one was not the important one.

Found by running the Linux suite on both arches: generators_exceptions was the only amd64 failure, and it reproduced at 273e23e, so it predates everything currently in flight.

1. The exception went to the wrong catch — on every host

function counter() {
    $i = 0;
    while (true) {
        try { $x = yield $i; $i = $i + 1; }
        catch (Exception $e) { echo 'inner caught: ', $e->getMessage(), "\n"; $i = 100; }
    }
}
$gen = counter();
echo $gen->current(), "\n";
try { echo $gen->throw(new Exception('injected')), "\n"; }
catch (Exception $c) { echo "outer\n"; }

php prints 0 / inner caught: injected / 100. We printed 0 / outer: $gen->throw() raised at @__mir_jmp_depth - 1 — the innermost try of whoever called throw(), not the generator's own. Plain PHP, so php is the oracle and difftest gates it; no case had ever wrapped ->throw() in a try, which is why nothing said so.

2. With no consumer try, x86_64 crashed

The slot then happened to be the generator's own — but that jmp_buf was captured by a setjmp in the invocation that entered the try, and that stack frame died when the yield returned. arm64 tolerated the resurrected frame; x86_64 turned it into a SIGSEGV as soon as the catch body allocated dynamically, which is what a virtual dispatch does (emitVirtualDispatch emits %res = alloca i64).

That is the whole story of the amd64-only failure: declaring any class X extends Exception makes $e->getMessage() virtual, and the virtual dispatch brought the alloca. Nothing to do with exceptions — a virtual call on an unrelated class in the same catch crashed identically, while get_class($e) and a plain function call did not.

catch body (amd64)
$e->getMessage(), a user Exception subclass present SIGSEGV
$e->getMessage(), no subclass — direct call, no alloca ok
get_class($e) ok
a plain function call ok
a virtual call on an unrelated class SIGSEGV
a plain throw after a yield (not injected) ok

The fix

A resume point re-arms every enclosing try in the current frame before anything can throw, driven by a push/pop stack around the try body.

Slots come from the current top of the jump stack, not from the depth the try was first entered at: that index belongs to an invocation that has since returned, and by now it may be a live try of the consumer — or of another generator. The first version of this fix reused the old index, and an exception promptly landed in a different generator's catch. The new pre-try depth is written back into the frame slot so the catch and the normal exit restore what was borrowed.

Every suspend and the final return put the consumer's depth back; otherwise the slot armed on resume outlives the suspension and catches something meant for someone else.

tests/aot/cases/generator_throw_scopes.php pins six shapes against php: consumer inside a try, consumer outside one, a generator with no catch of its own (the consumer should get it then), a throw raised by the body after a resume, try/finally around the yield, and nested trys where the inner catch must win.

Gates

Re-run on the merge with main, so these are the numbers for what would land:

Linux (run_tests.sh --gate, arm64): cold seed · 728/728 · difftest 672 MATCH / 0 DIFF · fixpoint IR byte-identical · self-host 728/728 · MIR golden · stability 2×2 — PASS.
macOS: fixpoint IR byte-identical · self-host 728/728 · MIR golden · stability 5×2 — PASS.

Generators pass 10/10 on macOS, Linux arm64 and Linux amd64 — including the case that used to SIGSEGV.

Based on current main

Merged after #7 landed. The merge needed a cold bin/build --seed, because main brought codegen builtins the installed binary did not know — the usual rule when a new intrinsic crosses a merge.

blacktrs added 2 commits July 29, 2026 10:06
…nning in

`$gen->throw()` raised at `@__mir_jmp_depth - 1` — the innermost try of whoever
CALLED throw(), not the generator's own. Wrap the call in a try and the
consumer's catch stole an exception php delivers to the generator; that is wrong
on every host, and no case wrapped ->throw() in a try, so nothing said so.

With no consumer try it was worse in a quieter way. The slot happened to be the
generator's own — but that jmp_buf was captured by a `setjmp` in the invocation
that ENTERED the try, and that stack frame died when the yield returned. arm64
tolerated the resurrected frame; x86_64 turned it into a SIGSEGV as soon as the
catch body allocated dynamically, which is what a virtual dispatch does. That is
the whole story of `generators_exceptions` failing only on amd64: declaring any
`class X extends Exception` makes `$e->getMessage()` virtual, and the virtual
dispatch brought the alloca. Nothing to do with exceptions — a virtual call on
an unrelated class in the same catch crashed identically.

So a resume point re-arms every enclosing try in the CURRENT frame before
anything can throw, taking its slots from the current top of the jmp stack
rather than the depth the try was first entered at — that index belongs to an
invocation that has since returned, and may now be a live try of the consumer or
of another generator. (It was: the first version of this fix reused the old
index and an exception landed in a different generator's catch.) Every suspend
and the final return put the consumer's depth back, or the slot we armed
outlives the suspension and catches something meant for someone else.

⚠ The re-arm list is three parallel arrays, not a list of tuples: a
heterogeneous `[string, int, string]` literal unions to an erased element type
natively, and the compiler segfaulted on the first program with a yield in a try.
@blacktrs
blacktrs merged commit 7f89607 into main Jul 29, 2026
@blacktrs
blacktrs deleted the generator-throw branch August 19, 2026 17:22
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