Plugins in Rust

I just wrote a blog post about writing safe binary plugins in Rust! It's a fairly niche topic but when you need the sort of flexibility that only comes from loading code at runtime, it pays to know how to do it in the right way.

I'd love to hear if this was useful or interesting to you! Likewise, flick me a message if there's a topic you feel is worth this sort of technical deep dive and I can write something up :slight_smile:

12 Likes

This is a fantastic writeup. Cant' wait to give this a shot.

1 Like

You mention this:

I think you should say this a little more loudly because this is a very important point that the casual C++ programmer will easily overlook. The kinds of bugs you'll experience from ABI incompatibilities are very hard to debug. You shouldn't use this kind of mechanism unless you tightly control the entire integration chain yourself.

1 Like

This is very true. I think we should have this covered by checking the rustc and plugins_core version numbers, but that assumes invoking the same compiler version on the same platform and source code will generate identical layouts and usages for the plugins_core types... It's not technically correct (rust doesn't have a stable ABI and we don't guarantee reproducible builds), but in practice this tends to work out without a problem.

The alternative would be to write extern "C" shims for every Rust type (vtable for trait methods, wrap all functions in an extern "C" function, etc.), which gets really annoying really quickly, plus tends to be quite brittle if done manually. I believe this is the approach the abi_stable crate takes, except it uses macros heavily (both proc and macro_rules), and had to write extern "C" wrappers for half the standard library...