fix(memory): exclude arena host holes from every write-tracking query #4186

Merged
Scramblrrrr merged 3 commits from upstream-pr/4186 into main 2026-10-12 10:49:18 +00:00
Scramblrrrr commented 2026-10-12 00:54:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T00:54:18Z.


What

Windows host holes inside the reserved guest-arena interval must be unwatched for every write-tracking query. Move the exclusion into WriteTracker::covers() so MarkWritten, StoreOwnBytes, WrittenSince, ChangedBlocks and the other stamp readers agree, and remove the duplicated checks. A twelve-query Windows fixture checks an actual host hole discovered with VirtualQuery.

Carries screamm's 03860b02 unchanged in content and preserving authorship, as explicitly offered in #4087. Based on main 7a84b222, including #4131's StoredOver fix; this covers the remaining queries. Original branch: https://github.com/screamm/AnyPS5/tree/fix/write-tracking-host-holes. Relates to #4087 and #3594.

Tested

Windows 11 Pro 26200, GCC 15.2 Release, RTX 5070 Ti, required Vulkan. Current-main integration 27c134117e5aa548252123d3cbb66cda0af5b82b combines this exact commit with a separate CPU-copy profiling-only change: agc_write_tracking, agc_write_watch_coverage, guest_memory, agc_driver_graphics, agc_driver_bda_device passed 5/5 in 8.53 seconds. No Linux or full-suite claim. Updated carry head b45232dda67ccea86b19f5b7a24a1b0944a01f72 passes conventions and diff checks. The fixture now prints an explicit subcase skip when Windows ASLR leaves no host allocation inside the guest arena, instead of failing the whole test. Other checks still run. On current-main integration 162daec28a730068a210268738b61f32bec05e1f, three fresh write-tracking runs passed (0.08/0.04/0.04 seconds), all with actual host-hole coverage and no skip message. A separate rendering-stack run exercised the no-hole skip and passed the other checks. The original author commit is retained unchanged; the fixture correction is a separate carry-review commit.

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; no silent stubs added
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes (original author Claude Code; carry/review/Windows cross-test Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823659767 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T00:54:18Z. --- ### What Windows host holes inside the reserved guest-arena interval must be unwatched for every write-tracking query. Move the exclusion into `WriteTracker::covers()` so `MarkWritten`, `StoreOwnBytes`, `WrittenSince`, `ChangedBlocks` and the other stamp readers agree, and remove the duplicated checks. A twelve-query Windows fixture checks an actual host hole discovered with `VirtualQuery`. Carries screamm's `03860b02` unchanged in content and preserving authorship, as explicitly offered in #4087. Based on main `7a84b222`, including #4131's `StoredOver` fix; this covers the remaining queries. Original branch: https://github.com/screamm/AnyPS5/tree/fix/write-tracking-host-holes. Relates to #4087 and #3594. ### Tested Windows 11 Pro 26200, GCC 15.2 Release, RTX 5070 Ti, required Vulkan. Current-main integration `27c134117e5aa548252123d3cbb66cda0af5b82b` combines this exact commit with a separate CPU-copy profiling-only change: `agc_write_tracking`, `agc_write_watch_coverage`, `guest_memory`, `agc_driver_graphics`, `agc_driver_bda_device` passed 5/5 in 8.53 seconds. No Linux or full-suite claim. Updated carry head `b45232dda67ccea86b19f5b7a24a1b0944a01f72` passes conventions and diff checks. The fixture now prints an explicit subcase skip when Windows ASLR leaves no host allocation inside the guest arena, instead of failing the whole test. Other checks still run. On current-main integration `162daec28a730068a210268738b61f32bec05e1f`, three fresh write-tracking runs passed (0.08/0.04/0.04 seconds), all with actual host-hole coverage and no skip message. A separate rendering-stack run exercised the no-hole skip and passed the other checks. The original author commit is retained unchanged; the fixture correction is a separate carry-review commit. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation files or images added to the repository - [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] AI-assisted: yes (original author Claude Code; carry/review/Windows cross-test Codex)
Dextroid17 commented 2026-10-12 01:09:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:09:19Z.


This does what the title says for four of the five migrated call sites — Watched, UnchangedSince, UnchangedSinceAll (which still checks covers() per-iteration inside the loop, not once outside it), and storeOwn all produce the same value the removed per-site check did, through the same tracker.covers() gate each already called early. MarkWritten, WrittenSince, ChangedBlocks, and ImportWatched were already routed through covers() before this PR and correctly inherit the exclusion without being touched.

There's a real gap in the fifth: collectWrites. The new covers() gate sits at the point the old per-site check used to be, but the function's per-thread memoization scan (threadCollectMemo) runs before that point and can return early with a nonzero generation if the hole's page-rounded range was already memoized — a path the old, unconditional first-statement check always caught regardless of memo state. So a memoized CollectWrites over a host hole can still report it as written, which is exactly the class of bug this PR exists to close. The fix is straightforward: hoist the hole check back to the top of the function, before the memo scan, the same way the original per-site check was positioned.

The new CheckHostHole test doesn't catch this gap — it calls CollectWrites cold (a fresh collect epoch each call), so the memoized path is never exercised. Worth adding a case that warms the memo (bump the collect epoch, collect once, bump again, collect the hole again) to actually pin this.

Everything here is Windows-only by construction — the non-Windows branch of covers() is untouched, and every removed check was already inside #ifdef _WIN32. #4131's StoredOver fix is confirmed live in the base this PR is built on, so the stated dependency is real, not just reviewed-but-unmerged.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115967452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6115967452). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T01:09:19Z. --- This does what the title says for four of the five migrated call sites — `Watched`, `UnchangedSince`, `UnchangedSinceAll` (which still checks `covers()` per-iteration inside the loop, not once outside it), and `storeOwn` all produce the same value the removed per-site check did, through the same `tracker.covers()` gate each already called early. `MarkWritten`, `WrittenSince`, `ChangedBlocks`, and `ImportWatched` were already routed through `covers()` before this PR and correctly inherit the exclusion without being touched. There's a real gap in the fifth: `collectWrites`. The new `covers()` gate sits at the point the old per-site check used to be, but the function's per-thread memoization scan (`threadCollectMemo`) runs *before* that point and can return early with a nonzero generation if the hole's page-rounded range was already memoized — a path the old, unconditional first-statement check always caught regardless of memo state. So a memoized `CollectWrites` over a host hole can still report it as written, which is exactly the class of bug this PR exists to close. The fix is straightforward: hoist the hole check back to the top of the function, before the memo scan, the same way the original per-site check was positioned. The new `CheckHostHole` test doesn't catch this gap — it calls `CollectWrites` cold (a fresh collect epoch each call), so the memoized path is never exercised. Worth adding a case that warms the memo (bump the collect epoch, collect once, bump again, collect the hole again) to actually pin this. Everything here is Windows-only by construction — the non-Windows branch of `covers()` is untouched, and every removed check was already inside `#ifdef _WIN32`. `#4131`'s `StoredOver` fix is confirmed live in the base this PR is built on, so the stated dependency is real, not just reviewed-but-unmerged. Written with AI assistance.
github-actions[bot] commented 2026-10-12 01:18:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T01:18: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
#2863 core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp L186, L314
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116042892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6116042892). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T01:18: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 | |---|---|---|---| | #2863 | `core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp` [L186](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp#L186), [L314](https://github.com/boykopovar/AnyPS5/blob/c3204078e4aa5adf1bfb93798c81444a99893e20/core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp#L314) | | |
Scramblrrrr commented 2026-10-12 01:19:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T01:19:03Z.


Carry-review update: head b45232dda6 preserves the original author commit and adds a fixture-only correction. Windows ASLR can leave every host allocation outside the guest-arena interval; I reproduced that in a separate 007-stack test launch. With no hole, the fixture now explicitly reports only the host-hole subcase skipped and continues the other write-tracking checks.

Current-main test integration 162daec28a730068a210268738b61f32bec05e1f, three fresh agc_write_tracking runs: pass0.08/0.04/0.04s, all with an actual hole and no skip message. The separate rendering-stack default run exercised the skip path and passed; the opt-in memo run found a hole and passed the twelve queries. Production code is unchanged by this update. Conventions/diff checks pass. AI-assisted: yes (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116051054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6116051054). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T01:19:03Z. --- Carry-review update: head **b45232dda67ccea86b19f5b7a24a1b0944a01f72** preserves the original author commit and adds a fixture-only correction. Windows ASLR can leave every host allocation outside the guest-arena interval; I reproduced that in a separate 007-stack test launch. With no hole, the fixture now explicitly reports only the host-hole subcase skipped and continues the other write-tracking checks. Current-main test integration **162daec28a730068a210268738b61f32bec05e1f**, three fresh `agc_write_tracking` runs: pass0.08/0.04/0.04s, all with an actual hole and no skip message. The separate rendering-stack default run exercised the skip path and passed; the opt-in memo run found a hole and passed the twelve queries. Production code is unchanged by this update. Conventions/diff checks pass. AI-assisted: yes (Codex).
Scramblrrrr commented 2026-10-12 02:16:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T02:16:54Z.


@Dextroid17 Thanks for checking the early-return path. I am reviewing this separately from the 007 performance run. In the current code a thread memo entry is inserted only after tracker.covers(address, bytes) and a successful walkWrites; Windows host-hole boundaries come from page-aligned VirtualQuery regions/allocation-granularity rounding, and _hostRegions is fixed during arena initialization. A page-rounded memo entry for a valid range therefore should not contain a host-hole page. Bumping the collect epoch also invalidates the prior memo entry, rather than making it hit.

I agree the fixture should exercise repeated queries with a warm valid-range memo, not only cold hole queries. I will add that coverage before treating this review as resolved. If you have a concrete sequence that seeds a memo over a host hole through public APIs, please share it; I want to reproduce it rather than assume the invariant holds. No production-code change or claim of a reproduced memo bypass yet.

AI-assisted: yes (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116563042 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6116563042). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T02:16:54Z. --- @Dextroid17 Thanks for checking the early-return path. I am reviewing this separately from the 007 performance run. In the current code a thread memo entry is inserted only after `tracker.covers(address, bytes)` and a successful `walkWrites`; Windows host-hole boundaries come from page-aligned VirtualQuery regions/allocation-granularity rounding, and `_hostRegions` is fixed during arena initialization. A page-rounded memo entry for a valid range therefore should not contain a host-hole page. Bumping the collect epoch also invalidates the prior memo entry, rather than making it hit. I agree the fixture should exercise repeated queries with a warm valid-range memo, not only cold hole queries. I will add that coverage before treating this review as resolved. If you have a concrete sequence that seeds a memo over a host hole through public APIs, please share it; I want to reproduce it rather than assume the invariant holds. No production-code change or claim of a reproduced memo bypass yet. AI-assisted: yes (Codex).
Scramblrrrr commented 2026-10-12 02:27:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T02:27:27Z.


Warm-memo coverage is now in 29a843579d. With an actual host hole, the fixture allocates a watched range, warms and repeatedly queries it within each of two explicit collect epochs, and interleaves repeated full-range and sub-page host-hole queries. Those queries must return0 while the watched range stays collectable. This does not fabricate a memo entry over a host hole or claim a reproduced bypass.

On the current-main host-hole integration, three fresh Windows agc_write_tracking runs passed (0.14/0.04/0.04s), all with actual holes and no host-hole skip message. Production code remains the original author's commit. The invariant concern remains open for a concrete public-API counterexample; warming a valid-range memo and changing epochs did not reproduce it here.

AI-assisted: yes (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116656295 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6116656295). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T02:27:27Z. --- Warm-memo coverage is now in **29a843579d5b0214e3553720ec10e83ab52df0ea**. With an actual host hole, the fixture allocates a watched range, warms and repeatedly queries it within each of two explicit collect epochs, and interleaves repeated full-range and sub-page host-hole queries. Those queries must return0 while the watched range stays collectable. This does not fabricate a memo entry over a host hole or claim a reproduced bypass. On the current-main host-hole integration, three fresh Windows `agc_write_tracking` runs passed (0.14/0.04/0.04s), all with actual holes and no host-hole skip message. Production code remains the original author's commit. The invariant concern remains open for a concrete public-API counterexample; warming a valid-range memo and changing epochs did not reproduce it here. AI-assisted: yes (Codex).
xXPBSXx commented 2026-10-12 04:51:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T04:51:39Z.


Test note (Windows 10, GCC Release, libs only, no other tests run): merge of this PR onto current main (git merge pr/4186 on upstream/main, 52 commits after ceb342b94) against plain upstream/main, same relinked title directories, 90 s per run, alternating order.

Title main main + #4186
DREDGE PPSA09076 (Unity, write-watch path) 5 runs: 0 fatal, 14 err lines each 5 runs: 1 fatal (0xc0000005 on Job.Worker 9, app.exe+0x17f0843, run 2), 4 without fatal
Sifu PPSA03001 2 runs: both fatal 0xc0000005 at libc.prx.guest.prx+0x14270 2 runs: identical fault and address

Result: neutral for Sifu (the fault is the same on plain main, so it is not caused by this PR). DREDGE: one fatal in five runs with the PR, none without; 0/5 vs 1/5 is not significant (Fisher p = 1.0), and DREDGE is known to fault intermittently in the write-watch path, so I cannot attribute it to this PR or rule it out. I did not check whether the faulting address lies in a host hole, and I did not run the agc_write_tracking tests.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117971605 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6117971605). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T04:51:39Z. --- Test note (Windows 10, GCC Release, libs only, no other tests run): merge of this PR onto current main (`git merge pr/4186` on upstream/main, 52 commits after ceb342b94) against plain upstream/main, same relinked title directories, 90 s per run, alternating order. | Title | main | main + #4186 | | - | - | - | | DREDGE PPSA09076 (Unity, write-watch path) | 5 runs: 0 fatal, 14 err lines each | 5 runs: 1 fatal (0xc0000005 on `Job.Worker 9`, `app.exe+0x17f0843`, run 2), 4 without fatal | | Sifu PPSA03001 | 2 runs: both fatal 0xc0000005 at `libc.prx.guest.prx+0x14270` | 2 runs: identical fault and address | Result: neutral for Sifu (the fault is the same on plain main, so it is not caused by this PR). DREDGE: one fatal in five runs with the PR, none without; 0/5 vs 1/5 is not significant (Fisher p = 1.0), and DREDGE is known to fault intermittently in the write-watch path, so I cannot attribute it to this PR or rule it out. I did not check whether the faulting address lies in a host hole, and I did not run the `agc_write_tracking` tests. AI-assisted (Claude Code).
Scramblrrrr commented 2026-10-12 05:35:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T05:35:04Z.


Thanks for the alternating comparison and for separating the shared Sifu fault from the DREDGE observation. I will keep the DREDGE result recorded as unresolved; the small sample does not establish a regression or rule one out.

Our Windows write-tracking fixture runs passed with actual host holes, including valid-range memo warming and queries across collect epochs, but those checks do not explain the intermittent title fault. If a future failing run gives a fault address relative to the arena/host-hole ranges or a short tracker-only stack, that would help narrow a concrete reproduction without sharing title code. No need to upload the title or shader captures.

The production patch remains the original author's host-hole exclusions; I have not changed it in response to this result. AI-assisted: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6118397580 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6118397580). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T05:35:04Z. --- Thanks for the alternating comparison and for separating the shared Sifu fault from the DREDGE observation. I will keep the DREDGE result recorded as unresolved; the small sample does not establish a regression or rule one out. Our Windows write-tracking fixture runs passed with actual host holes, including valid-range memo warming and queries across collect epochs, but those checks do not explain the intermittent title fault. If a future failing run gives a fault address relative to the arena/host-hole ranges or a short tracker-only stack, that would help narrow a concrete reproduction without sharing title code. No need to upload the title or shader captures. The production patch remains the original author's host-hole exclusions; I have not changed it in response to this result. AI-assisted: Codex.
Scramblrrrr commented 2026-10-12 08:20:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T08:20:14Z.


Additional Windows cross-test note: the current hostWriteWatchExclusionTests fixture can fail before any exclusion query checks with "write-watched arena contained no host exclusion to test". This occurred once in the 007 sparse-mirror test stack; repeating the same graphics executable passed. The fixture currently assumes a host exclusion exists inside the reserved arena, which appears sensitive to process layout. This is an observed fixture failure, not evidence that an excluded host region passed a write-watch proof. I will keep it separate from the unresolved alternating DREDGE fault report and make fixture setup explicit before treating it as reliable coverage. AI-assisted: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6120461375 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4186#issuecomment-6120461375). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T08:20:14Z. --- Additional Windows cross-test note: the current hostWriteWatchExclusionTests fixture can fail before any exclusion query checks with "write-watched arena contained no host exclusion to test". This occurred once in the 007 sparse-mirror test stack; repeating the same graphics executable passed. The fixture currently assumes a host exclusion exists inside the reserved arena, which appears sensitive to process layout. This is an observed fixture failure, not evidence that an excluded host region passed a write-watch proof. I will keep it separate from the unresolved alternating DREDGE fault report and make fixture setup explicit before treating it as reliable coverage. AI-assisted: Codex.
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!4186
No description provided.