Skip to content

gh-117139: Fix a few wrong steals in bytecodes.c #121127

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

Merged
merged 1 commit into from
Jun 28, 2024
Merged
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
16 changes: 8 additions & 8 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ dummy_func(
}

replicate(8) pure inst(LOAD_FAST, (-- value)) {
assert(PyStackRef_AsPyObjectBorrow(GETLOCAL(oparg)) != NULL);
assert(!PyStackRef_IsNull(GETLOCAL(oparg)));
value = PyStackRef_DUP(GETLOCAL(oparg));
}

Expand Down Expand Up @@ -673,7 +673,7 @@ dummy_func(
err = 1;
}
else {
err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectSteal(v));
err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectBorrow(v));
Copy link
Member Author

Choose a reason for hiding this comment

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

PyObject_SetItem and the entire family actually takes borrowed references to the container and item. So this steal is wrong, it should be borrow.

This was caught when the PyStackRef_CLOSE(v) below tried to close an already invalid stackref (it was made invalid by the steal).

Py_DECREF(slice);
}
PyStackRef_CLOSE(v);
Expand Down Expand Up @@ -789,7 +789,7 @@ dummy_func(

inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
int err = PySet_Add(PyStackRef_AsPyObjectBorrow(set),
PyStackRef_AsPyObjectSteal(v));
PyStackRef_AsPyObjectBorrow(v));
Copy link
Member Author

Choose a reason for hiding this comment

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

Same for PySet_Add

DECREF_INPUTS();
ERROR_IF(err, error);
}
Expand All @@ -813,7 +813,7 @@ dummy_func(

op(_STORE_SUBSCR, (v, container, sub -- )) {
/* container[sub] = v */
int err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), PyStackRef_AsPyObjectSteal(sub), PyStackRef_AsPyObjectSteal(v));
int err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), PyStackRef_AsPyObjectBorrow(sub), PyStackRef_AsPyObjectBorrow(v));
DECREF_INPUTS();
ERROR_IF(err, error);
}
Expand Down Expand Up @@ -1235,7 +1235,7 @@ dummy_func(
inst(POP_EXCEPT, (exc_value -- )) {
_PyErr_StackItem *exc_info = tstate->exc_info;
Py_XSETREF(exc_info->exc_value,
PyStackRef_AsPyObjectBorrow(exc_value) == Py_None
PyStackRef_Is(exc_value, PyStackRef_None)
? NULL : PyStackRef_AsPyObjectSteal(exc_value));
}

Expand Down Expand Up @@ -1330,9 +1330,9 @@ dummy_func(
ERROR_IF(true, error);
}
if (PyDict_CheckExact(ns))
err = PyDict_SetItem(ns, name, PyStackRef_AsPyObjectSteal(v));
err = PyDict_SetItem(ns, name, PyStackRef_AsPyObjectBorrow(v));
else
err = PyObject_SetItem(ns, name, PyStackRef_AsPyObjectSteal(v));
err = PyObject_SetItem(ns, name, PyStackRef_AsPyObjectBorrow(v));
DECREF_INPUTS();
ERROR_IF(err, error);
}
Expand Down Expand Up @@ -1450,7 +1450,7 @@ dummy_func(
op(_STORE_ATTR, (v, owner --)) {
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
int err = PyObject_SetAttr(PyStackRef_AsPyObjectBorrow(owner),
name, PyStackRef_AsPyObjectSteal(v));
name, PyStackRef_AsPyObjectBorrow(v));
Copy link
Member Author

@Fidget-Spinner Fidget-Spinner Jun 28, 2024

Choose a reason for hiding this comment

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

PyObject_SetAttr actually takes borrowed refs too, so this shouldn't be stolen. This was caught when the DECREF_INPUTS tried to close an already stolen stackref.

DECREF_INPUTS();
ERROR_IF(err, error);
}
Expand Down
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
goto kw_fail;
}

if (PyDict_SetItem(kwdict, keyword, PyStackRef_AsPyObjectSteal(value_stackref)) == -1) {
if (PyDict_SetItem(kwdict, keyword, PyStackRef_AsPyObjectBorrow(value_stackref)) == -1) {
goto kw_fail;
}
PyStackRef_CLOSE(value_stackref);
Expand Down
32 changes: 16 additions & 16 deletions Python/executor_cases.c.h

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

16 changes: 8 additions & 8 deletions Python/generated_cases.c.h

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

Loading