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

59 lines
1.6 KiB
Go

package leafabi
import (
"runtime"
"sync"
"sync/atomic"
"testing"
)
// host_ctx is opaque. leaf puts a small instance number in it -- 1, 2, 3 --
// and the ABI allows any value at all. The SDK must never hold it as a Go
// pointer: the collector checks every pointer it finds on a stack, and one
// pointing at address 1 stops the process with "invalid pointer found on
// stack". That is what happened the first time a plugin that allocates
// heavily -- uTLS building ClientHellos -- ran under leaf.
//
// The check that fires is the one the runtime makes when it copies a stack to
// grow it: every pointer in every frame is adjusted, and one that cannot be a
// heap address stops the process. So each call here is made from a fresh
// goroutine at a different recursion depth, which puts a stack growth at the
// call into the C callback sooner or later -- with the frame holding host_ctx
// live. Before the fix this crashed on the first few hundred depths.
func TestHostContextIsNeverAGoPointer(t *testing.T) {
for _, ctx := range []uintptr{1, 2, 7, 4095} {
host := testHost(ctx)
var stop atomic.Bool
var collector sync.WaitGroup
collector.Add(1)
go func() {
defer collector.Done()
for !stop.Load() {
runtime.GC()
}
}()
for depth := range 1500 {
done := make(chan struct{})
go func() {
defer close(done)
callAtDepth(depth, host)
}()
<-done
}
stop.Store(true)
collector.Wait()
}
}
//go:noinline
func callAtDepth(depth int, host Host) {
var pad [64]byte
if depth == 0 {
host.Wake()
host.Log(LevelDebug, string(pad[:1]))
return
}
callAtDepth(depth-1, host)
pad[0]++
}