fix(libc): refuse Windows read traps over host regions in the guest arena #28028

Open
Plyxen wants to merge 2 commits from upstream-pr/28028 into main
Plyxen commented 2026-11-28 05:01:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-28T05:01:48Z.


What

windows_read_trap_unknown fails on Windows main (a163d4b886) every run:

memory outside the guest arena was trapped

The Windows ReadTraps::TrapReads only checks that the range falls inside the guest arena's address span (GuestArenaContains_nid_postfix). On Windows that span runs from 0x10000 to ApplicationAreaEnd, and the arena reserves only what was free when it was created. Whatever the host already had mapped there stays a host region (_holes, then _hostRegions). When the host frees one of those regions, a later host VirtualAlloc can land in it. The test's VirtualAlloc page landed at 0x340000, 0xab0000 and 0xa0000 in three runs. A probe in the test gave GuestArenaContains = 1 and GuestArenaHostRegionOverlaps = 1 for all three.

TrapReads accepted that page and set it to PAGE_NOACCESS. The next host read of it would fault into the trap handler, and the resolver would store guest bytes over host memory.

TrapReads now also refuses a range that overlaps a host region (GuestArenaHostRegionOverlaps_nid_postfix), as OwnedByGuest in GuestArena.cpp already does for guest stores. On Windows the host region list is built once when the arena is created and never changes after that (ReleaseHostRegion is not built on Windows). The lookup is the binary search of HostRegionIndex, so trapping a guest range costs one search more. Guest ranges never overlap a host region, so their traps are unchanged.

The test landed with 650df957b4 (2026-11-24), after the last Windows Build run that finished (ab6fe4a981), so CI has not run it yet.

Tested

Windows 11, WinLibs GCC 15.2, Release, on main a163d4b886. I ran the 14 windows_read_trap_* and windows_write_watch* tests with ctest -R '^(windows_read_trap_.*|windows_write_watch.*)$' --repeat until-fail:10:

  • main: windows_read_trap_unknown fails on its first run.
  • this change: it passes all 10 runs, and so do the other read trap tests.
  • On both, windows_write_watch failed once. That is the unrelated flaky case fixed in #28027.

I also ran the read trap and write watch GPU tests, 3 runs each on the NVIDIA GeForce RTX 5080 and the AMD Radeon gfx1036 iGPU, with VK_LAYER_KHRONOS_validation inserted:

  • They pass on main and with this change.
  • Neither run logs a VUID- error.

The tests covered were agc_read_traps_over_host_pointer_imports_driver, agc_driver_label_trap_committer_resolve, agc_write_tracking* and agc_write_watch_coverage*.

The full CPU suite (5,120 tests), with this change applied to main a163d4b886 in a batch with other reviews, has no failure from this change. The three that failed are frame_capture_minimize (a load timeout that also fails on main), guest_np_common_runtime (from another PR in the batch) and guest_webrtc_null_socket_server, a load flake that passes 30/30 when rerun. windows_read_trap_unknown passes in it.

The branch is rebased onto main 4c7578e0a4. None of the 30 commits in between touch this file.

python3 tools/pr_overlap.py --names TrapReads reports no matching export changes, because TrapReads is not an export.

python3 tools/pr_overlap.py --branch fix/libc-windows-read-trap-host-regions merged all 8 open PRs that share GuestWriteWatch.cpp into the base. It could not compare two of them, because they conflict with main, so I compared those by hand:

  • #12792 changes the Linux Watch and GuestWriteWatchCollect*.
  • #20580 changes the macOS read trap path.

Neither touches the Windows ReadTraps.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • On Windows, read-trap operations now reject memory ranges that overlap host memory. Ranges must also remain within the guest arena and avoid protected Windows mappings, preventing traps from being applied to ranges that cross guest and host memory regions.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5536866725 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28028). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-28T05:01:48Z. --- ### What `windows_read_trap_unknown` fails on Windows `main` (`a163d4b886`) every run: ``` memory outside the guest arena was trapped ``` The Windows `ReadTraps::TrapReads` only checks that the range falls inside the guest arena's address span (`GuestArenaContains_nid_postfix`). On Windows that span runs from `0x10000` to `ApplicationAreaEnd`, and the arena reserves only what was free when it was created. Whatever the host already had mapped there stays a host region (`_holes`, then `_hostRegions`). When the host frees one of those regions, a later host `VirtualAlloc` can land in it. The test's `VirtualAlloc` page landed at `0x340000`, `0xab0000` and `0xa0000` in three runs. A probe in the test gave `GuestArenaContains = 1` and `GuestArenaHostRegionOverlaps = 1` for all three. `TrapReads` accepted that page and set it to `PAGE_NOACCESS`. The next host read of it would fault into the trap handler, and the resolver would store guest bytes over host memory. `TrapReads` now also refuses a range that overlaps a host region (`GuestArenaHostRegionOverlaps_nid_postfix`), as `OwnedByGuest` in `GuestArena.cpp` already does for guest stores. On Windows the host region list is built once when the arena is created and never changes after that (`ReleaseHostRegion` is not built on Windows). The lookup is the binary search of `HostRegionIndex`, so trapping a guest range costs one search more. Guest ranges never overlap a host region, so their traps are unchanged. The test landed with `650df957b4` (2026-11-24), after the last Windows Build run that finished (`ab6fe4a981`), so CI has not run it yet. ### Tested Windows 11, WinLibs GCC 15.2, Release, on `main` `a163d4b886`. I ran the 14 `windows_read_trap_*` and `windows_write_watch*` tests with `ctest -R '^(windows_read_trap_.*|windows_write_watch.*)$' --repeat until-fail:10`: - `main`: `windows_read_trap_unknown` fails on its first run. - this change: it passes all 10 runs, and so do the other read trap tests. - On both, `windows_write_watch` failed once. That is the unrelated flaky case fixed in #28027. I also ran the read trap and write watch GPU tests, 3 runs each on the NVIDIA GeForce RTX 5080 and the AMD Radeon gfx1036 iGPU, with `VK_LAYER_KHRONOS_validation` inserted: - They pass on `main` and with this change. - Neither run logs a `VUID-` error. The tests covered were `agc_read_traps_over_host_pointer_imports_driver`, `agc_driver_label_trap_committer_resolve`, `agc_write_tracking*` and `agc_write_watch_coverage*`. The full CPU suite (5,120 tests), with this change applied to `main` `a163d4b886` in a batch with other reviews, has no failure from this change. The three that failed are `frame_capture_minimize` (a load timeout that also fails on `main`), `guest_np_common_runtime` (from another PR in the batch) and `guest_webrtc_null_socket_server`, a load flake that passes 30/30 when rerun. `windows_read_trap_unknown` passes in it. The branch is rebased onto `main` `4c7578e0a4`. None of the 30 commits in between touch this file. `python3 tools/pr_overlap.py --names TrapReads` reports no matching export changes, because `TrapReads` is not an export. `python3 tools/pr_overlap.py --branch fix/libc-windows-read-trap-host-regions` merged all 8 open PRs that share `GuestWriteWatch.cpp` into the base. It could not compare two of them, because they conflict with `main`, so I compared those by hand: - #12792 changes the Linux `Watch` and `GuestWriteWatchCollect*`. - #20580 changes the macOS read trap path. Neither touches the Windows `ReadTraps`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * On Windows, read-trap operations now reject memory ranges that overlap host memory. Ranges must also remain within the guest arena and avoid protected Windows mappings, preventing traps from being applied to ranges that cross guest and host memory regions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-28 09:21:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T09:21:58Z.

Review: APPROVED


Reviewed — looks good

The host region overlap guard rejects ranges that should not be read-trapped, and the reviewed code supports merging this change.

What I ran

Reviewed head a6b85c7654c625dd6c994e886aea8c07e98f20b3 using the prepared diff and repo_read.py for targeted source reads; no tests were run.

Results

Check Result Note
Source review passed Changed guard and overlap lookup reviewed
Tests not run No independent test execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author-reported Windows and GPU test results were not independently verified. GuestWriteWatch.cpp was read around TrapReads and the changed guard; GuestArena.cpp was read around OwnedByGuest and the overlap helper; HostRegionIndex.hpp was read in full. No excluded changed files were listed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6096944631 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28028#pullrequestreview-6096944631). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T09:21:58Z. **Review:** APPROVED --- # Reviewed — looks good The host region overlap guard rejects ranges that should not be read-trapped, and the reviewed code supports merging this change. ## What I ran Reviewed head `a6b85c7654c625dd6c994e886aea8c07e98f20b3` using the prepared diff and `repo_read.py` for targeted source reads; no tests were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed guard and overlap lookup reviewed | | Tests | not run | No independent test execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author-reported Windows and GPU test results were not independently verified. `GuestWriteWatch.cpp` was read around `TrapReads` and the changed guard; `GuestArena.cpp` was read around `OwnedByGuest` and the overlap helper; `HostRegionIndex.hpp` was read in full. No excluded changed files were listed. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 18:25:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:25:41Z.

Review: COMMENTED


I checked that the Windows path now tests the whole page-rounded trap span against the arena's host-region index before creating a trap or applying page protection. That is the same ownership check used by the guest-store path. The existing unknown-region test covers the behavior this fixes. Source review at 67fb3534e1. I did not run the Windows fixture locally.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6175929364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28028#pullrequestreview-6175929364). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T18:25:41Z. **Review:** COMMENTED --- I checked that the Windows path now tests the whole page-rounded trap span against the arena's host-region index before creating a trap or applying page protection. That is the same ownership check used by the guest-store path. The existing unknown-region test covers the behavior this fixes. Source review at `67fb3534e1`. I did not run the Windows fixture locally. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/28028:upstream-pr/28028'
git switch 'upstream-pr/28028'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/28028'
git switch 'upstream-pr/28028'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28028'
git switch 'upstream-pr/28028'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28028'
git switch 'main'
git merge --squash 'upstream-pr/28028'
git switch 'main'
git merge --ff-only 'upstream-pr/28028'
git switch 'main'
git merge 'upstream-pr/28028'
git push origin 'main'
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!28028
No description provided.