Skip to content
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

bpo-42015: Reorder dereferencing calls in meth_dealloc, to make sure m_self is kept alive long enough #22670

Merged

Conversation

YannickJadoul
Copy link
Contributor

@YannickJadoul YannickJadoul commented Oct 12, 2020

In Python 3.9, the line Py_XDECREF(PyCFunction_GET_CLASS(m)); was added to meth_dealloc (in methodobject.c). Unfortunately for pybind11, it's inserted exactly two lines too low, since it accesses the PyMethodDef and we store the PyMethodDef instance in the capsule that's used as self-argument of the PyCFunction.

Result: UB, since Py_XDECREF(m->m_self); brings down the refcount of the capsule to 0 and (indirectly) frees the PyMethodDef, while its contents are now still accessed.

From the pybind11 perspective, it would be optimal if this could be fixed in CPython itself, by moving up this one Py_XDECREF 2 lines. This would a) be more efficient than creating a workaround, and b) allow old, existing versions of pybind11 to work with Python 3.9 (well, 3.9.1, then, hopefully); the user base of pybind11 has grown quite a bit and now includes giants like scipy or some Google libraries.
This PR reorders those lines.

If there's a different, recommended way of creating these PyCFunctionObjects dynamically and cleaning up the PyMethodDef, we'd be interested as well, to make sure these kinds of breakages are avoided in the future.

Apologies for only figuring out now how to debug this, using valgrind. Up until yesterday, we only saw some failures in CI on macOS, but it was hard to reproduce and debug locally.

https://bugs.python.org/issue42015

@serhiy-storchaka
Copy link
Member

And since this PR fixes a bug exposed in user code, add please a NEWS entry.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Oct 12, 2020
@YannickJadoul
Copy link
Contributor Author

And since this PR fixes a bug exposed in user code, add please a NEWS entry.

Not sure whether that mostly belongs in "C API" or "Core and Builtins", but I went with "C API" because I expected users are mostly affected when using the C API. If necessary, I can still move it.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you! Minor nitpick about the NEWS entry.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Great!

@YannickJadoul
Copy link
Contributor Author

Thanks for the amazingly quick review, @serhiy-storchaka! Amazing to submit a PR like this :-)

@serhiy-storchaka serhiy-storchaka merged commit 04b8631 into python:master Oct 12, 2020
@miss-islington
Copy link
Contributor

Thanks @YannickJadoul for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 12, 2020
…m_self is kept alive long enough (pythonGH-22670)

(cherry picked from commit 04b8631)

Co-authored-by: Yannick Jadoul <[email protected]>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Oct 12, 2020
@bedevere-bot
Copy link

GH-22674 is a backport of this pull request to the 3.9 branch.

@serhiy-storchaka
Copy link
Member

I will be glad to work with you!

@danielleoking7474
Copy link

YannickJadoul:reorder-meth_dealloc-decref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants