fix(libSceGnmDriver): refuse a REWIND in a submitted draw command buffer #22375

Merged
adam25136 merged 1 commit from upstream-pr/22375 into main 2026-11-29 10:12:49 +00:00
adam25136 commented 2026-11-17 13:22:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adam25136 on 2026-11-17T13:22:38Z.


What

On the PS4 Gnm path, sceGnmSubmitCommandBuffers and sceGnmSubmitAndFlipCommandBuffers go through SubmitLocked. It merges each draw and constant command buffer into a local copy (MergeEngines), passes the copy to AgcDriverSubmitGraphics_nid_postfix and frees it on return.

AGC copies the packets it is given, except at a REWIND. A REWIND's second dword is its control word: the packets after the REWIND may run once bit 31 of that word is set. At a REWIND, copyLink keeps a pointer to the packets after it (submission.rewindTail = guest + cursor, Submission.cpp). executeRewindTail then waits on the control word and reads the rest of the buffer through that pointer. On the Gnm path that pointer is into the freed copy, a use-after-free:

  • the title releases the REWIND by setting the control word in its own buffer, which the copy never sees, so the graphics queue waits there;
  • if the freed memory is reused, a word with bit 31 set at the control word's address releases the REWIND, and AGC copies whatever is there as packets (read from the code, not reproduced).

The change:

  • The refusal: MergeEngines now throws a REWIND packet in a submitted draw command buffer (NotImplemented_nid_no_patch). This is the reason #18546 gave for refusing a REWIND in a compute ring: "the AGC driver would keep a pointer into the copy made for the submit, which is freed when sceGnmDingDong returns". A REWIND in a buffer a compute ring calls also throws (#20384), and so does one in a constant command buffer (constant engine packet 89 is not implemented).
  • What changes for a title (read from the code):
    • such a submission now throws before any of its buffers is passed to AGC, though sceGnmSubmitAndFlipCommandBuffers has already patched its flip into the title's buffer by then;
    • a REWIND whose control word was already set when the title submitted may run today, from the freed copy before it is reused; it now throws too.
  • Not changed: a REWIND in a buffer the draw command buffer calls (INDIRECT_BUFFER). AGC follows the call into the title's memory, so its tail pointer is the title's own and the release is seen.
  • Left as is: after such a call, AGC does not run the caller's packets that follow it (copyLink returns at the called buffer's REWIND). On the Gnm path that would include the constant engine packets MergeEngines appends at the end and a flip at the end of the buffer (read from the code; a WRITE_DATA after the call was measured, below). This is separate AGC behaviour, not changed here.
  • Technical debt: a fragment adds a REWIND in a submitted draw command buffer entry under Functional.

Tested

Windows 10 Pro 22H2 (19045), NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 (r7), CMake 4.3.1, Release, this branch on main 9c0d96916c. main's own agc_driver_upload_cache_read_only_flush_tests does not build with MinGW (setenv; open #19899 fixes it), so the builds went on past it.

  • The bug on main 47b33132f9 (a local test and temporary prints, not part of this PR; addresses as printed, ranges end one past the last dword):
    • Top-level REWIND: the test submits a draw command buffer of a REWIND (control word 0) and a WRITE_DATA of a label through sceGnmSubmitCommandBuffers, waits 500 ms, then sets the control word to 0x80000000 in its own buffer. 5 s later the label is still 0. The prints show the title's buffer at 0xfc00001000-0xfc0000101c, SubmitLocked's copy at 0xfc40d59710-0xfc40d5972c, and AGC keeping the REWIND tail at 0xfc40d59718, inside the copy, which is freed when SubmitLocked returns.
    • The same REWIND in a called buffer: the draw command buffer only calls it. AGC keeps the tail at 0xfc000d2008 in the title's buffer, and the label is 1 after the release.
  • A WRITE_DATA after such a call (this branch at dad3ef290b7 on main c825105a77, with a temporary change to the new test): after the release, the called buffer's label is 1 and the caller's label is still 0.
  • The new cases in gnm_driver_submit_tests pass:
    • gnm_driver_packets: MergeEngines throws with the message above for a draw command buffer with a REWIND between two NOPs;
    • gnm_driver_submit_wait: a draw command buffer that calls a buffer holding a REWIND and a WRITE_DATA is submitted. The label is still 0 after 200 ms, and 1 once the title sets the control word.
  • Deliberate breaks: removing the refusal makes gnm_driver_packets fail (expected an exception). Refusing every INDIRECT_BUFFER as well makes gnm_driver_submit_wait fail (the call throws).
  • The 1,118 tests tools/select_tests.py selects for the 3 changed files (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1), on main and on this branch: no test fails only on the branch, and neither run skips one. performance_runner_PerformanceRunTests fails on both: 3 of its 5 cases error, because run_performance_title refuses ANYPS5_NO_SHADER_CACHE=1 (clean performance capture requires enabled shader caches).
  • Validation layer: VK_LAYER_KHRONOS_validation 1.4.367 (through VK_ADD_LAYER_PATH) with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, VK_LOADER_LAYERS_DISABLE=~implicit~, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, ctest -V, on main and on this branch.
    • The 49 tests matching gnm pass on both.
    • 3 of them create a Vulkan instance: gnm_driver_submit_wait (which runs the new GPU case), gnm_driver_compute_queue and agc_driver_gds_integer_atomics64_alignment.
    • Each log has 3 Insert instance layer "VK_LAYER_KHRONOS_validation" lines, no "was not found" line and no VUID- message.
  • python tools/check_conventions.py --base origin/main --title "<this title>" --body <this description>: All conventions checks passed.
  • python tools/pr_overlap.py --branch:
    • 6 of the 1,687 open PRs share files or exports with this branch. It compared 5 with no overlap, among them #20730 and #21225, which add a test in the same places of tests/Submit.cpp. This branch's test sits where each of them merges with it without a conflict (git merge-file on main's file).
    • It could not compare #17711, which conflicts with main, so pr_overlap exits 2. #17711's Export.cpp hunks are an include at the top and exports after sceGnmUnmapComputeQueue, away from MergeEngines.
  • Not covered: other OSes and GPUs; sceGnmSubmitAndFlipCommandBuffers itself (it reaches the same MergeEngines through SubmitLocked); any title. No title is known to hit this; it was found by reading the REWIND path.

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: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 22H2
  • AI-assisted: yes

AI-assisted: Claude Code found the bug while reading the REWIND path, reproduced it with a local test, wrote the change and its tests, ran the tests, and drafted this description; separate review agents checked it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5357659231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22375). Originally posted by [adam25136](https://github.com/adam25136) on 2026-11-17T13:22:38Z. --- ### What On the PS4 Gnm path, `sceGnmSubmitCommandBuffers` and `sceGnmSubmitAndFlipCommandBuffers` go through `SubmitLocked`. It merges each draw and constant command buffer into a local copy (`MergeEngines`), passes the copy to `AgcDriverSubmitGraphics_nid_postfix` and frees it on return. AGC copies the packets it is given, except at a REWIND. A REWIND's second dword is its control word: the packets after the REWIND may run once bit 31 of that word is set. At a REWIND, `copyLink` keeps a pointer to the packets after it (`submission.rewindTail = guest + cursor`, `Submission.cpp`). `executeRewindTail` then waits on the control word and reads the rest of the buffer through that pointer. On the Gnm path that pointer is into the freed copy, a use-after-free: - the title releases the REWIND by setting the control word in its own buffer, which the copy never sees, so the graphics queue waits there; - if the freed memory is reused, a word with bit 31 set at the control word's address releases the REWIND, and AGC copies whatever is there as packets (read from the code, not reproduced). The change: - **The refusal:** `MergeEngines` now throws `a REWIND packet in a submitted draw command buffer` (`NotImplemented_nid_no_patch`). This is the reason #18546 gave for refusing a REWIND in a compute ring: "the AGC driver would keep a pointer into the copy made for the submit, which is freed when `sceGnmDingDong` returns". A REWIND in a buffer a compute ring calls also throws (#20384), and so does one in a constant command buffer (`constant engine packet 89 is not implemented`). - **What changes for a title** (read from the code): - such a submission now throws before any of its buffers is passed to AGC, though `sceGnmSubmitAndFlipCommandBuffers` has already patched its flip into the title's buffer by then; - a REWIND whose control word was already set when the title submitted may run today, from the freed copy before it is reused; it now throws too. - **Not changed:** a REWIND in a buffer the draw command buffer calls (INDIRECT_BUFFER). AGC follows the call into the title's memory, so its tail pointer is the title's own and the release is seen. - **Left as is:** after such a call, AGC does not run the caller's packets that follow it (`copyLink` returns at the called buffer's REWIND). On the Gnm path that would include the constant engine packets `MergeEngines` appends at the end and a flip at the end of the buffer (read from the code; a WRITE_DATA after the call was measured, below). This is separate AGC behaviour, not changed here. - **Technical debt:** a fragment adds a `REWIND in a submitted draw command buffer` entry under Functional. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 10 Pro 22H2 (19045), NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 (r7), CMake 4.3.1, Release, this branch on `main` `9c0d96916c`. `main`'s own `agc_driver_upload_cache_read_only_flush_tests` does not build with MinGW (`setenv`; open #19899 fixes it), so the builds went on past it. - **The bug on `main` `47b33132f9`** (a local test and temporary prints, not part of this PR; addresses as printed, ranges end one past the last dword): - **Top-level REWIND:** the test submits a draw command buffer of a REWIND (control word 0) and a WRITE_DATA of a label through `sceGnmSubmitCommandBuffers`, waits 500 ms, then sets the control word to `0x80000000` in its own buffer. 5 s later the label is still 0. The prints show the title's buffer at `0xfc00001000`-`0xfc0000101c`, `SubmitLocked`'s copy at `0xfc40d59710`-`0xfc40d5972c`, and AGC keeping the REWIND tail at `0xfc40d59718`, inside the copy, which is freed when `SubmitLocked` returns. - **The same REWIND in a called buffer:** the draw command buffer only calls it. AGC keeps the tail at `0xfc000d2008` in the title's buffer, and the label is 1 after the release. - **A WRITE_DATA after such a call** (this branch at `dad3ef290b7` on `main` `c825105a77`, with a temporary change to the new test): after the release, the called buffer's label is 1 and the caller's label is still 0. - **The new cases in `gnm_driver_submit_tests` pass:** - `gnm_driver_packets`: `MergeEngines` throws with the message above for a draw command buffer with a REWIND between two NOPs; - `gnm_driver_submit_wait`: a draw command buffer that calls a buffer holding a REWIND and a WRITE_DATA is submitted. The label is still 0 after 200 ms, and 1 once the title sets the control word. - **Deliberate breaks:** removing the refusal makes `gnm_driver_packets` fail (`expected an exception`). Refusing every INDIRECT_BUFFER as well makes `gnm_driver_submit_wait` fail (the call throws). - **The 1,118 tests `tools/select_tests.py` selects for the 3 changed files** (`ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`), on `main` and on this branch: no test fails only on the branch, and neither run skips one. `performance_runner_PerformanceRunTests` fails on both: 3 of its 5 cases error, because `run_performance_title` refuses `ANYPS5_NO_SHADER_CACHE=1` (`clean performance capture requires enabled shader caches`). - **Validation layer:** `VK_LAYER_KHRONOS_validation` 1.4.367 (through `VK_ADD_LAYER_PATH`) with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `VK_LOADER_LAYERS_DISABLE=~implicit~`, `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1`, `ctest -V`, on `main` and on this branch. - The 49 tests matching `gnm` pass on both. - 3 of them create a Vulkan instance: `gnm_driver_submit_wait` (which runs the new GPU case), `gnm_driver_compute_queue` and `agc_driver_gds_integer_atomics64_alignment`. - Each log has 3 `Insert instance layer "VK_LAYER_KHRONOS_validation"` lines, no "was not found" line and no `VUID-` message. - **`python tools/check_conventions.py --base origin/main --title "<this title>" --body <this description>`:** `All conventions checks passed.` - **`python tools/pr_overlap.py --branch`:** - 6 of the 1,687 open PRs share files or exports with this branch. It compared 5 with no overlap, among them #20730 and #21225, which add a test in the same places of `tests/Submit.cpp`. This branch's test sits where each of them merges with it without a conflict (`git merge-file` on `main`'s file). - It could not compare #17711, which conflicts with `main`, so pr_overlap exits 2. #17711's `Export.cpp` hunks are an include at the top and exports after `sceGnmUnmapComputeQueue`, away from `MergeEngines`. - **Not covered:** other OSes and GPUs; `sceGnmSubmitAndFlipCommandBuffers` itself (it reaches the same `MergeEngines` through `SubmitLocked`); any title. No title is known to hit this; it was found by reading the REWIND path. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 22H2 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> AI-assisted: Claude Code found the bug while reading the REWIND path, reproduced it with a local test, wrote the change and its tests, ran the tests, and drafted this description; separate review agents checked it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-23 01:29:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T01:29:06Z.

Review: APPROVED


Reviewed — looks good

Nothing in the reviewed change blocks merging; the refusal targets the top-level draw-buffer REWIND path that retains a pointer into a temporary merged buffer, and the called-buffer test covers the documented exception.

What I ran

Reviewed head b441dcb72bb2a21af3caed41e0435a31b726c4b9. No commands or tests were run. Read review/description.md, review/commits.md, review/manifest.json, review/diff.patch, core/libs/prx/libSceGnmDriver/Export.cpp, core/libs/prx/libSceGnmDriver/tests/Submit.cpp, docs/dev/debt/changes/fix-gnm-top-level-rewind.md, and targeted AGC submission code in core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp.

Results

Check Result Note
Change review passed Changed files and relevant callers read
Automated checks not run Author-reported checks not independently run

Findings

Blocking

None.

Non-blocking

None.

Sources

  • MergeEngines rejects OpRewind at core/libs/prx/libSceGnmDriver/Export.cpp:341-342; SubmitLocked builds merged vectors before passing them to AGC at core/libs/prx/libSceGnmDriver/Export.cpp:381-395.
  • AGC keeps the REWIND tail pointer into its input at core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:281-289 and reads the control word and tail through that pointer at core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:700-734.
  • The new tests assert refusal for a submitted draw-buffer REWIND and exercise a REWIND inside an indirect called buffer at core/libs/prx/libSceGnmDriver/tests/Submit.cpp:61-62 and core/libs/prx/libSceGnmDriver/tests/Submit.cpp:156-173.

Not verified

The author-reported build, test suite, validation-layer run, and hardware behavior were not independently verified. The described changes in sceGnmSubmitAndFlipCommandBuffers and title-specific behavior were not exercised here. The prepared bundle lists all three changed files as included and all three were read; excluded files: none reported.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6015631196 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22375#pullrequestreview-6015631196). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T01:29:06Z. **Review:** APPROVED --- # Reviewed — looks good Nothing in the reviewed change blocks merging; the refusal targets the top-level draw-buffer REWIND path that retains a pointer into a temporary merged buffer, and the called-buffer test covers the documented exception. ## What I ran Reviewed head `b441dcb72bb2a21af3caed41e0435a31b726c4b9`. No commands or tests were run. Read `review/description.md`, `review/commits.md`, `review/manifest.json`, `review/diff.patch`, `core/libs/prx/libSceGnmDriver/Export.cpp`, `core/libs/prx/libSceGnmDriver/tests/Submit.cpp`, `docs/dev/debt/changes/fix-gnm-top-level-rewind.md`, and targeted AGC submission code in `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp`. ## Results | Check | Result | Note | |---|---|---| | Change review | passed | Changed files and relevant callers read | | Automated checks | not run | Author-reported checks not independently run | ## Findings ### Blocking None. ### Non-blocking None. ## Sources - `MergeEngines` rejects `OpRewind` at `core/libs/prx/libSceGnmDriver/Export.cpp:341-342`; `SubmitLocked` builds merged vectors before passing them to AGC at `core/libs/prx/libSceGnmDriver/Export.cpp:381-395`. - AGC keeps the REWIND tail pointer into its input at `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:281-289` and reads the control word and tail through that pointer at `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp:700-734`. - The new tests assert refusal for a submitted draw-buffer REWIND and exercise a REWIND inside an indirect called buffer at `core/libs/prx/libSceGnmDriver/tests/Submit.cpp:61-62` and `core/libs/prx/libSceGnmDriver/tests/Submit.cpp:156-173`. ## Not verified The author-reported build, test suite, validation-layer run, and hardware behavior were not independently verified. The described changes in `sceGnmSubmitAndFlipCommandBuffers` and title-specific behavior were not exercised here. The prepared bundle lists all three changed files as included and all three were read; excluded files: none reported.
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!22375
No description provided.