fix(libc): check a whole range before collecting its writes with reset #2328

Merged
TheAggie merged 1 commit from upstream-pr/2328 into main 2026-10-10 05:19:18 +00:00
TheAggie commented 2026-10-09 15:41:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-09T15:41:13Z.


What

WindowsMappings::Collect (behind GuestArenaCollectWrites_nid_postfix) resets each part of a range as it walks it when clear is set. It marks a shared view's page seen and re-arms its aliases, and it calls GetWriteWatch with WRITE_WATCH_FLAG_RESET on private memory. When it then reaches a PAGE_NOACCESS view, or memory that isn't committed private memory, it returns false. By then the parts before that point are already reset.

The callers treat false as "this range can't be tracked" and fall back, for example to collecting the pieces separately. But the writes to the reset parts are gone, so the fallback no longer sees them. walkWrites documents the failure ("Uncommitted pages inside the range make the call fail; such ranges are compared instead") but not the lost writes. Today's callers mostly collect exactly one allocation, which avoids it. I hit it while trying to collect several nearby heap mirrors in one walk: a span with an uncommitted page in the middle lost the writes before the hole.

With clear, Collect now checks the whole range first, with the same tests the walk uses (no PAGE_NOACCESS view, private memory committed), and returns false before resetting anything. A collect without clear resets nothing and is unchanged. The walk itself is unchanged.

Test (guest_memory, Windows), CheckFailedCollectKeepsWrites:

  • Private arena memory, committed / uncommitted / committed. After a write to the first page, a resetting collect over all three returns false, and a collect of the first page alone still reports the written page.
  • A two-page shared direct-memory mapping whose second page is mprotected to no access: same check, with the shared view's four 4 KiB pages reported afterwards.
  • Both fail without the change (the later collect reports nothing).

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, NVIDIA GeForce RTX 5070 Ti (driver 610.88).

  • Full local suite: 507 tests, 504 passed, 1 skipped. guest_sce_net timed out (it times out locally on unrelated branches too, likely my network filtering), and audio_out2_timing failed once in the parallel run but passed 3 of 3 runs alone. It was run from the generated CTestTestfile.cmake commands because antivirus blocks ctest.exe on this machine.
  • tools/check_conventions.py --base origin/main passes.

The Linux path (GuestWriteWatch::Collect) isn't touched. Not tested on Linux or macOS.

#2054 also changes Collect's private-memory branch. WindowsMappings.hpp merges cleanly with it; the conflicts are adjacent additions in tests/GuestMemory.cpp and TechnicalDebt.md. I'll rebase onto whichever lands first.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4802622577 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2328). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-09T15:41:13Z. --- ### What `WindowsMappings::Collect` (behind `GuestArenaCollectWrites_nid_postfix`) resets each part of a range as it walks it when `clear` is set. It marks a shared view's page seen and re-arms its aliases, and it calls `GetWriteWatch` with `WRITE_WATCH_FLAG_RESET` on private memory. When it then reaches a `PAGE_NOACCESS` view, or memory that isn't committed private memory, it returns `false`. By then the parts before that point are already reset. The callers treat `false` as "this range can't be tracked" and fall back, for example to collecting the pieces separately. But the writes to the reset parts are gone, so the fallback no longer sees them. `walkWrites` documents the failure ("Uncommitted pages inside the range make the call fail; such ranges are compared instead") but not the lost writes. Today's callers mostly collect exactly one allocation, which avoids it. I hit it while trying to collect several nearby heap mirrors in one walk: a span with an uncommitted page in the middle lost the writes before the hole. With `clear`, `Collect` now checks the whole range first, with the same tests the walk uses (no `PAGE_NOACCESS` view, private memory committed), and returns `false` before resetting anything. A collect without `clear` resets nothing and is unchanged. The walk itself is unchanged. Test (`guest_memory`, Windows), `CheckFailedCollectKeepsWrites`: - Private arena memory, committed / uncommitted / committed. After a write to the first page, a resetting collect over all three returns `false`, and a collect of the first page alone still reports the written page. - A two-page shared direct-memory mapping whose second page is mprotected to no access: same check, with the shared view's four 4 KiB pages reported afterwards. - Both fail without the change (the later collect reports nothing). ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, NVIDIA GeForce RTX 5070 Ti (driver 610.88). - Full local suite: 507 tests, 504 passed, 1 skipped. `guest_sce_net` timed out (it times out locally on unrelated branches too, likely my network filtering), and `audio_out2_timing` failed once in the parallel run but passed 3 of 3 runs alone. It was run from the generated `CTestTestfile.cmake` commands because antivirus blocks `ctest.exe` on this machine. - `tools/check_conventions.py --base origin/main` passes. The Linux path (`GuestWriteWatch::Collect`) isn't touched. Not tested on Linux or macOS. #2054 also changes `Collect`'s private-memory branch. `WindowsMappings.hpp` merges cleanly with it; the conflicts are adjacent additions in `tests/GuestMemory.cpp` and `TechnicalDebt.md`. I'll rebase onto whichever lands first. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kikoarl commented 2026-10-09 17:53:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:53:03Z.


Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at bbdb35b1:

  • Build: Built cleanly in RAM (/tmp/anyps5_build) with zero warnings/errors.
  • Conventions: python3 tools/check_conventions.py --base origin/main --head HEAD -> PASS (0 errors).
  • Verification: Verified range validation in WindowsMappings.hpp and GuestMemory.cpp. Prevents partial reset and inconsistent write tracking.

LGTM!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086311675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2328#issuecomment-6086311675). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:53:03Z. --- Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at `bbdb35b1`: - **Build**: Built cleanly in RAM (`/tmp/anyps5_build`) with zero warnings/errors. - **Conventions**: `python3 tools/check_conventions.py --base origin/main --head HEAD` -> **PASS (0 errors)**. - **Verification**: Verified range validation in `WindowsMappings.hpp` and `GuestMemory.cpp`. Prevents partial reset and inconsistent write tracking. LGTM!
github-actions[bot] commented 2026-10-09 23:32:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T23:32:32Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2054 core/libs/tests/GuestMemory.cpp L1191
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091046299 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2328#issuecomment-6091046299). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T23:32:32Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2054 | `core/libs/tests/GuestMemory.cpp` [L1191](https://github.com/boykopovar/AnyPS5/blob/bc40a6b5a09754843c7a7e358a336d759ad090ec/core/libs/tests/GuestMemory.cpp#L1191) | | |
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!2328
No description provided.