Skip to content

feat: purego and goffi backends alongside cgo, and fix the musl verifier stack overflow - #4

Open
YOU54F wants to merge 4 commits into
masterfrom
feat/purego-rebased
Open

YOU54F wants to merge 4 commits into
masterfrom
feat/purego-rebased

Conversation

@YOU54F

@YOU54F YOU54F commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Summary

pact-go can now load libpact_ffi without cgo. There are three backends, chosen by build tag, and cgo stays the default.

Backend Selected when Needs a C toolchain
cgo CGO_ENABLED=1 and no backend tag (default) Yes
purego CGO_ENABLED=0, or -tags pact_purego No
goffi -tags pact_goffi No

purego and goffi share one symbol table and loader (internal/native/ffi_dynamic.go). The dynamic backends find the library through PACT_LD_LIBRARY_PATH.

This branch also fixes the intermittent SIGSEGV in provider tests on Alpine, which happened on every backend and almost always under -race. pactffi_verifier_execute overflows musl's 128 KiB default thread stack. On Linux, all three backends now call it on a pthread with an 8 MiB stack and join it, falling back to a direct call if the thread can't be created. The same fix is proposed on the Rust side in YOU54F/pact-reference (branch fix/verifier-stack-size). Either fix alone is enough; this one also covers users on FFI releases that are already out.

Commits

  • feat: support CGO_ENABLED=0: adds the purego backend.
  • feat: pact-go test wrapper around go test: sets PACT_LD_LIBRARY_PATH automatically.
  • feat: goffi backend alongside cgo and purego
  • fix: run the verifier on a thread with an 8 MiB stack on Linux

This is rebased on upstream master at 2.9.1 (0eaee3d5). The only conflicts were in go.mod, and they were resolved to upstream's dependency versions plus purego and goffi.

Testing

  • macOS arm64: the internal/native, consumer, provider and message tests pass on cgo, purego and goffi, and internal/native also passes with CGO_ENABLED=0.
  • Cross-compile: CGO_ENABLED=0 builds pass for Linux and Windows.
  • Before the rebase, on Alpine arm64: with the stack fix, the internal/native provider tests passed 10/10 on all six configurations (three backends, with and without -race). Without it they passed between 0 and 5 of 10.
  • Not run: Windows tests, and x86_64 musl.

🤖 Generated with Claude Code

YOU54F and others added 4 commits November 26, 2026 15:24
users now required to set PACT_LD_LIBRARY_PATH env var with
location of download pact ffi lib.

It can exist anywhere on the filesystem
to automatically set PACT_LD_LIBRARY_PATH
Add a third libpact_ffi backend built on github.com/go-webgpu/goffi,
selected with -tags pact_goffi. It shares the symbol table, library
search and stub-on-failure behaviour of the purego backend
(ffi_dynamic.go) and binds each symbol through reflection.

Backends are mutually exclusive by build tag:
  cgo     cgo && !pact_purego && !pact_goffi  (default, CGO_ENABLED=1)
  purego  (!cgo || pact_purego) && !pact_goffi (default, CGO_ENABLED=0)
  goffi   pact_goffi

Also:
- purego: look symbols up through a per-platform helper, so the
  backend builds on Windows (purego.Dlsym is unix only)
- wrap loader errors, drop unused declarations, keep the package
  comment in an untagged file
- golangci: scope gosec G103/G115 out of internal/native, the FFI
  boundary, where cgo conversions previously hid the same narrowing
pactffi_verifier_execute runs the verification on the calling thread
and needs more stack than a Go-created thread has on musl (Alpine),
where the default is 128 KiB. The provider tests crashed with SIGSEGV
intermittently, and almost always under -race.

On Linux, every backend (cgo, purego, goffi) now calls it on a pthread
created with an 8 MiB stack and waits for it, falling back to a direct
call if the thread cannot be created. Other platforms are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant