fix(libkernel): keep the process start block mapped when the main stack moves #15698

Merged
javierUDP merged 1 commit from upstream-pr/15698 into main 2026-11-05 12:29:36 +00:00
javierUDP commented 2026-11-04 07:32:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by javierUDP on 2026-11-04T07:32:04Z.


What

On macOS under Rosetta, Aps5StartGuest moves the main thread off the host stack and releases the old range to the guest arena (#12126), because a title can map fixed addresses there. dyld keeps its own pointers into the process start block at the top of that range and offers no call that redirects them, where argv and environ have one and MoveHostStack already uses it. _NSGetExecutablePath copies the path string out of the block on every call, so a call after the move reads unmapped memory and the process dies with KERN_PROTECTION_FAILURE.

main already makes that call after the move: ShaderCacheDirectory uses it on macOS (ShaderDiskCache.cpp:71-73), and libSceAgcDriver reaches it from PipelineCache.cpp:48 and HostReportSources.cpp:98 on guest calls. A title does not have to call it either. The fault I traced is libxpc's:

_platform_strlcpy        EXC_BAD_ACCESS (KERN_PROTECTION_FAILURE), inside the released stack
_NSGetExecutablePath
_xpc_copy_own_bundle
xpc_connection_resume

so anything that opens an XPC connection or touches Cocoa dies there.

This releases the old stack only up to the pages that hold the block, and only while argv still points into the stack. Here that keeps the top two 4 KiB pages, 8192 bytes of the 8372224-byte stack: a title that fixes a mapping over them now fails with a host region overlap, and the rest of the old stack is free as before. Keeping both is not possible, because the pointers into the block are dyld's and absolute, so the block cannot move.

guest_main_stack_move asked for a 64 KiB guest mapping aligned down from a local of main, which covers those pages, so it fails with this change as it was. It now calls _NSGetExecutablePath after the move, probes the 64 KiB below the kept pages, and requires the kept pages to be refused.

Tested

macOS 27.2 (Apple M5), x86-64 under Rosetta, Release, Ninja, AppleClang.

main does not build on macOS by itself, so this ran with this change and the four open PRs the macOS build needs merged in (#11049, #11119, #7804, #4630): everything below on main b8efa6292a, and guest_main_stack_move again on 5b1d381d5a after the rebase, where none of the files this touches had changed. The merges of #7804 and #4630 are not clean; I kept main's getrusage structure and main's GuestRaiseException.cpp. Test targets outside libkernel that still fail to build on macOS were skipped with ninja -k 0.

  • guest_main_stack_move: passes. Negative control, with the new test and MainThreadMacOs.cpp from main: the test dies with SIGSEGV; _NSGetExecutablePath is the first call it makes after the move.
  • The amount kept, read from a temporary print in MoveHostStack: old stack [0x3095ee000, 0x309dea000), kept from 0x309de8000, 8192 bytes, page size 4096.
  • CPU tests (2458, ctest -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu'), with and without this change on the same tree: the same failures, apart from three tests that do not import Aps5StartGuest and are unstable here (guest_hmd_setup_dialog, guest_refused_connect, guest_socket_read_write: PPPPP, FFFFF and FFPPP over five reruns). With a temporary marker in Aps5StartGuest, only guest_main_stack_move started a guest in that set; the relinker fixture tests report "native execution skipped" on this host. So that run shows nothing else changed, not that the moved path works in many cases.
  • video_out_flip_*: 2 pass, 26 skip (no Vulkan device here).

Not verified: arm64, where MoveHostStack is a no-op; a title that wants a fixed mapping in the kept 8 KiB; any title run.

Checklist

  • Based on current main; no other open PR implements the same functions
  • 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:5150649575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15698). Originally posted by [javierUDP](https://github.com/javierUDP) on 2026-11-04T07:32:04Z. --- ### What On macOS under Rosetta, `Aps5StartGuest` moves the main thread off the host stack and releases the old range to the guest arena (#12126), because a title can map fixed addresses there. dyld keeps its own pointers into the process start block at the top of that range and offers no call that redirects them, where `argv` and `environ` have one and `MoveHostStack` already uses it. `_NSGetExecutablePath` copies the path string out of the block on every call, so a call after the move reads unmapped memory and the process dies with `KERN_PROTECTION_FAILURE`. `main` already makes that call after the move: `ShaderCacheDirectory` uses it on macOS ([ShaderDiskCache.cpp:71-73](https://github.com/boykopovar/AnyPS5/blob/5b1d381d5a/core/shader/recompiler/ShaderDiskCache.cpp#L71-L73)), and libSceAgcDriver reaches it from `PipelineCache.cpp:48` and `HostReportSources.cpp:98` on guest calls. A title does not have to call it either. The fault I traced is libxpc's: ``` _platform_strlcpy EXC_BAD_ACCESS (KERN_PROTECTION_FAILURE), inside the released stack _NSGetExecutablePath _xpc_copy_own_bundle xpc_connection_resume ``` so anything that opens an XPC connection or touches Cocoa dies there. This releases the old stack only up to the pages that hold the block, and only while `argv` still points into the stack. Here that keeps the top two 4 KiB pages, 8192 bytes of the 8372224-byte stack: a title that fixes a mapping over them now fails with a host region overlap, and the rest of the old stack is free as before. Keeping both is not possible, because the pointers into the block are dyld's and absolute, so the block cannot move. `guest_main_stack_move` asked for a 64 KiB guest mapping aligned down from a local of `main`, which covers those pages, so it fails with this change as it was. It now calls `_NSGetExecutablePath` after the move, probes the 64 KiB below the kept pages, and requires the kept pages to be refused. ### Tested macOS 27.2 (Apple M5), x86-64 under Rosetta, Release, Ninja, AppleClang. `main` does not build on macOS by itself, so this ran with this change and the four open PRs the macOS build needs merged in (#11049, #11119, #7804, #4630): everything below on `main` b8efa6292a, and `guest_main_stack_move` again on 5b1d381d5a after the rebase, where none of the files this touches had changed. The merges of #7804 and #4630 are not clean; I kept `main`'s `getrusage` structure and `main`'s `GuestRaiseException.cpp`. Test targets outside libkernel that still fail to build on macOS were skipped with `ninja -k 0`. - `guest_main_stack_move`: passes. Negative control, with the new test and `MainThreadMacOs.cpp` from `main`: the test dies with SIGSEGV; `_NSGetExecutablePath` is the first call it makes after the move. - The amount kept, read from a temporary print in `MoveHostStack`: old stack `[0x3095ee000, 0x309dea000)`, kept from `0x309de8000`, 8192 bytes, page size 4096. - CPU tests (2458, `ctest -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu'`), with and without this change on the same tree: the same failures, apart from three tests that do not import `Aps5StartGuest` and are unstable here (`guest_hmd_setup_dialog`, `guest_refused_connect`, `guest_socket_read_write`: PPPPP, FFFFF and FFPPP over five reruns). With a temporary marker in `Aps5StartGuest`, only `guest_main_stack_move` started a guest in that set; the relinker fixture tests report "native execution skipped" on this host. So that run shows nothing else changed, not that the moved path works in many cases. - `video_out_flip_*`: 2 pass, 26 skip (no Vulkan device here). Not verified: arm64, where `MoveHostStack` is a no-op; a title that wants a fixed mapping in the kept 8 KiB; any title run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/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
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!15698
No description provided.