-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[BUG] pickleable base C++ classes can incur object slicing if derived typeid is not registered with pybind11 #3062
Comments
Suggested (but invasive) solution:Fail fast if we attempt to pickle an object whose Less invasive solution:Document this sharp edge, and tell people to be careful / provide an example of how to do this properly. |
Oh, I saw your In a way the How could we detect the situation when pickling? (I'm currently drawing a blank.) |
Agree this is an issue. It doesn't affect the open spiel use case because we effectively have our own type registration - the unpickling code will call a factory method, which might be native C++, or a Python callable, which ensures we get the right derived type. (This wasn't deliberate, but just a consequence of the general design). |
* pickle setstate: setattr __dict__ only if not empty, to not force use of py::dynamic_attr() unnecessarily. * Adding unit test. * Clang 3.6 & 3.7 compatibility. * PyPy compatibility. * Minor iwyu fix, additional comment. * Addressing reviewer requests. * Applying clang-tidy suggested fixes. * Adding check_dynamic_cast_SimpleCppDerived, related to issue #3062.
Issue description
Context: #2972 (comment)
If you have C++ base class that is exposed to
pybind11
, but then you have a derived class that is not exposed to pybind11, then you will encounter object slicing (wikipedia) via a pickle -> unpickle round trip.Slicing may mean that the unpickled object has the wrong vtable, data, etc.
Reproducible example code
master...EricCousineau-TRI:issue-3062 (derived from fixes in #2972)
commit: 7ce1ad0
Upon running the test:
\cc @rwgk @YannickJadoul @elkhrt
The text was updated successfully, but these errors were encountered: