fix(libSceAgc): copy patch, predication and workload guest pointers safely #22417

Merged
domx4q merged 1 commit from upstream-pr/22417 into main 2026-11-27 15:40:25 +00:00
domx4q commented 2026-11-17 14:36:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by domx4q on 2026-11-17T14:36:13Z.


What

sceAgcWaitRegMemPatchAddress, sceAgcAcquireMemSetEngine, sceAgcBranchPatchSetCompareAddress and sceAgcSetPacketPredication read and patched the command packet directly through the guest pointer. sceAgcDcbSetWorkloadsActive read the guest workload id array the same way. An unmapped or read-only packet or id array crashed the process.

These exports can run once per draw or dispatch, so each one does as few guest copies as it can (see the review on #22138). Each patch export reads only the words it has to inspect or keep, patches a host copy, and writes the patched words back with one GuestCopyOut_nid_no_patch. A patch has to read some words first: it validates the packet header, and it keeps the bits around the patched field. A pure writer can skip that read; a patch cannot.

  • sceAgcWaitRegMemPatchAddress: copies in the 4 wrapper words, then the rest of the 14- or 16-dword wait packet, and runs the existing Agc::Command::ValidateWait on the host copy. It patches words 2, 3, 6 and 7 and writes words 2–7 with one copy (words 4 and 5 are written back with the values just read).
  • sceAgcAcquireMemSetEngine: one copy in of words 0–1 and one copy out of word 1. A non-ACQUIRE_MEM packet still returns 0x8a6c000c and is not written.
  • sceAgcBranchPatchSetCompareAddress: one copy in of the header and one copy out of words 2–3.
  • sceAgcSetPacketPredication: one copy in and one copy out of the header word.
  • sceAgcDcbSetWorkloadsActive: one copy in of the at most 63 ids. The packet writes stay as they are (the packet comes from Agc::Command::Allocate, and the audit does not list them).

An unreadable or unwritable packet throws through Agc::Command::Require, as the packet info exports do (#22137). An unreadable workload id array returns null, as a null array already does, and no packet is allocated. Results for accessible pointers are byte-for-byte the same as on main.

None of these is a false positive: the packets and ids are guest memory that the title passes in. The debt entries for sceAgcAcquireMemSetEngine and sceAgcDcbSetWorkloadsActive describe the packet layout and the null and range checks, not the dereference, so no debt fragment was added.

Not changed here (follow-ups): the other WaitRegMem patch exports (PatchReference, PatchMask, PatchCompareFunction, the WriteData patches), the PatchBranchTarget helpers in Branch.cpp, sceAgcSetRangePredication and sceAgcAcbSetWorkloadsActive still access the guest pointer directly. tools/guest_pointer_audit.py lists only sceAgcAcbSetWorkloadsActive of these.

Overlap

  • python3 tools/pr_overlap.py --names sceAgcAcquireMemSetEngine sceAgcWaitRegMemPatchAddress sceAgcBranchPatchSetCompareAddress sceAgcSetPacketPredication sceAgcDcbSetWorkloadsActive → each one: main implemented, open: none.
  • python3 tools/pr_overlap.py --branch fix/agc-patch-predication-guest-pointers → no overlap with open pull requests.

Tested

Arch Linux, GCC, Release. ctest passed all 36 tests registered by core/libs/prx/libSceAgc/CMakeLists.txt, plus public_headers (37/37). New checks:

  • agc_commands, testUnmappedPatchPackets: an unmapped wait or acquire packet is rejected. So is a read-only one, which stays unchanged. The read-only fixtures are first checked to patch correctly when they are writable.
  • agc_commands, testUnmappedWorkloadIds: an unmapped id array on a registered stream returns null and does not advance the cursor.
  • agc_branch_patch_tests: an unmapped or read-only branch packet. The read-only fixture header is compared with a real sceAgcCbBranch packet.
  • agc_predication_tests: an unmapped or read-only type-3 header.

Mutation checks: with the four source files reset to origin/main and the new tests kept, all three test executables segfault. With only Workload.cpp reset, agc_commands segfaults. tools/check_conventions.py --base origin/main passes. tools/guest_pointer_audit.py libSceAgc no longer lists the five functions. tools/progress.py --module libSceAgc gives 239 done / 3 todo, the same as origin/main.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (CPU-side command packet patching; no Vulkan, recompiler or VideoOut path touched)
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5358857076 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22417). Originally posted by [domx4q](https://github.com/domx4q) on 2026-11-17T14:36:13Z. --- ### What `sceAgcWaitRegMemPatchAddress`, `sceAgcAcquireMemSetEngine`, `sceAgcBranchPatchSetCompareAddress` and `sceAgcSetPacketPredication` read and patched the command packet directly through the guest pointer. `sceAgcDcbSetWorkloadsActive` read the guest workload id array the same way. An unmapped or read-only packet or id array crashed the process. These exports can run once per draw or dispatch, so each one does as few guest copies as it can (see the review on #22138). Each patch export reads only the words it has to inspect or keep, patches a host copy, and writes the patched words back with one `GuestCopyOut_nid_no_patch`. A patch has to read some words first: it validates the packet header, and it keeps the bits around the patched field. A pure writer can skip that read; a patch cannot. - `sceAgcWaitRegMemPatchAddress`: copies in the 4 wrapper words, then the rest of the 14- or 16-dword wait packet, and runs the existing `Agc::Command::ValidateWait` on the host copy. It patches words 2, 3, 6 and 7 and writes words 2–7 with one copy (words 4 and 5 are written back with the values just read). - `sceAgcAcquireMemSetEngine`: one copy in of words 0–1 and one copy out of word 1. A non-ACQUIRE_MEM packet still returns `0x8a6c000c` and is not written. - `sceAgcBranchPatchSetCompareAddress`: one copy in of the header and one copy out of words 2–3. - `sceAgcSetPacketPredication`: one copy in and one copy out of the header word. - `sceAgcDcbSetWorkloadsActive`: one copy in of the at most 63 ids. The packet writes stay as they are (the packet comes from `Agc::Command::Allocate`, and the audit does not list them). An unreadable or unwritable packet throws through `Agc::Command::Require`, as the packet info exports do (#22137). An unreadable workload id array returns null, as a null array already does, and no packet is allocated. Results for accessible pointers are byte-for-byte the same as on main. None of these is a false positive: the packets and ids are guest memory that the title passes in. The debt entries for `sceAgcAcquireMemSetEngine` and `sceAgcDcbSetWorkloadsActive` describe the packet layout and the null and range checks, not the dereference, so no debt fragment was added. Not changed here (follow-ups): the other WaitRegMem patch exports (`PatchReference`, `PatchMask`, `PatchCompareFunction`, the WriteData patches), the `PatchBranchTarget` helpers in `Branch.cpp`, `sceAgcSetRangePredication` and `sceAgcAcbSetWorkloadsActive` still access the guest pointer directly. `tools/guest_pointer_audit.py` lists only `sceAgcAcbSetWorkloadsActive` of these. #### Overlap - `python3 tools/pr_overlap.py --names sceAgcAcquireMemSetEngine sceAgcWaitRegMemPatchAddress sceAgcBranchPatchSetCompareAddress sceAgcSetPacketPredication sceAgcDcbSetWorkloadsActive` → each one: main implemented, open: none. - `python3 tools/pr_overlap.py --branch fix/agc-patch-predication-guest-pointers` → no overlap with open pull requests. ### Tested Arch Linux, GCC, Release. `ctest` passed all 36 tests registered by `core/libs/prx/libSceAgc/CMakeLists.txt`, plus `public_headers` (37/37). New checks: - `agc_commands`, `testUnmappedPatchPackets`: an unmapped wait or acquire packet is rejected. So is a read-only one, which stays unchanged. The read-only fixtures are first checked to patch correctly when they are writable. - `agc_commands`, `testUnmappedWorkloadIds`: an unmapped id array on a registered stream returns null and does not advance the cursor. - `agc_branch_patch_tests`: an unmapped or read-only branch packet. The read-only fixture header is compared with a real `sceAgcCbBranch` packet. - `agc_predication_tests`: an unmapped or read-only type-3 header. Mutation checks: with the four source files reset to `origin/main` and the new tests kept, all three test executables segfault. With only `Workload.cpp` reset, `agc_commands` segfaults. `tools/check_conventions.py --base origin/main` passes. `tools/guest_pointer_audit.py libSceAgc` no longer lists the five functions. `tools/progress.py --module libSceAgc` gives 239 done / 3 todo, the same as `origin/main`. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 (CPU-side command packet patching; no Vulkan, recompiler or VideoOut path touched) - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-11-20 09:32:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T09:32:49Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the changed code uses checked guest copies for the targeted reads and writes, and the added tests cover unreadable and read-only inputs.

What I ran

Reviewed head 97ee720d6603e8da6a14cbefddc43605acadd41b. I read all seven changed files, the complete prepared diff, and targeted context in Packet.cpp, General.cpp, Memory.cpp, and Commands.cpp. No commands or tests were run; the test results in the description are author-reported.

Results

Check Result Note
Diff and changed code review passed All changed files read
Tests and build not run Results supplied by author only

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run the reported tests, build, mutation checks, convention check, guest pointer audit, or progress check. I did not inspect files excluded from the prepared review bundle; files.json was not available in the listed review documents. Repository files read: core/libs/prx/libSceAgc/DcbState/src/Workload.cpp, core/libs/prx/libSceAgc/Patch/src/Branch.cpp, core/libs/prx/libSceAgc/Patch/src/WaitRegMem.cpp, core/libs/prx/libSceAgc/Predication/src/Predication.cpp, core/libs/prx/libSceAgc/tests/BranchPatch.cpp, core/libs/prx/libSceAgc/tests/Commands.cpp, core/libs/prx/libSceAgc/tests/Predication.cpp, core/libs/prx/libSceAgc/Command/include/Packet.hpp, core/libs/prx/libSceAgc/Command/src/Packet.cpp, core/libs/prx/libSceAgc/Command/src/Memory.cpp, and core/libs/prx/libc/src/General.cpp. All seven changed files were included and read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5990468698 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22417#pullrequestreview-5990468698). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T09:32:49Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the changed code uses checked guest copies for the targeted reads and writes, and the added tests cover unreadable and read-only inputs. ## What I ran Reviewed head `97ee720d6603e8da6a14cbefddc43605acadd41b`. I read all seven changed files, the complete prepared diff, and targeted context in `Packet.cpp`, `General.cpp`, `Memory.cpp`, and `Commands.cpp`. No commands or tests were run; the test results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code review | passed | All changed files read | | Tests and build | not run | Results supplied by author only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run the reported tests, build, mutation checks, convention check, guest pointer audit, or progress check. I did not inspect files excluded from the prepared review bundle; `files.json` was not available in the listed review documents. Repository files read: `core/libs/prx/libSceAgc/DcbState/src/Workload.cpp`, `core/libs/prx/libSceAgc/Patch/src/Branch.cpp`, `core/libs/prx/libSceAgc/Patch/src/WaitRegMem.cpp`, `core/libs/prx/libSceAgc/Predication/src/Predication.cpp`, `core/libs/prx/libSceAgc/tests/BranchPatch.cpp`, `core/libs/prx/libSceAgc/tests/Commands.cpp`, `core/libs/prx/libSceAgc/tests/Predication.cpp`, `core/libs/prx/libSceAgc/Command/include/Packet.hpp`, `core/libs/prx/libSceAgc/Command/src/Packet.cpp`, `core/libs/prx/libSceAgc/Command/src/Memory.cpp`, and `core/libs/prx/libc/src/General.cpp`. All seven changed files were included and read. ## To get this to a good state Nothing is required.
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!22417
No description provided.