fix: define decoded AGC state and bound size arithmetic #10249

Closed
zoelmoj wants to merge 1 commit from upstream-pr/10249 into main
zoelmoj commented 2026-10-23 15:37:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zoelmoj on 2026-10-23T15:37:53Z.


What

Fixes undefined-behaviour and correctness defects in the AGC driver, guest libc, NGS2, IME, VideoOut, the shader recompiler and the relinker:

  • Graphics::State, ShaderStages, ColorTarget and DepthTarget had members without initializers (scissor, cullMode, frontFace, blend, blendConstants, viewport, renderExtent, topology, hasColorTarget, negativeOneToOne, path, registerValue, the wave sizes, address, extent, format, bytes, componentMapping, clearDepth, clearStencil). Driver::sameDecode compares blendConstants and the sample masks, so a default-constructed State read indeterminate memory.
  • Ngs2CustomModuleOption is a 4-byte size field; OptionWord read 4 bytes at offset 4, past the declared object. It now validates the guest-declared option size and returns the option error code when the option is truncated.
  • Recorder indexed syncWaitedMs[source] (5 entries) and, inside CountThreadSync, counts[source] and waitedMs[source], before any range check.
  • ThreadOwned lost the object when push_back threw while registering it.
  • sceLibcMspaceCreate returned success when the map insertion failed.
  • Size arithmetic was performed in 32 bits and widened afterwards in MultisampledDepthBytes, StorageTexture::sliceWindows, the vertex input table read, VideoOutDriver::processFlip and the Mach-O init stub displacement.
  • Sequence::SubmitCompute copied the compute shader register and specials pointers with sizeof applied to pointer variables, which reads as a mistake. The copy now goes through std::uintptr_t, so the width is the pointer size by construction.
  • fprintf received Registers::Reference proxies (Pm4 context trace) and uncast register values (draw capture dump).
  • StencilReferenceUnused compared VkCompareOp against VK_COMPARE_OP_NEVER, which is 0 and therefore unreachable.
  • FilterText computed type - 0x100 on std::uint32_t before checking type >= 0x100.
  • ~Replay called its virtual Cancel/Wait unqualified, the tessellation attribute switch left InstanceInput/InstanceOutput implicit, and AdjustedDelta carried an unused cutStart parameter.

Tested

  • OS: NixOS 26.05, x86-64, GCC 15.3, Release.
  • cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release ..., cmake --build build --parallel 12 and cmake --build build --target libs --parallel 12 all succeed on top of current main; the patched .prx set is produced.
  • ctest -R '^(mspace|agc_driver_pm4|agc_driver_draw_program_trace|guest_ime_filter_text|ngs2_core_render)$': 5/5 pass.
  • clang-tidy -p build -checks='-*,bugprone-sizeof-expression' core/libs/prx/libSceJobManager/Export.cpp: reported Export.cpp:349 before the change, nothing after it.
  • GPU tests were not run: the build environment has no Vulkan device, so the AGC and VideoOut tests with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and the validation layer still need a run on hardware.
  • python3 tools/check_conventions.py --base origin/main passes.
  • python3 tools/pr_overlap.py --branch fix/agc-state-init-and-bounds reports file-level overlap with open PRs that implement unrelated features and fixes. The two hunks that duplicated open work were dropped: the DepthSurface tile-mode format is #7504, and the Sequence::Advance erase line is replaced by #10231/#10244.

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
  • 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 (GPU tests not run; no Vulkan device in the build environment)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4985290542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10249). Originally posted by [zoelmoj](https://github.com/zoelmoj) on 2026-10-23T15:37:53Z. --- ### What Fixes undefined-behaviour and correctness defects in the AGC driver, guest libc, NGS2, IME, VideoOut, the shader recompiler and the relinker: - `Graphics::State`, `ShaderStages`, `ColorTarget` and `DepthTarget` had members without initializers (`scissor`, `cullMode`, `frontFace`, `blend`, `blendConstants`, `viewport`, `renderExtent`, `topology`, `hasColorTarget`, `negativeOneToOne`, `path`, `registerValue`, the wave sizes, `address`, `extent`, `format`, `bytes`, `componentMapping`, `clearDepth`, `clearStencil`). `Driver::sameDecode` compares `blendConstants` and the sample masks, so a default-constructed `State` read indeterminate memory. - `Ngs2CustomModuleOption` is a 4-byte `size` field; `OptionWord` read 4 bytes at offset 4, past the declared object. It now validates the guest-declared option size and returns the option error code when the option is truncated. - `Recorder` indexed `syncWaitedMs[source]` (5 entries) and, inside `CountThreadSync`, `counts[source]` and `waitedMs[source]`, before any range check. - `ThreadOwned` lost the object when `push_back` threw while registering it. - `sceLibcMspaceCreate` returned success when the map insertion failed. - Size arithmetic was performed in 32 bits and widened afterwards in `MultisampledDepthBytes`, `StorageTexture::sliceWindows`, the vertex input table read, `VideoOutDriver::processFlip` and the Mach-O init stub displacement. - `Sequence::SubmitCompute` copied the compute shader register and specials pointers with `sizeof` applied to pointer variables, which reads as a mistake. The copy now goes through `std::uintptr_t`, so the width is the pointer size by construction. - `fprintf` received `Registers::Reference` proxies (Pm4 context trace) and uncast register values (draw capture dump). - `StencilReferenceUnused` compared `VkCompareOp` against `VK_COMPARE_OP_NEVER`, which is 0 and therefore unreachable. - `FilterText` computed `type - 0x100` on `std::uint32_t` before checking `type >= 0x100`. - `~Replay` called its virtual `Cancel`/`Wait` unqualified, the tessellation attribute switch left `InstanceInput`/`InstanceOutput` implicit, and `AdjustedDelta` carried an unused `cutStart` parameter. ### Tested - OS: NixOS 26.05, x86-64, GCC 15.3, Release. - `cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release ...`, `cmake --build build --parallel 12` and `cmake --build build --target libs --parallel 12` all succeed on top of current `main`; the patched `.prx` set is produced. - `ctest -R '^(mspace|agc_driver_pm4|agc_driver_draw_program_trace|guest_ime_filter_text|ngs2_core_render)$'`: 5/5 pass. - `clang-tidy -p build -checks='-*,bugprone-sizeof-expression' core/libs/prx/libSceJobManager/Export.cpp`: reported `Export.cpp:349` before the change, nothing after it. - GPU tests were not run: the build environment has no Vulkan device, so the AGC and VideoOut tests with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and the validation layer still need a run on hardware. - `python3 tools/check_conventions.py --base origin/main` passes. - `python3 tools/pr_overlap.py --branch fix/agc-state-init-and-bounds` reports file-level overlap with open PRs that implement unrelated features and fixes. The two hunks that duplicated open work were dropped: the DepthSurface tile-mode format is #7504, and the `Sequence::Advance` erase line is replaced by #10231/#10244. ### 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) - [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 (GPU tests not run; no Vulkan device in the build environment) - [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!10249
No description provided.