Repository navigation
fix(libkernel): move the macOS main stack out of the guest arena - #12126
morten-holm wants to merge 2 commits into
Conversation
Under Rosetta the main thread stack lies at about 12 GiB, inside the guest arena, where a title can map fixed addresses; a fixed mapping over it aborts with a host region overlap. - Aps5StartGuest moves the main thread onto a 16 MiB stack at 0x600000000000 when the old stack overlaps a host region of the arena. It copies argv, environ, the program name and the initial block off the old stack, then frees the old stack and releases its range to the arena. - The guest thread keeps the stack size of the original main thread. - The arena gets GuestArenaOverlapsHostRegion and GuestArenaReleaseHostRegion for POSIX hosts. - guest_main_stack_move starts the guest, then requires the main thread outside the arena, a fixed mapping where the old stack was, and argv and the environment intact. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
@morten-holm thanks, the move is careful: the replacement stack is checked to be outside the arena before use, the old range goes back to the arena, and Linux and Windows are untouched. One hardening request: |
adriadam10
left a comment
There was a problem hiding this comment.
Thanks, the approach is sound. One change is needed before merge, the one @oneandonlydean already asked for:
MainThreadMacOs.cpp:85-95(FindHostStack) grows the range downward over every adjacent region taggedVM_MEMORY_STACK, andServeMainQueue(:48-55) deallocates the whole merged range and releases it to the arena. A thread stack mapped right below the main stack has the same tag and would be freed with it. Bound the range withpthread_get_stackaddr_np/pthread_get_stacksize_npof the main thread, intersected with the region found, so only the main stack is released. Extending the test to put a secondVM_MEMORY_STACKregion right below the main stack would pin it.
Everything else checked out against the diff (argv/environ/initial block copy, skip conditions, debt entry, non-Apple paths untouched). The macOS build and the new test could not be run on our side.
… the arena Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
@oneandonlydean @adriadam10 Fixed in
AI-assisted: yes (Claude Code) |
What
Under Rosetta 2 the main thread stack lies at about 12 GiB, inside the guest arena. A title can map fixed addresses there, and a fixed mapping over the stack aborts with a host region overlap.
Aps5StartGuestmoves the main thread onto a 16 MiB stack at0x600000000000when the old stack overlaps a host region of the arena. It first copiesargv,environ, the program name and the initial block off the old stack. Then it frees the old stack and releases its range to the arena.GuestArenaOverlapsHostRegion_nid_no_patchandGuestArenaReleaseHostRegion_nid_no_patchfor POSIX hosts.docs/dev/debt/libkernel.mdrecords this.Test
guest_main_stack_move(macOS only): it starts the guest, then requires that the main thread is outside the arena, that a fixed guest mapping fits where the old stack was, and thatargvand the environment are intact.Tested
mainf759e165d9.libkerneldoes not build on macOS frommainalone (link.h,RUSAGE_THREAD,sched_getcpu,REG_*inRtld.cpp,Process.cpp,Exception.cpp), and feat(libkernel): direct memory mappings on macOS hosts #7631, one of the open macOS PRs that fix it, conflicts withmain. So I builtlibcfrom this branch and linkedMainThreadMacOs.cppandGuestMainStackMove.cppby hand against it.main'sMainThreadMacOs.cpp: exit code 1 in 3 of 3 runs, atthe main thread stack is outside the guest arena.python3 tools/check_conventions.py --base origin/mainpasses.fixed guest mapping 0x300000000+0x7e000000 overlaps a host region in the guest arena. With the move active, the same title gets past launch and runs draw packets for the 44 s of the run. This title run used a local equivalent of the move in an older tree, not this branch'sMainThreadMacOs.cpp, which could not be linked into a full build (see above). It shows the abort and that moving the stack removes it.libkerneldoes not build here), Linux and Windows (the code is__APPLE__only, apart from the two arena functions, which are under#ifndef _WIN32), a title run on this base.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)🤖 Generated with Claude Code