Skip to content

Conversation

@qmuntal
Copy link
Contributor

@qmuntal qmuntal commented Oct 16, 2025

threadentry_trampoline is called from glibc (that is, C code) when creating a new thread. When the thread exists, threadentry_trampoline return back to glibc, which can perform some additional cleanups.

We currently aren't honoring the Linux C ABI. It specify some registers as calle-saved which are clobbered by the Go code. This can cause unexpected behaviors when returning to glibc when the thread exists, as the C compiler assumes some registers will have the same value as before the threadentry_trampoline function was called, but they don't.

This is a precondition to merge #344.
Also, might fix #304.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes ABI compliance issues in threadentry_trampoline functions across multiple architectures (amd64, arm64, loong64). The trampoline functions are called from C code (glibc) when creating threads, and must preserve callee-saved registers according to the Linux C ABI to prevent unexpected behavior when returning to glibc after thread exit.

Key Changes:

  • Implements proper preservation and restoration of callee-saved registers in threadentry_trampoline for all architectures
  • Adjusts stack frame management to accommodate saved registers
  • Ensures X15 register is zeroed on amd64 as required by Go's ABIInternal

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
internal/fakecgo/trampolines_amd64.s Uses existing PUSH/POP macros to save/restore callee-saved registers; zeros X15 for ABIInternal compliance
internal/fakecgo/trampolines_arm64.s Manually saves/restores callee-saved registers R19-R28, F8-F15, and frame pointer/link register with adjusted stack frame
internal/fakecgo/trampolines_loong64.s Manually saves/restores callee-saved registers R22-R31, F24-F31, and return address with adjusted stack frame

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@qmuntal qmuntal requested a review from hajimehoshi October 16, 2025 17:52
Copy link
Member

@hajimehoshi hajimehoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me in general

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@qmuntal qmuntal requested a review from hajimehoshi October 16, 2025 18:55
Copy link
Collaborator

@TotallyGamerJet TotallyGamerJet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the open comment this LGTM

Clarified comment regarding calling ABIInternal and zero register.
Copy link
Member

@hajimehoshi hajimehoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@hajimehoshi hajimehoshi merged commit 4ad96f6 into ebitengine:main Oct 16, 2025
41 checks passed
@hajimehoshi
Copy link
Member

Note that this didn't fix the NetBSD segv issue yet unfortunately.

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.

suspicious segmentation fault on NetBSD

3 participants