Files
eric 5ac82cd54f fix(plugins): three Go SDK defects, and test helpers for plugin authors
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.
2026-12-03 19:31:53 +08:00

567 lines
16 KiB
Go

package leafabi
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"io"
"math/big"
"net"
"runtime"
"sync"
"testing"
"time"
)
// driver runs an engine the way the plugin host does: one call at a time, the
// network side wired to a real conn, and everything else driven from the state
// the engine reports.
//
// It exists so that these tests exercise the same loop leaf runs rather than a
// convenient shortcut around it. It polls where the host would wait on the wake
// callback, which a test cannot install: wake is a C function pointer.
type driver struct {
// calls is the host's promise that no two calls on one instance overlap.
calls sync.Mutex
engine StreamEngine
wire net.Conn
appMu sync.Mutex
app bytes.Buffer
stopOnce sync.Once
stopped chan struct{}
wg sync.WaitGroup
}
const drivePeriod = 200 * time.Microsecond
func newDriver(engine StreamEngine, wire net.Conn) *driver {
d := &driver{engine: engine, wire: wire, stopped: make(chan struct{})}
d.wg.Add(2)
go func() {
defer d.wg.Done()
d.readWire()
}()
go func() {
defer d.wg.Done()
d.drain()
}()
return d
}
// readWire is the host reading the socket and handing the bytes to the engine.
func (d *driver) readWire() {
buf := make([]byte, 4096)
for {
n, err := d.wire.Read(buf)
if n > 0 {
d.calls.Lock()
_, _ = d.engine.Push(SideNet, buf[:n])
d.calls.Unlock()
}
if err != nil {
d.calls.Lock()
_ = d.engine.Close(CloseNet)
d.calls.Unlock()
return
}
select {
case <-d.stopped:
return
default:
}
}
}
// drain is the host moving whatever the engine offers: network output to the
// socket, application output to the reader.
func (d *driver) drain() {
out := make([]byte, 16*1024)
for {
select {
case <-d.stopped:
return
default:
}
d.calls.Lock()
flags := d.engine.PollState()
var netOut, appOut int
if flags.Has(StateHasNetOutput) {
netOut, _ = d.engine.Pull(SideNet, out)
}
var netBytes []byte
if netOut > 0 {
netBytes = append([]byte(nil), out[:netOut]...)
}
if flags.Has(StateHasAppOutput) {
appOut, _ = d.engine.Pull(SideApp, out)
if appOut > 0 {
d.appMu.Lock()
d.app.Write(out[:appOut])
d.appMu.Unlock()
}
}
d.calls.Unlock()
if len(netBytes) > 0 {
if _, err := d.wire.Write(netBytes); err != nil {
return
}
}
if netOut == 0 && appOut == 0 {
time.Sleep(drivePeriod)
}
}
}
// writeApp pushes until the engine has taken everything, which is how the host
// deals with a short count.
func (d *driver) writeApp(t *testing.T, data []byte) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for len(data) > 0 {
if time.Now().After(deadline) {
t.Fatalf("the engine never took the last %d bytes", len(data))
}
d.calls.Lock()
n, err := d.engine.Push(SideApp, data)
d.calls.Unlock()
if err != nil {
t.Fatalf("push(app) failed: %v", err)
}
data = data[n:]
if n == 0 {
time.Sleep(drivePeriod)
}
}
}
func (d *driver) readApp(t *testing.T, n int) []byte {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for {
d.appMu.Lock()
have := d.app.Len()
d.appMu.Unlock()
if have >= n {
d.appMu.Lock()
defer d.appMu.Unlock()
return d.app.Next(n)
}
if time.Now().After(deadline) {
t.Fatalf("only %d of %d application bytes came back", have, n)
}
time.Sleep(drivePeriod)
}
}
func (d *driver) closeApp() {
d.calls.Lock()
defer d.calls.Unlock()
_ = d.engine.Close(CloseApp)
}
func (d *driver) pollState() StateFlags {
d.calls.Lock()
defer d.calls.Unlock()
return d.engine.PollState()
}
func (d *driver) stop() {
d.stopOnce.Do(func() { close(d.stopped) })
_ = d.wire.Close()
d.wg.Wait()
}
// A real TLS handshake and a real exchange over it, driven exactly as the host
// drives an engine. This is the case ConnEngine exists for: crypto/tls insists
// on blocking, and the ABI forbids it.
func TestConnEngineCarriesATLSSession(t *testing.T) {
serverConfig := testServerTLSConfig(t)
clientSide, serverSide := net.Pipe()
serverDone := make(chan error, 1)
go func() {
server := tls.Server(serverSide, serverConfig)
if err := server.Handshake(); err != nil {
serverDone <- err
return
}
// Echo until the client half closes, then say goodbye.
_, err := io.Copy(server, server)
_ = server.Close()
serverDone <- err
}()
engine, err := NewConnEngine(ConnEngineConfig{
Wrap: func(inner net.Conn) (net.Conn, error) {
return tls.Client(inner, &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
}), nil
},
})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
d := newDriver(engine, clientSide)
defer func() {
d.stop()
engine.Destroy()
}()
payload := []byte("hello over a real handshake")
d.writeApp(t, payload)
if got := d.readApp(t, len(payload)); !bytes.Equal(got, payload) {
t.Fatalf("echoed %q, want %q", got, payload)
}
// A second exchange: a handshake that leaves the engine in a bad state
// still passes the first.
again := []byte("and again")
d.writeApp(t, again)
if got := d.readApp(t, len(again)); !bytes.Equal(got, again) {
t.Fatalf("echoed %q, want %q", got, again)
}
if flags := d.pollState(); !flags.Has(StateEstablished) {
t.Fatalf("state = %#x, want StateEstablished once data is flowing", uint32(flags))
}
d.closeApp()
select {
case err := <-serverDone:
if err != nil && !errors.Is(err, io.EOF) {
t.Fatalf("the server side ended with %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("the half close never reached the server")
}
}
// A half close is the application saying it has finished sending, not that
// it has stopped listening. The server here replies only once it has seen the
// end of the request -- the shape of a request whose length is the
// connection -- so the reply exists only if the engine ends its sending side
// and goes on reading.
func TestConnEngineHalfCloseKeepsReading(t *testing.T) {
serverConfig := testServerTLSConfig(t)
clientSide, serverSide := net.Pipe()
reply := bytes.Repeat([]byte("tail "), 4096)
go func() {
server := tls.Server(serverSide, serverConfig)
defer server.Close()
if _, err := io.ReadAll(server); err != nil {
return
}
_, _ = server.Write(reply)
}()
engine, err := NewConnEngine(ConnEngineConfig{
Wrap: func(inner net.Conn) (net.Conn, error) {
return tls.Client(inner, &tls.Config{InsecureSkipVerify: true}), nil
},
})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
d := newDriver(engine, clientSide)
defer func() {
d.stop()
engine.Destroy()
}()
d.writeApp(t, []byte("the whole request"))
d.closeApp()
if got := d.readApp(t, len(reply)); !bytes.Equal(got, reply) {
t.Fatalf("got %d bytes of the reply after a half close, want all %d", len(got), len(reply))
}
}
// identityWrap is a transport that adds nothing, which is what the tests that
// are about the engine's own bookkeeping want.
func identityWrap(inner net.Conn) (net.Conn, error) { return inner, nil }
// An engine that has taken all it can hold must say so by consuming less, not
// by growing without limit: a short count is the only backpressure signal the
// host acts on.
func TestConnEngineReportsBackpressureOnApplicationInput(t *testing.T) {
const capacity = 1024
engine, err := NewConnEngine(ConnEngineConfig{
Wrap: identityWrap,
MaxPendingAppInput: capacity,
})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
defer engine.Destroy()
// Nothing drains the network side here, so the write loop parks on the
// first Write and the buffer is what fills up.
total := 0
input := make([]byte, capacity)
for range 8 {
n, err := engine.Push(SideApp, input)
if err != nil {
t.Fatalf("push(app) failed: %v", err)
}
total += n
if n < len(input) {
break
}
}
if total > 2*capacity {
t.Fatalf("the engine took %d bytes with a %d byte cap", total, capacity)
}
deadline := time.Now().Add(2 * time.Second)
for engine.PollState().Has(StateWantAppInput) {
if time.Now().After(deadline) {
t.Fatal("the engine kept asking for application input while full")
}
time.Sleep(drivePeriod)
}
}
// BLOCKED means "I have everything I can use and am working on it"; it is only
// safe to report when something will actually arrive to un-block it. Reporting
// it with nothing in hand would park the host forever.
func TestConnEngineReportsBlockedOnlyWithInputInHand(t *testing.T) {
engine, err := NewConnEngine(ConnEngineConfig{Wrap: identityWrap})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
defer engine.Destroy()
if flags := engine.PollState(); flags.Has(StateBlocked) {
t.Fatalf("state = %#x, want no StateBlocked on a fresh engine", uint32(flags))
}
if flags := engine.PollState(); !flags.Has(StateWantNetInput) {
t.Fatalf("state = %#x, want StateWantNetInput while waiting on the peer", uint32(flags))
}
// Bytes the read loop has not picked up yet are exactly the state BLOCKED
// describes, and it must clear again once they turn into output.
if _, err := engine.Push(SideNet, []byte("payload")); err != nil {
t.Fatalf("push(net) failed: %v", err)
}
deadline := time.Now().Add(2 * time.Second)
for {
if engine.PollState().Has(StateHasAppOutput) {
break
}
if time.Now().After(deadline) {
t.Fatal("pushed network input never became application output")
}
time.Sleep(drivePeriod)
}
if flags := engine.PollState(); flags.Has(StateBlocked) {
t.Fatalf("state = %#x, want no StateBlocked once there is output to take", uint32(flags))
}
}
// A full application buffer behind a busy write loop is the engine's own work
// in progress, and BLOCKED is the only way to say so. Without it the host
// reads a refused write as an engine waiting for its output to be taken: it
// decodes the peer's bytes into a buffer of its own to make room, and when a
// reader saturated in the other direction cannot keep up with that, it fails
// the connection as stalled. Two bulk transfers at once against sing-box did
// exactly that, about one run in ten.
//
// Once the handshake is done the write loop needs nothing from the host to
// take what is waiting -- and it wakes the host when it does -- so the flag
// always clears on its own.
func TestConnEngineReportsBlockedWhileItsWriterIsBusy(t *testing.T) {
release := make(chan struct{})
const capacity = 1024
engine, err := NewConnEngine(ConnEngineConfig{
Wrap: func(inner net.Conn) (net.Conn, error) { return &stallingConn{Conn: inner, release: release}, nil },
MaxPendingAppInput: capacity,
})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
defer engine.Destroy()
defer func() {
select {
case <-release:
default:
close(release)
}
}()
// The first push is taken by the write loop, which then sits in Write;
// the next ones fill the buffer behind it.
chunk := bytes.Repeat([]byte("x"), capacity)
deadline := time.Now().Add(2 * time.Second)
for {
n, err := engine.Push(SideApp, chunk)
if err != nil {
t.Fatalf("push(app) failed: %v", err)
}
if n == 0 {
break
}
if time.Now().After(deadline) {
t.Fatal("the engine never refused input behind a stalled writer")
}
time.Sleep(drivePeriod)
}
if flags := engine.PollState(); !flags.Has(StateBlocked) {
t.Fatalf("state = %#x with a full buffer and a busy writer, want StateBlocked", uint32(flags))
}
close(release)
deadline = time.Now().Add(2 * time.Second)
for engine.PollState().Has(StateBlocked) {
if time.Now().After(deadline) {
t.Fatal("StateBlocked never cleared once the writer moved on")
}
time.Sleep(drivePeriod)
}
if n, _ := engine.Push(SideApp, chunk); n == 0 {
t.Fatal("the engine still refuses input after the writer took the buffer")
}
}
// stallingConn's writes wait until release is closed.
type stallingConn struct {
net.Conn
release chan struct{}
}
func (c *stallingConn) Write(p []byte) (int, error) {
<-c.release
return c.Conn.Write(p)
}
// The ABI lets destroy block, and requires it to: once it returns, nothing the
// plugin owns may still be able to call the host back.
func TestConnEngineDestroyJoinsItsGoroutinesEvenMidHandshake(t *testing.T) {
before := runtime.NumGoroutine()
started := make(chan struct{})
engine, err := NewConnEngine(ConnEngineConfig{
Wrap: identityWrap,
// A handshake that can only end when the conn under it is closed,
// which is what destroy has to be able to do.
Handshake: func(c net.Conn) error {
close(started)
_, err := io.ReadAll(c)
return err
},
})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
<-started
done := make(chan struct{})
go func() {
engine.Destroy()
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("Destroy did not return while the handshake was still running")
}
deadline := time.Now().Add(2 * time.Second)
for runtime.NumGoroutine() > before+2 {
if time.Now().After(deadline) {
t.Fatalf("goroutines went from %d to %d and stayed there",
before, runtime.NumGoroutine())
}
time.Sleep(drivePeriod)
}
}
// A handshake that fails has to reach the host as a fatal engine, not as a
// connection that hangs.
func TestConnEngineReportsAFailedHandshakeAsFatal(t *testing.T) {
engine, err := NewConnEngine(ConnEngineConfig{
Wrap: identityWrap,
Handshake: func(net.Conn) error { return errors.New("no common cipher") },
})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
defer engine.Destroy()
deadline := time.Now().Add(2 * time.Second)
for !engine.PollState().Has(StateFatal) {
if time.Now().After(deadline) {
t.Fatal("a failed handshake never showed up as StateFatal")
}
time.Sleep(drivePeriod)
}
if _, err := engine.Pull(SideApp, make([]byte, 16)); err == nil {
t.Fatal("pull succeeded on a fatal engine")
} else if statusOf(err) != StatusPluginFailure {
t.Fatalf("pull reported %v, want a plugin failure", statusOf(err))
}
}
func TestNewConnEngineRefusesAConfigItCannotUse(t *testing.T) {
if _, err := NewConnEngine(ConnEngineConfig{}); err == nil {
t.Fatal("expected an error when Wrap is nil")
}
if _, err := NewConnEngine(ConnEngineConfig{
Wrap: func(net.Conn) (net.Conn, error) { return nil, nil },
}); err == nil {
t.Fatal("expected an error when Wrap returns no conn")
}
sentinel := errors.New("cannot build the config")
if _, err := NewConnEngine(ConnEngineConfig{
Wrap: func(net.Conn) (net.Conn, error) { return nil, sentinel },
}); !errors.Is(err, sentinel) {
t.Fatalf("err = %v, want the error Wrap returned", err)
}
}
func TestConnEngineRejectsAnUnknownSide(t *testing.T) {
engine, err := NewConnEngine(ConnEngineConfig{Wrap: identityWrap})
if err != nil {
t.Fatalf("NewConnEngine failed: %v", err)
}
defer engine.Destroy()
if _, err := engine.Push(Side(9), []byte("x")); statusOf(err) != StatusInvalidArgument {
t.Errorf("push status = %v, want %v", statusOf(err), StatusInvalidArgument)
}
if _, err := engine.Pull(Side(9), make([]byte, 8)); statusOf(err) != StatusInvalidArgument {
t.Errorf("pull status = %v, want %v", statusOf(err), StatusInvalidArgument)
}
}
func testServerTLSConfig(t *testing.T) *tls.Config {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("generating a key failed: %v", err)
}
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "leafabi-test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
DNSNames: []string{"leafabi-test"},
}
der, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
t.Fatalf("creating a certificate failed: %v", err)
}
return &tls.Config{
Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}},
MinVersion: tls.VersionTLS12,
}
}