fix(libc): stop write-protecting resident runs on Windows #9181

Merged
Dripl1x merged 2 commits from upstream-pr/9181 into main 2026-10-25 13:26:13 +00:00
Dripl1x commented 2026-10-21 14:30:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dripl1x on 2026-10-21T14:30:51Z.


What

On Windows, #7473 made clean resident runs of at least 256 KiB read-only so that a collect could skip GetWriteWatch. The first guest store into such a run then faulted and was resumed on the guest thread. With live AVX state, the exception frame Windows builds for that fault overwrites the System V red zone below rsp (#361). That is the fault #5596 removed for singly mapped direct memory. In Grand Theft Auto V (PPSA04264) these were 44 to 89 MEM_PRIVATE resumed faults per run, up to 75 of them in the game's zlib inflate_fast, which keeps pointers in its red zone (#9136).

  • collectResident no longer arms the run. Resident memory is collected with GetWriteWatch only, as before #7473; #7471's collection straight into the output buffer stays.
  • armResident, resumeResidentWrites and Run::writeObserved are removed, and HandleWrite handles a fault on a resident view as before #7473.
  • HostImportAccess stays. With no armed resident page it opens nothing, and View::hostImports is counted but no longer read.
  • The docs/dev/debt/libc.md entry for the resident clean proofs is removed.
  • docs/dev/CONVENTIONS.md names the pages of shared views (shared memory, shared file mappings, and direct memory mapped at more than one guest address), not resident runs, as what the Windows write watch leaves read-only between collections.

Tested

  • guest_memory: CheckLargeResidentWritesStopFaulting is now CheckLargeResidentWritesDoNotFault. A 2 MiB resident mapping stays PAGE_READWRITE after clean collects, and 16 write/collect rounds over every page raise no access violation. With main's WindowsMappings.hpp it fails at GuestMemory.cpp:1773, where the run is read-only after two clean collects.
  • Windows 11 Pro 26300, Ryzen 7 5800H, RTX 3070 Laptop GPU with NVIDIA 617.42, WinLibs GCC 15.2.0 r7, Release:
    • guest_memory and guest_mmap_fixed_anonymous: 3/3.
    • agc_driver_host_import_windows, agc_driver_host_import_at_map, agc_driver_host_import_teardown, agc_driver_host_import_failure_handles and agc_host_import_retire: 3/3. agc_host_import_sparse skipped itself (exit 77).
    • agc_resident_present, agc_host_import_unmap, agc_host_import_retirement, agc_host_import_second_mapping, agc_parked_import_unmap, agc_sparse_run_import, agc_sparse_run_import_copied, agc_driver_host_import_lifetime and agc_driver_host_import_mutation_race: 1/1.
  • Grand Theft Auto V (PPSA04264), measured by @tibzejoker in #9136 with their resumed-fault counter (Windows 11 26200, RTX 4090 with NVIDIA 616.92, i9-13900K). One run of 150 s of Story each; this branch is d6e270ea9f on main 80565c594:
tree total MEM_MAPPED MEM_PRIVATE in zlib crash
main 7280a17ec with APS5_NO_DEFERRED_SETTLES=1 (first post of #9136) 133 85 44 0 none in 150 s
this branch with APS5_NO_DEFERRED_SETTLES=1 0 0 0 0 none in 150 s
this branch, default settings 399 394 0 64 none in 150 s (the crash is random)

The MEM_MAPPED faults left with default settings come from #6678's deferred settles, which #9159 covers.

  • Not run: other titles, and no FPS capture. This is a correctness fix that gives up the GetWriteWatch skip for clean resident runs. #7473's WindowsMappings::Collect counters were measured together with #7471, so its own share of them is unknown, and its single SuperTuxKart captures showed no FPS gain beyond run-to-run spread.
  • Linux and macOS are unchanged: WindowsMappings is Windows-only, and the test change is inside #ifdef _WIN32.

Checklist

  • Based on current main: one commit on 80565c5942, merges cleanly with 2c4b116da9; no other open PR implements the same functions (#9159 and #9098 change other parts of WindowsMappings.hpp and merge cleanly with this)
  • 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none (Related: #9136; its MEM_MAPPED part is #9159)
  • Unblocks: none claimed (Grand Theft Auto V (PPSA04264)'s open world on Windows also needs #9159)
  • GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 11 26300 (the AGC host-import tests above)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4953279920 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9181). Originally posted by [Dripl1x](https://github.com/Dripl1x) on 2026-10-21T14:30:51Z. --- ### What On Windows, #7473 made clean resident runs of at least 256 KiB read-only so that a collect could skip `GetWriteWatch`. The first guest store into such a run then faulted and was resumed on the guest thread. With live AVX state, the exception frame Windows builds for that fault overwrites the System V red zone below `rsp` (#361). That is the fault #5596 removed for singly mapped direct memory. In Grand Theft Auto V (PPSA04264) these were 44 to 89 `MEM_PRIVATE` resumed faults per run, up to 75 of them in the game's zlib `inflate_fast`, which keeps pointers in its red zone (#9136). - `collectResident` no longer arms the run. Resident memory is collected with `GetWriteWatch` only, as before #7473; #7471's collection straight into the output buffer stays. - `armResident`, `resumeResidentWrites` and `Run::writeObserved` are removed, and `HandleWrite` handles a fault on a resident view as before #7473. - `HostImportAccess` stays. With no armed resident page it opens nothing, and `View::hostImports` is counted but no longer read. - The `docs/dev/debt/libc.md` entry for the resident clean proofs is removed. - `docs/dev/CONVENTIONS.md` names the pages of shared views (shared memory, shared file mappings, and direct memory mapped at more than one guest address), not resident runs, as what the Windows write watch leaves read-only between collections. ### Tested - `guest_memory`: `CheckLargeResidentWritesStopFaulting` is now `CheckLargeResidentWritesDoNotFault`. A 2 MiB resident mapping stays `PAGE_READWRITE` after clean collects, and 16 write/collect rounds over every page raise no access violation. With `main`'s `WindowsMappings.hpp` it fails at `GuestMemory.cpp:1773`, where the run is read-only after two clean collects. - Windows 11 Pro 26300, Ryzen 7 5800H, RTX 3070 Laptop GPU with NVIDIA 617.42, WinLibs GCC 15.2.0 r7, Release: - `guest_memory` and `guest_mmap_fixed_anonymous`: 3/3. - `agc_driver_host_import_windows`, `agc_driver_host_import_at_map`, `agc_driver_host_import_teardown`, `agc_driver_host_import_failure_handles` and `agc_host_import_retire`: 3/3. `agc_host_import_sparse` skipped itself (exit 77). - `agc_resident_present`, `agc_host_import_unmap`, `agc_host_import_retirement`, `agc_host_import_second_mapping`, `agc_parked_import_unmap`, `agc_sparse_run_import`, `agc_sparse_run_import_copied`, `agc_driver_host_import_lifetime` and `agc_driver_host_import_mutation_race`: 1/1. - Grand Theft Auto V (PPSA04264), measured by @tibzejoker in #9136 with their resumed-fault counter (Windows 11 26200, RTX 4090 with NVIDIA 616.92, i9-13900K). One run of 150 s of Story each; this branch is `d6e270ea9f` on `main` 80565c594: | tree | total | `MEM_MAPPED` | `MEM_PRIVATE` | in zlib | crash | |---|---|---|---|---|---| | `main` 7280a17ec with `APS5_NO_DEFERRED_SETTLES=1` (first post of #9136) | 133 | 85 | 44 | 0 | none in 150 s | | this branch with `APS5_NO_DEFERRED_SETTLES=1` | 0 | 0 | 0 | 0 | none in 150 s | | this branch, default settings | 399 | 394 | 0 | 64 | none in 150 s (the crash is random) | The `MEM_MAPPED` faults left with default settings come from #6678's deferred settles, which #9159 covers. - Not run: other titles, and no FPS capture. This is a correctness fix that gives up the `GetWriteWatch` skip for clean resident runs. #7473's `WindowsMappings::Collect` counters were measured together with #7471, so its own share of them is unknown, and its single SuperTuxKart captures showed no FPS gain beyond run-to-run spread. - Linux and macOS are unchanged: `WindowsMappings` is Windows-only, and the test change is inside `#ifdef _WIN32`. ### Checklist - [x] Based on current `main`: one commit on 80565c5942, merges cleanly with 2c4b116da9; no other open PR implements the same functions (#9159 and #9098 change other parts of `WindowsMappings.hpp` and merge cleanly with this) - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none (Related: #9136; its `MEM_MAPPED` part is #9159) - [x] Unblocks: none claimed (Grand Theft Auto V (PPSA04264)'s open world on Windows also needs #9159) - [x] GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 11 26300 (the AGC host-import tests above) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-21 19:18:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T19:18:22Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_memory_tests; ctest -N -R guest_memory lists exactly one test.

PR head tested: d6e270ea9febfb05c56c3cca8e0e4a00bc4566c0.

Results (3 runs each):

  • PR head: guest_memory passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libc/include/WindowsMappings.hpp with the version from the PR's merge base (80565c5942), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324476180 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9181#issuecomment-6324476180). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T19:18:22Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_memory_tests`; `ctest -N -R guest_memory` lists exactly one test. **PR head tested:** `d6e270ea9febfb05c56c3cca8e0e4a00bc4566c0`. **Results (3 runs each):** - PR head: `guest_memory` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libc/include/WindowsMappings.hpp` with the version from the PR's merge base (`80565c5942`), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
MarcZX10 commented 2026-10-22 01:42:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:42:53Z.

Review: DISMISSED


I reviewed the Windows mapping change, the updated resident-write regression, the debt removal and the reported #9136 measurements. I found no code-level blocker in the submitted fix. The current hosted conventions, eligibility, relinker, register-context and write-watch checks pass; platform Windows/Linux product jobs were skipped, and I did not run a local Windows test.

One repository-rule item blocks approval: this head is based on 80565c59424190cd0f660f8097945443a0017c42, while current main is 94fbb8cea80af91db62ab6ee7e4da23341675c40; current main is not an ancestor of the PR head. Please rebase/update to current main and rerun the relevant focused Windows tests/checks so the behavior is verified on the current base. The reported GTA V run remains useful evidence for the original commit, but does not replace that revalidation.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608309072 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9181#pullrequestreview-5608309072). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T01:42:53Z. **Review:** DISMISSED --- I reviewed the Windows mapping change, the updated resident-write regression, the debt removal and the reported #9136 measurements. I found no code-level blocker in the submitted fix. The current hosted conventions, eligibility, relinker, register-context and write-watch checks pass; platform Windows/Linux product jobs were skipped, and I did not run a local Windows test. One repository-rule item blocks approval: this head is based on `80565c59424190cd0f660f8097945443a0017c42`, while current `main` is `94fbb8cea80af91db62ab6ee7e4da23341675c40`; current main is not an ancestor of the PR head. Please rebase/update to current main and rerun the relevant focused Windows tests/checks so the behavior is verified on the current base. The reported GTA V run remains useful evidence for the original commit, but does not replace that revalidation.
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!9181
No description provided.