diff --git a/doc/modules/ROOT/pages/error_handling.adoc b/doc/modules/ROOT/pages/error_handling.adoc index dd3313b9..bc9b616f 100644 --- a/doc/modules/ROOT/pages/error_handling.adoc +++ b/doc/modules/ROOT/pages/error_handling.adoc @@ -58,3 +58,23 @@ struct throw_error_handler : error_handler { } // namespace boost::openmethod::policies ``` + +## Runtime Checks + +Only a registry that contains the cpp:runtime_checks[] policy diagnoses a +call that passes an object of an unregistered class, a call made before +`initialize`, or a cpp:final_virtual_ptr[] whose static and dynamic types +differ. Without the policy, nothing stands between the mistake and the +dispatch: the v-table pointer is read from a slot that `initialize` never +filled - a null one, or one past the end of the table - and the word found +there is called as a function. The call does not fail cleanly; where it +lands depends on what happens to be in memory at that address. + +`default_registry` contains the policy only if +xref:reference:BOOST_OPENMETHOD_ENABLE_RUNTIME_CHECKS.adoc[BOOST_OPENMETHOD_ENABLE_RUNTIME_CHECKS] +is defined, which the CMake build does for debug builds. A program that +dispatches on objects it does not fully control - from a plugin, or built +from external input - should consider keeping the policy in release builds +too. The cost is a test of the initialized flag per call, and, with a +`type_hash` policy, one load and compare per virtual argument, against the +table of registered type ids. diff --git a/doc/modules/ROOT/pages/registries_and_policies.adoc b/doc/modules/ROOT/pages/registries_and_policies.adoc index 534863a8..635e5bd2 100644 --- a/doc/modules/ROOT/pages/registries_and_policies.adoc +++ b/doc/modules/ROOT/pages/registries_and_policies.adoc @@ -272,6 +272,16 @@ registry with the cpp:runtime_checks[] policy diagnoses a call made in the meantime; without it, the call dispatches through the previous tables, which - after a `dlclose` - may point into unloaded code. +The dispatch tables are built at run time, so they live in ordinary writable +memory - a vector in the registry's state - and so do the classes' static +v-table pointers, unlike the v-tables the compiler emits for virtual +functions, which the linker places in read-only memory. A program that +hardens itself against memory corruption should know that overwriting one +word of that vector redirects every call that goes through it. The tables +are never written between two calls to cpp:initialize[]; a program may make +the vector read-only with the facilities of its platform once +cpp:initialize[] has returned. + A registry can also be created by copying an existing registry's policies, using the cpp:with[] and cpp:without[] nested templates. For example, `indirect_registry` is a tweak of `default_registry`: diff --git a/doc/modules/ROOT/pages/shared_libraries.adoc b/doc/modules/ROOT/pages/shared_libraries.adoc index 6522221e..b175bf77 100644 --- a/doc/modules/ROOT/pages/shared_libraries.adoc +++ b/doc/modules/ROOT/pages/shared_libraries.adoc @@ -114,6 +114,23 @@ A program can also load a shared library after it has started - a plugin, typically - and the library can add classes, methods and overriders to the registry. Call `initialize` again after loading or unloading a library. +Call it *before* anything is dispatched: a library's overriders are copied +into the dispatch tables as plain function pointers, and, once the library +is unloaded, every call that reaches one jumps into memory that is no longer +mapped, or into whatever a later load has mapped there. The +cpp:runtime_checks[] policy does not catch this by itself: the registry still +counts as initialized. Calling cpp:finalize[] before unloading, and +`initialize` after, makes a registry that has the policy refuse every call +made in between. + +The library does not synchronize anything. `initialize`, `finalize`, and +the loading and unloading of a library that registers classes, methods or +overriders, write to the registry's state, and must not overlap with a +method call or the construction of a `virtual_ptr` in another thread. Two +threads loading libraries at the same time race on the registration lists +as well. Dispatch itself only reads, and any number of threads may dispatch +concurrently between those operations. + The program is then the natural owner of the state: it must work before the library is loaded, and whether or not it is. The `dynamic_loading` example uses a header like the one above. The program defines `OWNS_REGISTRY_STATE`, diff --git a/include/boost/openmethod/core.hpp b/include/boost/openmethod/core.hpp index a17b4c6b..8276f7dc 100644 --- a/include/boost/openmethod/core.hpp +++ b/include/boost/openmethod/core.hpp @@ -1344,6 +1344,9 @@ class virtual_ptr { //! //! @li @c Other* must be constructible from @c Class*. //! + //! @li @c other must not be null: the object is read to find its class. + //! Construct from `nullptr` to make a null `virtual_ptr`. + //! //! @par Errors //! //! The following errors may occur, depending on the policies selected in @@ -1451,6 +1454,8 @@ class virtual_ptr { //! @li @c Other must be a polymorphic class, according to the @c rtti //! policy of @c Registry. //! @li @c Other* must be constructible from @c Class*. + //! @li @c other must not be null: the object is read to find its class. + //! Assign `nullptr` to make a null `virtual_ptr`. //! //! @par Errors //! @@ -1872,7 +1877,8 @@ class virtual_ptr< virtual_ptr& operator=(const Other& other) { obj = other; vp = detail::box_vptr( - detail::acquire_vptr(*other)); + other ? detail::acquire_vptr(*other) + : detail::null_vptr); return *this; } diff --git a/include/boost/openmethod/initialize.hpp b/include/boost/openmethod/initialize.hpp index a1a069eb..74ab56df 100644 --- a/include/boost/openmethod/initialize.hpp +++ b/include/boost/openmethod/initialize.hpp @@ -2339,6 +2339,14 @@ void registry::compiler::print_slots() { //! registered. //! @li The registry's policies may report additional errors. //! +//! @par Thread safety +//! +//! `initialize` writes to the registry's state, and performs no +//! synchronization. It must not overlap with a method call, or with the +//! construction of a `virtual_ptr`, in another thread; nor may two threads +//! initialize the same registry at the same time. Any number of threads may +//! dispatch concurrently once it has returned. +//! //! @par Exception safety //! //! If `initialize` throws - because a policy's `initialize` does, or an diff --git a/include/boost/openmethod/preamble.hpp b/include/boost/openmethod/preamble.hpp index 9e887fa2..9cb75027 100644 --- a/include/boost/openmethod/preamble.hpp +++ b/include/boost/openmethod/preamble.hpp @@ -502,6 +502,11 @@ struct trace { //! Returns a `trace` object with `on` set to `true` if the environment //! variable `BOOST_OPENMETHOD_TRACE` is set to the string "1", and false //! otherwise. + //! + //! @note The trace prints the addresses of type ids, v-tables and + //! overriders, which defeat address space layout randomization. A program + //! that runs with more privileges than the user who sets its environment + //! should not hand the decision to the environment. static trace from_env(); }; diff --git a/test/test_smart_virtual_ptr_value_semantics.cpp b/test/test_smart_virtual_ptr_value_semantics.cpp index bf789b06..36b02cce 100644 --- a/test/test_smart_virtual_ptr_value_semantics.cpp +++ b/test/test_smart_virtual_ptr_value_semantics.cpp @@ -345,6 +345,15 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( BOOST_TEST(p.vptr() == nullptr); } + { + // copy-assign from an empty smart pointer lvalue + auto p = smart::template make_virtual(); + const dog_ptr empty; + p = empty; + BOOST_TEST(p.get() == nullptr); + BOOST_TEST(p.vptr() == nullptr); + } + static_assert(!construct_assign_ok); static_assert(!construct_assign_ok); }