Skip to content

[mypyc] Fix crashes from unchecked throw() arguments, bare raise and unbound async calls - #22417

Merged
JukkaL merged 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-throw-raise-validation
Nov 10, 2026
Merged

JukkaL merged 3 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-throw-raise-validation

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

This fixes three crashes in mypyc's runtime, each caused by an argument or piece of interpreter state that wasn't validated.

1. generator.throw() with invalid arguments

def g() -> Iterator[int]:
    yield 1

it = g(); next(it)
it.throw(5)                     # segfault (3.10-3.14t); throw(object()) aborts; throw(int) segfaults on 3.10
it.throw(ValueError("x"), 5)    # TypeError raised *inside* the generator
it.throw(ValueError, None, 42)  # same

The generated throw now calls CPyGen_CheckThrowArgs before resuming the generator. It makes the same checks, with the same messages, as CPython's _gen_throw. The first argument must be an exception class, or an instance with no separate value. The traceback must be a traceback object or None.

2. Bare raise with no active exception

def f() -> None:
    raise

This segfaulted on 3.10, corrupted interpreter state on 3.11 and raised TypeError: 'NoneType' object is not callable on 3.12+. CPy_Reraise now raises RuntimeError: No active exception to reraise, as CPython does.

3. Calling an async method or classmethod without self

class C:
    async def m(self) -> int: ...

C.m()                       # segfault on 3.10/3.14
C.__dict__["m"].__call__()  # segfault on all versions

CPyFunction_Vectorcall read args[0] without checking nargs. It now raises TypeError: unbound method C.m() needs an argument. tp_call goes through the same handler, so it is covered too.

Regression tests are added to run-generators.test, run-exceptions.test and run-async.test.

The generated throw() method passed its arguments to the generator body
without validation, so throwing a non-exception (such as gen.throw(5))
segfaulted, and other invalid argument combinations raised a TypeError
inside the generator, where an except clause could catch it.

Check the arguments like CPython's _gen_throw does before resuming the
generator: the first argument must be an exception class or instance, an
exception instance may not have a separate value, and the traceback must
be a traceback object or None.
CPy_Reraise passed the handled exception to PyErr_Restore even when
there was none. This segfaulted on Python 3.10, left the error state
corrupted on 3.11 and raised "TypeError: 'NoneType' object is not
callable" on 3.12+.

Raise "RuntimeError: No active exception to reraise" instead, like
CPython.
Async methods and classmethods of native classes are wrapped in a
CPyFunction object whose vectorcall handler takes self from the first
positional argument without checking that there is one. Calling the
function with no arguments, such as C.m() or C.__dict__['m'](), read
past the end of the argument array and usually segfaulted.

Raise a TypeError instead. tp_call also goes through the vectorcall
handler, so this covers both paths.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, these fixes are good!

@JukkaL
JukkaL merged commit b53c712 into python:master Nov 10, 2026
18 checks passed
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-throw-raise-validation branch November 14, 2026 22:05
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.

2 participants