fix(libkernel): keep the process start block mapped when the main stack moves #15698
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15698"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by javierUDP on 2026-11-04T07:32:04Z.
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 onmainb8efa6292a, andguest_main_stack_moveagain on5b1d381d5aafter 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)