fix(agc): store a storage image's DCC keys at write-back only when not stored yet #3458

Merged
alfalfa-star merged 2 commits from upstream-pr/3458 into main 2026-10-11 11:49:15 +00:00
alfalfa-star commented 2026-10-11 10:58:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-11T10:58:17Z.


What

A storage image's write-back stores uncompressed DCC keys over its metadata again, even when the image's first write already stored them (b8d30603). The write-back is deferred until something flushes it. By then the title may have reused the metadata memory, and the store overwrites the title's data with 0xff.

Title: RIDE 4 (PPSA01599, Unreal Engine 4) on Linux, main 8ec0e131. With #3435 and #3440 for its earlier stops, it shows the logo and legal screen. Then, about 150 s in, it crashes in game code (app.elf+0x260884e, SIGSEGV): UE4's small-block allocator pops a per-thread free list whose head is 0xffffffffffffffff.

What happens, from a gdb run (breakpoint on MarkDccUncompressed, hardware watchpoint on the corrupted word):

  1. The title renders into four 1920x1088 RGBA8 targets. Each one's 64 KiB DCC key block sits right after it; for example the target at 0x2020800000 has keys at 0x2021800000.
  2. At the first write, StorageTexture::MarkDirty stores 0xc000 uncompressed keys there (uploadedKeys becomes Uncompressed).
  3. After the legal screen the title frees the targets. UE4's allocator takes the key block: game code (app+0x25f9ea8, app+0x26086dc, ...) writes heap pointers (0x1020e5....) to 0x2021800020.
  4. A later dispatch's AcquireRegistered → FlushForAccess → writeBack → writeBackLayers runs the targets' pending write-backs. Each calls MarkDccUncompressed again. Its AlreadyUncompressed check sees the allocator's bytes, not 0xff, so it stores 0xc000 bytes of 0xff over them.
  5. The next free-list pop dereferences 0xff...ff.

The address changes from run to run (0x2021800020, 0x2021a00020, 0x2021e00020), but it is always a key block that a write-back stores after the reuse.

Fix

The three write-back paths in writeBackLayers (GPU-direct, retile and CPU round trip) store the keys only when the image's uploaded keys are not Uncompressed already. In that case the first write, or an earlier write-back, already stored them, and nothing the driver saw has changed them since.

  • A title's key fill (filledKeys) still skips the store, as before.
  • A refresh that reads clear keys again (uploaded keys Clear*) still gets them stored at the next write-back.

Tested

Linux (Arch), GCC, Release, RADV, on main 99c1c263.

  • agc_driver_dcc_keys_first_write gets RunReusedKeys:
    • a first write stores 0xff keys;
    • the CPU then overwrites the key bytes with 0x5a, standing in for the title reusing the memory;
    • after FlushPending, the keys must still be 0x5a and the texels must hold the image's results.
  • On main it fails with 0 of 256 key bytes are 0x5a (key 0 is 0xff); with the fix it passes. The existing first-write, fill and refresh cases pass unchanged.
  • Full ctest: 825/825 (4 video-out flip tests skipped as usual).
  • RIDE 4 with the fix runs past the crash. It reaches its title screen at about 118 s and runs to about 290 s, where it stops at sceAjmBatchJobSetResampleParametersEx: a ratio change per sample is unsupported.
  • tools/check_conventions.py passes.

@Celegans12, this changes when the keys from your first-write store are written again at write-back. Does it miss a case you had in mind?

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4818552811 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3458). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-11T10:58:17Z. --- ### What A storage image's write-back stores uncompressed DCC keys over its metadata again, even when the image's first write already stored them (b8d30603). The write-back is deferred until something flushes it. By then the title may have reused the metadata memory, and the store overwrites the title's data with `0xff`. **Title:** RIDE 4 (PPSA01599, Unreal Engine 4) on Linux, main 8ec0e131. With #3435 and #3440 for its earlier stops, it shows the logo and legal screen. Then, about 150 s in, it crashes in game code (`app.elf+0x260884e`, SIGSEGV): UE4's small-block allocator pops a per-thread free list whose head is `0xffffffffffffffff`. What happens, from a gdb run (breakpoint on `MarkDccUncompressed`, hardware watchpoint on the corrupted word): 1. The title renders into four 1920x1088 RGBA8 targets. Each one's 64 KiB DCC key block sits right after it; for example the target at `0x2020800000` has keys at `0x2021800000`. 2. At the first write, `StorageTexture::MarkDirty` stores `0xc000` uncompressed keys there (`uploadedKeys` becomes `Uncompressed`). 3. After the legal screen the title frees the targets. UE4's allocator takes the key block: game code (`app+0x25f9ea8`, `app+0x26086dc`, ...) writes heap pointers (`0x1020e5....`) to `0x2021800020`. 4. A later dispatch's `AcquireRegistered` → `FlushForAccess` → `writeBack` → `writeBackLayers` runs the targets' pending write-backs. Each calls `MarkDccUncompressed` again. Its `AlreadyUncompressed` check sees the allocator's bytes, not `0xff`, so it stores `0xc000` bytes of `0xff` over them. 5. The next free-list pop dereferences `0xff...ff`. The address changes from run to run (`0x2021800020`, `0x2021a00020`, `0x2021e00020`), but it is always a key block that a write-back stores after the reuse. ### Fix The three write-back paths in `writeBackLayers` (GPU-direct, retile and CPU round trip) store the keys only when the image's uploaded keys are not `Uncompressed` already. In that case the first write, or an earlier write-back, already stored them, and nothing the driver saw has changed them since. - A title's key fill (`filledKeys`) still skips the store, as before. - A refresh that reads clear keys again (uploaded keys `Clear*`) still gets them stored at the next write-back. ### Tested Linux (Arch), GCC, Release, RADV, on main 99c1c263. - `agc_driver_dcc_keys_first_write` gets `RunReusedKeys`: - a first write stores `0xff` keys; - the CPU then overwrites the key bytes with `0x5a`, standing in for the title reusing the memory; - after `FlushPending`, the keys must still be `0x5a` and the texels must hold the image's results. - On main it fails with `0 of 256 key bytes are 0x5a (key 0 is 0xff)`; with the fix it passes. The existing first-write, fill and refresh cases pass unchanged. - Full `ctest`: 825/825 (4 video-out flip tests skipped as usual). - RIDE 4 with the fix runs past the crash. It reaches its title screen at about 118 s and runs to about 290 s, where it stops at `sceAjmBatchJobSetResampleParametersEx: a ratio change per sample is unsupported`. - `tools/check_conventions.py` passes. @Celegans12, this changes when the keys from your first-write store are written again at write-back. Does it miss a case you had in mind? ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-11 11:49:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T11:49:12Z.


Checked build and ctest (libSceAgcDriver scope, 382/382) on the PR merged onto main. agc_driver_dcc_keys_first_write passes with the change and RunReusedKeys fails without it. The three write-back paths now skip the key store only when the first write or an earlier write-back already stored Uncompressed; the fill and Clear* refresh cases are unchanged. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108706209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3458#issuecomment-6108706209). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T11:49:12Z. --- Checked build and ctest (libSceAgcDriver scope, 382/382) on the PR merged onto main. `agc_driver_dcc_keys_first_write` passes with the change and `RunReusedKeys` fails without it. The three write-back paths now skip the key store only when the first write or an earlier write-back already stored `Uncompressed`; the fill and `Clear*` refresh cases are unchanged. Merging.
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!3458
No description provided.