fix(agc): reject NOP data packets whose count overflows the header #14993

Merged
MarcZX10 merged 7 commits from upstream-pr/14993 into main 2026-11-06 23:08:12 +00:00
MarcZX10 commented 2026-11-02 08:52:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T08:52:55Z.


What

  • sceAgcDriverNopDataWritePacket stores totalBytes / 4 - 2 in the 14-bit PM4 type-3 count field (bits 16-29). For payloads above 65532 bytes the count wraps into the opcode bits.
  • Firmware 13.42 (libSceAgcDriver.sprx, read from disassembly, not measured on a console) has no size check here: the header is computed in 32 bits, the whole packet is still written and the result is still total / 4. This PR matches that, so there is no code change in Unimplemented.cpp.
  • Test: boundary cases at 65532 bytes (header 0xffff1000, 16385 dwords) and 65536 bytes (wrapped header 0xc0001000, 16386 dwords, destination bounds checked).
  • Debt: the wrap above 65532 bytes is recorded as read from firmware 13.42's disassembly and not measured on a console.

Commits: fix(agc) (rejection, later reverted), test(agc), docs(agc), then revert(agc) commits that remove the rejection again and restore Unimplemented.cpp to the PR base.

Tested

  • Head 91ca789c74, Linux (Debian, kernel 6.1), archive build via the shared validation tool: agc_driver_nop_data_packet passed (1/1, ctest #1596).
  • python3 tools/check_conventions.py --base and git diff --check clean.
  • Not run: the rest of the AGC suite, Windows, GPU tests (the change does not touch GPU paths).
  • The verification step's syntax pass reported errors on 51 unrelated files (missing SDL and generated headers in the throwaway tree); none of them is a file this PR changes.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5117561441 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14993). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T08:52:55Z. --- ### What - `sceAgcDriverNopDataWritePacket` stores `totalBytes / 4 - 2` in the 14-bit PM4 type-3 count field (bits 16-29). For payloads above 65532 bytes the count wraps into the opcode bits. - Firmware 13.42 (`libSceAgcDriver.sprx`, read from disassembly, not measured on a console) has no size check here: the header is computed in 32 bits, the whole packet is still written and the result is still `total / 4`. This PR matches that, so there is no code change in `Unimplemented.cpp`. - Test: boundary cases at 65532 bytes (header `0xffff1000`, 16385 dwords) and 65536 bytes (wrapped header `0xc0001000`, 16386 dwords, destination bounds checked). - Debt: the wrap above 65532 bytes is recorded as read from firmware 13.42's disassembly and not measured on a console. Commits: `fix(agc)` (rejection, later reverted), `test(agc)`, `docs(agc)`, then `revert(agc)` commits that remove the rejection again and restore `Unimplemented.cpp` to the PR base. ### Tested - Head `91ca789c74`, Linux (Debian, kernel 6.1), archive build via the shared validation tool: `agc_driver_nop_data_packet` passed (1/1, ctest #1596). - `python3 tools/check_conventions.py --base` and `git diff --check` clean. - Not run: the rest of the AGC suite, Windows, GPU tests (the change does not touch GPU paths). - The verification step's syntax pass reported errors on 51 unrelated files (missing SDL and generated headers in the throwaway tree); none of them is a file this PR changes. ### 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 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-02 10:33:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T10:33:38Z.

Review: CHANGES_REQUESTED


Thanks for the boundary test. The new check, though, makes this export depart from what firmware 13.42 does, so I'm requesting changes.

Firmware 13.42's libSceAgcDriver.sprx (sha256 78d5ad5fb70cc874b88841035218963a063199c4513607fa8f87242b66b6277b) exports sceAgcDriverNopDataWritePacket (mQfUoU5-d1g) at 0x7120. I read it to the return, from disassembly only, not measured on a console. It has no size check at all:

7131: mov eax, ecx ; ... ; lea r15, [rax+3] ; and r15, ~3 ; lea r14, [r15+8]
7151: mov rdx, r14 ; xor esi, esi ; call 0xb6d0        ; (destination, 0, total): memset
7159: shl r14d, 0xe ; add r14d, 0x3ffe0000 ; or r14d, 0xc0001000
717e: mov [rbx], r14d ; mov [rbx+4], eax               ; header, address
7175: test rsi, rsi ; sete dl ; test r13d, r13d ; sete cl ; or cl, dl ; jne 719a
718b: mov rdx, [rbp-0x38] ; mov rdi, r12 ; call 0xb850 ; (destination+8, data, bytes): memcpy
7197: add r12, r15
719a: sub r12, rbx ; shr r12, 2 ; mov eax, r12d ; ret

The header is computed in 32 bits (shl r14d, 0xe), so for bytes above 65532 the count wraps into the opcode bits exactly as on main. The whole packet is still zeroed and written, and the result is still total / 4 (2 for null data or zero bytes). main's Unimplemented.cpp already reproduces this, wrap included.

Returning 0 and writing nothing is a result the module never gives. The debt entry would then record an invented answer as unmeasured, when the module settles it. I'd suggest either:

  • dropping the code change and keeping the 65532-byte boundary test, perhaps with a 65536-byte case asserting the wrapped header 0xc0001000 | ((65544 << 14) + 0x3ffe0000) and a result of 16386, which pins the firmware behaviour; or
  • if the concern is the executor accepting a malformed NOP, handling that where the PM4 stream is parsed rather than in this export.

I didn't build this branch. The objection is about the contract, not the test.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5735218423 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14993#pullrequestreview-5735218423). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T10:33:38Z. **Review:** CHANGES_REQUESTED --- Thanks for the boundary test. The new check, though, makes this export depart from what firmware 13.42 does, so I'm requesting changes. Firmware 13.42's `libSceAgcDriver.sprx` (sha256 `78d5ad5fb70cc874b88841035218963a063199c4513607fa8f87242b66b6277b`) exports `sceAgcDriverNopDataWritePacket` (`mQfUoU5-d1g`) at 0x7120. I read it to the return, from disassembly only, not measured on a console. It has no size check at all: ``` 7131: mov eax, ecx ; ... ; lea r15, [rax+3] ; and r15, ~3 ; lea r14, [r15+8] 7151: mov rdx, r14 ; xor esi, esi ; call 0xb6d0 ; (destination, 0, total): memset 7159: shl r14d, 0xe ; add r14d, 0x3ffe0000 ; or r14d, 0xc0001000 717e: mov [rbx], r14d ; mov [rbx+4], eax ; header, address 7175: test rsi, rsi ; sete dl ; test r13d, r13d ; sete cl ; or cl, dl ; jne 719a 718b: mov rdx, [rbp-0x38] ; mov rdi, r12 ; call 0xb850 ; (destination+8, data, bytes): memcpy 7197: add r12, r15 719a: sub r12, rbx ; shr r12, 2 ; mov eax, r12d ; ret ``` The header is computed in 32 bits (`shl r14d, 0xe`), so for `bytes` above 65532 the count wraps into the opcode bits exactly as on `main`. The whole packet is still zeroed and written, and the result is still `total / 4` (2 for null data or zero bytes). `main`'s `Unimplemented.cpp` already reproduces this, wrap included. Returning 0 and writing nothing is a result the module never gives. The debt entry would then record an invented answer as unmeasured, when the module settles it. I'd suggest either: - dropping the code change and keeping the 65532-byte boundary test, perhaps with a 65536-byte case asserting the wrapped header `0xc0001000 | ((65544 << 14) + 0x3ffe0000)` and a result of 16386, which pins the firmware behaviour; or - if the concern is the executor accepting a malformed NOP, handling that where the PM4 stream is parsed rather than in this export. I didn't build this branch. The objection is about the contract, not the test.
BunnyFl commented 2026-11-06 15:57:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-06T15:57:58Z.

Review: APPROVED


@MarcZX10 re-checked at 91ca789c74, thanks for taking the first option. My change request was on 686225f1ac.

  • Unimplemented.cpp is identical to main at this head (git diff upstream/main... is empty for it), so the export keeps the 32-bit header and the total / 4 result my earlier note described.
  • The new rows pin that behaviour. NopDataWritePacket.cpp:56 expects header 0xffff1000 and 16385 for 65532 bytes, and NopDataWritePacket.cpp:65 expects 0xc0001000 and 16386 for 65536 bytes, plus the payload ends and the bytes on either side of the packet.
  • I computed both headers separately, in a Python model of the instructions quoted in my earlier review (32-bit shl 0xe, add 0x3ffe0000, or 0xc0001000, result total >> 2): 65532 gives 0xffff1000/16385 and 65536 gives 0xc0001000/16386, matching the test.
  • The debt entry (docs/dev/debt/libSceAgcDriver.md:46 at this head) states the wrap as read from 13.42's disassembly, not measured on a console.

What I ran. This head merged onto main 7383be1a19 (merge 6e5372fc74, clean), Windows 11 Pro, WinLibs GCC, Release:

  • agc_driver_nop_data_packet: 1 of 1 passes, none unbuilt.
  • The PR changes only the test and the debt entry (GitHub's file list). The controls are two mutations of main's sceAgcDriverNopDataWritePacket on the merge, each rebuilt and run. Both fail agc_driver_nop_data_packet:
    • the header count clamped at 65540 bytes instead of wrapping;
    • a return 0 for packets above 65540 bytes (the rejection this PR first added).
  • tools/pr_overlap.py --branch at this head: no line conflicts. 47 pull requests share only docs/dev/debt/libSceAgcDriver.md and couldn't be compared; in GitHub's patches for 45 of them no changed line names NopDataWritePacket, #13486's file list doesn't include that file, and #14212's patch is too large for GitHub to show.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5802950045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14993#pullrequestreview-5802950045). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-06T15:57:58Z. **Review:** APPROVED --- @MarcZX10 re-checked at `91ca789c74`, thanks for taking the first option. My change request was on `686225f1ac`. - `Unimplemented.cpp` is identical to `main` at this head (`git diff upstream/main...` is empty for it), so the export keeps the 32-bit header and the `total / 4` result my earlier note described. - The new rows pin that behaviour. `NopDataWritePacket.cpp:56` expects header `0xffff1000` and 16385 for 65532 bytes, and `NopDataWritePacket.cpp:65` expects `0xc0001000` and 16386 for 65536 bytes, plus the payload ends and the bytes on either side of the packet. - I computed both headers separately, in a Python model of the instructions quoted in my earlier review (32-bit `shl 0xe`, `add 0x3ffe0000`, `or 0xc0001000`, result `total >> 2`): 65532 gives `0xffff1000`/16385 and 65536 gives `0xc0001000`/16386, matching the test. - The debt entry (`docs/dev/debt/libSceAgcDriver.md:46` at this head) states the wrap as read from 13.42's disassembly, not measured on a console. **What I ran.** This head merged onto `main` `7383be1a19` (merge `6e5372fc74`, clean), Windows 11 Pro, WinLibs GCC, Release: - `agc_driver_nop_data_packet`: 1 of 1 passes, none unbuilt. - The PR changes only the test and the debt entry (GitHub's file list). The controls are two mutations of `main`'s `sceAgcDriverNopDataWritePacket` on the merge, each rebuilt and run. Both fail `agc_driver_nop_data_packet`: - the header count clamped at 65540 bytes instead of wrapping; - a `return 0` for packets above 65540 bytes (the rejection this PR first added). - `tools/pr_overlap.py --branch` at this head: no line conflicts. 47 pull requests share only `docs/dev/debt/libSceAgcDriver.md` and couldn't be compared; in GitHub's patches for 45 of them no changed line names `NopDataWritePacket`, #13486's file list doesn't include that file, and #14212's patch is too large for GitHub to show. AI-assisted: yes (Claude Code)
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!14993
No description provided.