Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
javierUDP:fix/macos-exec-path-after-stack-move
Nov 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
javierUDP:fix/macos-exec-path-after-stack-move

Conversation

@javierUDP

Copy link
Copy Markdown
Contributor

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 b8efa62, and guest_main_stack_move again on 5b1d381 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

…ck moves

On macOS under Rosetta, Aps5StartGuest moves the main thread off the host
stack and releases the old range to the guest arena. 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:
_NSGetExecutablePath copies the path string out of the block on every
call, so the first call after the move read unmapped memory and the
process died with KERN_PROTECTION_FAILURE. A title does not have to call
it: libxpc reaches it from xpc_connection_resume, and ShaderCacheDirectory
calls it for the shader disk cache.

Release the old stack only up to the pages that hold the block, and only
while argv still points into the stack. They are the top two pages, 8 KiB
of the 8372224-byte stack here, so a title that fixes a mapping over that
range now fails with a host region overlap; the rest is free as before.

guest_main_stack_move asked for a 64 KiB guest mapping aligned down from
a local of main, which falls inside the kept pages. It now probes the
64 KiB below them, requires the kept pages to be refused, and calls
_NSGetExecutablePath after the move, which segfaults without this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants