fix(libkernel): keep a protected host stack's guard page, and protect the Linux main stack #6443
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6443"
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 EagleBearerYS on 2026-10-15T15:36:33Z.
What
Follow-up to #5703, which commits the reserved pages of a host stack before protecting it on Windows. This closes the three gaps that commit leaves.
1. Windows: the guard page. Committing every reserved page of the range also cleared the guard page's
PAGE_GUARD:A protect now leaves the guard page and the reserved pages below it as they are, and commits and protects only what lies above them. Reserved pages above the guard are still committed first, and the protection is recorded for the whole range. This also drops the commit charge #5703 adds, up to the whole reserve of each thread.
Resident Evil Village (PPSA01556, 01.210.000) does this at startup: thread 23012 protects its whole 0x210000-byte host stack with 0xc2 while its lowest 0xd000 bytes are still reserved, so with #5703 alone that thread loses its guard page. A trace of every protect that overlaps a thread's own stack, on
main0b6d033b8with #5703's commit:The other nine protect part of their own stack that is already committed.
2. Windows: the bottom of the reserve. HotSpot makes its stack guard zones with a read/write protect of the lowest pages of a thread's stack, then
PROT_NONE. On native Windows it commits them there (os::pd_create_stack_guard_pages, thenos::guard_memory, in OpenJDK'sos_windows.cpp). Under change 1 alone such a protect would only be recorded: the pages would stay reserved, so they couldn't be written after the read/write protect.3. Linux: the main thread's stack.
sceKernelIsStackreports the rangepthread_getattr_npgives, which for the main thread is sized byRLIMIT_STACKand mostly unmapped (0x7fd000 bytes, 0x23000 of them mapped, on glibc 2.39). A protect of that range failed withENOMEM, andsceKernelMprotectlet the exception through, which stopped the title. When the stack branch'smprotectfails withENOMEM, the unmapped runs are now mapped read/write in place (MAP_FIXED_NOREPLACE, no swap reserved) and the protection is applied again. The usual call still costs onemprotect./proc/self/mapson each call and cuts it at the mapping below the stack, so after the first protect it reported a smaller stack that started above the mapped pages, and a second protect of the same pages failed. Each thread's first queried range is now kept, so the main thread's range is the one from before any protect. A thread's stack never moves, so this changes nothing for other threads.msyncper run instead of one per page. Underulimit -s unlimitedglibc reports the main stack down to the heap (tens of TiB), and a whole-stack protect probed it page by page for hours; it now takes a few dozen calls.The commits:
fix(libkernel): map the unmapped pages of a host stack before protecting it on Linux (3).test(libkernel): protect the whole main-thread stack on Linux too.fix(libkernel): keep a host stack's guard page and reserve when protecting it on Windows (1).test(libkernel): protect the bottom of a reserved host stack, then make it inaccessible (passes at its own commit: both protects return 0, the guard page stays, and the stack still grows).fix(libkernel): commit a protect at the bottom of a host stack's reserve (2), with the test's checks that the zone is writable, then inaccessible, and how far it holds.fix(libkernel): map a host stack's unmapped pages in runs on Linux (3, runs), with an unlimited-stack check.fix(libkernel): keep a host thread's stack range once queried on Linux (3, the range is kept), with a check that protects the bottom of the main stack twice.fix(libkernel): map a protected main stack up to its top on Linux (3, up to the top), with read-only and re-protected bottom checks, and the debt entry's Linux sentence.fix(libkernel): keep the cached host stack range in two scalar thread-locals (3, the range is kept), after a merge ofmain. Commit 7 kept the range in one 16-bytethread_local std::pair, which GCC stores with an aligned SSE move; in CI's Linux linklibkernel.prx's TLS segment isn't 16-byte aligned, so a library that loadslibkernel.prxwithdlopengot that pair 8 bytes off and faulted on the first store. Two 8-byte thread-locals carry no such alignment.Scope:
PROT_NONEincluded, doesn't hold, where on the console every page of the reported range takes it; the debt file records this. #5918 reports an adopted thread's committed range instead, which keeps such pages out of the range.mprotectdoesn't map to an errno, so FreeBSD'sENOMEMfor a failed commit isn't reproduced. Pages committed or mapped before a later step throws stay read/write; nothing rolls them back.Tested
guest_kernel_stack_protect(core/libs/tests/GuestKernelStackProtect.cpp):sceKernelIsStack) with 0xc2. Host threads created with a reserved, uncommitted stack (STACK_SIZE_PARAM_IS_A_RESERVATION) protect their whole stack, and their stack from the middle of the reserve.PROT_NONEprotect of the lowest page leaves all of that as it is.PROT_NONEmakes them inaccessible and unreadable. The stack then grows through the guard to one guard region above them: the guard sits right on the zone, the zone is intact, and the stack limit matches. One page further, the guard's lowest page moves into the zone's top page as read/write guard, while the rest of the zone stays inaccessible.PROT_NONE, then read/write:sceKernelIsStackreports the same range after each, and writes just above the zone and 256 KiB above it succeed;DirectMemory.cpp, the test fails on Windows in every one of 3 runs, at the check that the read/write-protected zone is writable; with commit 5 it passes 20 of 20 on both (run).mprotectreturns an error, and on Windows at the check that a guard page is still above the reserved pages. With commits 1-3 it passes 20 of 20 on both (run). With commits 1-2 it still fails on Windows at the same guard-page check and passes on Linux; with commit 3, 20 of 20 on both (run).guest_kqueueon Windows, which fails onmainin the same runs.main92e50418f3, which has #5703: withmain'sDirectMemory.cppthe test fails in every one of 3 runs, on Linux at the main thread's whole-stack protect and on Windows at the check that a guard page is still above the reserved pages. With this PR it passes 20 of 20 on both (run). Commit 4 alone passes 5 of 5 on both (run).check_skips.py, and the write-watch, relinker and register-context jobs (macOS and Linux) pass.msynccalls; commit 7's check fails at "sceKernelIsStackafter a protect must report the same range"; commit 8's two checks die with SIGSEGV at the write just above the zone. Each passes at its own commit, the test passes 10 of 10 at commit 8, and the libc and libkernel suites pass apart from five network tests that need IPv6, which that machine lacks and which fail the same at commit 5.sceKernelIsStackafter a protect must report the same range"; with commits 6-8 it passes 5 of 5. On Windows, where the new checks don't run, it passes with and without them. The only suite failure isguest_kqueueon Windows, with and without the change.AboveStackGuard,CommitReservedPagesandDoMprotect's stack branch) is identical to commit 3's; the tree carries other local changes on an oldermain. NVIDIA GeForce RTX 3070 Laptop GPU, driver 617.42; Windows 11 Home 10.0.26300. Resident Evil Village went from the start-up dialogs to the title (0.28 fps) and was held there 5.5 min, with no access violation or stack overflow in any thread. The 17,734-line log has no FATAL orabort()line.187e5fc0ac) on #5918. Its stack guard zones take the bottom-of-reserve path, which commits and protects them as that commit does: they behave as with #5703 until the stack grows to within one guard region of them (12 KiB on GitHub's runners). karimibrahim07 then ran commit 5 merged ontomain5d9dcc42d(review): VM creation gets as far as with #5703, HotSpot commits the main thread's guard zones at the bottom of the reservation, andJNI_CreateJavaVMreturns 0;guest_kernel_stack_protectandguest_thread_selfpass on that tree.maine1ac3300d3: without it,guest_memory_lifetimefails 5 of 5 ("memory lifetime: child failed during native exit (status=11)", a SIGSEGV inHostStackLimitscalled fromDoMprotectafter the child's module loadedlibkernel.prxwithdlopen), whilemainpasses it 5 of 5. With it, the test passes 10 of 10 andguest_kernel_stack_protect10 of 10. The libkernel suite passes 10 times under--parallel 4apart from two tests that fail onmainon that machine (a shared-memory test that fails as root, andguest_tcp, which needs IPv6), and the full ctest run (1,328 tests) fails only the tests that fail onmainthere.guest_kernel_stack_protectand theguest_thread*andguest_pthread*tests pass 5 of 5 on Windows and on Linux, andguest_memory_lifetime5 of 5 on Linux (it isn't built on Windows). The libc and libkernel suites pass on Linux. On Windows their only failure isguest_kqueue, which the same verify script fails on a branch without this PR too (run).main6a32550fd1(run): 1,366 of 1,366 pass on Windows,guest_kqueueincluded, and the 505 skipped are all expected bycheck_skips.py.python3 tools/check_conventions.py --base origin/main: passed.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)Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-15T15:37:58Z.
@karimibrahim07, you ran #5703's commit on PPSA99869 (#5918). Could you run this one too, at
5796ddfcfd, when you have a moment? Its guard-zone path at the bottom of a thread's stack commits and protects the zones as #5703 did. What changes is that the guard page and the reserve above the zones now stay, so the zones behave as they did with #5703 until the stack grows to within one guard region of them. A run that gets as far as it did with #5703, or a log line if it doesn't, would confirm that.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T15:45:46Z.
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.
core/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mdcore/libs/tests/GuestKernelStackProtect.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/tests/GuestKernelStackProtect.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/tests/GuestKernelStackProtect.cppdocs/dev/debt/libkernel.mdcore/libs/prx/libkernel/Pthread/src/Thread.cppdocs/dev/debt/libkernel.mddocs/dev/debt/libkernel.mdImported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-16T07:35:58Z.
Review: COMMENTED
@EagleBearerYS here's PPSA99869 with this PR. Head
f697e1a41merged ontomain5d9dcc42d(clean merge; it also merges cleanly with #6534). Windows 11 Pro (26300), Ryzen 5 3600, RTX 3060, WinLibs GCC 15.2.0 r7, Ninja Release. Same setup as in #5918 (--windows --to-intel --trap-sdk-syscall-stubs --registry). Two runs:DONE: Native RW memory commit addr=0x400000 bytes=49152 mode=3 errno=0. There is noAttempt to allocate stack guard pages failed, andJNI_CreateJavaVM returned 0.DestroyJavaVM complete, with the usualAttempt to deallocate stack guard pages failedfor the main thread (0x400000) and one VM thread. #6534 addresses those.main5d9dcc42d, same day and machine, times out the same way, while the host resolvesexample.comfine. With #5703 it passed here two days ago. The LAN row has no helper and wasn't run.guest_kernel_stack_protectandguest_thread_selfpass on this tree.AI-assisted: yes. I ran the builds, the tests and the title above.
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-16T07:49:05Z.
Thanks for running it on PPSA99869, @karimibrahim07. I've added your run to the Tested section.