Repository navigation
fix(libkernel): keep the process start block mapped when the main stack moves - #15698
Merged
oneandonlydean merged 1 commit intoNov 5, 2026
Merged
oneandonlydean merged 1 commit into
oneandonlydean merged 1 commit into
Conversation
…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.
12 tasks done
12 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On macOS under Rosetta,
Aps5StartGuestmoves 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, whereargvandenvironhave one andMoveHostStackalready uses it._NSGetExecutablePathcopies the path string out of the block on every call, so a call after the move reads unmapped memory and the process dies withKERN_PROTECTION_FAILURE.mainalready makes that call after the move:ShaderCacheDirectoryuses it on macOS (ShaderDiskCache.cpp:71-73), and libSceAgcDriver reaches it fromPipelineCache.cpp:48andHostReportSources.cpp:98on guest calls. A title does not have to call it either. The fault I traced is libxpc's: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
argvstill 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_moveasked for a 64 KiB guest mapping aligned down from a local ofmain, which covers those pages, so it fails with this change as it was. It now calls_NSGetExecutablePathafter 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.
maindoes 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 onmainb8efa62, andguest_main_stack_moveagain on 5b1d381 after the rebase, where none of the files this touches had changed. The merges of #7804 and #4630 are not clean; I keptmain'sgetrusagestructure andmain'sGuestRaiseException.cpp. Test targets outside libkernel that still fail to build on macOS were skipped withninja -k 0.guest_main_stack_move: passes. Negative control, with the new test andMainThreadMacOs.cppfrommain: the test dies with SIGSEGV;_NSGetExecutablePathis the first call it makes after the move.MoveHostStack: old stack[0x3095ee000, 0x309dea000), kept from0x309de8000, 8192 bytes, page size 4096.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 importAps5StartGuestand 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 inAps5StartGuest, onlyguest_main_stack_movestarted 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
MoveHostStackis a no-op; a title that wants a fixed mapping in the kept 8 KiB; any title run.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)