-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix dangling pointer in internals::registered_types_cpp_fast from #5842 #5867
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e36686c
Fix dangling pointer in internals::registered_types_cpp_fast from #5842
swolchok 9877f5b
review feedback, attempt to fix -Werror in CI
swolchok 44e1110
use const ref, skip test on python 3.13 free-threaded
swolchok e2c6b53
Skip test on 3.13t more robustly
oremanj 8764a30
style: pre-commit fixes
pre-commit-ci[bot] 532bf4e
CI fix
oremanj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/test_class_cross_module_use_after_one_module_dealloc.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "pybind11_tests.h" | ||
|
||
#include <iostream> | ||
|
||
class CrossDSOClass { | ||
public: | ||
CrossDSOClass() = default; | ||
virtual ~CrossDSOClass(); | ||
CrossDSOClass(const CrossDSOClass &) = default; | ||
}; | ||
|
||
CrossDSOClass::~CrossDSOClass() = default; | ||
|
||
struct UnrelatedClass {}; | ||
|
||
TEST_SUBMODULE(class_cross_module_use_after_one_module_dealloc, m) { | ||
m.def("register_and_instantiate_cross_dso_class", [](const py::module_ &m) { | ||
py::class_<CrossDSOClass>(m, "CrossDSOClass").def(py::init<>()); | ||
return CrossDSOClass(); | ||
}); | ||
m.def("register_unrelated_class", | ||
[](const py::module_ &m) { py::class_<UnrelatedClass>(m, "UnrelatedClass"); }); | ||
} |
67 changes: 67 additions & 0 deletions
67
tests/test_class_cross_module_use_after_one_module_dealloc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
import gc | ||
import sys | ||
import sysconfig | ||
import types | ||
import weakref | ||
|
||
import pytest | ||
|
||
import env | ||
from pybind11_tests import class_cross_module_use_after_one_module_dealloc as m | ||
|
||
is_python_3_13_free_threaded = ( | ||
env.CPYTHON | ||
and sysconfig.get_config_var("Py_GIL_DISABLED") | ||
and (3, 13) <= sys.version_info < (3, 14) | ||
) | ||
|
||
|
||
def delattr_and_ensure_destroyed(*specs): | ||
wrs = [] | ||
for mod, name in specs: | ||
wrs.append(weakref.ref(getattr(mod, name))) | ||
delattr(mod, name) | ||
|
||
for _ in range(5): | ||
gc.collect() | ||
if all(wr() is None for wr in wrs): | ||
break | ||
else: | ||
pytest.fail( | ||
f"Could not delete bindings such as {next(wr for wr in wrs if wr() is not None)!r}" | ||
) | ||
|
||
|
||
@pytest.mark.skipif("env.PYPY or env.GRAALPY or is_python_3_13_free_threaded") | ||
def test_cross_module_use_after_one_module_dealloc(): | ||
# This is a regression test for a bug that occurred during development of | ||
# internals::registered_types_cpp_fast (see #5842). registered_types_cpp_fast maps | ||
# &typeid(T) to a raw non-owning pointer to a Python type object. If two DSOs both | ||
# look up the same global type, they will create two separate entries in | ||
# registered_types_cpp_fast, which will look like: | ||
# +=========================================+ | ||
# |&typeid(T) from DSO 1|type object pointer| | ||
# |&typeid(T) from DSO 2|type object pointer| | ||
# +=========================================+ | ||
# | ||
# Then, if the type object is destroyed and we don't take extra steps to clean up | ||
# the table thoroughly, the first row of the table will be cleaned up but the second | ||
# one will contain a dangling pointer to the old type object. Further lookups from | ||
# DSO 2 will then return that dangling pointer, which will cause use-after-frees. | ||
|
||
import pybind11_cross_module_tests as cm | ||
|
||
module_scope = types.ModuleType("module_scope") | ||
instance = m.register_and_instantiate_cross_dso_class(module_scope) | ||
cm.consume_cross_dso_class(instance) | ||
|
||
del instance | ||
delattr_and_ensure_destroyed((module_scope, "CrossDSOClass")) | ||
|
||
# Make sure that CrossDSOClass gets allocated at a different address. | ||
m.register_unrelated_class(module_scope) | ||
|
||
instance = m.register_and_instantiate_cross_dso_class(module_scope) | ||
cm.consume_cross_dso_class(instance) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.