fix(libkernel): measure the guest stack on Apple #12112

Closed
wanderingbug wants to merge 1 commit from upstream-pr/12112 into main
wanderingbug commented 2026-10-27 21:24:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wanderingbug on 2026-10-27T21:24:55Z.


What

Darwin has no pthread_getattr_np. Current main still calls it from HostStackLimits, so that function does not compile on macOS. This PR reads the host stack with pthread_get_stackaddr_np and pthread_get_stacksize_np. The address is the high end of the stack.

CallGuestOnMeasuredStack is [[gnu::noinline]]. It samples the stack pointer, registers that window, calls Aps5CallGuestEntry, and emits a memory clobber so Clang does not tail-call it. The extra 320 KiB host margin is added only on Apple. GuestThreadSelf prints the worker window after join. Windows guard-page paths stay as they are.

Open PR #7804 also queries pthread_get_stackaddr_np and pthread_get_stacksize_np for host stack limits, and does not add CallGuestOnMeasuredStack. If #7804 merges first, the duplicate HostStackLimits lines can be dropped while keeping CallGuestOnMeasuredStack.

Tested

Linux, gcc 13, Release, against upstream main e0cd1259a9. Rebuilt libkernel. Passed guest_thread_self and guest_pthread_stack. python3 tools/check_conventions.py --base upstream/main passed.

Guest smoke on the combined reference f91b74b1c passed on Apple M1 Max, macOS 27, Rosetta 2.

Checklist

  • Based on current main (e0cd1259a9). Open PR #7804 also queries pthread_get_stackaddr_np and pthread_get_stacksize_np for host stack limits, and does not add CallGuestOnMeasuredStack.
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5041356300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12112). Originally posted by [wanderingbug](https://github.com/wanderingbug) on 2026-10-27T21:24:55Z. --- ### What Darwin has no `pthread_getattr_np`. Current main still calls it from `HostStackLimits`, so that function does not compile on macOS. This PR reads the host stack with `pthread_get_stackaddr_np` and `pthread_get_stacksize_np`. The address is the high end of the stack. `CallGuestOnMeasuredStack` is `[[gnu::noinline]]`. It samples the stack pointer, registers that window, calls `Aps5CallGuestEntry`, and emits a memory clobber so Clang does not tail-call it. The extra 320 KiB host margin is added only on Apple. `GuestThreadSelf` prints the worker window after join. Windows guard-page paths stay as they are. Open PR #7804 also queries `pthread_get_stackaddr_np` and `pthread_get_stacksize_np` for host stack limits, and does not add `CallGuestOnMeasuredStack`. If #7804 merges first, the duplicate `HostStackLimits` lines can be dropped while keeping `CallGuestOnMeasuredStack`. ### Tested Linux, gcc 13, Release, against upstream main e0cd1259a95e6dfaf97f8f73e74410bbc5d8d199. Rebuilt libkernel. Passed guest_thread_self and guest_pthread_stack. `python3 tools/check_conventions.py --base upstream/main` passed. Guest smoke on the combined reference f91b74b1c passed on Apple M1 Max, macOS 27, Rosetta 2. ### Checklist - [x] Based on current `main` (e0cd1259a95e6dfaf97f8f73e74410bbc5d8d199). Open PR #7804 also queries `pthread_get_stackaddr_np` and `pthread_get_stacksize_np` for host stack limits, and does not add `CallGuestOnMeasuredStack`. - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation .md files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12112
No description provided.