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

Merged
EagleBearerYS merged 1 commit from upstream-pr/5703 into main 2026-10-15 15:04:17 +00:00
EagleBearerYS commented 2026-10-14 07:29:23 +00:00 (Migrated from github.com)

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

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 (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):

[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

  • guest_kernel_stack_protect (core/libs/tests/GuestKernelStackProtect.cpp), on Windows:
    • the main thread protects its whole stack (from sceKernelIsStack) with 0xc2 and writes to it;
    • so does a host thread created with a reserved, uncommitted stack (STACK_SIZE_PARAM_IS_A_RESERVATION).
  • GitHub's runners, on my fork, on the same tree as this head (https://github.com/EagleBearerYS/AnyPS5/actions/runs/38523184207):
    • Windows: fails with main's DirectMemory.cpp (abort), and passes 20 of 20 runs with the change;
    • Linux: the case is Windows-only and passes either way;
    • the libc and libkernel suites pass.
  • Full Windows and Linux builds and tests on my fork, with the same tree merged into main f87799aa3 (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.
  • Village on current main 0b6d033b8. 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 main can'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), plus APS5_NO_GPL=1. They change only ShaderRegistry.cpp.

    • #5688 is added too, for a separate startup abort.

    • Without this change, Village stops with the message above:

      • 37 s in with #5688;
      • 93 s in with APS5_ADDRESS_COPY_MAX_MIB=512 in place of #5688;
      • 44 s in on thread 5340 with #3965 added (the trace above).
    • 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:

      [stack-protect] thread 23012 (stack 0x176b40000-0x176d50000): guest-stack protect 0x176b40000+0x210000 prot 0xc2, guest stack 0x176b40000-0x176d50000, own stack, reserved bytes committed 0xd000
      
  • python3 tools/check_conventions.py --base origin/main: passed.

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: #3965
  • Unblocks: PPSA01556 #5169 (a startup stop on current main)
  • GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU, driver 617.42, Windows 11
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4854480081 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5703). Originally posted by [EagleBearerYS](https://github.com/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 ``` 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 (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): ``` [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 - `guest_kernel_stack_protect` (`core/libs/tests/GuestKernelStackProtect.cpp`), on Windows: - the main thread protects its whole stack (from `sceKernelIsStack`) with 0xc2 and writes to it; - so does a host thread created with a reserved, uncommitted stack (`STACK_SIZE_PARAM_IS_A_RESERVATION`). - GitHub's runners, on my fork, on the same tree as this head (https://github.com/EagleBearerYS/AnyPS5/actions/runs/38523184207): - Windows: fails with `main`'s `DirectMemory.cpp` (abort), and passes 20 of 20 runs with the change; - Linux: the case is Windows-only and passes either way; - the libc and libkernel suites pass. - Full Windows and Linux builds and tests on my fork, with the same tree merged into `main` f87799aa3 (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. - **Village on current `main`** 0b6d033b8. 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 `main` can'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`), plus `APS5_NO_GPL=1`. They change only `ShaderRegistry.cpp`. - #5688 is added too, for a separate startup abort. - **Without this change,** Village stops with the message above: - 37 s in with #5688; - 93 s in with `APS5_ADDRESS_COPY_MAX_MIB=512` in place of #5688; - 44 s in on thread 5340 with #3965 added (the trace above). - **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: ``` [stack-protect] thread 23012 (stack 0x176b40000-0x176d50000): guest-stack protect 0x176b40000+0x210000 prot 0xc2, guest stack 0x176b40000-0x176d50000, own stack, reserved bytes committed 0xd000 ``` - `python3 tools/check_conventions.py --base origin/main`: passed. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Related: #3965 - [x] Unblocks: PPSA01556 #5169 (a startup stop on current `main`) - [x] GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU, driver 617.42, Windows 11 - [x] AI-assisted: yes
CnRJay commented 2026-10-14 17:24:25 +00:00 (Migrated from github.com)

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:

  • 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.
  • #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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174217907 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5703#issuecomment-6174217907). Originally posted by [CnRJay](https://github.com/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: - **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. - **#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 commented 2026-10-14 17:32:22 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174367169 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5703#issuecomment-6174367169). Originally posted by [EagleBearerYS](https://github.com/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.
github-actions[bot] commented 2026-10-15 12:08:47 +00:00 (Migrated from github.com)

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.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191689125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5703#issuecomment-6191689125). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:08:47Z. --- <!-- pr-overlap --> 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](https://www.github.com/boykopovar/AnyPS5/pull/3891) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4156](https://www.github.com/boykopovar/AnyPS5/pull/4156) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4396](https://www.github.com/boykopovar/AnyPS5/pull/4396) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4397](https://www.github.com/boykopovar/AnyPS5/pull/4397) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4422](https://www.github.com/boykopovar/AnyPS5/pull/4422) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4423](https://www.github.com/boykopovar/AnyPS5/pull/4423) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4424](https://www.github.com/boykopovar/AnyPS5/pull/4424) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4449](https://www.github.com/boykopovar/AnyPS5/pull/4449) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4520](https://www.github.com/boykopovar/AnyPS5/pull/4520) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4521](https://www.github.com/boykopovar/AnyPS5/pull/4521) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4522](https://www.github.com/boykopovar/AnyPS5/pull/4522) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4523](https://www.github.com/boykopovar/AnyPS5/pull/4523) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4566](https://www.github.com/boykopovar/AnyPS5/pull/4566) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4599](https://www.github.com/boykopovar/AnyPS5/pull/4599) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4718](https://www.github.com/boykopovar/AnyPS5/pull/4718) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4740](https://www.github.com/boykopovar/AnyPS5/pull/4740) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4749](https://www.github.com/boykopovar/AnyPS5/pull/4749) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4760](https://www.github.com/boykopovar/AnyPS5/pull/4760) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 4813](https://www.github.com/boykopovar/AnyPS5/pull/4813) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKernelStackProtect.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp` |
adriadam10 commented 2026-10-15 12:08:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:08:54Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191691028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5703#issuecomment-6191691028). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T12:08:54Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!5703
No description provided.