A VST3 host in Rust that keeps running when a plugin crashes
Building a VST plugin host may be one of the harder problems in audio software. VST3 is Steinberg's format for audio plugins, and a host is the program that loads them, as a DAW does. The host loads plugins, which are code that someone else wrote, into its own process, and calls them from the audio thread, which has to deliver each block of audio on time. When that code crashes, it takes the host down with it, unless the host is built for that.
RS VST Host is my VST3 host in Rust. It plays audio through the Rust crate cpal and takes MIDI through midir, and its GUI is egui. The bindings to VST3 are the vst3-rs crate of coupler-rs. The plugins run in a routing graph, a directed acyclic graph with a topological sort and cycle detection, and the host does latency compensation with sample-accurate delay lines.
When a plugin crashes
Section titled “When a plugin crashes”By default the host catches a crash with a signal handler. When a plugin crashes, the handler gets the signal, and the host recovers and keeps running.
As an option, each plugin can run in a child process of its own. The audio goes between the host and the child through POSIX shared memory, and the control messages go through Unix sockets. A crash then ends the child, and the host stays up. The GUI runs in a child process too. If a plugin crashes it, a supervisor starts the GUI again while the audio keeps playing.
Talking to a plugin
Section titled “Talking to a plugin”A VST3 plugin is a set of C++ interfaces in the style of Microsoft's COM. Every interface derives from FUnknown, which counts references with addRef and release, and gives access to other interfaces through queryInterface and an interface id. From Rust, an interface is a pointer to a table of function pointers with the C layout, and every call through it is unsafe.
1,414 tests
Section titled “1,414 tests”The host has 1,414 tests: 763 unit tests and 651 integration tests. They cover the layouts of the structs that VST3 calls through, the audio pipeline, the translation of MIDI, the messages between the processes, the isolation of plugins and the state of the GUI.
The compiler can't prove that raw pointers and the calls through them are valid. Some of that can be checked at run time: 109 of the tests also run under Miri, which finds undefined behaviour, and 564 run under AddressSanitizer, which finds bad memory access.
I also build plugins and hosts for clients: services.