fix(libkernel): commit a reserved host stack before protecting it #5703
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5703"
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-14T07:29:23Z.
What
This complements #3965.
On Windows, guest threads run on host stacks that Windows reserves (0x210000 bytes by default) and commits on demand. When a title protects such a stack while its lowest pages are still only reserved, the mprotect emulation throws
Where the stack lay inside the arena bounds as a host region, the call took the arena commit path instead, and failed to split a placeholder that was never one.
Resident Evil Village (PPSA01556, 01.210.000) hits this at startup. One guest thread protects its whole 0x210000-byte host stack with 0xc2 while the lowest 0xd000 bytes are still only reserved.
With #3965 (49b51d205), which moves the main thread onto an arena-backed stack, the main thread runs on that stack as intended. Village still stops 44 s in, on this other thread. The emulation sees native protection 0x3, read/write (this trace prints only the native value):
DoMprotectcommits the reserved pages of a range the arena doesn't own (CommitReservedPages) before it protects them.mprotectuses the arena commit only for ranges the arena owns.KernelArena::Ownsmeans inside the arena and not overlapping a host region.The change applies to any host stack the arena doesn't own.
Tested
guest_kernel_stack_protect(core/libs/tests/GuestKernelStackProtect.cpp), on Windows:sceKernelIsStack) with 0xc2 and writes to it;STACK_SIZE_PARAM_IS_A_RESERVATION).main'sDirectMemory.cpp(abort), and passes 20 of 20 runs with the change;mainf87799aa3(https://github.com/EagleBearerYS/AnyPS5/actions/runs/38523213127). Windows: 1137 of 1137 pass, with the Vulkan tests skipped because the runner has no Vulkan device. Linux: 1114 of 1114 pass.main0b6d033b8. NVIDIA GeForce RTX 3070 Laptop GPU, driver 617.42; Windows 11 Home 10.0.26300; AMD Ryzen 9 5900HS, 15.4 GB of RAM.Plain
maincan't boot Village on this PC. Preparing every shader at registration gave a black screen for 10 minutes here, with private memory at 21.9 GB and heading for the commit limit. So these runs add two local commits and a one-line adaptation that prepare each shader at its first draw instead (APS5_RE8_LAZY_PREPARE=1), plusAPS5_NO_GPL=1. They change onlyShaderRegistry.cpp.#5688 is added too, for a separate startup abort.
Without this change, Village stops with the message above:
APS5_ADDRESS_COPY_MAX_MIB=512in place of #5688;With this change (and #5688, without #3965), there's no stop. Village shows the autosave notice and the VR question, then goes on to the title. It was still running 300-360 s in, when the test stopped it.
A trace of every protect that overlaps a thread's own stack showed 10 of them. Nine worker threads protect part of their own already committed stack. One protects its whole 0x210000-byte host stack, and the change commits its 0xd000 reserved bytes first:
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)main)Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T17:24:25Z.
This overlaps in effect with #5918, which is about the same Windows host-stack layout (a 0x210000 reservation with only the top committed). The two take different routes:
DoMprotectcommits the reserved pages of a non-arena range before protecting them, somprotectover the uncommitted bottom of a host stack succeeds.HostStackLimitsreports only the committed part of a precommitted host stack, so the bottom the title gets frompthread_attr_get_np/sceKernelIsStackis already committed, andmprotectthere succeeds without committing anything.With this PR alone, HotSpot's guard-page
mprotectin #5918's title (PPSA99869) would probably succeed too, at the old bottom address. With #5918 alone, Village's thread would protect only the committed range if it takes its bounds from the reported stack. Village protects the whole 0x210000 from what looks like the host's own layout, so #5918 may not cover it. If both land, the reported bounds shrink andmprotectalso commits, which is consistent but double-covers the Java case. It would help the maintainers if you two agreed whether both are needed. The deciding question is whether a title can observe the reported stack bottom (for example a GC scanning from it), which is the part only #5918 changes.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T17:32:22Z.
Answered on #5918 with our trace: https://github.com/boykopovar/AnyPS5/pull/5918#issuecomment-6174365139. We'll boot Village with #5918 in place of this PR.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:08:47Z.
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/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppcore/libs/tests/GuestKernelStackProtect.cppcore/libs/prx/libkernel/DirectMemory/DirectMemory.cppImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:08:54Z.
Checked the diff:
CommitReservedPagesonly commitsMEM_RESERVEregions,Ownskeeps the arena commit path off host-region ranges, and the newguest_kernel_stack_protectcases cover the main and a reserved-stack host thread. Linux build and ctest are green (the case is Windows-only); the fork run shows the test aborting without the change and passing with it, and @karimibrahim07 confirmed it on PPSA99869. It doesn't conflict with #5918. Merging after the Windows build.