Repository navigation
Generators: a resumed try owns its landing pad, in the frame it is running in - #9
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A resumed generator ran its
trywith ajmp_bufcaptured 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_exceptionswas the only amd64 failure, and it reproduced at273e23e, so it predates everything currently in flight.1. The exception went to the wrong
catch— on every hostphp prints
0 / inner caught: injected / 100. We printed0 / outer:$gen->throw()raised at@__mir_jmp_depth - 1— the innermosttryof whoever calledthrow(), not the generator's own. Plain PHP, sophpis the oracle and difftest gates it; no case had ever wrapped->throw()in atry, which is why nothing said so.2. With no consumer
try, x86_64 crashedThe slot then happened to be the generator's own — but that
jmp_bufwas captured by asetjmpin 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 (emitVirtualDispatchemits%res = alloca i64).That is the whole story of the amd64-only failure: declaring any
class X extends Exceptionmakes$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, whileget_class($e)and a plain function call did not.$e->getMessage(), a userExceptionsubclass present$e->getMessage(), no subclass — direct call, no allocaget_class($e)throwafter a yield (not injected)The fix
A resume point re-arms every enclosing
tryin 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
tryof 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.phppins 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/finallyaround 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.