Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
chitters2004:fix/compare-mapped-fault-guard-v2
Nov 27, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
chitters2004:fix/compare-mapped-fault-guard-v2

Conversation

@chitters2004

@chitters2004 chitters2004 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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 perf(agc): take the /proc/self/maps fallback off the draw path on kernels without PROCMAP_QUERY #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 (c3a8236); no other open PR implements the same functions. fix(agc): keep the driver's reads of known-zero guest pages out of Windows write-watch #20084 edits the memcmp inside CompareMapped for known-zero pages but does not guard it; fix(agc): copy guest memory shared with other threads byte-wise atomically #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

CompareMapped read guest bytes with a plain memcmp after the cached page
state said they were readable. Invalidation of that state runs after the
protection change, so an arena reset on another thread between the check
and the read faulted the process. boykopovar#15487 closed the same race for
CopyMapped.

On Windows, ranges the arena owns go through the guarded arena compare.
Only arena memory is reset under a compare there (host regions are never
released on Windows), so every other range keeps the cached-state path. On
Linux and macOS the bytes are copied in chunks through the fault-guarded
guest copy (the same primitive CopyMapped uses) and compared there. A fault
reports the range unmapped and leaves errno alone. When a chunk faults, its
pages are compared one by one, so a difference before the unmapped page is
still reported as a difference.

Tests reset the range from the compare hook, between the page-state check
and the read, check that unequal bytes across two committed units are
still reported, and check that a difference or an equal prefix before a
reset unit reports Differs or Unmapped.
@github-actions github-actions Bot added build agc libSceAgc* labels Nov 20, 2026
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

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
PR 15058 core/libs/prx/libSceAgcDriver/tests/cmake/agc_mapped_copy_tests.cmake L12

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1896 docs/dev/ENVIRONMENT.md
PR 4448 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 4472 docs/dev/ENVIRONMENT.md
PR 6186 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6192 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6209 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6924 docs/dev/ENVIRONMENT.md
PR 7275 docs/dev/ENVIRONMENT.md
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 7643 docs/dev/ENVIRONMENT.md
PR 8246 docs/dev/ENVIRONMENT.md
PR 9289 docs/dev/ENVIRONMENT.md
PR 9345 docs/dev/ENVIRONMENT.md
PR 9356 docs/dev/ENVIRONMENT.md
PR 9387 docs/dev/ENVIRONMENT.md
PR 9508 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 9712 docs/dev/ENVIRONMENT.md
PR 9852 docs/dev/ENVIRONMENT.md
PR 10193 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 10442 docs/dev/ENVIRONMENT.md
PR 11435 docs/dev/ENVIRONMENT.md
PR 11436 docs/dev/ENVIRONMENT.md
PR 12324 docs/dev/ENVIRONMENT.md
PR 12746 docs/dev/ENVIRONMENT.md
PR 12849 docs/dev/ENVIRONMENT.md
PR 13809 docs/dev/ENVIRONMENT.md
PR 13863 docs/dev/ENVIRONMENT.md
PR 13920 docs/dev/ENVIRONMENT.md
PR 13928 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 13929 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 14262 docs/dev/ENVIRONMENT.md
PR 14473 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 15136 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 15456 docs/dev/ENVIRONMENT.md
PR 15529 docs/dev/ENVIRONMENT.md
PR 15548 docs/dev/ENVIRONMENT.md
PR 15570 docs/dev/ENVIRONMENT.md
PR 16217 docs/dev/ENVIRONMENT.md
PR 16793 docs/dev/ENVIRONMENT.md
PR 16831 docs/dev/ENVIRONMENT.md
PR 16879 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 16953 docs/dev/ENVIRONMENT.md
PR 17557 docs/dev/ENVIRONMENT.md
PR 17719 docs/dev/ENVIRONMENT.md
PR 17729 docs/dev/ENVIRONMENT.md
PR 18082 docs/dev/ENVIRONMENT.md
PR 18173 docs/dev/ENVIRONMENT.md
PR 18226 docs/dev/ENVIRONMENT.md
PR 18390 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 20084 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 20172 docs/dev/ENVIRONMENT.md
PR 20248 docs/dev/ENVIRONMENT.md
PR 20315 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 20519 docs/dev/ENVIRONMENT.md
PR 21211 docs/dev/ENVIRONMENT.md
PR 21225 docs/dev/ENVIRONMENT.md
PR 22062 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 22071 docs/dev/ENVIRONMENT.md
PR 22190 docs/dev/ENVIRONMENT.md
PR 22424 docs/dev/ENVIRONMENT.md
PR 22434 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/ENVIRONMENT.md
PR 22647 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_mapped_copy_tests.cmake
docs/dev/ENVIRONMENT.md
PR 22692 docs/dev/ENVIRONMENT.md
PR 22944 docs/dev/ENVIRONMENT.md
PR 23244 docs/dev/ENVIRONMENT.md
PR 23596 docs/dev/ENVIRONMENT.md
PR 23975 docs/dev/ENVIRONMENT.md
PR 24346 docs/dev/ENVIRONMENT.md
PR 24661 docs/dev/ENVIRONMENT.md
PR 25534 docs/dev/ENVIRONMENT.md
PR 26165 docs/dev/ENVIRONMENT.md
PR 26340 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 26608 docs/dev/ENVIRONMENT.md
PR 26613 docs/dev/ENVIRONMENT.md
PR 26913 docs/dev/ENVIRONMENT.md
PR 27133 docs/dev/ENVIRONMENT.md

@Colewiz7

Copy link
Copy Markdown
Contributor

Head 426310bb13 merged onto main e825fa1492 as f444ac39e4, no conflict. Linux 7.3.4, GCC 16.2.1 Release, Mesa 26.2.6 RADV, RX 7900 XT (gfx1100) and 7800X3D iGPU (gfx1036).

No new failures and no new validation messages on either GPU.

RX 7900 XT iGPU
tests run (6358 on the 7900 XT, the 2039 GPU ones on the iGPU), new failures vs main none none
-R '^agc' ( tests) with the layer, new failures vs main none none
VUID- / SYNC-HAZARD lines 0 / 0 0 / 0

Layer runs used VK_LAYER_KHRONOS_validation 1.4.363.1 with VK_LOADER_DEBUG=layer and ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1; the loader inserted the layer in every run, and the set of distinct validation messages matches main.

AI-assisted: yes

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head 426310bb13, applied with git apply on main 86d2ba2 together with the other eight PRs of one batch (#23921, #23955, #24070, #24072, #24073, #24077, #24081, #24130). All nine applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU runs: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon".

tests RTX 3080 iGPU
agc_mapped_copy_* (9 tests, including the new compare-reset, compare-differs, compare-prefix) all pass all pass

Whole batch: the full ctest -j 12 (6496 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main 86d2ba2 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). The 2099 agc_/video tests on the iGPU ran twice. The second run fails only agc_driver_stencil_plane_memory and agc_driver_store_over_untracked, both known on main. The first run also failed agc_driver_image_table_packed_guards once; it passes 12 of 12 alone on both GPUs and did not recur.

On Windows the new code path is the GuestArenaCompareOwned one, so these runs exercise it.
This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 22, 2026
@chitters2004

Copy link
Copy Markdown
Contributor Author

Merged main (223e1fb) to resolve the conflict with #25446 in CompareMapped.

#25446 added compareWithoutQuery: on Linux kernels without PROCMAP_QUERY it compares host memory outside the arena and the main image through GuestCopyInUnsynced, falling back to the scan when a piece faults. This PR already compares every Linux and Apple range through GuestCopyInUnsynced, falling back per page instead of to the scan, so CompareMapped no longer reaches the /proc/self/maps scan at all. That makes compareWithoutQuery unreachable, so the merge drops it together with APS5_NO_COPY_COMPARE and its ENVIRONMENT.md row. @oneandonlydean, could you check that's fine with you? The CompareMapped gap checks your second commit added to the graphics tests hold this path to the same answers (equal, different, across an unmapped page, a difference before the gap, a range longer than one piece).

Tested on Windows 11 after the merge: the 56 runnable tests matching agc_mapped_copy, agc_driver_graphics, agc_driver_draw_allocations and linked_code pass (the three draw-allocation census tests skip on Windows). Upstream CI covers the Linux path.

AI-assisted: yes

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added docs and removed needs rebase Conflicts with main, rebase needed labels Nov 22, 2026

@mihiii1337 mihiii1337 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 24, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 24, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean
oneandonlydean merged commit 4cb7752 into boykopovar:main Nov 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants