The ABI's claim is that a plugin needs a C compiler and nothing else. These
two are the evidence: the same SOCKS5 stream engine written twice, once in
plain C11 against leaf_plugin_abi.h and once in Zig importing that same
header, with no SDK, no runtime and no dependency beyond libc between them.
Both are state machines over the greeting, the method reply, the request and
the reply, so neither ever blocks or holds a thread. Each carries its own
tests -- socks5_test.c runs the engine against truncated and malformed
replies, and the Zig build runs its tests through `zig build test`.
Writing a plugin in Go means writing the vtables and const-pointer shims cgo
cannot express, handle allocation, panic recovery, the two-pass
get_last_error protocol and address encoding -- once per plugin, by hand.
leaf-abi-go holds all of it once, so a plugin is a package that registers
engines and implements them over byte slices: tls-cabi-go is 143 lines,
trojan-cabi-go 321.
ConnEngine is the other half. crypto/tls insists on a blocking net.Conn,
which the ABI forbids an engine call from being, so it runs the
implementation on goroutines over an in-memory pipe and reports BLOCKED with
a wake while those are what it waits on. A codec needing none of that
implements StreamEngine directly, as trojan does.
The SDK revokes its host handle once Destroy returns, so a goroutine
outliving its engine drops a log line rather than calling into memory the
host has reclaimed, and it masks BLOCKED off when the host offered no wake
callback. Register runs from Go's init and calls out to C; it cannot go the
other way, since entering Go binds a thread to the Go runtime for good and
the thread that loads a plugin is the host's.
The descriptor declares that these plugins embed a language runtime, so the
host never unmaps them.
Two reference plugins on the C ABI, and the first users of the contract.
shadowsocks-cabi-rs is the sample the in-process API used to carry, rewritten
as a stream engine: AEAD ciphers, the SOCKS address codec, and the request
header written when the engine is created rather than on the first push, so a
connection that sends nothing still reaches the far end. The wire header stays
out of the address buffer it is derived from.
tls-cabi-rs wraps rustls as a stream engine, and depends on nothing from leaf,
so the cdylib carries no second copy of the proxy engine.
Both catch a panic at the boundary and report it as an error. A panic that
crosses the ABI is undefined behaviour, and a plugin that faults its host is
worse than one that fails.
The old API handed dynamic libraries Rust trait objects, tying every plugin to
leaf's exact compiler and crate versions and ruling out other languages. It is
removed, along with its shadowsocks sample, which returns on the new ABI in a
later commit.
The host loads plugins over leaf-plugin-abi instead. It validates the
descriptor, dials the endpoint, and drives the engines as sans-io codecs over
its own transport, exposing them as ordinary outbound handlers. Datagram
engines are either unreliable, with a UDP socket of their own, or reliable,
framed over the stream transport. Plugin failures carry the plugin's last error
string, and plugin logs join the session's tracing span.
Everything crossing the boundary is treated as untrusted: counts are checked
against the buffers they describe, reported strings are bounded and escaped,
engines get an opaque id rather than a pointer for host_ctx, and an engine that
takes no input is backpressure rather than failure. A poll does a bounded
amount of work, and a datagram the receiver cannot take is dropped instead of
the session.
An outbound names its path, host and port, and may pin the sha256 of the
library it loads -- the only check on this path that still holds against
someone who can write the file.
`leaf --verify-plugin <path>` runs the same checks without a config file and
prints what the plugin declares: its ABI version, its engines, whether the
outbound must carry host and port, and the digest to pin it to. A build can
then be vetted before it is in the path of any traffic.