Key each foreach step to its activation so generator instances don't … - #382
Merged
Conversation
…clash foreach iteration state lives in ph7_foreach_info.aStep, a per-STATEMENT stack shared by every activation, and OP_FOREACH_STEP peeked the last entry. Two suspended activations of the same textual foreach — two generator instances, two fibers, or a recursive call paused in the same loop — resume out of LIFO order and grab each other's step, so $g1 = g([1,2,3]); $g2 = g([10,20]) with interleaved next()/current() printed 11020 where php prints 110220 (a silent wrong answer). Record the owning activation frame on each step (ph7_foreach_step.pFrame, normalized past exception frames via VmSkipExceptionFrames, stamped at OP_FOREACH_INIT). OP_FOREACH_STEP now scans aStep top-down for the step whose pFrame matches the running activation — the most-recent push wins, so a leaked step that shares a recycled frame address never shadows the live one. The step teardown paths remove by pointer (new VmForeachStepUnlink) instead of popping the tail, order-preserving so a live step is never reordered below a leaked sibling. This mirrors the per-exec-context cursor the yield-from opcode already keeps for delegation. Byte-verified vs php 8.5.7 across two-instance, recursive, two-fiber, nested-in-generator, and break-leak-interleave scenarios; cross-engine test foreach_same_generator_two_instances; test-compat green; docker ASan+UBSan clean; full and tiny builds warning-free.
Coverage
Details: https://github.com/alganet/PHL/actions/runs/30112170816 |
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.
…clash
foreach iteration state lives in ph7_foreach_info.aStep, a per-STATEMENT stack shared by every activation, and OP_FOREACH_STEP peeked the last entry. Two suspended activations of the same textual foreach — two generator instances, two fibers, or a recursive call paused in the same loop — resume out of LIFO order and grab each other's step, so $g1 = g([1,2,3]); $g2 = g([10,20]) with interleaved next()/current() printed 11020 where php prints 110220 (a silent wrong answer).
Record the owning activation frame on each step (ph7_foreach_step.pFrame, normalized past exception frames via VmSkipExceptionFrames, stamped at OP_FOREACH_INIT). OP_FOREACH_STEP now scans aStep top-down for the step whose pFrame matches the running activation — the most-recent push wins, so a leaked step that shares a recycled frame address never shadows the live one. The step teardown paths remove by pointer (new VmForeachStepUnlink) instead of popping the tail, order-preserving so a live step is never reordered below a leaked sibling. This mirrors the per-exec-context cursor the yield-from opcode already keeps for delegation.
Byte-verified vs php 8.5.7 across two-instance, recursive, two-fiber, nested-in-generator, and break-leak-interleave scenarios; cross-engine test foreach_same_generator_two_instances; test-compat green; docker ASan+UBSan clean; full and tiny builds warning-free.