Skip to content

fix(libkernel): commit a reserved host stack before protecting it - #5703

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
EagleBearerYS:fix/libkernel-protect-reserved-host-stack
Oct 15, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
EagleBearerYS:fix/libkernel-protect-reserved-host-stack

Conversation

@EagleBearerYS

@EagleBearerYS EagleBearerYS commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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

mprotect of uncommitted memory outside the guest arena

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 (49b51d2), 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):

[mprotect] thread 5340: 0x1b0c00000+0x210000 prot 0x3; uncommitted region 0x1b0c00000+0xd000 state 0x2000 type 0x20000 allocation base 0x1b0c00000; this thread's stack 0x1b0c10000-0x1b0e10000
  • Committing: on Windows, DoMprotect commits the reserved pages of a range the arena doesn't own (CommitReservedPages) before it protects them.
  • Arena commits: mprotect uses the arena commit only for ranges the arena owns. KernelArena::Owns means inside the arena and not overlapping a host region.

The change applies to any host stack the arena doesn't own.

Tested

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Related: fix(windows): enter guest startup on an arena-backed main stack #3965
  • Unblocks: PPSA01556 Resident Evil Village (PPSA01556) #5169 (a startup stop on current main)
  • GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU, driver 617.42, Windows 11
  • AI-assisted: yes

Guest threads run on host stacks that Windows reserves (0x210000 bytes
by default) and commits on demand. Resident Evil Village protects one
thread's whole host stack with 0xc2 at startup while its lowest 0xd000
bytes are still only reserved, so the Windows mprotect emulation threw.
When such a stack sat inside the arena bounds as a host region, the call
took the arena commit path and failed to split a placeholder that was
never one.

Commit the reserved pages of a host range before protecting it, and only
use the arena commit for ranges the arena owns.
@CnRJay

CnRJay commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

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:

  • This PR: DoMprotect commits the reserved pages of a non-arena range before protecting them, so mprotect over the uncommitted bottom of a host stack succeeds.
  • fix(libkernel): report the precommitted stack of an adopted thread on Windows #5918: HostStackLimits reports only the committed part of a precommitted host stack, so the bottom the title gets from pthread_attr_get_np / sceKernelIsStack is already committed, and mprotect there succeeds without committing anything.

With this PR alone, HotSpot's guard-page mprotect in #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 and mprotect also 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.

@EagleBearerYS

Copy link
Copy Markdown
Contributor Author

Answered on #5918 with our trace: #5918 (comment). We'll boot Village with #5918 in place of this PR.

@adriadam10 adriadam10 self-assigned this Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 3891 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4156 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4396 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4397 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4422 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4423 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4424 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4449 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4520 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4521 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4522 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4523 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4566 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4599 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4718 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4740 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4749 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4760 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 4813 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 5021 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
PR 6044 core/libs/tests/GuestKernelStackProtect.cpp
PR 6222 core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff: CommitReservedPages only commits MEM_RESERVE regions, Owns keeps the arena commit path off host-region ranges, and the new guest_kernel_stack_protect cases 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.

@adriadam10
adriadam10 merged commit d8862a6 into boykopovar:main Oct 15, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants