fix(libSceGnmDriver): write draw packets through guest copies #22138

Merged
domx4q merged 2 commits from upstream-pr/22138 into main 2026-11-26 04:21:40 +00:00
domx4q commented 2026-11-17 06:19:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by domx4q on 2026-11-17T06:19:35Z.


What

The nine libSceGnmDriver draw writers in Export.Draw.cpp (sceGnmDrawIndex, sceGnmDrawIndexAuto, sceGnmDrawIndexOffset, sceGnmDrawIndexIndirect, sceGnmDrawIndirect, sceGnmDrawIndexIndirectMulti, sceGnmDrawIndirectMulti, sceGnmDrawIndexIndirectCountMulti, sceGnmDrawIndirectCountMulti, all _nid_disambig1) wrote their packets straight into the title's command buffer. Each writer now builds its packet in a host array and writes it with GuestCopyOut_nid_no_patch. It stores only the words main stored, so it never reads the command buffer and the NOP payload words main left alone stay untouched. Seven writers need one copy; the two count-multi writers need two (words 0-1 and 3-14), because word 2 is a NOP payload word main never wrote. An unwritable buffer returns -1, the error these writers already return for a null buffer, as in the VGT control writers (#20423). A valid buffer gets the same bytes as on main. The mode check still runs before the buffer is touched, so missing or unsupported metadata still throws before anything is written.

The first revision copied the whole buffer in and out (the WriteCommandBuffer pattern of libSceGnmDriverForNeoMode). After review it no longer reads the buffer; see the timing below.

guest_pointer_audit.py listed seven of these writers. The two count-multi writers write through a local alias, so the audit missed them, but they had the same direct writes and use the same helper now. The audit no longer lists anything in Export.Draw.cpp.

No debt entry described the direct writes, so no fragment.

Overlap: pr_overlap.py --names for all nine functions: main implements them in libSceGnmDriver and libSceGnmDriverForNeoMode, and no open PR touches them. pr_overlap.py --branch fix/gnm-draw-guest-pointers: 1775 open pull requests checked, none share files or exports.

Tested

Arch Linux, GCC 16, Release. Head b062b566b1.

  • GuestGnmDrawWriters.cpp gets CheckUnwritable: every writer gets an unmapped buffer (0x10) and must return -1. It runs for both providers in base and neo mode. The existing checks still pass: exact packet words, untouched NOP payload and guard words, and refusals that leave memory alone.
  • CheckPaddingUntouched (normal provider, base and neo): each packet is placed so the words after the last stored word sit on a PROT_NONE / PAGE_NOACCESS page. Every writer must return 0 and store the same words as into a normal buffer. With the previous head (19c2e9afef) guest_gnm_draw_normal_base and guest_gnm_draw_normal_neo fail with a draw writer touched padding words past the ones it writes; with b062b566b1 they pass.
  • ctest -R guest_gnm_draw: 8/8 passed.
  • Mutation checks (first revision): with the origin/main Export.Draw.cpp, guest_gnm_draw_normal_base and guest_gnm_draw_normal_neo segfault on CheckUnwritable. Without the GuestCopyOut, both fail on the packet check.
  • Timing: no title run. We have no PS5 and no PPSA21564 dump. Instead a throwaway harness (not committed) calls all nine writers 1e6 times each, on flexible memory from sceKernelMapFlexibleMemory (inside the guest arena, checked with GuestArenaContains_nid_postfix) and on host memory. 5 runs each, pinned to one core, same machine (i9-14900KF). Median ns per writer call (min-max): arena: main 15.60 (11.66-17.29), previous head 36.81 (34.14-46.56), this head 28.13 (25.04-37.76). Host: main 13.70 (11.59-19.97), previous head 37.27 (33.32-43.85), this head 27.37 (23.57-31.38). With APS5_GUEST_COPY_SYSCALL=1 (process_vm_writev fallback, not the default): main 11.88, previous head 867.65, this head 546.06. On Linux x86-64 the default path is GuestFaultCopy (rep movsb with fault recovery), not a syscall.
  • GPU run on b062b566b1: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -V -R '^(gnm_driver_packets|gnm_driver_submit_wait|gnm_driver_compute_queue|gnm_shader_writers|gnm_default_state_200|guest_gnm_driver|guest_gnm_driver_init_modes|guest_gnm_eq_event_type|guest_gnm_draw_.*)$': 16/16 passed. The log shows Insert instance layer "VK_LAYER_KHRONOS_validation" and no was not found but was requested by env var VK_INSTANCE_LAYERS. VUID- count 0, as on main (e4caa07950, first revision run).
  • progress.py --module libSceGnmDriver: the same as the previous head and origin/main (done 50, todo 31).
  • check_conventions.py --base origin/main: passed. debt_fold.py --check fails only on libressl3-20171-inventory.md / feat-libressl3-evp-pkey-new-mac-key-v2.md, already on main, which this PR does not touch.

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 4090, driver 620.70.06, Vulkan 1.4.357, validation layer 1.4.363.1, Arch Linux
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5351920853 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22138). Originally posted by [domx4q](https://github.com/domx4q) on 2026-11-17T06:19:35Z. --- ### What The nine `libSceGnmDriver` draw writers in `Export.Draw.cpp` (`sceGnmDrawIndex`, `sceGnmDrawIndexAuto`, `sceGnmDrawIndexOffset`, `sceGnmDrawIndexIndirect`, `sceGnmDrawIndirect`, `sceGnmDrawIndexIndirectMulti`, `sceGnmDrawIndirectMulti`, `sceGnmDrawIndexIndirectCountMulti`, `sceGnmDrawIndirectCountMulti`, all `_nid_disambig1`) wrote their packets straight into the title's command buffer. Each writer now builds its packet in a host array and writes it with `GuestCopyOut_nid_no_patch`. It stores only the words main stored, so it never reads the command buffer and the NOP payload words main left alone stay untouched. Seven writers need one copy; the two count-multi writers need two (words 0-1 and 3-14), because word 2 is a NOP payload word main never wrote. An unwritable buffer returns -1, the error these writers already return for a null buffer, as in the VGT control writers (#20423). A valid buffer gets the same bytes as on main. The mode check still runs before the buffer is touched, so missing or unsupported metadata still throws before anything is written. The first revision copied the whole buffer in and out (the `WriteCommandBuffer` pattern of `libSceGnmDriverForNeoMode`). After review it no longer reads the buffer; see the timing below. `guest_pointer_audit.py` listed seven of these writers. The two count-multi writers write through a local alias, so the audit missed them, but they had the same direct writes and use the same helper now. The audit no longer lists anything in `Export.Draw.cpp`. No debt entry described the direct writes, so no fragment. Overlap: `pr_overlap.py --names` for all nine functions: main implements them in `libSceGnmDriver` and `libSceGnmDriverForNeoMode`, and no open PR touches them. `pr_overlap.py --branch fix/gnm-draw-guest-pointers`: 1775 open pull requests checked, none share files or exports. ### 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. --> Arch Linux, GCC 16, Release. Head b062b566b1. - `GuestGnmDrawWriters.cpp` gets `CheckUnwritable`: every writer gets an unmapped buffer (`0x10`) and must return -1. It runs for both providers in `base` and `neo` mode. The existing checks still pass: exact packet words, untouched NOP payload and guard words, and refusals that leave memory alone. - `CheckPaddingUntouched` (normal provider, `base` and `neo`): each packet is placed so the words after the last stored word sit on a `PROT_NONE` / `PAGE_NOACCESS` page. Every writer must return 0 and store the same words as into a normal buffer. With the previous head (19c2e9afef) `guest_gnm_draw_normal_base` and `guest_gnm_draw_normal_neo` fail with `a draw writer touched padding words past the ones it writes`; with b062b566b1 they pass. - `ctest -R guest_gnm_draw`: 8/8 passed. - Mutation checks (first revision): with the `origin/main` `Export.Draw.cpp`, `guest_gnm_draw_normal_base` and `guest_gnm_draw_normal_neo` segfault on `CheckUnwritable`. Without the `GuestCopyOut`, both fail on the packet check. - Timing: no title run. We have no PS5 and no PPSA21564 dump. Instead a throwaway harness (not committed) calls all nine writers 1e6 times each, on flexible memory from `sceKernelMapFlexibleMemory` (inside the guest arena, checked with `GuestArenaContains_nid_postfix`) and on host memory. 5 runs each, pinned to one core, same machine (i9-14900KF). Median ns per writer call (min-max): arena: main 15.60 (11.66-17.29), previous head 36.81 (34.14-46.56), this head 28.13 (25.04-37.76). Host: main 13.70 (11.59-19.97), previous head 37.27 (33.32-43.85), this head 27.37 (23.57-31.38). With `APS5_GUEST_COPY_SYSCALL=1` (`process_vm_writev` fallback, not the default): main 11.88, previous head 867.65, this head 546.06. On Linux x86-64 the default path is `GuestFaultCopy` (`rep movsb` with fault recovery), not a syscall. - GPU run on b062b566b1: `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -V -R '^(gnm_driver_packets|gnm_driver_submit_wait|gnm_driver_compute_queue|gnm_shader_writers|gnm_default_state_200|guest_gnm_driver|guest_gnm_driver_init_modes|guest_gnm_eq_event_type|guest_gnm_draw_.*)$'`: 16/16 passed. The log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"` and no `was not found but was requested by env var VK_INSTANCE_LAYERS`. `VUID-` count 0, as on main (e4caa07950, first revision run). - `progress.py --module libSceGnmDriver`: the same as the previous head and `origin/main` (done 50, todo 31). - `check_conventions.py --base origin/main`: passed. `debt_fold.py --check` fails only on `libressl3-20171-inventory.md` / `feat-libressl3-evp-pkey-new-mac-key-v2.md`, already on main, which this PR does not touch. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> NVIDIA GeForce RTX 4090, driver 620.70.06, Vulkan 1.4.357, validation layer 1.4.363.1, Arch Linux - [x] AI-assisted: <!-- yes / no --> yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-23 06:49:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T06:49:19Z.

Review: APPROVED


Reviewed — looks good

The reviewed changes preserve the draw packet contents while routing writes through the guest copy helper and leaving untouched padding alone; no blocking defect was found in the changed code.

What I ran

Reviewed head 5d693e7d2f674210e37864331e2954e316ab68ae and the prepared diff; no tests or builds were run. Read description.md, commits.md, manifest.json, diff.patch, README.md, both changed files, and the GuestCopyOut_nid_no_patch implementation in General.cpp. Both changed files listed as reviewed in the manifest were read.

Results

Check Result Note
Diff and changed lines passed Both changed files read
Targeted copy helper passed Failure path returns false
Tests/build not run Author-reported results not independently run

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author-reported test, GPU, timing, and convention results were not independently verified. No runtime or hardware behavior was tested.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6017532092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22138#pullrequestreview-6017532092). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T06:49:19Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed changes preserve the draw packet contents while routing writes through the guest copy helper and leaving untouched padding alone; no blocking defect was found in the changed code. ## What I ran Reviewed head `5d693e7d2f674210e37864331e2954e316ab68ae` and the prepared diff; no tests or builds were run. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `README.md`, both changed files, and the `GuestCopyOut_nid_no_patch` implementation in `General.cpp`. Both changed files listed as reviewed in the manifest were read. ## Results | Check | Result | Note | |---|---|---| | Diff and changed lines | passed | Both changed files read | | Targeted copy helper | passed | Failure path returns false | | Tests/build | not run | Author-reported results not independently run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author-reported test, GPU, timing, and convention results were not independently verified. No runtime or hardware behavior was tested. ## 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!22138
No description provided.