fix(libc): track writes to singly mapped direct memory without faults on Windows #5596

Merged
tibzejoker merged 4 commits from upstream-pr/5596 into main 2026-10-14 02:53:45 +00:00
tibzejoker commented 2026-10-14 00:42:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-14T00:42:12Z.


What

Port of @DotDebian's #637 onto current main, as asked on #637. DotDebian stays the author of the main commit, and our changes are separate commits.

On Windows, AnyPS5 tracked CPU writes to direct memory by arming its section views read-only and taking a write fault. The exception frame Windows builds for such a fault, with live AVX state, overwrites the System V red zone below RSP, so guest leaf functions that keep values there read back garbage (#361). GTA V's zlib inflate_fast was one of them.

With this change, a direct memory page mapped at a single guest address is private MEM_WRITE_WATCH memory at that address, and its writes are collected with GetWriteWatch, like flexible memory: no fault. A page mapped at a second address moves its whole resident run into the section behind a read-only fence while it is copied, and every address then becomes a view, tracked by write protection as before.

Commits:

  • fix(libc): track writes to singly mapped direct memory without faults on Windows (author DotDebian): #637's two commits squashed and merged by hand onto main's 4 KiB subpage tracking of shared views. Resident runs keep a collected bit per page and enter the fresh ranges, so Fresh and Armed collections select them like shared views. A demoted run hands its collected bits to the new views and reports every page once. The fixes from the 2026-10-07 review are kept: fenced copies in demote and evict, demoting the whole run, unmapped pages kept out of the section, and the watchResident guard in Collect.
  • fix(libc): keep resident mappings to direct memory backings on Windows: only direct memory goes resident, through GuestArenaMapDirect. Other single-mapped sections (file-backed JIT and shm memory, whose aliases use duplicated handles) keep main's shared-view path; making them resident broke guest_jit_shared_memory.
  • test(libc): require resident direct memory in the resident run tests: CheckBreakingAResidentRunKeepsConcurrentAccesses and CheckRemappedDirectMemoryIsChargedOnce now check that the first mapping is MEM_PRIVATE, so they fail on main instead of passing there.
  • docs(libc): move the Windows direct memory debt entry to libc.md.

The 2026-10-11 review points:

  • MapAlias throws through refuse(...) when a resident run extends outside the aliased range. importAllocation keeps main's caller and fallback, with no entry.alias == nullptr branch.
  • tests/Recorder.cpp keeps main's switches and adds --host-import-second-mapping-only.
  • The two tests that passed on main now fail there, as above.

Tested

Windows 11, i9-13900K, RTX 4090, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON, on main ce5c83953.

  • guest_memory: passes (ctest -R guest_memory, 2/2). On main, the resident run tests fail at the new MEM_PRIVATE check, and #637's own g_accessViolations == 0 check fails as well.
  • Full ctest: 1112/1112 pass.
  • tools/check_conventions.py --base upstream/main passes.
  • GTA V (PPSA04264), Story prologue with write watch on: on current main without this change it crashes in the RAGE main thread right after the save loads (access violation at eboot 0x28aacee). With it, the prologue ran for 150 s with correct rendering, on main 8d98e8851 plus the other open PRs GTA needs to reach Story (#5003, #448's port, #1186, #1189).

Not tested on Linux: the change is in the Windows mapping code.

Checklist

  • Based on current main; no other open PR implements the same functions (supersedes #637; #2054 overlaps and is stale)
  • 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
  • Unblocks: PPSA04264 (GTA V, Story with write watch on Windows); fixes #361
  • GPUs tested: n/a (libc change; GTA run on NVIDIA RTX 4090, driver 616.92, Windows 11)
  • AI-assisted: yes (Claude Code); port by Claude Code, the original design and commits are DotDebian's

🤖 Generated with Claude Code

https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4851321696 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5596). Originally posted by [tibzejoker](https://github.com/tibzejoker) on 2026-10-14T00:42:12Z. --- ### What Port of @DotDebian's #637 onto current `main`, as asked on #637. DotDebian stays the author of the main commit, and our changes are separate commits. On Windows, AnyPS5 tracked CPU writes to direct memory by arming its section views read-only and taking a write fault. The exception frame Windows builds for such a fault, with live AVX state, overwrites the System V red zone below RSP, so guest leaf functions that keep values there read back garbage (#361). GTA V's zlib `inflate_fast` was one of them. With this change, a direct memory page mapped at a single guest address is private `MEM_WRITE_WATCH` memory at that address, and its writes are collected with `GetWriteWatch`, like flexible memory: no fault. A page mapped at a second address moves its whole resident run into the section behind a read-only fence while it is copied, and every address then becomes a view, tracked by write protection as before. Commits: - `fix(libc): track writes to singly mapped direct memory without faults on Windows` (author DotDebian): #637's two commits squashed and merged by hand onto `main`'s 4 KiB subpage tracking of shared views. Resident runs keep a collected bit per page and enter the fresh ranges, so `Fresh` and `Armed` collections select them like shared views. A demoted run hands its collected bits to the new views and reports every page once. The fixes from the 2026-10-07 review are kept: fenced copies in demote and evict, demoting the whole run, unmapped pages kept out of the section, and the `watchResident` guard in `Collect`. - `fix(libc): keep resident mappings to direct memory backings on Windows`: only direct memory goes resident, through `GuestArenaMapDirect`. Other single-mapped sections (file-backed JIT and shm memory, whose aliases use duplicated handles) keep `main`'s shared-view path; making them resident broke `guest_jit_shared_memory`. - `test(libc): require resident direct memory in the resident run tests`: `CheckBreakingAResidentRunKeepsConcurrentAccesses` and `CheckRemappedDirectMemoryIsChargedOnce` now check that the first mapping is `MEM_PRIVATE`, so they fail on `main` instead of passing there. - `docs(libc): move the Windows direct memory debt entry to libc.md`. The 2026-10-11 review points: - `MapAlias` throws through `refuse(...)` when a resident run extends outside the aliased range. `importAllocation` keeps `main`'s caller and fallback, with no `entry.alias == nullptr` branch. - `tests/Recorder.cpp` keeps `main`'s switches and adds `--host-import-second-mapping-only`. - The two tests that passed on `main` now fail there, as above. ### Tested Windows 11, i9-13900K, RTX 4090, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `BUILD_TESTING=ON`, on `main` ce5c83953. - `guest_memory`: passes (`ctest -R guest_memory`, 2/2). On `main`, the resident run tests fail at the new `MEM_PRIVATE` check, and #637's own `g_accessViolations == 0` check fails as well. - Full `ctest`: 1112/1112 pass. - `tools/check_conventions.py --base upstream/main` passes. - GTA V (PPSA04264), Story prologue with write watch on: on current `main` without this change it crashes in the RAGE main thread right after the save loads (access violation at eboot 0x28aacee). With it, the prologue ran for 150 s with correct rendering, on `main` 8d98e8851 plus the other open PRs GTA needs to reach Story (#5003, #448's port, #1186, #1189). Not tested on Linux: the change is in the Windows mapping code. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (supersedes #637; #2054 overlaps and is stale) - [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] Unblocks: PPSA04264 (GTA V, Story with write watch on Windows); fixes #361 - [x] GPUs tested: n/a (libc change; GTA run on NVIDIA RTX 4090, driver 616.92, Windows 11) - [x] AI-assisted: yes (Claude Code); port by Claude Code, the original design and commits are DotDebian's 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV
github-actions[bot] commented 2026-10-14 01:01:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T01:01:03Z.


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
#3864 core/libs/prx/libc/include/WindowsMappings.hpp L263-264, L366-367, L410
#3965 docs/dev/debt/libc.md L30
#5062 core/libs/prx/libc/include/WindowsMappings.hpp L77-78, L263-264, L366-367, L596-597
#5599 docs/dev/debt/libc.md L30
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158507337 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5596#issuecomment-6158507337). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T01:01:03Z. --- <!-- 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 | |---|---|---|---| | #3864 | `core/libs/prx/libc/include/WindowsMappings.hpp` [L263-264](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L263-L264), [L366-367](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L366-L367), [L410](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L410) | | | | #3965 | `docs/dev/debt/libc.md` [L30](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/docs/dev/debt/libc.md#L30) | | | | #5062 | `core/libs/prx/libc/include/WindowsMappings.hpp` [L77-78](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L77-L78), [L263-264](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L263-L264), [L366-367](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L366-L367), [L596-597](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/core/libs/prx/libc/include/WindowsMappings.hpp#L596-L597) | | | | #5599 | `docs/dev/debt/libc.md` [L30](https://github.com/boykopovar/AnyPS5/blob/0a56aaaf47c6e99633c710a524e7834eed4b450e/docs/dev/debt/libc.md#L30) | | |
oneandonlydean commented 2026-10-14 01:24:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:24:18Z.


@tibzejoker thanks, this covers the 10-11 points. Since it changes the Windows memory model, could you run GTA V (PPSA04264) on this head alone on top of main and post the result? The 150 s run in #5024 was on a combined stack.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158783535 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5596#issuecomment-6158783535). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:24:18Z. --- @tibzejoker thanks, this covers the 10-11 points. Since it changes the Windows memory model, could you run GTA V (PPSA04264) on this head alone on top of `main` and post the result? The 150 s run in #5024 was on a combined stack.
tibzejoker commented 2026-10-14 02:17:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-14T02:17:42Z.


@oneandonlydean GTA V (PPSA04264) on this head alone. Windows 11, i9-13900K, RTX 4090 (NVIDIA 616.92), MinGW-w64 GCC 15.2.0, Release. Write watch on, no other PR and no local change, one run each:

  • main ce5c83953 (this PR's base): the title reaches the menu and Story is requested. While Story loads, it crashes with an access violation in [RAGE] HDD Streamer: a write through a null pointer at eboot 0x28aacee. This is the same address as the crash in the RAGE main thread I reported on #637 and #5052.
  • This PR, b7d7e71bf: the same steps load Story without a crash. It then stops about 30 s into Story at AGC graphics: sample masks are unsupported, which is #448's territory and the next stop GTA hits on main.

So, on its own, this head removes the write-watch crash and nothing new shows up before the next known stop. With #448 (fdfawef324's port) on top, the Story prologue ran for 150 s with correct rendering, as posted in #5024.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159411121 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5596#issuecomment-6159411121). Originally posted by [tibzejoker](https://github.com/tibzejoker) on 2026-10-14T02:17:42Z. --- @oneandonlydean GTA V (PPSA04264) on this head alone. Windows 11, i9-13900K, RTX 4090 (NVIDIA 616.92), MinGW-w64 GCC 15.2.0, Release. Write watch on, no other PR and no local change, one run each: - **`main` ce5c83953** (this PR's base): the title reaches the menu and Story is requested. While Story loads, it crashes with an access violation in `[RAGE] HDD Streamer`: a write through a null pointer at eboot 0x28aacee. This is the same address as the crash in the RAGE main thread I reported on #637 and #5052. - **This PR, b7d7e71bf**: the same steps load Story without a crash. It then stops about 30 s into Story at `AGC graphics: sample masks are unsupported`, which is #448's territory and the next stop GTA hits on `main`. So, on its own, this head removes the write-watch crash and nothing new shows up before the next known stop. With #448 (fdfawef324's port) on top, the Story prologue ran for 150 s with correct rendering, as posted in #5024. AI-assisted (Claude Code).
tibzejoker commented 2026-10-14 06:16:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-14T06:16:07Z.


A correction to my run above: "removes the write-watch crash" was based on one run per tree. Since then I've run GTA more often with this change in (main ee9037d85 + #757 and the other Story PRs, write watch on). In 1 run of 6, the same crash came back 15 s into Story: [RAGE] HDD Streamer, a null write at eboot 0x28aacee. The other 5 ran 60-180 s without it. Without this change, both of my runs crashed there.

So this change makes the crash rare in GTA, but it doesn't remove it. One possible reason is that only direct memory goes resident, so writes to other shared views can still fault. I haven't traced that one crash, so I can't tell whether it was red-zone damage or something else at the same site.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162188183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5596#issuecomment-6162188183). Originally posted by [tibzejoker](https://github.com/tibzejoker) on 2026-10-14T06:16:07Z. --- A correction to my run above: "removes the write-watch crash" was based on one run per tree. Since then I've run GTA more often with this change in (main ee9037d85 + #757 and the other Story PRs, write watch on). In 1 run of 6, the same crash came back 15 s into Story: `[RAGE] HDD Streamer`, a null write at eboot 0x28aacee. The other 5 ran 60-180 s without it. Without this change, both of my runs crashed there. So this change makes the crash rare in GTA, but it doesn't remove it. One possible reason is that only direct memory goes resident, so writes to other shared views can still fault. I haven't traced that one crash, so I can't tell whether it was red-zone damage or something else at the same site. AI-assisted (Claude Code).
EagleBearerYS commented 2026-10-14 06:49:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T06:49:48Z.


Resident Evil Village (PPSA01556, 01.210.000) aborts at boot since this PR, 19 s in and before any dialog:

FATAL: an address-based build copies 419 MiB of 1 registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB); the largest: 0x260400000+0x1a300000 (419.0 MiB committed, outside the write-watched arena)

RTX 3070 Laptop GPU (8 GB), driver 617.42, Windows 11 Home 10.0.26300, Ryzen 9 5900HS, 15.4 GB RAM. main 0b6d033b8, relinked with main's relinker. Plain main can't boot Village here for an unrelated reason: it prepares every shader at registration, and after 10 minutes private memory was at 21.9 GB with 6.8 GB of commit free. So this run adds two local commits that prepare shaders at their first draw (APS5_RE8_LAZY_PREPARE=1, APS5_NO_GPL=1). They don't touch the import or mirror paths.

That range is a 419 MiB direct-memory heap mapped once. Before this PR it was shared views, served by a heap mirror, and Village booted to gameplay on our older branch. With this PR it's resident private memory. The address-based build's 64 MiB window import leaves the write watch for private memory, and the heap mirror refuses ranges with unwatched blocks, so the whole range would be copied on each build, past the 64 MiB guard. Raising the guard (APS5_ADDRESS_COPY_MAX_MIB=512) gets past it.

#5688 fixes it: heap mirrors keep such ranges and refresh the blocks outside the write watch. With it the abort is gone and the range is host-imported:

[gpu] host import of 0x260400000+0x1a300000 ok (3444 live, 5377.7 MiB live, 6511.3 MiB ever)
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162646181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5596#issuecomment-6162646181). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-14T06:49:48Z. --- Resident Evil Village (PPSA01556, 01.210.000) aborts at boot since this PR, 19 s in and before any dialog: ``` FATAL: an address-based build copies 419 MiB of 1 registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB); the largest: 0x260400000+0x1a300000 (419.0 MiB committed, outside the write-watched arena) ``` RTX 3070 Laptop GPU (8 GB), driver 617.42, Windows 11 Home 10.0.26300, Ryzen 9 5900HS, 15.4 GB RAM. `main` 0b6d033b8, relinked with `main`'s relinker. Plain `main` can't boot Village here for an unrelated reason: it prepares every shader at registration, and after 10 minutes private memory was at 21.9 GB with 6.8 GB of commit free. So this run adds two local commits that prepare shaders at their first draw (`APS5_RE8_LAZY_PREPARE=1`, `APS5_NO_GPL=1`). They don't touch the import or mirror paths. That range is a 419 MiB direct-memory heap mapped once. Before this PR it was shared views, served by a heap mirror, and Village booted to gameplay on our older branch. With this PR it's resident private memory. The address-based build's 64 MiB window import leaves the write watch for private memory, and the heap mirror refuses ranges with unwatched blocks, so the whole range would be copied on each build, past the 64 MiB guard. Raising the guard (`APS5_ADDRESS_COPY_MAX_MIB=512`) gets past it. #5688 fixes it: heap mirrors keep such ranges and refresh the blocks outside the write watch. With it the abort is gone and the range is host-imported: ``` [gpu] host import of 0x260400000+0x1a300000 ok (3444 live, 5377.7 MiB live, 6511.3 MiB ever) ```
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!5596
No description provided.