Repository navigation
fix(amd64): keep the args pointer of syscallN inside its frame - #109
Merged
Merged
Conversation
syscallN saved the args pointer at PTR_ADDRESS(BP), 72 bytes above the saved BP. That slot is outside the frame of syscallN, on the g0 stack, and it overwrote the return address of runtime.mstart0. When a goroutine exited while locked to its thread after a call, mstart0 returned through that slot and the process crashed with SIGSEGV. Save and load the pointer at PTR_ADDRESS(SP), as the frame layout comment states and as purego's syscallX does. TestCallThenLockedExit makes a call in each goroutine and then exits with the thread locked. It crashes without the fix. Fixes go-webgpu#108 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
kolkov
approved these changes
Oct 24, 2026
kolkov
left a comment
Contributor
There was a problem hiding this comment.
Thanks @aardwulf89, good catch, and a clean fix. PTR_ADDRESS(BP) landed 72 bytes above the saved BP, in the caller's frame on g0. The frame comment already said SP+72.
What I checked:
- linux/amd64 (WSL, go1.27.1): with
main'ssyscall_unix_amd64.sand your test, the test binary dies withsignal: segmentation faultunder bothCGO_ENABLED=0andCGO_ENABLED=1. With the fix,-count=3passes for both. - Full suite on the fix:
go test ./...with cgo 0 and 1, and-race -p 1with cgo=1, all pass. CI is green on all 49 checks, and the CI logs showTestCallThenLockedExitrunning (not skipped) on linux amd64/arm64, both macOS runners and the four FreeBSD VMs. - arm64 keeps the pointer inside its own frame (
80(RSP)insyscall_arm64.s), and Windows amd64 goes throughsyscall.SyscallN, so only the Unix amd64 path had this.
I added a CHANGELOG entry crediting you (6bc8870). Approving.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #108.
syscallNininternal/syscall/syscall_unix_amd64.ssaved its args pointer atPTR_ADDRESS(BP), 72 bytes above the saved BP. That slot is outside the frame ofsyscallN, on the g0 stack, and it overwrote the return address ofruntime.mstart0. When a goroutine exited while locked to its thread after a goffi call,mstart0returned through that slot and the process crashed with SIGSEGV. #108 has the details and a reproduction.Changes
internal/syscall/syscall_unix_amd64.s: save and load the pointer atPTR_ADDRESS(SP)(lines 55 and 108). This matches the frame layout comment of the file and purego'ssyscallX.ffi/locked_exit_test.go:TestCallThenLockedExitmakes a call togetpidin each of 16 goroutines per round, for 10 rounds, and lets each goroutine exit with its thread locked. Without the fix the test binary crashes with SIGSEGV under bothCGO_ENABLED=0andCGO_ENABLED=1. With the fix it passes.Checks
On linux/amd64, go1.27.3:
go fmt ./...golangci-lint run --config=.golangci.yml ./...: 0 issuesgo test -race ./...(CGO_ENABLED=1) andCGO_ENABLED=0 go test ./...: passgo test -bench=BenchmarkGoffi -benchmem ./ffi:BenchmarkGoffiIntArgs72.8 ns/op, no change expected (the instruction count is the same)scripts/check-spdx.sh: OK🤖 Generated with Claude Code