-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 1 commit
e36686c
9877f5b
44e1110
e2c6b53
8764a30
532bf4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -335,6 +335,22 @@ enum class holder_enum_t : uint8_t { | |
| custom_holder, | ||
| }; | ||
|
|
||
| // When a type appears in multiple DSOs, | ||
| // internals::registered_types_cpp_fast will have multiple distinct | ||
| // keys (the type_info from each DSO) mapped to the same | ||
| // type_info*. We need to keep track of these aliases so that we clean | ||
| // them up when our type is deallocated. A linked list is appropriate | ||
| // because this structure is expected to be 1) usually empty and 2) | ||
| // when it's not empty, usually very small. See also `struct | ||
| // nb_alias_chain` added in | ||
| // https://github.com/wjakob/nanobind/commit/b515b1f7f2f4ecc0357818e6201c94a9f4cbfdc2 | ||
| #if PYBIND11_INTERNALS_VERSION >= 12 | ||
| struct alias_chain_entry { | ||
|
||
| std::unique_ptr<alias_chain_entry> next; | ||
| const std::type_info *value; | ||
| }; | ||
| #endif | ||
|
|
||
| /// Additional type information which does not fit into the PyTypeObject. | ||
| /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`. | ||
| struct type_info { | ||
|
|
@@ -357,6 +373,11 @@ struct type_info { | |
| void *get_buffer_data = nullptr; | ||
| void *(*module_local_load)(PyObject *, const type_info *) = nullptr; | ||
| holder_enum_t holder_enum_v = holder_enum_t::undefined; | ||
|
|
||
| #if PYBIND11_INTERNALS_VERSION >= 12 | ||
| std::unique_ptr<alias_chain_entry> alias_chain; | ||
| #endif | ||
|
|
||
| /* A simple type never occurs as a (direct or indirect) parent | ||
| * of a class that makes use of multiple inheritance. | ||
| * A type can be simple even if it has non-simple ancestors as long as it has no descendants. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,13 @@ | |||||
| #include <numeric> | ||||||
| #include <utility> | ||||||
|
|
||||||
| class CrossDSOClass { | ||||||
| public: | ||||||
| virtual ~CrossDSOClass(); | ||||||
| }; | ||||||
|
|
||||||
| CrossDSOClass::~CrossDSOClass() = default; | ||||||
|
|
||||||
| PYBIND11_MODULE(pybind11_cross_module_tests, m, py::mod_gil_not_used()) { | ||||||
| m.doc() = "pybind11 cross-module test module"; | ||||||
|
|
||||||
|
|
@@ -146,4 +153,7 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m, py::mod_gil_not_used()) { | |||||
| // which appears when this header is missing. | ||||||
| m.def("missing_header_arg", [](const std::vector<float> &) {}); | ||||||
| m.def("missing_header_return", []() { return std::vector<float>(); }); | ||||||
|
|
||||||
| // test_class_cross_module_use_after_one_module_dealloc | ||||||
| m.def("consume_cross_dso_class", [](CrossDSOClass) {}); | ||||||
|
||||||
| m.def("consume_cross_dso_class", [](CrossDSOClass) {}); | |
| m.def("consume_cross_dso_class", [](const CrossDSOClass &) {}); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||
| #include "pybind11_tests.h" | ||||||
|
|
||||||
| #include <iostream> | ||||||
|
|
||||||
| class CrossDSOClass { | ||||||
| public: | ||||||
| virtual ~CrossDSOClass(); | ||||||
| }; | ||||||
|
|
||||||
| CrossDSOClass::~CrossDSOClass() = default; | ||||||
|
|
||||||
| struct UnrelatedClass {}; | ||||||
|
|
||||||
| TEST_SUBMODULE(class_cross_module_use_after_one_module_dealloc, m) { | ||||||
| m.def("register_and_instantiate_cross_dso_class", [](py::module_ m) { | ||||||
|
||||||
| m.def("register_and_instantiate_cross_dso_class", [](py::module_ m) { | |
| m.def("register_and_instantiate_cross_dso_class", [](const py::module_ &m) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [](py::module_ m) { py::class_<UnrelatedClass>(m, "UnrelatedClass"); }); | |
| [](const py::module_ &m) { py::class_<UnrelatedClass>(m, "UnrelatedClass"); }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import gc | ||
| import types | ||
| import weakref | ||
|
|
||
| import pytest | ||
|
|
||
| import env # noqa: F401 | ||
| from pybind11_tests import class_cross_module_use_after_one_module_dealloc as m | ||
|
|
||
|
|
||
| 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") | ||
| 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 metaclass. 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|metaclass pointer| | ||
| # |&typeid(T) from DSO 2|metaclass pointer| | ||
| # +=======================================+ | ||
| # | ||
| # Then, if the metaclass 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 metaclass instance. 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")) | ||
|
Check failure on line 53 in tests/test_class_cross_module_use_after_one_module_dealloc.py
|
||
|
|
||
| # 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) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest disambiguating since both the key and value types are
something::type_info