Skip to content

fix(agc): keep a rewatched import's memory compared once it is shared - #9159

Merged
adriadam10 merged 13 commits into
boykopovar:mainfrom
EagleBearerYS:fix/agc-rewatched-imports-compared-when-shared
Oct 24, 2026
Merged

adriadam10 merged 13 commits into
boykopovar:mainfrom
EagleBearerYS:fix/agc-rewatched-imports-compared-when-shared

Conversation

@EagleBearerYS

@EagleBearerYS EagleBearerYS commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows, a fault that AnyPS5 resumes in guest code can overwrite the guest's red zone, [rsp-0x80, rsp-0x10) (#361). Since #6678 this happens again in Grand Theft Auto V (PPSA04264)'s open world, in every run. @tibzejoker's report in #9136:

FATAL: unhandled exception 0xc0000005 on thread '[RAGE] HDD Streamer'
  rip gtav.exe+0x28baa50   (eboot 0x28aaa50)
  read of 0x000000000000774f

At eboot 0x28aaa50 the game's zlib inflate_fast reloads two pointers from its red zone, then reads through a garbage one. Their bisect counts the faults AnyPS5 resumes over 60 s of Story (Windows 11 26200, RTX 4090 with NVIDIA 616.92, i9-13900K):

tree (from #9136) resumed faults open world
main 47a266c, and the merge before #6678 0 no crash
#6678's merge 755fecb 134
main f6ce8e3 400+, ~60 of them in zlib, all MEM_MAPPED crash, 7 of 7 runs
main 7280a17 with APS5_NO_DEFERRED_SETTLES=1 133 (85 MEM_MAPPED, 44 MEM_PRIVATE), 0 in zlib no crash in 150 s

Why #6678 brings the faults back:

  • When a small import's deferred settle completes, rewatchSettledImports returns its range to the write watch through GuestMemory::Rewatch.
  • At that point Grand Theft Auto V's buffers are resident direct memory: private pages that GetWriteWatch tracks without faults.
  • Later, a second mapping of the same direct memory turns the run into 16 KiB shared views (WindowsMappings::demote). From then on every clearing collect over the range makes all its aliases read-only, so each guest store faults and is resumed.
  • Before perf(agc): settle Windows imports under 1 MiB without waiting #6678 the import kept the range out of the write watch for good, so it was compared and never armed.
  • A check at rewatch time can't see this, because the memory only turns shared afterwards.

A second source of the same faults, from @tibzejoker's next bisect (#9136 comment). These are MEM_MAPPED faults over 60 s of Story with APS5_NO_DEFERRED_SETTLES=1, so #6678's path is out:

tree (from #9136) MEM_MAPPED resumed faults
e9cc4a8, the merge before #8172 0
d9df34d, #8172's merge 6
28f1266, #6598's merge 134
28f1266 with #8172 reverted 1

The change, in seven commits, plus two test-only commits and a debt entry:

  1. libc (98adb5f): WindowsMappings::CompareWhenShared marks the pages of a range, and makes writable again any alias of them that is already armed.
    • Once a marked page is shared, collectable() and the fresh-collect pre-check refuse a clearing collect over it before anything is made read-only. Its aliases are never armed, and the caller gets no write record, so it compares the range.
    • While a marked page is still a resident run, it's collected through GetWriteWatch as before.
  2. agc (d98ee15): Rewatch marks each range with GuestArenaCompareWhenShared before it returns the range to the watch.
  3. test (ead292e): CheckComparedWhenShared forgets its section's handle value before it maps. WindowsMappings keys its physical pages by that value, so a value that an earlier case closed without ForgetSection could come back with that case's stored pages. Test-only.
  4. agc (81aec58): ImportWatched marks the resident pages of each run it keeps in the watch, with GuestArenaCompareResidentWhenShared, a resident-only variant of the same mark.
  5. libc (47c8df4): the mark survives a full unmap and remap.
    • It lived only on the shared page, which WindowsMappings holds weakly. Once every view of that memory was unmapped, the next Map made a fresh, unmarked page, and the next clearing collect armed its views again.
    • CompareWhenShared now also marks the physical page, and a shared page made for it starts marked. ForgetSection still drops the mark, so a reused section handle starts unmarked.
  6. libc (1ad5f29): the resident runs an import's read-write alias turns into shared views are marked too.
    • An import over a range that isn't all writable private memory maps a read-write alias first, and MapAlias turns every resident run in the range into shared views before ImportWatched runs. The import's resident-only mark skipped those views, so the import's walk and every later clearing collect armed them.
    • MapAlias now records the views it turns shared, and the resident-only mark includes them. A second guest mapping of the page clears the record, so memory the guest itself shares keeps today's tracking.
  7. libc (37a5d94): a compared shared page is reported once.
    • When a page this change marks turns into a shared view while it's compared, WindowsMappings flags it unreported.
    • The first refused collect over it takes the flag and reports the page as a CPU write, once, as main reports the pages at the first collect after the sharing.
  8. agc (1712336): compared shared memory is never read as unchanged since before a refused collect.
    • The stale read @embersas's GPU fixture found (comment): a texture cached before the sharing, then shared, then written with no lookup in between, came back from the first lookup unchanged, with no comparison.
    • A refused walk now records its generation for each 64 KiB block that holds compared shared pages. UnchangedSince, UnchangedSinceAll and UnchangedCommittedSince answer false against an older generation there, so a cache compares bytes instead of trusting stamps that never came.
    • The report alone wasn't enough: the dispatch cache takes the newest of several spans' collects, so a refused span's 0 hid behind another span's generation.
    • WrittenSince, ChangedBlocks, StoredOver and NewestStamp stay stamp-only, so storage textures and depth surfaces don't reload or drop at every epoch. Stamping the whole range at every refused collect would make depth surfaces reload from guest memory at every bind, dropping depth the GPU drew but hasn't stored.
    • Cost: one std::map::lower_bound per UnchangedSince* call (the map is empty on Linux), and on Windows two WindowsMappings listings per refused walk. Analytical, not timed.
  9. test (388f5f1): agc_driver_compared_shared_snapshot takes embersas's ordering through the production CachedSampledTexture.
  10. docs (cb9de80): a debt entry for depth surfaces over compared shared memory (below).

Scope:

  • Only memory that a rewatch returned to the watch, or that an import kept watched while it was resident or that its alias turned shared, is affected. Other shared views are armed and collected as before, including direct memory that was never imported and is mapped twice.
  • Linux and macOS are unchanged: WindowsMappings, Rewatch's Windows branch and the new mark in ImportWatched are Windows-only.
  • Behaviour change: native multisampled depth over compared shared memory now throws "guest writes over native multisample depth are not implemented" at the first check after a refused collect, as main throws it at the sharing. Before the eighth commit, this PR trusted the missing stamps there.
  • A gap left as it is: a depth surface over such memory doesn't see CPU stores made after the sharing, as over unwatched memory. The new debt entry says so.

Overlap:

Tested

  • Grand Theft Auto V (PPSA04264), by @tibzejoker (#9136 comment): main 663de1a + Dripl1x's d6e270e (fix(libc): stop write-protecting resident runs on Windows #9181) + 81aec58, 150 s of Story with deferred settles on.
    • Their verdict: "the crash is fixed with the two fixes together". 0 MEM_PRIVATE, 0 faults in zlib, 0x162b9e4 gone, no crash.
    • 105 MEM_MAPPED faults remain, at eboot 0x28f413a (a store in a loop, not a red-zone leaf) and in a system DLL's host code. Both write 16 KiB views that were shared when written. As they read it, that is direct memory that was never imported and is mapped twice, which this change leaves armed.
  • Grand Theft Auto V (PPSA04264) on a second machine, by @dantdmdarealguy (#9136 comment): Windows 11 Pro, i9-14900K, RTX 5080 (NVIDIA 32.0.16.1742), WinLibs GCC 15.2.0, Release, game 01.005.000, warm caches, the same scripted pad input in alternating runs of 600-900 s. Their tree needed fix(voice): accept valid worker priorities and affinities in sceVoiceSetThreadsParams #9730's sceVoiceSetThreadsParams fix to reach gameplay.
  • Grand Theft Auto V (PPSA04264), the faults that remain, by @tibzejoker (#9159 comment, #9136 comment): main 3bbd232 + fix(libc): stop write-protecting resident runs on Windows #9181 + this PR up to 81aec58 + fix(voice): accept valid worker priorities and affinities in sceVoiceSetThreadsParams #9730 + fix(agc): avoid CPU waits for GPU-ordered labels #9445, Windows 11, RTX 4090, APS5_MIMALLOC=1, 7-minute open-world runs with every resumed write fault logged.
    • Across about 15 runs with both PRs, 2 ended in the HDD Streamer crash.
    • In the 8 logged runs, 4 still had 1 to 4 resumed writes from zlib's inflate_fast, one per 4 KiB subpage of a fresh 16 KiB MEM_MAPPED view. One of those runs crashed, on the thread of its only zlib fault. The other 4 runs had none.
    • Their local version of the remap commit (the mark kept on the physical page) still saw resumed zlib writes in its 4 runs, with 1 crash. So the remaining faults come from another path, which this PR doesn't cover.
    • Their tree has neither 47c8df4 nor 1ad5f29. 1ad5f29 closes one path that arms such views, and this PR doesn't claim to fix these faults.
    • Two more paths that turn resident memory into unmarked shared views are being checked: breakRun, where unmapping part of a resident run turns the whole run shared, and the demotion of a whole resident run when a second mapping shares one of its pages. A diagnostic that names the path behind each armed view would tell them apart in their runs.
  • Independent CPU-only validation, by @embersas (#9136 comment; Ryzen 9 3900X, Windows 11 26300.9711, WinLibs GCC 15.2.0, Release; no game was run). This PR's head 1ad5f29 and fix(libc): stop write-protecting resident runs on Windows #9181's d6e270e, on main 0a0ed72 alone and together, plus test/9159-9181-together (d7cec80):
    • The focused tests pass on every tree, 3 runs each: guest_memory, windows_write_watch, agc_write_tracking, agc_write_tracking_metadata_commit, agc_resident_direct_remap, agc_rewatched_direct_shared, agc_import_alias_shared, agc_host_import_retire and more.
    • The probe: 16 known qwords in [rsp-0x80, rsp), one 32-byte store per case, any fault resumed by GuestArenaHandleWrite; 90 executions. With this PR, the four shared cases no longer fault and keep all 16 qwords: rewatched, then a second mapping; a watched import, then a second mapping; rewatched, every view unmapped, mapped twice; an import alias over resident memory. On main, each faults and loses 13.
    • Not this PR's cases: the resident 2 MiB case is fix(libc): stop write-protecting resident runs on Windows #9181's, and is fixed by it, not by this PR. Unmarked shared views still fault on every tree, which is the class feat(relinker): frame red-zone leaves without a frame pointer by moving their rsp operands #5640 (now merged) is for.
    • Failing controls: each piece of this PR reverted on its own (the two refusal checks, the Rewatch mark, the ImportWatched mark, the physical-page mark, demote(..., true)) makes its test fail and the store fault again. With only the refusal checks reverted, the alias store didn't fault in that sequence, but agc_import_alias_shared failed on the refusal.
    • Not covered there: the comparison callers use after a refused collect, breakRun and the whole-run demotion paths, the GPU tests, and any game.
  • windows_write_watch:
    • CheckComparedWhenShared: a marked resident range stays collected. A second mapping then makes it shared. From then on its collects fail, neither mapping is read-only, and stores into it raise no fault (the test's vectored handler counts them). The unmarked half of the same mapping is still collected.
    • CheckResidentOnlyComparedWhenShared: a resident-only mark leaves a shared view armed and collected, and still keeps a resident page compared once a second mapping makes it shared.
    • CheckComparedWhenSharedRemapped: a marked resident page is unmapped, then mapped into two views. A clearing collect through either view refuses, neither is made read-only, and a store raises no fault. After ForgetSection the same mapping is collected and armed again.
  • New ctest agc_import_alias_shared (WIN32, a process mode of agc_write_tracking_tests, next to agc_rewatched_direct_shared): it imports direct memory whose range needs a read-write alias, the way an import maps one, and requires that the pages the alias turned shared are compared and not armed. Memory the guest itself mapped twice before the alias is still armed, as before.
  • New ctest agc_rewatched_direct_shared (WIN32, its own process mode of agc_write_tracking_tests, in the PATH list), with two cases:
    • Rewatched: it maps direct memory resident, takes a block out of the watch and rewatches it as a settle does, then maps the same memory a second time. The block must collect while resident, then collect as 0 through either mapping, stay writable, and show a store through both mappings. A block that never left the watch must still be collected.
    • Imported: resident direct memory imported with the watch kept, then mapped a second time, must collect as 0 through either mapping, stay writable and show a store through both. Unimported shared direct memory must still be collected.
  • The stale read through the sampled cache, on a GPU (agc_driver_compared_shared_snapshot, this PC, Windows 11). The test runs on its first Vulkan device, which on this PC is the AMD Radeon integrated GPU (driver 31.0.21923.1000), not the RTX 3070. It checks the CPU-side cache logic; the GPU only reads the texels back.
  • The stale read on an NVIDIA GPU, by @embersas (comment): their external fixture, unchanged from their first report, on Windows 11 26300, Ryzen 9 3900X, RTX 3080 Ti (NVIDIA 610.88), WinLibs GCC 15.2.0, Release, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 and the Khronos validation layer; no game was run. Four orderings, three repetitions per tree:
    • main ef0d8ae: 12 of 12 pass. Their earlier branch of this PR with fix(libc): stop write-protecting resident runs on Windows #9181 (d7cec80): the two orderings that write from the CPU after the sharing, with no lookup in between, still return the stale first lookup, 3 of 3 each; the other two pass.
    • This head, cb9de80, without fix(libc): stop write-protecting resident runs on Windows #9181: 12 of 12 pass. In the two affected orderings the first lookup makes one TextureCompare read, replaces the texture, and the GPU reads the new texel (0xa0001234, against 0x10001234 before); the other texels match, and the store raises no write fault.
    • agc_driver_compared_shared_snapshot passes 3 of 3 on this head on the RTX 3080 Ti, none skipped.
    • The layer loaded in every execution, with no VUID-, validation or synchronization message.
    • Their scope: a synthetic fixture that calls Unwatch/Rewatch directly and reads through a compute texelFetch. It doesn't cover host-import settling, draws, other caches or orderings, other GPUs, or any game.
  • GitHub's runners:
    • df89dd1, merged with main dab0113 (run). The merge's one conflict was docs/dev/debt/libSceAgcDriver.md, where main added its "Unmapped depth planes" entry at the same place as this PR's "Depth surfaces over compared shared memory" entry; both are kept. The PR's own added and removed lines are unchanged.
      • on Windows, the same 12 tests run ×10: 11 pass, 1 skips, 0 fail. agc_driver_compared_shared_snapshot skips, as the runner has no Vulkan device;
      • on Linux, agc_write_tracking, agc_write_watch_coverage, guest_memory, guest_shm_open and guest_jit_shared_memory pass ×10: 5 run, 5 pass, 0 skip, 0 fail;
      • the libc and libkernel suites pass on both (169 and 217 tests on Windows, 142 and 204 on Linux), run with APS5_SKIP_TIMING_TESTS=1.
    • 719a048, merged with main 1e20650 (run). The merge's one conflict was the list of cases in WindowsWriteWatch.cpp's main(), where main added three private-commit cases next to this PR's four; all seven run. The PR's own added and removed lines are unchanged.
      • on Windows, windows_write_watch, agc_rewatched_direct_shared, agc_import_alias_shared, agc_compared_shared_generations, agc_resident_direct_remap, agc_write_tracking, agc_write_tracking_metadata_commit, agc_write_watch_coverage, guest_memory, guest_shm_open and guest_jit_shared_memory pass ×10: 12 run, 11 pass, 1 skip, 0 fail. agc_driver_compared_shared_snapshot skips, as the runner has no Vulkan device;
      • on Linux, agc_write_tracking, agc_write_watch_coverage, guest_memory, guest_shm_open and guest_jit_shared_memory pass ×10: 5 run, 5 pass, 0 skip, 0 fail;
      • the libc and libkernel suites pass on both (147 and 213 tests on Windows, 120 and 203 on Linux), run with APS5_SKIP_TIMING_TESTS=1.
    • The report commit, 37a5d94 (run): its only red is a build failure: its new windows_write_watch case uses the call the commit adds, so it doesn't build with 1ad5f29's code. The next commit's red covers the behaviour. At 37a5d94, on Windows, windows_write_watch, agc_rewatched_direct_shared, agc_import_alias_shared, agc_resident_direct_remap, agc_write_tracking and agc_write_tracking_metadata_commit pass 10 runs in a row, 6 of 6, none skipped; on Linux, agc_write_tracking passes 10 runs in a row. The libc suite passes on both and the libkernel suite on Linux; on Windows, guest_raise_in_system_call timed out once at the end of the libkernel suite, the runner timeout above.
    • The refusal-mark commit, 1712336 (run): red with 37a5d94's code, 3 of 3 on Windows: agc_compared_shared_generations fails with "write tracking test failed: a store into rewatched direct memory once shared reads as unchanged since a generation before the sharing", and the other 6 pass. Green at 1712336: on Windows those 7 pass 10 runs in a row, none skipped; on Linux, agc_write_tracking passes 10 runs in a row. The libc and libkernel suites pass on both.
    • The head, 388f5f1 (run): on Windows the same 7 pass 10 runs in a row, none skipped.
    • The alias commit, 1ad5f29 (run):
      • red on Windows with the head before it (47c8df4), 3/3: agc_import_alias_shared fails with "memory an import's alias demoted was armed by the import";
      • green at 1ad5f29: on Windows, agc_import_alias_shared, windows_write_watch, agc_rewatched_direct_shared, agc_resident_direct_remap, agc_write_tracking and agc_write_tracking_metadata_commit pass ×10; on Linux, agc_write_tracking passes ×10;
      • the libc suite passes on both (142 on Windows, 115 on Linux) and the libkernel suite on Linux (197). On Windows, guest_raise_in_system_call timed out at 30 s on both arms, 47c8df4 and 1ad5f29, so 206 of 207 pass; this commit changes nothing in libkernel. The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners;
      • under Wine, the new case is red 10/10 on 47c8df4 and green 10/10 at 1ad5f29, with two control cases, and a mutant without the line that clears the record fails.
    • The remap commit, 47c8df4 (run):
      • red on Windows with the head before it (99aeb0a), 3/3: "a page to be compared when shared was collected after every view was unmapped and it was mapped again";
      • green at 47c8df4: on Windows, windows_write_watch, guest_shm_open, guest_jit_shared_memory and guest_memory pass ×10, and the libc (142) and libkernel (207) suites pass; on Linux, the last three pass ×10, and the libc (115) and libkernel (197) suites pass;
      • the suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners;
      • under Wine 9.0, a build of the new case alone is red 10/10 on 99aeb0a and green 10/10 at 47c8df4. The full windows_write_watch stops earlier under Wine 9.0 on both trees, so the GitHub run is the check for the rest.
    • 99aeb0a, merged with main df2569d (run), with the PR's own diff unchanged by the merge:
      • on Windows, windows_write_watch, agc_rewatched_direct_shared, agc_write_tracking, agc_write_tracking_metadata_commit, agc_resident_direct_remap, agc_write_watch_coverage and guest_memory pass ×10, and the libc (142) and libkernel (207) suites pass;
      • on Linux, agc_write_tracking, agc_write_watch_coverage and guest_memory pass ×10, and the libkernel suite passes (197). libc's only failure is precise_wait, a timing test that fails there with main's code too.
    • Red for the first two commits, with only the two checks in collectable() and the fresh pre-check reverted (run): "a page to be compared when shared was collected once shared" and "rewatched direct memory was collected once shared".
    • Red for the import commit, with only its mark reverted on 81aec58 (run): only agc_rewatched_direct_shared fails, with "imported direct memory was collected once shared".
    • Green at 81aec58 (run):
      • on Windows, windows_write_watch, agc_rewatched_direct_shared, agc_write_tracking, agc_write_tracking_metadata_commit and agc_resident_direct_remap pass ×10, and the libc suite passes;
      • in the libkernel suite, guest_raise_in_system_call hit its 30 s timeout once; this change doesn't touch libkernel;
      • on Linux, agc_write_tracking and the suites pass.
    • ead292e on its own (run): green on both runners.
    • With main's code, the new cases don't build, because they use the new calls.
    • With fix(agc): put a retired import's unchanged ranges back in the write watch #8926 on top (test/9136-guard-8926-v3, run): 33 Windows tests pass, including agc_driver_graphics with fix(agc): put a retired import's unchanged ranges back in the write watch #8926's retired-import case. The same libkernel timeout appeared there once.
  • AGC: 4x DCC refill resolve loses watched color after an adjacent aligned import (Windows RTX5080) #8103's DCC tests on Linux (lavapipe, CI's flags), on 79cae21, which has the same patch as 81aec58:
    • agc_driver_msaa_dcc_clear_4 and agc_driver_msaa_dcc_state pass 10/10;
    • agc_driver_msaa_dcc_clear_2 and _clear_8 skip, because lavapipe has only 1 and 4 color samples; main skips them the same way;
    • ^agc passes 862/862 on the head and on main.
    • The change is Windows-only, so this shows the Linux side is unchanged. The Windows runner has no Vulkan device for these tests; the GPU run is below.
  • The same DCC tests on a GPU: NVIDIA GeForce RTX 3070 Laptop GPU (driver 617.42, the only device selected, Windows 11; GPU clocks weren't held, pass/fail only), on 5e8db4e, a Windows tree of main 27bcd6a with fix(agc): put a retired import's unchanged ranges back in the write watch #8926 and this PR's three code commits (all but the test-only ead292e), from before the merge with main. ctest -R '^agc_driver_msaa_dcc_(clear_[248]|state)$' --repeat until-fail:10 passes all four 10 of 10, _clear_2 and _clear_8 included.
  • Resident Evil Village (PPSA01556) on that tree and GPU, the intro check for Windows: since #6678, GTA V crashes in the open world: resumed write faults clobber the red zone again (#361) #9136's guard: a 12-minute run from a cold shader cache reached the VR question with no "an address-based build copies … past 64 MiB" line, no abort and no FATAL. The largest guest upload was 63.8 MiB.
  • Validation layer, main against cb9de80, on GitHub's Linux runner (llvmpipe, LLVM 20.1.2) (run): agc_write_tracking, agc_driver_partially_resident_snapshot and the three agc_driver_host_import_* tests, 5 of 5 passing on both trees. The layer loaded 4 times on each, and neither log has a VUID- message, so the change adds none. The new GPU test, agc_driver_compared_shared_snapshot, runs only on Windows, so this comparison doesn't cover it. On the Windows runner, without a Vulkan device, 7 of the 11 tests pass and the 4 device tests skip.
  • check_conventions.py passes, with no comment lines added.

Checklist

On Windows a fault that AnyPS5 resumes in guest code can overwrite the
guest's red zone, [rsp-0x80, rsp-0x10) (boykopovar#361). WindowsMappings arms every
alias of a shared page read-only when a clearing collect settles it, so
each guest store into that page faults and is resumed.

CompareWhenShared marks the pages of a range, and disarms any alias of
them that is already armed. Once a marked page is shared, collectable()
and the fresh pre-check refuse a clearing collect over it before anything
is armed, so its aliases are never armed and the caller compares the
range instead. While the page is still a resident run it is collected
through GetWriteWatch as before, which takes no fault.

windows_write_watch gains CheckComparedWhenShared: a marked resident
range stays collected, then a second mapping makes it shared, and from
then on its collects fail, neither mapping is armed and stores into it
take no fault, while the unmarked half of the same mapping is still
collected.
Since boykopovar#6678 a Windows host import under 1 MiB returns to the write watch
when its deferred settle completes (rewatchSettledImports). Grand Theft
Auto V (PPSA04264) imports buffers that are resident direct memory at that
point and later become shared 16 KiB views, when a second mapping of the
same direct memory demotes them. From then on the write watch arms them,
every guest store into them faults and is resumed, and the resumed faults
damage the red zone the game's zlib inflate_fast reloads its pointers
from, so the open world crashes in every run (boykopovar#9136).

Before boykopovar#6678 such a range stayed out of the write watch for good and was
compared. Rewatch now marks each range it returns to the watch with
GuestArenaCompareWhenShared first. While the memory stays resident it is
write-watched, as boykopovar#6678 intends. Once it is shared its collects fail and
it is compared again, as before boykopovar#6678, instead of being armed.

agc_rewatched_direct_shared maps direct memory resident, takes a block
out of the watch and rewatches it as a settle does, then maps the same
memory a second time. The rewatched block must collect while resident,
then collect as 0 through either mapping, stay unarmed and show the
store through both, while a block that never left the watch is still
collected.
@github-actions

github-actions Bot commented Oct 21, 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 7505 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp L1796
PR 10026 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp L1796
PR 10655 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp L1459

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 1368 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 1395 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 1835 core/libs/prx/libc/src/GuestArena.cpp
PR 1896 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 2434 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 4448 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 5021 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libc/src/GuestArena.cpp
PR 5058 docs/dev/debt/libSceAgcDriver.md
PR 5371 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 5372 docs/dev/debt/libSceAgcDriver.md
PR 5925 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6186 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6187 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6188 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6192 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6194 docs/dev/debt/libSceAgcDriver.md
PR 6209 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6223 docs/dev/debt/libSceAgcDriver.md
PR 6273 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
docs/dev/debt/libSceAgcDriver.md
PR 6280 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6300 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6412 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6481 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6548 docs/dev/debt/libSceAgcDriver.md
PR 6549 docs/dev/debt/libSceAgcDriver.md
PR 6642 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 6804 docs/dev/debt/libSceAgcDriver.md
PR 6808 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6867 docs/dev/debt/libSceAgcDriver.md
PR 6959 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 6960 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6977 docs/dev/debt/libSceAgcDriver.md
PR 7048 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7201 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
core/libs/prx/libc/src/GuestArena.cpp
PR 7223 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7256 docs/dev/debt/libSceAgcDriver.md
PR 7275 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7449 docs/dev/debt/libSceAgcDriver.md
PR 7457 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7464 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7604 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
docs/dev/debt/libSceAgcDriver.md
PR 7668 docs/dev/debt/libSceAgcDriver.md
PR 7677 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 7910 docs/dev/debt/libSceAgcDriver.md
PR 8014 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 8223 docs/dev/debt/libSceAgcDriver.md
PR 8246 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 8319 docs/dev/debt/libSceAgcDriver.md
PR 8484 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 8520 docs/dev/debt/libSceAgcDriver.md
PR 8592 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 9335 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 9356 docs/dev/debt/libSceAgcDriver.md
PR 9387 docs/dev/debt/libSceAgcDriver.md
PR 9393 docs/dev/debt/libSceAgcDriver.md
PR 9401 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 9431 docs/dev/debt/libSceAgcDriver.md
PR 9432 docs/dev/debt/libSceAgcDriver.md
PR 9592 docs/dev/debt/libSceAgcDriver.md
PR 9672 docs/dev/debt/libSceAgcDriver.md
PR 9673 docs/dev/debt/libSceAgcDriver.md
PR 9674 docs/dev/debt/libSceAgcDriver.md
PR 9675 docs/dev/debt/libSceAgcDriver.md
PR 9676 docs/dev/debt/libSceAgcDriver.md
PR 9700 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 9842 docs/dev/debt/libSceAgcDriver.md
PR 9852 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 9875 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 9891 docs/dev/debt/libSceAgcDriver.md
PR 9892 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 9893 docs/dev/debt/libSceAgcDriver.md
PR 9895 docs/dev/debt/libSceAgcDriver.md
PR 9897 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 9898 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 9904 docs/dev/debt/libSceAgcDriver.md
PR 9905 docs/dev/debt/libSceAgcDriver.md
PR 9906 docs/dev/debt/libSceAgcDriver.md
PR 10046 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 10049 docs/dev/debt/libSceAgcDriver.md
PR 10193 core/libs/prx/libSceAgcDriver/CMakeLists.txt
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
docs/dev/debt/libSceAgcDriver.md
PR 10233 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 10420 docs/dev/debt/libSceAgcDriver.md
PR 10442 core/libs/prx/libSceAgcDriver/CMakeLists.txt
PR 10515 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md
PR 10581 core/libs/prx/libSceAgcDriver/CMakeLists.txt
docs/dev/debt/libSceAgcDriver.md

…g case

WindowsMappings keys its physical pages by the section handle's value. A
handle value that an earlier case closed without ForgetSection can come
back from CreateFileMappingW with that case's stored pages, and then the
first mapping is shared from the start instead of resident, so
CheckComparedWhenShared would fail its residency check. It passes today
only because the case before it forgets its section. The case now
forgets its own handle value before it maps.
Since boykopovar#8172, a host import that stays in the write watch keeps each of
its watched runs in the watch instead of taking the whole import out
when part of it was unwatched. Grand Theft Auto V (PPSA04264) imports
resident direct memory that way, and later maps the same memory at a
second address, which turns it into shared views. From then on the write
watch arms them, and the game's own stores fault and are resumed, the
red zone hazard of boykopovar#361 (boykopovar#9136, eboot 0x162b9e4 and 0x28f413a).

ImportWatched now marks the resident pages of each run it keeps in the
watch with GuestArenaCompareResidentWhenShared. They stay write-watched
while they are private, as boykopovar#8172 needs for resident DCC targets. Once
they are shared, their collects fail and they are compared, as before
boykopovar#8172, instead of being armed. Memory that is already shared when it is
imported is not marked and stays tracked as before.

agc_rewatched_direct_shared gains the imported case: resident direct
memory imported with the watch kept, then mapped a second time, must
collect as 0 through either mapping, stay writable and show a store
through both, while unimported shared direct memory is still collected.
windows_write_watch gains CheckResidentOnlyComparedWhenShared: a
resident-only mark leaves a shared view armed and collected, and still
keeps a resident page compared once a second mapping makes it shared.

@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.

I reviewed the Windows shared-page comparison changes, the resident-only call path and the added write-watch regressions against the issue evidence. The latest PR body reports zero zlib/resident faults for the combined fix; hosted conventions, relinker, register-context and write-watch checks pass. Platform Windows/Linux product jobs were skipped, and I did not run a local Windows test. I found no code-level blocker in the submitted changes.

Approval is blocked on the repository freshness rule: this head is based on 27bcd6a29e2fd69aba0626610b2350eb8f2847f6, 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/write-watch validation on the updated branch. #9181 is an independent, complementary half of #9136; the two PR heads merge cleanly together, so I found no required dependency ordering between them.

@EagleBearerYS

Copy link
Copy Markdown
Contributor Author

Thanks, @MarcZX10. 99aeb0a merges current main (df2569d) into this branch with no conflicts, and the PR's own diff is unchanged by it. On GitHub's runners (run), the write-watch and resident tests pass 10 runs in a row on Windows, with the libc and libkernel suites, and the focused tests pass on Linux too. Tested has the details. Thanks for checking #9181 as well.

@EagleBearerYS

Copy link
Copy Markdown
Contributor Author

@dantdmdarealguy thanks for the alternating runs of Grand Theft Auto V on the RTX 5080 (#9136 comment): the HDD Streamer crash in 4 of 4 runs without #9181 and this PR, and in none of 3 with them. It's in Tested as a second machine, with your note that the runs didn't count resumed faults.

@VicRaster

Copy link
Copy Markdown
Contributor

@EagleBearerYS thanks, I reran the unchanged external fixture on cb9de8049caa1e730caab2b1ac03185a2badbca8. The stale first lookup is fixed in all three repetitions of both affected orderings.

Same workstation: Windows 11 build 26300, Ryzen 9 3900X, RTX 3080 Ti, NVIDIA 610.88, WinLibs GCC 15.2.0, Release. No game was launched.

Four orderings, three repetitions per tree (36 fixture executions):

Tree Result
Original main baseline ef0d8ae3b5 12/12 pass
Original combined branch d7cec80e8c Both previously failing orderings still return a stale first lookup, 3/3 each; the other two orderings pass
Corrected PR head cb9de8049c 12/12 pass

The two affected orderings cache the texture while resident, share its memory, then write from the CPU with no lookup between sharing and the write. One includes the fixture's explicit refusal-check collect; the other omits it. On the old combined branch, the first lookup still keeps the old texture, performs no byte comparison and the GPU reads 0x10001234. On your corrected head, that first lookup performs one TextureCompare read, replaces the texture and the GPU reads 0xa0001234. Exactly the stored texel changes; all other texels match. The mappings stay writable and the CPU store raises no write fault.

The fixture source and shader are unchanged from the original report. I rebuilt it against the corrected head's own libraries. The baseline and old-branch binaries were rerun, with their runtime library hashes checked against their original build manifests. The corrected PR head was tested directly, without adding #9181; the old control is the original branch with both PRs. This is a comparison of those pinned trees, not isolation of individual commits.

Your new agc_driver_compared_shared_snapshot also passes 3/3 on cb9de8049c, using verbose CTest with --repeat until-fail:3.

All fixture runs and the new regression test used ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. Each execution identified the RTX 3080 Ti and loaded the validation layer. No VUID errors, validation warnings, synchronization hazards, missing-layer warnings, skips or timeouts.

This closes the sampled-texture snapshot gap I reported, within this synthetic scope. I have not retested the host-import lifetime suite in this run, or tested real host-import storage views, buffer/draw/dispatch caches, other shared-memory orderings, other platforms or GPUs, any game, or performance. The fixture still calls Unwatch/Rewatch directly and reads the texture through compute texelFetch, rather than exercising production import settling or a draw.

AI-assisted: yes.

@EagleBearerYS

Copy link
Copy Markdown
Contributor Author

Thanks, @embersas, for rerunning the unchanged fixture on the corrected head, with the old branch alongside as the control. The two orderings that showed the stale first lookup now pass in all three repetitions, and so does agc_driver_compared_shared_snapshot on your RTX 3080 Ti. It's in the Tested section now, scoped the way you described it: the synthetic fixture, not production import settling, draws or a game.

@EagleBearerYS

Copy link
Copy Markdown
Contributor Author

@MarcZX10 99aeb0a answered the refresh with the write-watch tests 10 of 10 on Windows, and cb9de80 since adds the compared-shared fix and agc_driver_compared_shared_snapshot, which embersas reran on an RTX 3080 Ti. Could you re-review this head?

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 24, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 24, 2026 12:50

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 24, 2026
@adriadam10
adriadam10 merged commit 6b23aaf into boykopovar:main Oct 24, 2026
32 of 33 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Re-checked df89dd1: it only merges current main into the branch, the PR's own changes are unchanged, no reviewer request is open on this head (the earlier review was dismissed), and the one red job (nid_database_cache timing out on macOS in a Python tool this diff doesn't touch) passed on rerun; merged.

xXPBSXx added a commit to xXPBSXx/AnyPS5 that referenced this pull request Oct 24, 2026
… run

On Windows a cut inside a resident run turns the pages that are kept into shared views, which boykopovar#9159 keeps compared instead of armed. The cut lifetime test required the write watch to cover the larger side after such a cut. It now accepts a range that is not watched when every page of it is reported as compared, and still requires the GPU to read the guest's bytes. Fixes boykopovar#10746.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants