Repository navigation
An ephemeral value a generator yields from its own frame cannot leave the loop body - #3608
Merged
Merged
Conversation
… the loop body: the loop binding is its origin
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 soundness hole, found while building D216 (#2425).
The hole. Safe code could carry a view of a generator's frame out of the loop that runs the generator:
This compiled and ran. So did assigning
eto a variable outside the loop and pushing it into a list read after the loop. With a plain&Kelement the same programs were already refused ("returned view may outlive its origin 'e'"); only an ephemeral value (a struct or a sum holding the view) got through.Root cause.
check_forgives an ephemeral loop binding its origins from the iterated expression (record_for_binding_view_origins). For a generator call that is the call's arguments, or nothing: the generator's own frame is not among them, so the binding depended on nothing that dies.The fix. D69 (§13.4) already says it: a yielded view, "of the generator's own locals included, is an argument borrowed for that call, never retained past it". When the generator yields a view of its own frame (the fact
note_generator_local_view_yieldrecords for the pull rule; the generator's body is checked first if it has not been), the loop binding is itself an origin: a local of the body.What stays accepted. Using the value in the body, and returning or keeping a yielded value that views only what the caller passed in (
gen fn items(xs: &List[K]) -> VyieldingV { k: x }).This changes what safe code may do, by closing a hole: a program that kept such a value compiled before and held a dangling view. Flagged for your veto as a checker change.
Tests:
err_gen_yielded_ephemeral_returned,_kept_in_list,_assigned_outward,_param_and_frame(consumer declared before the generator; a parameter and the frame both yielded), andbehav_gen_yielded_ephemeral_in_body. The four fail on main.Evidence at 3ca31f6: both compilers check the source; release build;
:unit-return-review,:ceremony-census,:self-audit,:native-compile-error-tests,:behavior-testsgreen; battery green;:release-checkssurvey: all targets green (the corpora compile under the stricter rule).