Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
39 changes: 39 additions & 0 deletions Lib/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading