skip to content

Your team plans third-party Rust plugins as dynamic libraries whose entry point returns `Box<dyn Plugin>`: what does that boundary require, and what would you choose instead?

level: principalimportance: nice to knowfreq 22%

answer

  1. no stable Rust ABI
  2. vtable layout is unspecified
  3. the vtable lives in the plugin
  4. same compiler, same trait crate
  5. hand-built table or isolation

basics

~20 s

Rust promises no stable layout for repr(Rust) types or trait-object vtables, so a Box<dyn Plugin> crossing a separately built library needs, at minimum, the same compiler and trait crate on both sides. A C-ABI function table, WebAssembly or a process are sturdier.

solid answer

~40 s

A `Box<dyn Plugin>` is two words whose meaning the compiler decides: the vtable's layout and the default `repr(Rust)` layout of anything passed through it carry no stability guarantee across compiler versions. So host and plugin must be built by the same rustc, with the same version of the crate defining `Plugin`, and the vtable and drop glue point into the plugin's code, so unloading the library while any box is alive leaves dangling function pointers. That suits first-party plugins built in one workspace and pipeline. For third parties I would pick by trust and churn: compile plugins in and keep `dyn Plugin` internal; expose a C-ABI boundary (`extern "C"` functions and a `#[repr(C)]` table of function pointers, a hand-built vtable); or isolate untrusted plugins in a WebAssembly runtime or a separate process.

go deeper

for a junior

Know that a trait object holds a pointer to a compiler-generated vtable, and that Rust code built separately does not automatically agree on its layout.

for a middle

Explain why the vtable and repr(Rust) layouts tie host and plugin to the same compiler, and why unloading a library while its objects live is unsound.

for a senior

Design the loading path: version handshake, pinned toolchain, unload discipline and allocator agreement, or a C-ABI function table when that is too fragile.

for a principal

Choose the plugin boundary from who writes plugins, how much they are trusted and how often the trait changes, weighing compiled-in, Rust dylib, C ABI and isolation.

## What actually crosses the boundary Inside one program, `Box<dyn Plugin>` is the natural plugin type: a heap pointer plus a pointer to the vtable for that plugin's `Plugin` impl. When the plugin is a separately compiled dynamic library loaded at run time, those two words carry assumptions the host cannot check: - **The vtable's layout.** The Reference says what a vtable contains, a function pointer per method, but specifies no order or format; the core library treats the vtable as an opaque type reached only through compiler intrinsics. - **The data's layout.** Anything passed through the trait's methods, a `String`, a `Vec`, a config struct, uses the default `repr(Rust)` representation, for which the Reference makes almost no layout guarantees. - **The calling convention.** Ordinary Rust functions use the `extern "Rust"` ABI, and the Reference states that the Rust ABI offers no stability guarantees; `extern "C"` instead matches the target's C compiler. The practical rule: host and plugin must be built by the **same rustc version**, with the **same version of the crate that defines `Plugin`** and of every type crossing it, ideally with matching build settings. Two other hazards come with it: 1. **Unloading.** The vtable, the method code and the drop glue live in the plugin library. Unloading the library while any `Box<dyn Plugin>`, or any value whose drop code lives there, is still alive leaves dangling function pointers; the next call or drop jumps into unmapped memory. 2. **Allocators.** A box allocated in the plugin is freed by the host's code with the host's global allocator. Std leaves the default global allocator unspecified, and if either side sets its own `#[global_allocator]` the two no longer match, so the free goes to the wrong allocator. None of this is a type error. The failure mode is undefined behaviour after a routine toolchain upgrade on one side. ## The options | Design | Plugins written by | Hot reload | Main cost | |---|---|---|---| | Compile plugins in, `Vec<Box<dyn Plugin>>` registered at start-up | your team | no | a rebuild to add a plugin | | Rust dynamic library returning `Box<dyn Plugin>` | your team, one pipeline | possible, carefully | toolchain lockstep, unload discipline | | C-ABI boundary: `extern "C"` entry points and a `#[repr(C)]` struct of function pointers | anyone, any compiler | yes | hand-written glue, no Rust types across | | WebAssembly runtime or a separate process | anyone, untrusted | yes | serialisation and call overhead | The C-ABI row is a **hand-built vtable**: a `#[repr(C)]` struct holding a version field, an opaque instance pointer and `extern "C"` function pointers for each operation, including `destroy`. Inside the plugin you still implement a Rust trait and use `dyn Plugin` freely; only the edge is flattened. The details of `extern "C"`, `#[repr(C)]` and unwinding across that edge are covered with foreign function calls. ## How I would decide - **Who writes plugins?** If every plugin is built in your workspace by your CI, compiling them in is simplest and keeps every guarantee the compiler gives. A Rust dylib adds risk for no gain unless you need to ship plugins separately. - **Do plugins need to load without a host rebuild?** If yes and you own the toolchain, a Rust dylib with a pinned toolchain and a version handshake at load time can work; treat the pin as a hard contract. - **Are plugins third-party or untrusted?** Then the boundary must survive their compiler choice: a C ABI with an explicit version field, or isolation in WebAssembly or a process when a faulty plugin must not take the host down. A Rust trait object gives no memory isolation at all. - **What will change?** Adding a method to `Plugin` changes the vtable. With a C-ABI table you control evolution explicitly, for example by appending function pointers and checking the declared version. ## What stays true either way The trait object remains the right *internal* abstraction: the host keeps a `Vec<Box<dyn Plugin>>` and dispatches through it. The decision is only about whether a compiler-defined vtable may cross a line between two separately built binaries. For most teams the answer is no unless both sides ship from the same build.

  • If the host and plugin are built by the same rustc, is returning `Box<dyn Plugin>` from a dynamic library then fine?
    It is workable, not automatic. Both sides must also use the same version of the crate defining `Plugin` and every type crossing it, agree on the allocator, and never unload the library while a plugin value, its vtable or its drop code is still reachable. Add a version handshake at load time so a mismatched build is refused rather than called.
  • Why does adding a method to `Plugin` matter more across a dynamic-library boundary than inside one crate?
    Inside one build, every vtable is regenerated together. Across a boundary, a plugin compiled against the old trait has an old vtable while the host calls with the new one, and nothing at run time checks the match. A C-ABI table with an explicit version field makes that evolution checkable.

  • Says a Box<dyn Plugin> is a stable ABI because trait objects are just two pointers.
  • Unloads a plugin library while plugin objects created by it are still alive.
  • Assumes plugins built with a newer rustc stay compatible with an older host.
  • Believes a trait object isolates a crashing or malicious plugin from the host.
  • Ignores how plugins will evolve when a method is added to the trait.