On an unreliable transport, a decode_packet failure ended the receive
side of the session, which leaf then removed. Every reference plugin
reports a reply it rejects -- failed authentication, an address it cannot
parse -- with PLUGIN_FAILURE, and a reply that fails authentication needs
no key. So one forged packet sent to the client's socket discarded every
reply that arrived before the client next sent something.
A frame the engine refuses with PLUGIN_FAILURE on an unreliable
transport is now dropped like any other damaged packet. Any other status
-- an argument the engine cannot use, an unsupported call, an output it
still cannot fit -- says the engine itself cannot go on, and still ends
the session. So does any failure on a reliable transport, since the
stream cannot be resynchronised, and an engine that breaks the ABI's
contract. The ABI documents the exception.
The e2e case now keeps one association, sends a good reply straight behind
each bad one, and adds a forged reply. It fails on the old host.
host_ctx is opaque, and leaf puts an instance number in it. The SDK held it
as a Go pointer, so the first stack copy that found a value like 1 stopped
the process with "invalid pointer found on stack". It is now an integer on
the Go side and a pointer only in C.
ConnEngine refused application input behind a busy write loop without
reporting BLOCKED. The host read that as an engine waiting for its output to
be taken, buffered decoded bytes to make room, and failed saturated
two-way transfers as stalled, about one run in ten. It now reports BLOCKED
once the handshake is done and the input buffer is full.
On the application's half close ConnEngine closed the wrapped conn whole,
dropping the reply still on its way. It now calls CloseWrite when the conn
has one and keeps reading.
enginetest.Driver drives an engine the way the host does, for a plugin's own
tests; SharedLogger routes a per-outbound library logger into leaf's log.
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.