Skip to content

fix(libSceGnmDriver): copy the shader registers and packets of sceGnmSet*Shader safely - #24456

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
domx4q:fix/gnm-shader-guest-pointers
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
domx4q:fix/gnm-shader-guest-pointers

Conversation

@domx4q

@domx4q domx4q commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

The seven shader register writers of libSceGnmDriver (sceGnmSetPsShader, sceGnmSetVsShader, sceGnmSetCsShader, sceGnmSetEsShader, sceGnmSetGsShader, sceGnmSetHsShader and sceGnmSetLsShader) indexed the guest regs block directly and emitted their PM4 packets straight into the guest command buffer. An unmapped register block or command buffer crashed the process instead of failing the call.

Each writer now reads the words it uses with one GuestCopyIn_nid_no_patch into a host array: 12 for the pixel shader, 7 for the vertex, compute, geometry and hull shaders, 4 for the export and local shaders. It builds its packet in a host array of the packet's fixed length (0x28, 0x1d, 0x19, 0x14, 0x1d, 0x1e and 0x17 dwords) and writes it with one GuestCopyOut_nid_no_patch. Every packet already covered its whole length, ending with the NOP pad, so the command buffer is not copied in first. An unreadable register block or an unwritable command buffer returns -1, which these writers already return for a short buffer or a non-zero second register. The VGT control writers in the same file fail the same way since 72a75c6. The helpers follow ReadShaderRegs and WriteCommandBuffer in libSceGnmDriverForNeoMode (dffbd0c). For readable inputs the packets are byte-for-byte the same as on main, and the existing gnm_shader_writers checks pass unchanged.

Not changed

These tools/guest_pointer_audit.py findings in the libraries I looked at were left alone:

  • sceGnmDrawInitDefaultHardwareState200/350 and sceGnmDrawInitToDefaultContextState400 are fixed in a separate PR.
  • sceGnmSubmitAndFlipCommandBuffers shares its submission path with sceGnmSubmitCommandBuffers, and that path reads the command buffers themselves. It needs its own change.
  • Other open PRs already change the libSceAgc, libSceSdma and libSceHttp findings, sceUsbdGetDeviceList (fix(libSceUsbd): probe guest memory in the device list, event, transfer and configuration calls #21530) and sceNetGetsockopt/sceNetSetsockopt.
  • False positives:
    • The UsbdTransfer objects that the other libSceUsbd findings take come from sceUsbdAllocTransfer, which allocates them on the host with calloc.
    • sceNetRecvmsg copies the header in and opens msg with GuestArena::HostWrite before it stores to it.
    • sceNetSyncSignal and sceNetSyncWait check the sync object with GuestWritable_nid_no_patch and then use the mutex and condition variable inside it in place.
    • DnB6WJ91HGg only takes member addresses for its copy helpers.

Overlap

Tested

Arch Linux, GCC, Release. gnm_shader_writers (core/libs/prx/libSceGnmDriver/tests/ShaderWriters.cpp) has a new TestUnmapped:

  • each of the seven writers returns -1 for the register block at 0x10 and leaves the command buffer untouched.
  • each returns -1 for the command buffer at 0x10, the null-register pixel shader included.

Mutation checks (repeated after the rebase):

  • WriteShaderPacket ignoring the GuestCopyOut_nid_no_patch result fails with an unwritable pixel shader buffer was accepted.
  • ReadShaderRegs ignoring the GuestCopyIn_nid_no_patch result fails with an unreadable pixel shader register block was accepted.

Re-run after rebasing onto origin/main 0a025bd: ctest --test-dir build -R 'gnm|public_headers' passed 115/115. That covers gnm_shader_writers, gnm_default_state_200, guest_gnm_driver_init_modes, the guest_gnm_draw_* writers, gnm_driver_packets and the 65 public_headers tests. tools/check_conventions.py --base origin/main passes, and tools/guest_pointer_audit.py libSceGnmDriver no longer lists the seven writers. tools/progress.py --module libSceGnmDriver gives 53 done / 31 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
  • AI-assisted: yes (Claude Code)

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head 24828c0f14, applied with git apply on main fd4aeed483 together with the other sixteen PRs of one batch (#24596, #24594, #24595, #24599, #24600, #24602, #24593, #24659, #24651, #24626, #24625, #24624, #24656, #22380, #24502, #24351). All seventeen applied without conflicts. The tree built except libSceAjm.prx: #24659 breaks it with duplicate patched export name: -qLsfDAywIY (checked alone on main, reported there). No other target failed to build.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
gnm_shader_writers (1 test) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6524 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main fd4aeed483 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). The 2125 agc_/video tests on the iGPU fail agc_driver_stencil_plane_memory (also on main), agc_driver_store_over_untracked (also fails alone on main on this iGPU, 6 of 6) and agc_driver_image_table_packed_guards (passes 5 of 5 alone on this tree, known flaky). None of this batch touches AGC.

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the reviewed implementation preserves packet construction while using checked guest memory copies.

What I ran

Reviewed head 24828c0f142c439d3fae91b8438b07cdb65ff29e. No commands or tests were run in this review; no merged preview was checked. Read description.md, commits.md, manifest.json, diff.patch, Export.cpp, and ShaderWriters.cpp; also read targeted context from libc/src/General.cpp and libSceGnmDriverForNeoMode/Export.cpp. Both files listed as reviewed in the manifest were read in full.

Results

Check Result Note
Source review passed Both changed files read
Tests not run Test results in description are author reported
Copy helper behavior passed Checked copy helpers return failure for unmapped ranges

Findings

Blocking

None.

Non-blocking

None.

Sources

The shader writers read registers into local arrays and write fixed-length packets using checked guest copies (Export.cpp at reviewed head). The copy helpers validate address ranges and report failed guest memory access (General.cpp at reviewed head). The unmapped-pointer tests cover all seven writers (ShaderWriters.cpp at reviewed head).

Not verified

Did not run the test suite, mutation checks, convention checks, or guest pointer audit; their reported outcomes are from the PR description. Did not verify physical console behavior. files.json was not available through the reader, so excluded files beyond those identified by the manifest were not checked.

To get this to a good state

Nothing is required.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 24741 core/libs/prx/libSceGnmDriver/tests/ShaderWriters.cpp L134

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 20730 core/libs/prx/libSceGnmDriver/Export.cpp
PR 21225 core/libs/prx/libSceGnmDriver/Export.cpp
PR 23696 core/libs/prx/libSceGnmDriver/Export.cpp

@oneandonlydean
oneandonlydean merged commit 5cda957 into boykopovar:main Nov 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants