Skip to content

Unsafe DECREFs of borrowed references in the interpreter. #125323

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

Closed
markshannon opened this issue Oct 11, 2024 · 1 comment
Closed

Unsafe DECREFs of borrowed references in the interpreter. #125323

markshannon opened this issue Oct 11, 2024 · 1 comment
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@markshannon
Copy link
Member

markshannon commented Oct 11, 2024

Bug report

Bug description:

It is unsafe to borrow a PyObject * reference from a _PyStackRef and Py_DECREF the PyObject * reference and not close the _PyStackRef. This is quite a common pattern in bytecodes.c, and prevents any optimizations based on reference lifetimes as the inferred lifetime is incorrect.

The fix for this is to change the incorrect pattern:

PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
use(left_o);
Py_DECREF(left_o);

to the correct pattern:

PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
use(left_o);
PyStackRef_CLOSE(left);

This is causing problems, as optimizations to exploit stack refs don't work: https://github.com/python/cpython/compare/main...faster-cpython:cpython:use-stackrefs-opt-experiment?expand=1

CPython versions tested on:

CPython main branch

Operating systems tested on:

Other

Linked PRs

@markshannon markshannon added the type-bug An unexpected behavior, bug, or error label Oct 11, 2024
@Fidget-Spinner
Copy link
Member

Main culprits are Py_DECREF_SPECIALIZED. But removing that will get a slight perf hit. Should I do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants