fix(agc): read unaligned guest words through a one-byte-aligned type in the ASan copy #10973

Merged
MarcZX10 merged 2 commits from upstream-pr/10973 into main 2026-10-27 05:29:32 +00:00
MarcZX10 commented 2026-10-25 11:44:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:44:14Z.


What

In an ANYPS5_ENABLE_ASAN build, GuestMemory::CopyGuestBytes (core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp) reads the guest source eight bytes at a time through a const volatile std::uint64_t*. The source has no alignment guarantee, so those loads are undefined behavior. UBSan reports them from agc_driver_pm4, agc_driver_graphics and agc_driver_graphics_maps_fallback:

Execution/src/GuestMemory.cpp:2573:36: runtime error: load of misaligned address 0x100200000104 for type 'volatile const uint64_t', which requires 8 byte alignment

The words are now read through a local alias of std::uint64_t with [[gnu::aligned(1), gnu::may_alias]]. GCC still emits a single unaligned 8-byte load per word (movq), and builds without ANYPS5_ENABLE_ASAN, which use memcpy, are unchanged.

Two commits: fix, test. agc_copy_guest_bytes copies 0 to 80 bytes from each of the eight source offsets and checks the copied bytes and the bytes on both sides of the destination.

agc_driver_graphics and agc_driver_graphics_maps_fallback get past this load with the fix but then stop on a different report, Graphics/src/DepthSurface.cpp:730:76: runtime error: reference binding to null pointer of type 'struct TextureDetiler', which this pull request does not change.

Tested

Linux x86-64 (Debian 12, GCC 12), -DANYPS5_ENABLE_ASAN=ON with -fsanitize=undefined and UBSAN_OPTIONS=halt_on_error=1: agc_copy_guest_bytes fails on current main with the misaligned load and passes with the fix, and agc_driver_pm4 passes with the fix after failing the same way. python3 tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main pass. Following AGENTS.md, agc_driver_pm4, agc_driver_graphics and agc_driver_graphics_maps_fallback were also run on main and on this change with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (ctest -R <pattern> -V, Khronos validation layer 1.3.239 installed, UBSan reporting without halting): all three pass in both runs, the misaligned-load reports go from 3 on main to 0, and no VUID is reported in either run. In this build these tests do not load the Vulkan loader (VK_LOADER_DEBUG=all prints nothing), so the validation layer had nothing to check; the change only affects CPU reads of guest memory. The new test was not built without ANYPS5_ENABLE_ASAN, on Windows or macOS, and no hardware was run.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • 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 (not applicable; no third-party code added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (the new test runs on the CPU; the AGC tests above ran on llvmpipe)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5004426791 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10973). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:44:14Z. --- ### What In an `ANYPS5_ENABLE_ASAN` build, `GuestMemory::CopyGuestBytes` (`core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp`) reads the guest source eight bytes at a time through a `const volatile std::uint64_t*`. The source has no alignment guarantee, so those loads are undefined behavior. UBSan reports them from `agc_driver_pm4`, `agc_driver_graphics` and `agc_driver_graphics_maps_fallback`: ``` Execution/src/GuestMemory.cpp:2573:36: runtime error: load of misaligned address 0x100200000104 for type 'volatile const uint64_t', which requires 8 byte alignment ``` The words are now read through a local alias of `std::uint64_t` with `[[gnu::aligned(1), gnu::may_alias]]`. GCC still emits a single unaligned 8-byte load per word (`movq`), and builds without `ANYPS5_ENABLE_ASAN`, which use `memcpy`, are unchanged. Two commits: fix, test. `agc_copy_guest_bytes` copies 0 to 80 bytes from each of the eight source offsets and checks the copied bytes and the bytes on both sides of the destination. `agc_driver_graphics` and `agc_driver_graphics_maps_fallback` get past this load with the fix but then stop on a different report, `Graphics/src/DepthSurface.cpp:730:76: runtime error: reference binding to null pointer of type 'struct TextureDetiler'`, which this pull request does not change. ### Tested Linux x86-64 (Debian 12, GCC 12), `-DANYPS5_ENABLE_ASAN=ON` with `-fsanitize=undefined` and `UBSAN_OPTIONS=halt_on_error=1`: `agc_copy_guest_bytes` fails on current `main` with the misaligned load and passes with the fix, and `agc_driver_pm4` passes with the fix after failing the same way. `python3 tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check upstream/main` pass. Following AGENTS.md, `agc_driver_pm4`, `agc_driver_graphics` and `agc_driver_graphics_maps_fallback` were also run on `main` and on this change with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (`ctest -R <pattern> -V`, Khronos validation layer 1.3.239 installed, UBSan reporting without halting): all three pass in both runs, the misaligned-load reports go from 3 on `main` to 0, and no VUID is reported in either run. In this build these tests do not load the Vulkan loader (`VK_LOADER_DEBUG=all` prints nothing), so the validation layer had nothing to check; the change only affects CPU reads of guest memory. The new test was not built without `ANYPS5_ENABLE_ASAN`, on Windows or macOS, and no hardware was run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source (not applicable; no third-party code added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (the new test runs on the CPU; the AGC tests above ran on llvmpipe) - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 12:04:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T12:04:06Z.

Review: COMMENTED


Reviewed exact head c5bf7899b491e49dc0d73d08423064647aae5bbc. The ASan-only copy now uses a one-byte-aligned, may-alias word type for the 8-byte source loads, avoiding the reported misaligned uint64_t access while preserving the existing non-ASan memcpy path. The new test covers all source offsets modulo 8 and lengths through 80 bytes, checks both destination canaries, and is registered in the test list. I verified the diff and local convention checks against current upstream/main; both passed. The hosted eligibility, intake, conventions, labels, and report checks pass; hosted build jobs were skipped. I have not independently run the UBSan reproducer or AGC tests, so the failure-on-main/pass-on-change results are based on the author’s report. The author also documents the later, separate TextureDetiler null-reference report in two graphics tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643127866 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10973#pullrequestreview-5643127866). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T12:04:06Z. **Review:** COMMENTED --- Reviewed exact head `c5bf7899b491e49dc0d73d08423064647aae5bbc`. The ASan-only copy now uses a one-byte-aligned, may-alias word type for the 8-byte source loads, avoiding the reported misaligned `uint64_t` access while preserving the existing non-ASan `memcpy` path. The new test covers all source offsets modulo 8 and lengths through 80 bytes, checks both destination canaries, and is registered in the test list. I verified the diff and local convention checks against current `upstream/main`; both passed. The hosted eligibility, intake, conventions, labels, and report checks pass; hosted build jobs were skipped. I have not independently run the UBSan reproducer or AGC tests, so the failure-on-main/pass-on-change results are based on the author’s report. The author also documents the later, separate `TextureDetiler` null-reference report in two graphics tests.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10973
No description provided.