fix(agc): compare mapped guest bytes through fault-guarded reads #24031

Merged
chitters2004 merged 3 commits from upstream-pr/24031 into main 2026-11-27 05:59:37 +00:00
chitters2004 commented 2026-11-20 01:28:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-20T01:28:32Z.


What

GuestMemory::CompareMapped (GuestMemory.cpp) asked the cached page state whether a run was readable, then compared it with a plain memcmp. The cache is invalidated after the protection change (GuestArenaCommit and SetProtection call noteAccess after the syscall), so an arena reset on another thread between the check and the read faulted the process. #15487 closed the same race for CopyMapped. Callers include ShaderRegistry, Dispatch, CaptureValidation, DeferredLabels and MultisampleTarget.

The race is arena-only on Windows: GuestArenaReset only touches arena ranges and ReleaseHostRegion is #ifndef _WIN32 (GuestArena.cpp), so nothing outside the arena is released under a compare. A ReadProcessMemory call per chunk (General.cpp) would also cost a syscall and a copy where the old path did a page-state lookup and a memcmp. So the split is by host:

  • Windows: ranges the arena owns and no host region overlaps go through GuestArenaCompareOwned, which is fault-guarded and reports a fault as Unmapped. Every other range keeps the cached-state path. A range that straddles the arena edge or a host region also keeps it and stays unguarded; a follow-up can split such a range at the boundary.
  • Linux (all architectures) and macOS: the bytes are copied in 64-page chunks through GuestCopyInUnsynced_nid_no_patch, the primitive CopyMapped uses (GuestFaultCopy, process_vm_readv, mach_vm_read_overwrite), into a thread-local buffer and compared there. A fault gives Unmapped and errno is preserved. When a chunk faults, its pages are compared one by one, so a difference before the unmapped page is still reported as Differs, as the page-run walk did. This covers every range #25446's compareWithoutQuery handled (host memory outside the arena and image on kernels without PROCMAP_QUERY), so that function and its APS5_NO_COPY_COMPARE switch are removed; CompareMapped no longer reaches the /proc/self/maps scan on Linux.

MappedCopy.cpp gets compare-reset (the compare hook resets the range between the page-state check and the read: result Unmapped, hook runs once, errno kept), compare-differs (a last-byte difference across two committed units is found, and equal bytes compare equal) and compare-prefix (with the second unit reset, a difference in the readable prefix gives Differs and an equal prefix gives Unmapped). +77 lines in GuestMemory.cpp and the tests.

Tested

Checklist

  • Based on current main (c3a823624f); no other open PR implements the same functions. #20084 edits the memcmp inside CompareMapped for known-zero pages but does not guard it; #22156 swaps that memcmp for a byte-wise atomic compare in the cached-state fallback, which this PR leaves to Windows ranges the arena does not own and to unsupported hosts. Both merge cleanly with this change by git merge-tree
  • 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: lavapipe (Mesa, fork CI Linux and Windows); the change is host-side, no GPU work
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5406057871 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24031). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-20T01:28:32Z. --- ### What `GuestMemory::CompareMapped` (`GuestMemory.cpp`) asked the cached page state whether a run was readable, then compared it with a plain `memcmp`. The cache is invalidated after the protection change (`GuestArenaCommit` and `SetProtection` call `noteAccess` after the syscall), so an arena reset on another thread between the check and the read faulted the process. #15487 closed the same race for `CopyMapped`. Callers include `ShaderRegistry`, `Dispatch`, `CaptureValidation`, `DeferredLabels` and `MultisampleTarget`. The race is arena-only on Windows: `GuestArenaReset` only touches arena ranges and `ReleaseHostRegion` is `#ifndef _WIN32` (`GuestArena.cpp`), so nothing outside the arena is released under a compare. A `ReadProcessMemory` call per chunk (`General.cpp`) would also cost a syscall and a copy where the old path did a page-state lookup and a `memcmp`. So the split is by host: - Windows: ranges the arena owns and no host region overlaps go through `GuestArenaCompareOwned`, which is fault-guarded and reports a fault as `Unmapped`. Every other range keeps the cached-state path. A range that straddles the arena edge or a host region also keeps it and stays unguarded; a follow-up can split such a range at the boundary. - Linux (all architectures) and macOS: the bytes are copied in 64-page chunks through `GuestCopyInUnsynced_nid_no_patch`, the primitive `CopyMapped` uses (`GuestFaultCopy`, `process_vm_readv`, `mach_vm_read_overwrite`), into a thread-local buffer and compared there. A fault gives `Unmapped` and errno is preserved. When a chunk faults, its pages are compared one by one, so a difference before the unmapped page is still reported as `Differs`, as the page-run walk did. This covers every range #25446's `compareWithoutQuery` handled (host memory outside the arena and image on kernels without `PROCMAP_QUERY`), so that function and its `APS5_NO_COPY_COMPARE` switch are removed; `CompareMapped` no longer reaches the `/proc/self/maps` scan on Linux. `MappedCopy.cpp` gets `compare-reset` (the compare hook resets the range between the page-state check and the read: result `Unmapped`, hook runs once, errno kept), `compare-differs` (a last-byte difference across two committed units is found, and equal bytes compare equal) and `compare-prefix` (with the second unit reset, a difference in the readable prefix gives `Differs` and an equal prefix gives `Unmapped`). +77 lines in `GuestMemory.cpp` and the tests. ### Tested - Fork CI on this head `426310bb13c` (Linux gate and Windows, lavapipe), `agc_mapped_copy`: 9/9 pass on both, nothing new failing: https://github.com/chitters2004/AnyPS5/actions/runs/44649218166 - Same head, `agc_linked_code_dependencies` (it counts compares through the compare hook): 43/43 pass on Linux and Windows: https://github.com/chitters2004/AnyPS5/actions/runs/44652426291 - Negative control for the race, the tests on main `c3a823624f` without the fix: `agc_mapped_copy_compare-reset` fails on Linux and Windows, the other 7 then-existing tests pass: https://github.com/chitters2004/AnyPS5/actions/runs/44640943648 - Negative control for the page-wise fallback, the chunk compare without it plus the same tests (branch `draft/ci-cc714715891`): `agc_mapped_copy_compare-prefix` fails on Linux, the other 8 pass: https://github.com/chitters2004/AnyPS5/actions/runs/44649293683 - Cost: on Windows an arena-owned range is compared in place by the guarded arena compare (no copy, no syscall) and every other range takes the existing path, so Windows adds no copy or syscall. Linux and macOS add one copy into a thread-local buffer per 64 pages compared; not timed. - #20084 makes the Windows compare known-zero aware; whichever of the two lands second wraps the arena compare in its `splitKnownZero`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main` (c3a823624f); no other open PR implements the same functions. #20084 edits the `memcmp` inside `CompareMapped` for known-zero pages but does not guard it; #22156 swaps that `memcmp` for a byte-wise atomic compare in the cached-state fallback, which this PR leaves to Windows ranges the arena does not own and to unsupported hosts. Both merge cleanly with this change by `git merge-tree` - [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: lavapipe (Mesa, fork CI Linux and Windows); the change is host-side, no GPU work - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-21 08:53:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T08:53:38Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the targeted source review found the guarded comparison paths consistent with the stated behavior.

What I ran

Reviewed head 426310bb13c1cc639da064bb100f497789d3b8af. No commands or tests were run; repo_read.py was used to read the prepared diff and selected source ranges.

Results

Check Result Note
Diff and targeted source review passed All three changed files were read
Automated tests not run CI results in the description were author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

Runtime tests and the author-reported CI results were not independently verified. Read: description.md, commits.md, manifest.json, diff.patch, GuestMemory.cpp, MappedCopy.cpp, agc_mapped_copy_tests.cmake, General.cpp, GuestArena.cpp, and HostThreadLocal.hpp. No changed files were excluded from reading; files.json was unavailable in the review bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6004260807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24031#pullrequestreview-6004260807). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T08:53:38Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the targeted source review found the guarded comparison paths consistent with the stated behavior. ## What I ran Reviewed head `426310bb13c1cc639da064bb100f497789d3b8af`. No commands or tests were run; `repo_read.py` was used to read the prepared diff and selected source ranges. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | All three changed files were read | | Automated tests | not run | CI results in the description were author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Runtime tests and the author-reported CI results were not independently verified. Read: `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `GuestMemory.cpp`, `MappedCopy.cpp`, `agc_mapped_copy_tests.cmake`, `General.cpp`, `GuestArena.cpp`, and `HostThreadLocal.hpp`. No changed files were excluded from reading; `files.json` was unavailable in the review bundle. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-22 22:24:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T22:24:17Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the Linux and macOS fault guarded compare path and the Windows arena owned path are supported by the code inspected.

What I ran

Reviewed head 223e1fb4297376f60245f3e7dd401adc98f02b3c using repo_read.py for the prepared review materials and targeted source ranges. No build or tests were run.

Results

Check Result Note
Source review passed Changed code and relevant helpers read
Build and tests not run No local execution
Author-reported CI not run Description reports passing CI on a different head

Findings

Blocking

None.

Non-blocking

None.

Not verified

The description reports CI on head 426310bb13c, which differs from the reviewed head, so those results are not evidence of a run on this revision. Runtime behavior across supported OSes, including races outside the covered test scenarios, was not verified. Read: GuestMemory.cpp, MappedCopy.cpp, agc_mapped_copy_tests.cmake, GuestArena.cpp, General.cpp, and HostThreadLocal.hpp. Excluded: none of the changed files listed in the manifest; files.json was not available as a review document.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6014845225 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24031#pullrequestreview-6014845225). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T22:24:17Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the Linux and macOS fault guarded compare path and the Windows arena owned path are supported by the code inspected. ## What I ran Reviewed head `223e1fb4297376f60245f3e7dd401adc98f02b3c` using `repo_read.py` for the prepared review materials and targeted source ranges. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed code and relevant helpers read | | Build and tests | not run | No local execution | | Author-reported CI | not run | Description reports passing CI on a different head | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The description reports CI on head `426310bb13c`, which differs from the reviewed head, so those results are not evidence of a run on this revision. Runtime behavior across supported OSes, including races outside the covered test scenarios, was not verified. Read: `GuestMemory.cpp`, `MappedCopy.cpp`, `agc_mapped_copy_tests.cmake`, `GuestArena.cpp`, `General.cpp`, and `HostThreadLocal.hpp`. Excluded: none of the changed files listed in the manifest; `files.json` was not available as a review document. ## To get this to a good state Nothing is required.
mihiii1337 commented 2026-11-23 14:55:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T14:55:03Z.

Review: APPROVED


Reviewed exact head 223e1fb4297376f60245f3e7dd401adc98f02b3c after the #25446 integration.

I ran a native CPU fixture that compiles the exact Linux/macOS branch logic against an array-backed copy test double, without touching unreadable host memory. All 7 cases passed: an unaligned range larger than 256 KiB, EOF and gap ordering, a difference before a simulated unreadable page, one-shot hook invocation, and preserved errno. A mutation that removed the page-sized retry exited 4 because it reported Unmapped instead of the earlier Differs, so the fixture is sensitive to the core fix.

I also checked the exact-head Hosted Windows log: the compare reset/differs/prefix tests passed 3/3 and the job reported zero failures across 5,531 registered cases. The Linux Hosted job was skipped and the Census tests were skipped on Windows, so I am not claiming an actual Linux syscall run, Linux Census result, or performance measurement for this head. No GPU path was involved.

Within that scope, I found no remaining correctness blocker. This approval is limited to the exact commit above.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6023810071 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24031#pullrequestreview-6023810071). Originally posted by [mihiii1337](https://github.com/mihiii1337) on 2026-11-23T14:55:03Z. **Review:** APPROVED --- Reviewed exact head `223e1fb4297376f60245f3e7dd401adc98f02b3c` after the #25446 integration. I ran a native CPU fixture that compiles the exact Linux/macOS branch logic against an array-backed copy test double, without touching unreadable host memory. All 7 cases passed: an unaligned range larger than 256 KiB, EOF and gap ordering, a difference before a simulated unreadable page, one-shot hook invocation, and preserved `errno`. A mutation that removed the page-sized retry exited 4 because it reported `Unmapped` instead of the earlier `Differs`, so the fixture is sensitive to the core fix. I also checked the exact-head Hosted Windows log: the compare reset/differs/prefix tests passed 3/3 and the job reported zero failures across 5,531 registered cases. The Linux Hosted job was skipped and the Census tests were skipped on Windows, so I am not claiming an actual Linux syscall run, Linux Census result, or performance measurement for this head. No GPU path was involved. Within that scope, I found no remaining correctness blocker. This approval is limited to the exact commit above. AI-assisted: yes
MarcZX10 commented 2026-11-25 08:45:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T08:45:34Z.

Review: APPROVED


Reviewed — looks good

No blocking defect was found in the changed code inspected at head 0ceac5904e2e789733bfe37c13beee954d747fcb; the full CI checks described in the PR were not run here.

What I ran

Read the prepared diff and all four changed files at the pinned head. No build or test command was run; author-reported CI results are not results from this review.

Results

Check Result Note
Changed code inspection passed Four changed files read
Build and tests not run CI results supplied by author

Findings

Blocking

None.

Non-blocking

None.

Not verified

The PR's reported Linux and Windows test runs and negative controls were not independently verified. The full repository contents were not read; changed file contents were read for GuestMemory.cpp, MappedCopy.cpp, agc_mapped_copy_tests.cmake, and ENVIRONMENT.md.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6054344860 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24031#pullrequestreview-6054344860). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T08:45:34Z. **Review:** APPROVED --- # Reviewed — looks good No blocking defect was found in the changed code inspected at head `0ceac5904e2e789733bfe37c13beee954d747fcb`; the full CI checks described in the PR were not run here. ## What I ran Read the prepared diff and all four changed files at the pinned head. No build or test command was run; author-reported CI results are not results from this review. ## Results | Check | Result | Note | |---|---|---| | Changed code inspection | passed | Four changed files read | | Build and tests | not run | CI results supplied by author | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The PR's reported Linux and Windows test runs and negative controls were not independently verified. The full repository contents were not read; changed file contents were read for `GuestMemory.cpp`, `MappedCopy.cpp`, `agc_mapped_copy_tests.cmake`, and `ENVIRONMENT.md`. ## To get this to a good state Nothing is required.
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!24031
No description provided.