-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SIGSEGV with generators after direct change in gi_frame #125038
Comments
Backtrace on MacOS:
The crash is in the FOR_ITER instruction; it blindly accesses The repro case is a bit dubious as it's mutating f_locals, but probably we should indeed support this. The fix would be as simple as inserting a NULL check. |
SIGSEGV on generators in case of gi_frame.f_locals is fixed. This applies to _FOR_ITER bytecode. Similar checks are added to _FOR_ITER_TIER_TWO and INSTRUMENTED_FOR_ITER bytecode implementations.
Thank you for such a concrete comment! |
SIGSEGV on generators in case of gi_frame.f_locals is fixed. This applies to _FOR_ITER bytecode. Similar checks are added to _FOR_ITER_TIER_TWO and INSTRUMENTED_FOR_ITER bytecode implementations.
…is fixed Some iterator checks are added for _FOR_ITER, _FOR_ITER_TIER_TWO and INSTRUMENTED_FOR_ITER bytecode implementations. TypeError is raised in case of tp_iternext == NULL. Tests on generator modifying through gi_frame.f_locals are added, both to genexpr generators and function generators.
I think there are two reasonable approaches to fixing this:
Given that setting an internal variable like |
I can segfault 3.12 and earlier with a variant of this. g = (x for x in range(10))
gen_expr_func = types.FunctionType(g.gi_code, {})
list(gen_expr_func(range(20)))
print("No segfault") The root cause is the same: the generator expression does not check that the value passed to it is an iterator. |
So it looks like (1) is the correct fix. See #125178 (comment) for a way to do this. |
IMHO, additional GET_ITER before FOR_ITER for genexpr is a best way to prevent all such crashes. |
New tests are moved back to test_generators.py. Tests on generator creation via FunctionType from gi_code are added.
…ipulations (pythonGH-125178) (cherry picked from commit 079875e) Co-authored-by: Mikhail Efimov <[email protected]>
The initial fix is merged into main, though we'll want to eventually implement a different fix. I thought it was worth investigating whether a similar crash was possible with async genexps. However, it seems that the GET_ANEXT impression is implemented more robustly and I couldn't find a crash by manipulating the locals of an asyncgen frame. |
…ions (GH-125178) (#125846) (cherry picked from commit 079875e) Co-authored-by: Mikhail Efimov <[email protected]>
Yes, it seems so. Maybe, we can remove On the other side, TypeErrors in |
…nexpr code Adds CHECK_ITERABLE instruction. It checks that TOS is iterable or async iterable. Redundant GET_ITER and GET_AITER instructions are removed from generator expression code.
…nexpr code Adds CHECK_ITERABLE instruction. It checks that TOS is iterable or async iterable. Redundant GET_ITER and GET_AITER instructions are removed from generator expression code.
See #125723 for another generator |
Crash report
What happened?
There is a segmentation fault with simple code snippet:
There is a SIGSEGV on my linux machine (Debian GNU/Linux 10) with both main branch Python and 3.13 version.
Message "No segfault" is printed on Python 3.7.3 (default, Oct 31 2022, 14:04:00).
CPython versions tested on:
3.13, CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.14.0a0 (heads/peg_parser_remove_redundant_functions:e1b62e5cf79, Oct 3 2024, 14:38:46) [GCC 8.3.0]
Linked PRs
The text was updated successfully, but these errors were encountered: