Skip to content

Commit 4766065

Browse files
authored
[smart_holder] test_class_sh_mi_thunks (started from PR #4374) (#4380)
* Content of PR #4374 applied on top of smart_holder branch. * More tests, with USE_SH switch. [ci skip] * Use `std::dynamic_pointer_cast<Base0>` [ci skip] * All tests pass when using `m.make_derived_as_base0_raw_ptr()`, with `USE_SH` defined or not defined. [ci skip] * WIP * Debug LOOOK & one-line bug fix: ```diff - auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src); + auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(std::shared_ptr<void>(src, const_cast<void *>(st.first))); ``` * Remove all print LOOOK and clang-format the fix. * Resolve clang-tidy errors. * Systematic test matrix. * Bug fix in `smart_holder_type_caster<std::unique_ptr<T, D>>::cast()` * Rename: test_mi_debug -> test_class_sh_mi_thunks * Add `test_ptrdiff_derived_base0()` * Miscellaneous polishing (naming, comments). No functional changes. * Improve test_class_sh_mi_thunks.py implementation. No change in test coverage. * Resolve clang-tidy error.
1 parent 53b80b4 commit 4766065

File tree

5 files changed

+175
-12
lines changed

5 files changed

+175
-12
lines changed

include/pybind11/detail/smart_holder_poc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ struct smart_holder {
301301

302302
template <typename T, typename D>
303303
static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
304-
bool void_cast_raw_ptr = false) {
304+
void *void_ptr = nullptr) {
305305
smart_holder hld;
306306
hld.rtti_uqp_del = &typeid(D);
307307
hld.vptr_is_using_builtin_delete = is_std_default_delete<T>(*hld.rtti_uqp_del);
@@ -311,8 +311,8 @@ struct smart_holder {
311311
} else {
312312
gd = make_guarded_custom_deleter<T, D>(true);
313313
}
314-
if (void_cast_raw_ptr) {
315-
hld.vptr.reset(static_cast<void *>(unq_ptr.get()), std::move(gd));
314+
if (void_ptr != nullptr) {
315+
hld.vptr.reset(void_ptr, std::move(gd));
316316
} else {
317317
hld.vptr.reset(unq_ptr.get(), std::move(gd));
318318
}

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
386386
template <typename T, typename D>
387387
static smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
388388
bool void_cast_raw_ptr) {
389-
return pybindit::memory::smart_holder::from_unique_ptr(std::move(unq_ptr),
390-
void_cast_raw_ptr);
389+
void *void_ptr = void_cast_raw_ptr ? static_cast<void *>(unq_ptr.get()) : nullptr;
390+
return pybindit::memory::smart_holder::from_unique_ptr(std::move(unq_ptr), void_ptr);
391391
}
392392

393393
template <typename T>
@@ -836,7 +836,8 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
836836
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
837837
valueptr = src_raw_void_ptr;
838838

839-
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src);
839+
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(
840+
std::shared_ptr<void>(src, const_cast<void *>(st.first)));
840841
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
841842

842843
if (policy == return_value_policy::reference_internal) {
@@ -890,17 +891,16 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
890891
return none().release();
891892
}
892893

893-
auto src_raw_ptr = src.get();
894-
auto st = type_caster_base<T>::src_and_type(src_raw_ptr);
894+
auto st = type_caster_base<T>::src_and_type(src.get());
895895
if (st.second == nullptr) {
896896
return handle(); // no type info: error will be set already
897897
}
898898

899-
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
899+
void *src_raw_void_ptr = const_cast<void *>(st.first);
900900
const detail::type_info *tinfo = st.second;
901901
if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) {
902902
auto *self_life_support
903-
= dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(src_raw_ptr);
903+
= dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(src.get());
904904
if (self_life_support != nullptr) {
905905
value_and_holder &v_h = self_life_support->v_h;
906906
if (v_h.inst != nullptr && v_h.vh != nullptr) {
@@ -926,8 +926,14 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
926926
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
927927
valueptr = src_raw_void_ptr;
928928

929-
auto smhldr = pybindit::memory::smart_holder::from_unique_ptr(std::move(src),
930-
/*void_cast_raw_ptr*/ false);
929+
if (static_cast<void *>(src.get()) == src_raw_void_ptr) {
930+
// This is a multiple-inheritance situation that is incompatible with the current
931+
// shared_from_this handling (see PR #3023).
932+
// SMART_HOLDER_WIP: IMPROVABLE: Is there a better solution?
933+
src_raw_void_ptr = nullptr;
934+
}
935+
auto smhldr
936+
= pybindit::memory::smart_holder::from_unique_ptr(std::move(src), src_raw_void_ptr);
931937
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
932938

933939
if (policy == return_value_policy::reference_internal) {

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ set(PYBIND11_TEST_FILES
127127
test_class_sh_disowning_mi
128128
test_class_sh_factory_constructors
129129
test_class_sh_inheritance
130+
test_class_sh_mi_thunks
130131
test_class_sh_module_local.py
131132
test_class_sh_property
132133
test_class_sh_shared_ptr_copy_move

tests/test_class_sh_mi_thunks.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/smart_holder.h>
3+
4+
#include "pybind11_tests.h"
5+
6+
#include <cstddef>
7+
#include <memory>
8+
#include <vector>
9+
10+
namespace test_class_sh_mi_thunks {
11+
12+
// For general background: https://shaharmike.com/cpp/vtable-part2/
13+
// C++ vtables - Part 2 - Multiple Inheritance
14+
// ... the compiler creates a 'thunk' method that corrects `this` ...
15+
16+
struct Base0 {
17+
virtual ~Base0() = default;
18+
Base0() = default;
19+
Base0(const Base0 &) = delete;
20+
};
21+
22+
struct Base1 {
23+
virtual ~Base1() = default;
24+
// Using `vector` here because it is known to make this test very sensitive to bugs.
25+
std::vector<int> vec = {1, 2, 3, 4, 5};
26+
Base1() = default;
27+
Base1(const Base1 &) = delete;
28+
};
29+
30+
struct Derived : Base1, Base0 {
31+
~Derived() override = default;
32+
Derived() = default;
33+
Derived(const Derived &) = delete;
34+
};
35+
36+
} // namespace test_class_sh_mi_thunks
37+
38+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Base0)
39+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Base1)
40+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Derived)
41+
42+
TEST_SUBMODULE(class_sh_mi_thunks, m) {
43+
using namespace test_class_sh_mi_thunks;
44+
45+
m.def("ptrdiff_drvd_base0", []() {
46+
auto drvd = std::unique_ptr<Derived>(new Derived);
47+
auto *base0 = dynamic_cast<Base0 *>(drvd.get());
48+
return std::ptrdiff_t(reinterpret_cast<char *>(drvd.get())
49+
- reinterpret_cast<char *>(base0));
50+
});
51+
52+
py::classh<Base0>(m, "Base0");
53+
py::classh<Base1>(m, "Base1");
54+
py::classh<Derived, Base1, Base0>(m, "Derived");
55+
56+
m.def(
57+
"get_drvd_as_base0_raw_ptr",
58+
[]() {
59+
auto *drvd = new Derived;
60+
auto *base0 = dynamic_cast<Base0 *>(drvd);
61+
return base0;
62+
},
63+
py::return_value_policy::take_ownership);
64+
65+
m.def("get_drvd_as_base0_shared_ptr", []() {
66+
auto drvd = std::make_shared<Derived>();
67+
auto base0 = std::dynamic_pointer_cast<Base0>(drvd);
68+
return base0;
69+
});
70+
71+
m.def("get_drvd_as_base0_unique_ptr", []() {
72+
auto drvd = std::unique_ptr<Derived>(new Derived);
73+
auto base0 = std::unique_ptr<Base0>(std::move(drvd));
74+
return base0;
75+
});
76+
77+
m.def("vec_size_base0_raw_ptr", [](const Base0 *obj) {
78+
const auto *obj_der = dynamic_cast<const Derived *>(obj);
79+
if (obj_der == nullptr) {
80+
return std::size_t(0);
81+
}
82+
return obj_der->vec.size();
83+
});
84+
85+
m.def("vec_size_base0_shared_ptr", [](const std::shared_ptr<Base0> &obj) -> std::size_t {
86+
const auto obj_der = std::dynamic_pointer_cast<Derived>(obj);
87+
if (!obj_der) {
88+
return std::size_t(0);
89+
}
90+
return obj_der->vec.size();
91+
});
92+
93+
m.def("vec_size_base0_unique_ptr", [](std::unique_ptr<Base0> obj) -> std::size_t {
94+
const auto *obj_der = dynamic_cast<const Derived *>(obj.get());
95+
if (obj_der == nullptr) {
96+
return std::size_t(0);
97+
}
98+
return obj_der->vec.size();
99+
});
100+
}

tests/test_class_sh_mi_thunks.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
3+
from pybind11_tests import class_sh_mi_thunks as m
4+
5+
6+
def test_ptrdiff_drvd_base0():
7+
ptrdiff = m.ptrdiff_drvd_base0()
8+
# A failure here does not (necessarily) mean that there is a bug, but that
9+
# test_class_sh_mi_thunks is not exercising what it is supposed to.
10+
# If this ever fails on some platforms: use pytest.skip()
11+
# If this ever fails on all platforms: don't know, seems extremely unlikely.
12+
assert ptrdiff != 0
13+
14+
15+
@pytest.mark.parametrize(
16+
"vec_size_fn",
17+
[
18+
m.vec_size_base0_raw_ptr,
19+
m.vec_size_base0_shared_ptr,
20+
],
21+
)
22+
@pytest.mark.parametrize(
23+
"get_fn",
24+
[
25+
m.get_drvd_as_base0_raw_ptr,
26+
m.get_drvd_as_base0_shared_ptr,
27+
m.get_drvd_as_base0_unique_ptr,
28+
],
29+
)
30+
def test_get_vec_size_raw_shared(get_fn, vec_size_fn):
31+
obj = get_fn()
32+
assert vec_size_fn(obj) == 5
33+
34+
35+
@pytest.mark.parametrize(
36+
"get_fn", [m.get_drvd_as_base0_raw_ptr, m.get_drvd_as_base0_unique_ptr]
37+
)
38+
def test_get_vec_size_unique(get_fn):
39+
obj = get_fn()
40+
assert m.vec_size_base0_unique_ptr(obj) == 5
41+
with pytest.raises(ValueError) as exc_info:
42+
m.vec_size_base0_unique_ptr(obj)
43+
assert (
44+
str(exc_info.value)
45+
== "Missing value for wrapped C++ type: Python instance was disowned."
46+
)
47+
48+
49+
def test_get_shared_vec_size_unique():
50+
obj = m.get_drvd_as_base0_shared_ptr()
51+
with pytest.raises(ValueError) as exc_info:
52+
m.vec_size_base0_unique_ptr(obj)
53+
assert (
54+
str(exc_info.value)
55+
== "Cannot disown external shared_ptr (loaded_as_unique_ptr)."
56+
)

0 commit comments

Comments
 (0)