fix(libSceAgc): read and write sceAgcLinkShaders' shaders and outputs through checked copies #22057

Closed
milescoviello wants to merge 1 commit from upstream-pr/22057 into main
milescoviello commented 2026-11-17 03:32:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-17T03:32:03Z.


What

sceAgcLinkShaders read the vertex and pixel shader headers (type, the semantic counts and pointers) through the guest pointers. Then it stored the 34 context and 3 primitive registers with std::copy into the caller's outputs. An unmapped output or shader pointer crashed the host.

  • Both outputs and both headers are now read through GuestCopyIn_nid_no_patch before any work, and the checks use the copied headers.
  • The registers are written through GuestCopyOut_nid_no_patch before the pipeline is committed. If the primitive registers cannot be written, the context registers are restored, so the outputs stay unchanged on failure, as they already do for an invalid shader.
  • An inaccessible pointer returns GRAPHICS5_ERROR_INVALID_SHADER_PROGRAM, the result for a null output or vertex shader, with errno preserved. The ShaderPreparationScope is not committed, so nothing is published.

Every result for valid pointers is unchanged. The special-register and semantic tables the headers point to are still read directly; the debt fragment says so.

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON:

  • guest_shader_link gains four calls, with errno held at 71, after the first successful link: an unmapped (0x10) context output, primitive output, vertex shader and pixel shader. Each returns GRAPHICS5_ERROR_INVALID_SHADER_PROGRAM and keeps errno, and both outputs are byte-for-byte unchanged afterwards. The test's final count of driver ABI calls (7 preparations, 3 mappings, 7 links) is unchanged, so none of the four reaches the driver.
  • guest_shader_link passes, 3 runs.
  • Negative control: with main's LinkShaders.cpp and the new calls, it ends in fatal SIGSEGV (code 1) at address 0x0000000000000010.
  • git merge-tree against main is clean; no open PR edits LinkShaders.cpp or GuestShaderLink.cpp.
  • tools/check_conventions.py passes on the branch and this body. tools/debt_fold.py --check reports nothing for this fragment (main itself fails it on two LibreSSL3 fragments that change the same entry).

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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5350051294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22057). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-17T03:32:03Z. --- ### What `sceAgcLinkShaders` read the vertex and pixel shader headers (`type`, the semantic counts and pointers) through the guest pointers. Then it stored the 34 context and 3 primitive registers with `std::copy` into the caller's outputs. An unmapped output or shader pointer crashed the host. - Both outputs and both headers are now read through `GuestCopyIn_nid_no_patch` before any work, and the checks use the copied headers. - The registers are written through `GuestCopyOut_nid_no_patch` before the pipeline is committed. If the primitive registers cannot be written, the context registers are restored, so the outputs stay unchanged on failure, as they already do for an invalid shader. - An inaccessible pointer returns `GRAPHICS5_ERROR_INVALID_SHADER_PROGRAM`, the result for a null output or vertex shader, with `errno` preserved. The `ShaderPreparationScope` is not committed, so nothing is published. Every result for valid pointers is unchanged. The special-register and semantic tables the headers point to are still read directly; the debt fragment says so. ### Tested Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, `-DBUILD_TESTING=ON`: - `guest_shader_link` gains four calls, with `errno` held at 71, after the first successful link: an unmapped (`0x10`) context output, primitive output, vertex shader and pixel shader. Each returns `GRAPHICS5_ERROR_INVALID_SHADER_PROGRAM` and keeps `errno`, and both outputs are byte-for-byte unchanged afterwards. The test's final count of driver ABI calls (7 preparations, 3 mappings, 7 links) is unchanged, so none of the four reaches the driver. - `guest_shader_link` passes, 3 runs. - Negative control: with `main`'s `LinkShaders.cpp` and the new calls, it ends in `fatal SIGSEGV (code 1) at address 0x0000000000000010`. - `git merge-tree` against `main` is clean; no open PR edits `LinkShaders.cpp` or `GuestShaderLink.cpp`. - `tools/check_conventions.py` passes on the branch and this body. `tools/debt_fold.py --check` reports nothing for this fragment (`main` itself fails it on two LibreSSL3 fragments that change the same entry). ### 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) 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 - [x] AI-assisted: yes

Pull request closed

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!22057
No description provided.