Skip to content

internal/fakecgo: threadentry overwrites a register saved by threadentry_trampoline (amd64, arm, ppc64le) #571

Done

Description

@AnyCPU

PureGo Version

v0.11.2+

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • illumos
  • NetBSD
  • Android
  • iOS

Go Version (go version)

go1.25.0+

What steps will reproduce the problem?

Before #568, on an Intel Mac, an unoptimized build without cgo hung in TestThreadEntryReturn:

CGO_ENABLED=0 go test "-gcflags=all=-N -l" -run '^TestThreadEntryReturn$' -count=1 ./internal/fakecgo/

CI runs this test unoptimized on Linux, FreeBSD, NetBSD and illumos amd64, where it passes. Its macOS runner is arm64. Nothing fails on arm or ppc64le either.

Each of the three PRs first came with a probe test, which showed the wrong register in a default build on its architecture. The tests have since been removed from all three (see the comments).

What is the expected result?

The test passes, and threadentry_trampoline returns to libc with every callee-saved register as it received it.

What happens instead?

threadentry_trampoline saves the C caller's callee-saved registers on its stack and calls threadentry. On amd64 (until #568), arm and ppc64le, one of them is saved in a slot that threadentry is allowed to write, so libc gets a wrong value back when the thread exits:

Register Saved in Overwritten Effect Fix
amd64 BX the spill slot of threadentry's argument when threadentry spills it, as -N builds do before #568: hang on darwin, nothing visible elsewhere #568 (merged)
arm R4 threadentry's result slot always: return nil stores 0 nothing visible with glibc #569
ppc64le R14 the spill slot of threadentry's argument when threadentry spills it nothing visible: optimized builds don't spill, and -N builds don't link #570

On macOS, _pthread_start keeps the thread's pthread_t in rbx and passes it to _pthread_exit after the start routine returns. With the wrong value the thread faulted while holding a libpthread lock, and the process hung. #568 has the details.

Anything else you feel useful to add?

Activity

  1. added
    bug
    on Nov 17, 2026
  2. AnyCPU commented on Nov 17, 2026

    @AnyCPU
    ContributorAuthor

    @hajimehoshi fyi,

    by reading only, s390x's threadentry_trampoline doesn't save F8-F15, which the s390x C ABI makes callee-saved (purego's crosscall2 and Go's gcc_s390x.S do save them). Like ppc64le's V20-V31, it's a missing save, not an overlap.

    a separate report, if wanted.

  3. added this to the v0.12.0 milestone on Nov 17, 2026
  4. AnyCPU commented on Nov 23, 2026

    @AnyCPU
    ContributorAuthor

    @hajimehoshi @TotallyGamerJet shall i remove tests for ppc and arm too?

  5. hajimehoshi commented on Nov 23, 2026

    @hajimehoshi
    Member

    @AnyCPU I think yes.

    @TotallyGamerJet Wdyt?

  6. TotallyGamerJet commented on Nov 23, 2026

    @TotallyGamerJet
    Collaborator

    Yes they can be deleted too

  7. AnyCPU commented on Nov 23, 2026

    @AnyCPU
    ContributorAuthor
  8. added a commit that references this issue on Nov 24, 2026
    01e51a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions