diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index c404fc6e67d193..136d28b1f97f94 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -266,7 +266,7 @@ extern int _PyEval_EnsureBuiltinsWithModule( PyObject *, PyObject **p_builtins); -PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func); +PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func, PyCodeObject *code); /* Handle signals, pending calls, GIL drop request and asynchronous exception */ diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 6eb25960101a9e..899af4995d5c2d 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -330,6 +330,45 @@ def generator(arg): g.gi_frame.clear() self.assertTrue(DetectDelete.deleted) + def test_code_reassigned_before_generator_created(self): + # A generator must be created from the code object of the frame being + # run, even if __code__ was reassigned after that frame was pushed. + # Here a keyword default lookup runs __eq__ during argument binding, + # and swaps in a code object with a much smaller frame. + def small(): + yield 'small' + + src = ('def big(*, x):\n' + + ''.join(f' v{i} = {i}\n' for i in range(1000)) + + ' yield x\n') + ns = {} + exec(src, ns) + big = ns['big'] + big_code = big.__code__ + + class Key(str): + __hash__ = str.__hash__ + + def __eq__(self, other): + big.__code__ = small.__code__ + return str.__eq__(self, other) + + gens = [] + for i in range(10): + big.__code__ = big_code + big.__kwdefaults__ = {Key('x'): i} + gens.append(big()) + self.assertIs(big.__code__, small.__code__) + + # Run a GC pass to touch memory and increase the chance of a crash if + # the generators trampled over memory when they copied their frames. + support.gc_collect() + + for i, gen in enumerate(gens): + self.assertIs(gen.gi_code, big_code) + self.assertEqual(list(gen), [i]) + + class ModifyUnderlyingIterableTest(unittest.TestCase): iterables = [ range(0), diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-00-20-16.gh-issue-163129.QVAnjm.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-00-20-16.gh-issue-163129.QVAnjm.rst new file mode 100644 index 00000000000000..4d0b2f3dda6100 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-00-20-16.gh-issue-163129.QVAnjm.rst @@ -0,0 +1,3 @@ +Fix a heap buffer overflow when creating a generator, coroutine or +asynchronous generator for a function whose :attr:`~function.__code__` +was reassigned after the call began. diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8bb3f857437792..f8f2dc67c1d063 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -11456,7 +11456,7 @@ PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func); + PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func, _PyFrame_GetCode(frame)); _PyFrame_StackPointerInvalidate(frame); if (gen == NULL) { JUMP_TO_LABEL(error); diff --git a/Objects/genobject.c b/Objects/genobject.c index 82a1bb1ff42068..56ceccda804cf5 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -1104,9 +1104,8 @@ PyTypeObject PyGen_Type = { }; static PyObject * -make_gen(PyTypeObject *type, PyFunctionObject *func) +make_gen(PyTypeObject *type, PyFunctionObject *func, PyCodeObject *code) { - PyCodeObject *code = (PyCodeObject *)func->func_code; int slots = _PyFrame_NumSlotsForCodeObject(code); PyGenObject *gen = PyObject_GC_NewVar(PyGenObject, type, slots); if (gen == NULL) { @@ -1127,18 +1126,23 @@ make_gen(PyTypeObject *type, PyFunctionObject *func) return (PyObject *)gen; } +/* Create a generator object for a frame that is executing + RETURN_GENERATOR. *code* must be the frame's code object. The + function's __code__ may have been reassigned since the frame was + pushed, but the generator must be sized for the frame it will hold. +*/ PyObject * -_Py_MakeCoro(PyFunctionObject *func) +_Py_MakeCoro(PyFunctionObject *func, PyCodeObject *code) { - int coro_flags = ((PyCodeObject *)func->func_code)->co_flags & + int coro_flags = code->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR); assert(coro_flags); if (coro_flags == CO_GENERATOR) { - return make_gen(&PyGen_Type, func); + return make_gen(&PyGen_Type, func, code); } if (coro_flags == CO_ASYNC_GENERATOR) { PyAsyncGenObject *ag; - ag = (PyAsyncGenObject *)make_gen(&PyAsyncGen_Type, func); + ag = (PyAsyncGenObject *)make_gen(&PyAsyncGen_Type, func, code); if (ag == NULL) { return NULL; } @@ -1150,7 +1154,7 @@ _Py_MakeCoro(PyFunctionObject *func) } assert (coro_flags == CO_COROUTINE); - PyObject *coro = make_gen(&PyCoro_Type, func); + PyObject *coro = make_gen(&PyCoro_Type, func, code); if (!coro) { return NULL; } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c6f1f61f3e08fe..dfcd3a46052859 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -5810,7 +5810,7 @@ dummy_func( inst(RETURN_GENERATOR, (-- res)) { assert(PyStackRef_FunctionCheck(frame->f_funcobj)); PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj); - PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func); + PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func, _PyFrame_GetCode(frame)); ERROR_IF(gen == NULL); assert(STACK_LEVEL() <= 2); SAVE_STACK(); diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5df8120e7424b9..3b7e0842750ffc 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -20728,7 +20728,7 @@ PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func); + PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func, _PyFrame_GetCode(frame)); _PyFrame_StackPointerInvalidate(frame); if (gen == NULL) { SET_CURRENT_CACHED_VALUES(0); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3af7863e80d2c6..66e12799f1b5c0 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -11453,7 +11453,7 @@ PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func); + PyGenObject *gen = (PyGenObject *)_Py_MakeCoro(func, _PyFrame_GetCode(frame)); _PyFrame_StackPointerInvalidate(frame); if (gen == NULL) { JUMP_TO_LABEL(error);