fix(libSceAgc): copy patch, predication and workload guest pointers safely #22417
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22417"
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 domx4q on 2026-11-17T14:36:13Z.
What
sceAgcWaitRegMemPatchAddress,sceAgcAcquireMemSetEngine,sceAgcBranchPatchSetCompareAddressandsceAgcSetPacketPredicationread and patched the command packet directly through the guest pointer.sceAgcDcbSetWorkloadsActiveread 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 existingAgc::Command::ValidateWaiton 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 returns0x8a6c000cand 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 fromAgc::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
sceAgcAcquireMemSetEngineandsceAgcDcbSetWorkloadsActivedescribe 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), thePatchBranchTargethelpers inBranch.cpp,sceAgcSetRangePredicationandsceAgcAcbSetWorkloadsActivestill access the guest pointer directly.tools/guest_pointer_audit.pylists onlysceAgcAcbSetWorkloadsActiveof 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.
ctestpassed all 36 tests registered bycore/libs/prx/libSceAgc/CMakeLists.txt, pluspublic_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 realsceAgcCbBranchpacket.agc_predication_tests: an unmapped or read-only type-3 header.Mutation checks: with the four source files reset to
origin/mainand the new tests kept, all three test executables segfault. With onlyWorkload.cppreset,agc_commandssegfaults.tools/check_conventions.py --base origin/mainpasses.tools/guest_pointer_audit.py libSceAgcno longer lists the five functions.tools/progress.py --module libSceAgcgives 239 done / 3 todo, the same asorigin/main.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-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 inPacket.cpp,General.cpp,Memory.cpp, andCommands.cpp. No commands or tests were run; the test results in the description are author-reported.Results
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.jsonwas 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, andcore/libs/prx/libc/src/General.cpp. All seven changed files were included and read.To get this to a good state
Nothing is required.