fix(agc): copy guest memory shared with other threads byte-wise atomically #22156
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22156"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-17T07:05:10Z.
What
Depends on #20185 and #22029. Those make polled guest labels acquire loads and release stores. Their out-of-scope list was the other half of the same pattern: bulk copies of guest memory that another thread may write at the same time. They are deliberate, like the guest's own memory model: a capture samples pages the title is still filling, and a replay restores a delta into command words a queue is reading. With plain
memcpyandmemcmp, ThreadSanitizer reports every one of them as a data race, so theagc_driver_frame_replay*tests cannot be clean under TSan.This PR gives those copies the semantics of C++'s proposed
atomic_load_per_byte_memcpy/atomic_store_per_byte_memcpy(P1478): each byte is read or written atomically and the copy as a whole is not. The three primitives are inGuestMemory:CopyGuestBytesreads guest bytes. It already existed, with an ASan variant.CopyToGuestBytesstores guest bytes.GuestBytesEqualcompares guest bytes with a host copy.The release build is unchanged: plain
memcpyandmemcmp. Under ASan or TSan the guest side is walked with relaxed__atomicloads and stores: single bytes up to 8-byte alignment, then aligned 8-byte words, then single bytes for the tail. An aligned 8-byte access is single-copy atomic on x86-64 and AArch64, so nothing tears and each byte is read or written atomically. TSan instruments these accesses (nono_sanitize_thread), so it still sees every partner.CopyGuestBytesand its two callbacks keepno_sanitize_address(on the callbacks in the GNU form after the parameter list, since a function attribute does not reach a lambda's call operator);CopyToGuestBytesandGuestBytesEqualstay checked by ASan. The exemption is there because guest reads such asReadRawShader's 256 bytes can run past a small host object into its ASan redzone. Release builds keepmemcpy, because an atomic per word stops large copies from vectorising; there is no suppressions file entry either, which would hide new races in these functions' callers.Routed through them:
copyCommittedBytes(Linux and macOS path),equalsCommittedBytes,CompareMappedandCopyMapped's fallback;storeGuestBytes's bulk path, the part ofGuestMemory::Writethat is not a label;restoreGroup's delta copy around the control words,writeRecordedValue,loadand the synthesised release inreleaseAwaited;copyLink, which reads command words throughGuestMemory::LoadGuestWordand copies them withAppendGuestWords(relaxed atomics in sanitizer builds, the plain read andvector::insertin release);equal, which polls guest memory the emulated GPU writes, the test fixture'sWaitingOutput::Reservelabel poll, which now uses an acquire load, andtestConcurrentCopies' write of the DMA source.The
libSceAgcpacket writersEmit(header and payload),WriteData,WriteNopandWriteRegisterRangestore through the inlineAgc::Command::StoreWords: relaxedatomic_refstores in TSan builds,std::copy_nin release. A title may rewrite a REWIND packet it has already submitted, asagc_driver_frame_replay_rewind_filldoes, while the queue worker polls the packet's control word with an acquire load inexecuteRewindTail. With a plainstd::copythat is a data race. Release builds keep the plain copy, for the same reason as the primitives: an atomic store per word was about 9% slower on anEmit-only microbenchmark.Not changed: the other
libSceAgcpacket writers. No test reaches them as a TSan partner of the driver's reads.Tested
Upstream has no TSan job (
sanitizers.ymlruns address and undefined only). These were run with the supportedANYPS5_ENABLE_TSANbuild (CMakeLists.txt, documented indocs/dev/BUILD.md), locally (Ubuntu 24.04, GCC 13, RelWithDebInfo, lavapipe) and in our fork's TSan CI.agc_driver_frame_replay*andagc_copy_guest_bytes: on the base, 6 fail with data races (blocking,host_neighbors,concurrent,rewind_release,fence_reuse,busy_queue). On this head fork CI passes them with no race reports, together with thelibSceAgccommand tests, in TSan, ASan, the Linux gate and Windows: run 45282395307.memcpy/memcmpreports 8 data races under TSan and failsagc_copy_guest_bytes,concurrentandfence_reuse: run 45274687057.agc_driver_graphics,agc_driver_graphics_maps_fallbackandagc_driver_pm4_order_signalfail under TSan and ASan onmaintoo (run 45274734706), unrelated to this PR. Locally,busy_queuestill reports lavapipe's ownmemcpyinto mapped memory againstPm4::LoadLabel, which is the Vulkan driver writing, not our code; it passed in the fork run.host_neighborsalso failed under TSan for a reason unrelated to races. The test unmapped one page above an occupied page and expected the replay to map it again, but the kernel puts new anonymous mappings top-down in the highest free gap, so the TSan runtime's own page-sized mappings landed in the hole first. The test now reserves 256 pages and frees all but the lowest, so the page beside the occupied one stays free. It passes 20 of 20 runs under TSan and in release. Merging adjacent envelopes inrestoreMemorystill makes it fail.agc_copy_guest_byteschecks:GuestBytesEqualfor each single-byte difference;CopyToGuestByteswhile a reader copies and compares; the reader waits for the writer to start, reads until the writer's store counter has changed between two consecutive reads at least 16 times and then once more, and checks every byte it copies and each comparison result. Under TSan,memcpyinCopyToGuestBytesormemcmpinGuestBytesEqualfails it with a data race;Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T01:14:16Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed change is at head
ab341e8f96e39fbb44c99be07975702e4a193ecd; the prepared diff shows no verified blocking defect, but repository context and runtime checks were not available for confirmation.What I ran
No tests or repository-reader commands were run. The supplied reader path was not accessible through the available review tool, which rejected it as not being a repository file.
Results
Findings
Blocking
None identified in the prepared diff.
Non-blocking
None.
Not verified
The manifest lists 13 changed files under
reviewed, all marked included; their full text was not read. I inspectedreview/description.md,review/commits.md,review/manifest.json, andreview/diff.patchonly. Files excluded by rule are listed inreview/files.json, which was not available through the review tool. Callers, surrounding repository code, and the author-reported test results were not independently verified.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T13:47:27Z.
Review: COMMENTED
Reviewed — comments
Changes are required before merge: restore the 30-second timeout for the existing
agc_driver_pm4_context_reg_rmwtest.What I ran
Reviewed head
cf6516773d069475cb5ebc7799db371d734e81f7using the prepared diff and pinned repository reader. No build or tests were run. The author reports release and TSan test runs in the PR description; these were not independently verified.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake:103replaces the existing timeout property assignment foragc_driver_pm4_context_reg_rmw. The following line applies the timeout to both tests, but the prior 30-second timeout line was removed in the diff. Restore the timeout property for the existing test as well.Not verified
No build, test, sanitizer, or console run was performed. Author-reported test results were not independently confirmed. All changed files listed in the manifest were included and read:
core/libs/prx/libSceAgc/Command/src/Packet.cpp,core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp,core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp,core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp,core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitMemory.cpp,core/libs/prx/libSceAgcDriver/Execution/src/Driver/Synchronization/WaitStatistics.cpp,core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp,core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp,core/libs/prx/libSceAgcDriver/tests/CopyGuestBytes.cpp,core/libs/prx/libSceAgcDriver/tests/FrameReplay.cpp,core/libs/prx/libSceAgcDriver/tests/FrameReplayState.cpp,core/libs/prx/libSceAgcDriver/tests/Pm4.cpp,core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmake,core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp,docs/dev/BUILD.md, anddocs/dev/debt/libSceAgcDriver.md.To get this to a good state
Nothing is required. The timeout adjustment is optional; it does not block merging.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T05:19:44Z.
Review: COMMENTED
Reviewed — no blocking findings
The diff addresses the stated guest-memory race paths, but I did not read the changed source files or run the reported checks, so the evidence is insufficient for approval.
What I ran
Read the PR description, commit list, manifest and diff for head
e564415bb1dd58823ec092bb2e5682178b65697f. No tests or build checks were run.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
The manifest lists 13 changed files, all marked included. I read the manifest and diff, but did not read the changed file snapshots under
review/files/or inspect repository callers throughrepo_read.py. Therefore implementation details, test coverage, and the author-reported CI results were not independently verified. Thefiles.jsoninventory was not available through the reader.To get this to a good state
Nothing is required. The review evidence is incomplete, so this is a comment rather than an approval.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T19:23:44Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: make the instrumented guest-memory accesses genuinely atomic, or use another approach that preserves race detection and prevents torn accesses.
What I ran
Head reviewed:
c265e8d58cf3e0704d32cc1ecf81f93a2880755e; base:main. I read the prepared description, commits, manifest, diff, and all changed-line ranges for the nine listed changed files using the review reader. I did not run build or test commands. The review bundle listed no excluded files.Results
Findings
Blocking
core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:3819andcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp:3838: the sanitizer paths use volatile 64-bit and byte loads/stores, then disable ThreadSanitizer instrumentation on those functions.volatiledoes not make accesses atomic or establish synchronization; in particular, unaligned 64-bit accesses can tear. Concurrent guest access therefore remains a C++ data race, while TSan is prevented from reporting it. This does not implement the stated per-byte atomic semantics and can hide real races rather than fix them. Use genuinely atomic accesses compatible with the required alignment and semantics, or another mechanism that both avoids undefined concurrent accesses and remains visible to TSan.Non-blocking
None.
Sources
Not verified
Builds, tests, runtime behavior, and the author-reported CI runs were not independently verified. I inspected changed-line ranges for all nine changed files; no files were excluded from the review bundle.
To get this to a good state
Replace the sanitizer-only volatile loops with accesses that are actually atomic under the supported alignment and concurrency conditions, then rerun the sanitizer and release checks on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T08:58:35Z.
Review: COMMENTED
Reviewed — comments
The reviewed head is
c220b9012d0e3ce01a25ad4e8857dd852ca04ae7; the change is plausible, but the full text of every changed file was not read, so approval is not justified.What I ran
Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
The full text of the changed files was not read, including
GuestMemory.cpp,Submission.cpp, the replay tool, and the changed tests. No CI reports or author-reported test runs were independently verified.To get this to a good state
Nothing is required. Optional: complete the targeted source and test review before treating this as an approval.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T21:09:26Z.
Review: COMMENTED
Actionable comments posted: 3
4cd884e5-739d-48a1-9104-4d124c0b7607Reviewing files that changed from the base of the PR and between
fdf873a7d2andb555d6df69.core/libs/prx/libSceAgc/Command/include/Packet.hppcore/libs/prx/libSceAgc/Command/src/Memory.cppcore/libs/prx/libSceAgc/Command/src/Packet.cppcore/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/tests/CopyGuestBytes.cppcore/libs/prx/libSceAgcDriver/tests/FrameReplay.cppcore/libs/prx/libSceAgcDriver/tests/FrameReplayState.cppcore/libs/prx/libSceAgcDriver/tests/Pm4.cppcore/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_graphics_tests.cmakecore/libs/prx/libSceAgcDriver/tools/FrameReplay.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T21:23:07Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the checks reported for the head support the sanitizer-specific guest memory copy behavior.
What I ran
b555d6df694eaf3ba02a2e70e2b2b4b1f0ad1351from the prepared diff and repository reader context.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
files.json(that path is not available in the review bundle).To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T08:31:39Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the inspected code supports its stated sanitizer behavior and merge.
What I ran
Reviewed head
b140031cbfc0bff5071e08b442bccad09652b3bcusingrepo_read.pysearch and targeted reads. No tests or build commands were run.Results
Negative control
Not run in this review. The description reports an author-run TSan negative control; I did not independently verify that run.
Findings
Blocking
None.
Non-blocking
None.
Not verified
No build, sanitizer test, or platform run was performed. I read all 12 changed files listed as reviewed in the manifest.
files.jsonwas not available in the prepared review folder; the review folder README listed the changed files and no exclusions.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T15:43:26Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: preserve the tail bytes when releasing a staged wait with a non-word-sized span.
What I ran
Reviewed head
0451d9bf1b6ffe03516ee22e84694303b218bea1. Read the prepared description, commits, manifest and diff, plus targeted repository source ranges throughrepo_read.py. No tests were run in this review.Results
Findings
Blocking
core/libs/prx/libSceAgcDriver/tools/FrameReplay.cpp:951writes only complete 4-byte words from each staged wait value.AwaitedWait::bytesmay be 4 or 8, and staged values are constructed usingwait.bytes; however, an 8-byte wait can be clipped to the end of its capture chunk by the chunk-boundary check inrestoreGroup. If that leaves a 1–3 byte staged value, the loop writes none of it and silently fails to restore the staged bytes. Handle the remaining tail withCopyToGuestBytesor ensure staged ranges are word-sized and fully contained before adding them.Non-blocking
None.
Not verified
No local tests or builds were run. The description reports fork CI and local runs, which were not independently verified here. The prepared manifest lists 12 changed files and marks all as included; all 12 full file texts were available, but only targeted sections were read.
files.jsonwas not accessible through the reader, so additional exclusions are not verified.To get this to a good state
Handle staged release tails and rerun the affected frame replay tests under TSan and ASan.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T17:41:12Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
3db7006473668bffb47ecf68a8fe7ac5d8d96a26against the prepared diff; no build or tests were run. I read all 12 changed files listed as reviewed in the manifest, including their changed sections. The complete changed files were available through the review bundle.files.jsonwas not available as a separate document; file coverage was checked with the review folder listing.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No local build, test suite, sanitizer run, or negative control was performed. The description's CI and test results are author-reported. No files were excluded from the changed-file review; unrelated repository files were not reviewed.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T04:57:01Z.
Review: COMMENTED
Actionable comments posted: 1
Architecture findings (1)
Advisory architecture findings (1)
3b1e3f1c-8480-4648-9e1c-bb8f271db414Reviewing files that changed from the base of the PR and between
3db7006473andde6ccf00db.core/libs/prx/libSceAgc/Command/src/Packet.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/tests/FrameReplayState.cppcore/libs/prx/libSceAgcDriver/tests/Pm4.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T05:04:26Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected changes show no verified blocking defect, but I did not run the reported checks or read every changed file in full, so a formal approval is not justified.
What I ran
Reviewed the prepared diff and targeted source ranges at head
de6ccf00db1d2c25524184d9b2c1d8790291de37; no build or test command was run. The author reports release, ASan, TSan, Linux gate, and Windows results in the PR description; these were not independently verified.Results
Negative control
The author reports that reverting the sanitizer copies to plain
memcpy/memcmpproduced TSan race reports and test failures. I did not run this negative control.Findings
Blocking
None verified in the inspected code.
Non-blocking
None.
Not verified
I read the title and description, commit list, manifest, complete prepared diff, and targeted source ranges from
Packet.hpp,Memory.cpp,Packet.cpp,GuestMemory.hpp,Submission.cpp,GuestMemory.cpp, andtools/FrameReplay.cpp. I did not read the full text of each changed file, including the changed test files. The manifest lists all 12 changed files as included; I could not readfiles.jsonthrough the available reader. No repository files excluded by the preparation rules were reviewed. Builds, tests, CI runs, and console behavior were not independently verified.To get this to a good state
Nothing is required. Optional: run the reported sanitizer, release, and platform checks on this head and provide the results for independent verification.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-01T02:22:45Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
c73ae308bd9dfe5ba59d3283ad5dd39d02c223d0against the prepared diff and file text; no merged preview was run. I inspected the review materials withcatandsed, and attempted targeted source reads withpython3 repo_read.py cat. The reader returned source output but could not complete because it tried to append toreads.jsonlon the read-only filesystem. No build or tests were run.Results
Negative control
Could not run a negative control; no test was run.
Findings
Blocking
None.
Non-blocking
None.
Sources
Behavior claims were checked against the prepared files and diff at the pinned head. Relevant code: GuestMemory.cpp, GuestMemory.hpp, Submission.cpp, and FrameReplay.cpp.
Not verified
The author's reported CI and local sanitizer results were not independently verified. Additional source context through
repo_read.pywas unavailable after its read-only logging error. All 12 files listed underreviewedinfiles.jsonwere inspected through the prepared file text and diff;files.jsonlists no excluded changed files.To get this to a good state
Nothing is required. The reported sanitizer and test results are optional context; they were not run as part of this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T17:53:39Z.
Review: COMMENTED
Driver::copyLinkstill derivesnoteScannedinformation and packet lengths from one load, then rereads the same headers throughAppendGuestWordsto populatesubmission.commands. If a producer updates a packet between the reads, the scanner's packet boundaries and opcode notes describe a different stream from the one placed on the queue. Individual atomic loads do not keep those two uses consistent. Can the scanner use the copied words, or ensure the submitted header is the exact word it used for classification?Source review at
c73ae308bd. The instrumented fixtures are unverified in this review.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.