test(libc): protect a resident run region by region in the Windows write watch test #28027

Closed
Plyxen wants to merge 1 commit from upstream-pr/28027 into main
Plyxen commented 2026-11-28 05:01:28 +00:00 (Migrated from github.com)

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


What

windows_write_watch fails on Windows main (a163d4b886) in about one run in nine:

cannot restore full resident run protection

CheckResidentRunNoAccessTransitions (bc6c3abd98, 2026-11-26) reserves 256 KiB with Mappings::Reserve(nullptr, Large), maps it as a resident run with PAGE_NOACCESS, and later restores it with one VirtualProtect(first, Large, PAGE_READWRITE, ...). WindowsMappings::Map commits a resident run in pieces that end at 2 MiB chunk boundaries (chunkBytes), and each piece is its own allocation. When the reservation straddles a 2 MiB boundary, which happens in 3 of the 32 possible 64 KiB-aligned starts, the range is two allocations. VirtualProtect cannot span two allocations and fails with ERROR_INVALID_ADDRESS (487).

I logged VirtualQuery at the failure. Every failing run showed two committed PAGE_NOACCESS private regions, split 64 KiB + 192 KiB, 128 KiB + 128 KiB or 192 KiB + 64 KiB.

The case now restores the range region by region (ProtectEachRegion), as mprotect in DirectMemory.cpp does. The other VirtualProtect calls in the case cover one 16 KiB page, and a 2 MiB boundary never splits one. The library is unchanged.

The case landed 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 windows_write_watch 300 times on main and 300 times with this change:

  • main: 35 of 300 runs failed (11.7%, close to the 3/32 above), each with cannot restore full resident run protection.
  • this change: 0 of 300 failed.

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.

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

python3 tools/pr_overlap.py --names CheckResidentRunNoAccessTransitions reports no matching export changes, because the function is a test case, not an export.

python3 tools/pr_overlap.py --branch test/libc-windows-resident-run-protect-per-allocation failed closed. Two open PRs that change WindowsWriteWatch.cpp conflict with main, so I compared them by hand:

  • #18293 adds CheckArmedResidentRun.
  • #20084 adds a known-zero read case.

Neither changes CheckResidentRunNoAccessTransitions. The only shared spot is the include block at the top of the file.

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

  • Tests
    • Improved the reliability of Windows memory-protection checks for ranges that span multiple regions. This helps ensure the relevant test scenarios are checked consistently across the full requested range.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5536864329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28027). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-28T05:01:28Z. --- ### What `windows_write_watch` fails on Windows `main` (`a163d4b886`) in about one run in nine: ``` cannot restore full resident run protection ``` `CheckResidentRunNoAccessTransitions` (`bc6c3abd98`, 2026-11-26) reserves 256 KiB with `Mappings::Reserve(nullptr, Large)`, maps it as a resident run with `PAGE_NOACCESS`, and later restores it with one `VirtualProtect(first, Large, PAGE_READWRITE, ...)`. `WindowsMappings::Map` commits a resident run in pieces that end at 2 MiB chunk boundaries (`chunkBytes`), and each piece is its own allocation. When the reservation straddles a 2 MiB boundary, which happens in 3 of the 32 possible 64 KiB-aligned starts, the range is two allocations. `VirtualProtect` cannot span two allocations and fails with `ERROR_INVALID_ADDRESS` (487). I logged `VirtualQuery` at the failure. Every failing run showed two committed `PAGE_NOACCESS` private regions, split 64 KiB + 192 KiB, 128 KiB + 128 KiB or 192 KiB + 64 KiB. The case now restores the range region by region (`ProtectEachRegion`), as `mprotect` in `DirectMemory.cpp` does. The other `VirtualProtect` calls in the case cover one 16 KiB page, and a 2 MiB boundary never splits one. The library is unchanged. The case landed 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 `windows_write_watch` 300 times on `main` and 300 times with this change: - `main`: 35 of 300 runs failed (11.7%, close to the 3/32 above), each with `cannot restore full resident run protection`. - this change: 0 of 300 failed. 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. The branch is rebased onto `main` `4c7578e0a4`. None of the 30 commits in between touch this file. `python3 tools/pr_overlap.py --names CheckResidentRunNoAccessTransitions` reports no matching export changes, because the function is a test case, not an export. `python3 tools/pr_overlap.py --branch test/libc-windows-resident-run-protect-per-allocation` failed closed. Two open PRs that change `WindowsWriteWatch.cpp` conflict with `main`, so I compared them by hand: - #18293 adds `CheckArmedResidentRun`. - #20084 adds a known-zero read case. Neither changes `CheckResidentRunNoAccessTransitions`. The only shared spot is the include block at the top of the file. ### 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 * **Tests** * Improved the reliability of Windows memory-protection checks for ranges that span multiple regions. This helps ensure the relevant test scenarios are checked consistently across the full requested range. <!-- end of auto-generated comment: release notes by coderabbit.ai -->

Pull request closed

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!28027
No description provided.