Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/modules/ROOT/pages/error_handling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions doc/modules/ROOT/pages/registries_and_policies.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
17 changes: 17 additions & 0 deletions doc/modules/ROOT/pages/shared_libraries.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
8 changes: 7 additions & 1 deletion include/boost/openmethod/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
//!
Expand Down Expand Up @@ -1872,7 +1877,8 @@ class virtual_ptr<
virtual_ptr& operator=(const Other& other) {
obj = other;
vp = detail::box_vptr<use_indirect_vptrs>(
detail::acquire_vptr<Registry>(*other));
other ? detail::acquire_vptr<Registry>(*other)
: detail::null_vptr);

return *this;
}
Expand Down
8 changes: 8 additions & 0 deletions include/boost/openmethod/initialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,14 @@ void registry<Policies...>::compiler<Options...>::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
Expand Down
5 changes: 5 additions & 0 deletions include/boost/openmethod/preamble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down
9 changes: 9 additions & 0 deletions test/test_smart_virtual_ptr_value_semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dog>();
const dog_ptr empty;
p = empty;
BOOST_TEST(p.get() == nullptr);
BOOST_TEST(p.vptr() == nullptr);
}

static_assert(!construct_assign_ok<dog_virtual_ptr, const Dog&>);
static_assert(!construct_assign_ok<dog_virtual_ptr, const Dog*>);
}
Expand Down
Loading