fix(agc): avoid a misaligned wide load in the ASan CopyGuestBytes helper #9570
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9570"
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 jotitadev on 2026-10-22T02:39:49Z.
What
GuestMemory::CopyGuestByteshas an#ifdef ANYPS5_ENABLE_ASANvariant that copiesguest memory word by word so the reads stay
volatile(to force the access through thewrite-watch / read-guard machinery). The load side did a raw wide dereference:
fromis a guest address with no 8-byte alignment guarantee, so this is a misalignedload — undefined behavior, flagged by
-fsanitize=undefined. x86 tolerates it in hardware,so there is no misbehavior today, but it is UB and would fault on alignment-strict targets.
This affects ASan builds only. The shipping non-ASan path (
std::memcpy) is untouchedand was already clean.
Fix
Keep the wide
volatileread on the aligned fast path; whenfrom + indexis not 8-bytealigned, read those 8 bytes
volatilebyte-by-byte and assemble the word:Every one of the 8 bytes is still read through a
volatileaccess, in order, so the forcedread semantics the helper exists for are preserved — the
volatileis not removed and thefunction is not rewritten. Only the raw wide dereference of a possibly-unaligned address is
avoided.
<cstdint>is added to the unconditional include block sostd::uintptr_tisavailable on every platform (it was previously pulled in only on the non-Windows branch).
The non-ASan branch is not touched.
Tested
Repro
Linux, GCC 15.2, RelWithDebInfo, ASan + UBSan. GPU tests run under lavapipe (Mesa
llvmpipe software rasterizer) to avoid non-ASan-clean GPU driver noise.
Red -> green
Three independent execution tests hit the exact same line via
GuestMemory::Read -> CopyGuestBytes.Before the fix — all three abort (
fno-sanitize-recover), exit code 1:agc_driver_memory_value_fill_testsagc_driver_storage_file_write_testsagc_driver_stream_copy_testsAfter the fix — all three print their "tests passed" line and exit 0 with no UBSan runtime
error. A sample of extra execution tests (incl. unaligned-base coverage) was also run under
the same ASan+UBSan build with no new failures, and the normal build (no sanitizers, no
ANYPS5_ENABLE_ASAN) still compiles the touched translation unit.Introduced by
257894965(run the AGC GPU tests in an ANYPS5_ENABLE_ASAN build); the UBSanalignment hit there was almost certainly unintended.
AI-assisted (Claude Code); the builds and tests above were run on my machine.
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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T03:57:54Z.
Review: CHANGES_REQUESTED
The code change is narrowly scoped and preserves the volatile accesses on both the aligned and bytewise paths. The reported UBSan reproduction and three post-fix test results are relevant evidence, and I found no competing edit to
CopyGuestBytesin the manually checked shared-file warnings.I am requesting changes because the hosted
conventionscheck fails, and this PR body does not use the repository's required.github/pull_request_template.md(it omits the template's checklist and requiredDepends on,Fixes,Unblocks,GPUs tested, and AI-assisted fields in their prescribed form). Please copy the current template, keep every line, fill each required field, and rerun the conventions check. Eligibility, intake, labels and report pass; the hosted Linux, Windows and relinker jobs are skipped. I did not treat the author-reported local runs as hosted checks.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T04:46:24Z.
Thanks for the review. Addressed: the body now follows
.github/pull_request_template.md— the summary and repro are under### What/### Tested, and the full### Checklistis filled (includingGPUs tested:andAI-assisted: yes). The hosted Conventions check is green now. The code change itself is unchanged from what you reviewed (the aligned fast path keeps the widevolatileread; the unaligned path reads the 8 bytesvolatilebyte-by-byte). Could you take another look when you have a moment?Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-22T08:13:33Z.
The Conventions check fails on the head of this PR (job 119607257405):
Fill in the pull request template: section 'What' is missing or empty, and the same forTestedandChecklist.The relinker, relinker-asan, register-context and write-watch jobs all pass on the same commit, and
python3 tools/check_conventions.py --base origin/mainprints the same three template findings locally. Filling in every line of.github/pull_request_template.md(What, Tested, Depends on, Unblocks, GPUs tested, AI-assisted, and tick the boxes) is all that appears to block CI.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T08:27:37Z.
Thanks. That job ran on the old body; the body now follows the template and Conventions passes on the same head: https://github.com/boykopovar/AnyPS5/actions/runs/39722571996/job/119639169696
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:17:10Z.
Review: APPROVED
Re-reviewed exact head
c65ab7596ea86149b91249f0b2d38d465c5ff75a. The fix retains the aligned volatile 64-bit load and uses eight ordered volatile byte reads only for unaligned blocks, then copies the assembled word without changing the non-ASan path. I found no correctness issue in the changed code. The current PR description now includes the repository template fields and the latest hosted conventions run passes. I attempted an independent Linux UBSan run in an isolated detached worktree with the three reported targets (agc_driver_memory_value_fill_tests,agc_driver_storage_file_write_tests,agc_driver_stream_copy_tests),ANYPS5_ENABLE_ASAN=ON, and the PR's UBSan flags. CMake configuration succeeded and the build progressed through 599/808 steps, but stopped before reaching those targets because the root filesystem ran out of space while compiling unrelated library/shader objects (No space left on device). No test executable ran, so I am not claiming a local test pass. The PR-reported red/green runs remain author-reported. Hosted Linux/Windows product jobs are skipped; eligibility, intake, labels, report passed, while the original conventions failure was followed by a passing run after the body update. Current-main sync is 1 ahead / 107 behind (6ef5a1c4cmerge base), with a clean merge. Please refresh from currentmainbefore merge under the repository's branch rule. The change is narrowly scoped, and I found no competing edit toCopyGuestBytesin the open file list.I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:22:28Z.
Review: DISMISSED
I reviewed exact head
c65ab7596ea86149b91249f0b2d38d465c5ff75a. The alignment branch keeps the existing wide volatile read for aligned addresses and assembles unaligned words from eight volatile byte reads; I found no defect in that helper. I also ran a focused UBSan harness using the exact helper body across source offsets 0–7 and lengths 0–40; all byte comparisons and destination canaries passed. As a negative control, the current upstream/main helper aborts on the first unaligned wide load with UBSan’s misaligned-address diagnostic. This is a focused helper harness, not the repository AGC test targets.The current branch is 1 commit ahead and 152 behind upstream/main (
329efe1bfdaff116c57cdf62718026ee4f5521ae). Five intervening main commits touchedGuestMemory.cpp, so please rebase onto current main and rerun the three reported ASan+UBSan regression targets before merge, as required by CONTRIBUTING’s current-main rule. The current-basegit diff --checkand conventions check pass, and merge-tree is clean.The overlap scan could not compare 14 open PRs that also touch
GuestMemory.cppbecause they conflict with main. I inspected their changed hunks aroundCopyGuestBytes: none modifies this helper; #6188 changes adjacent speculative-read code and its combined merge tree retains this fix. I found no duplicate implementation of the misaligned-load correction. Hosted product build jobs remain skipped; I did not treat the author-reported three-target runs as hosted or as my local project test.Pull request closed