Skip to content

fix(agc): clamp indexed draws to MAX_SIZE instead of refusing them - #978

Open
Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/23774
Open

Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/23774

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#23774, originally opened by milescoviello.

Source commit: 85bad3a7ebff0725a7aa5e827fcf96fe0dc9c643. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

Astro Bot (PPSA21564, 01.007.000) stops on main f4383b0ac at 581 s, during the space flight after the opening cinematic, with AGC driver: DRAW_INDEX_OFFSET_2 at DWORD 263: index count exceeds maximum index size. The whole submission (7478 dwords, 232 of them DRAW_INDEX_OFFSET_2 packets) is rejected and the exception terminates the title (EXIT-134). This was the first time in eight 600 s runs of the title on main; the other seven ran to the timeout.

Pm4::Validate refuses DRAW_INDEX_2 (0x27) and DRAW_INDEX_OFFSET_2 (0x35) whenever INDEX_COUNT is larger than MAX_SIZE, but that is a packet AMD's own drivers write:

  • AMD PAL c5e80007 sets MAX_SIZE, for both packets, to the number of indices left in the bound index buffer from the draw's first index (validIndexCount), and INDEX_COUNT to the draw's own count. Its comment calls MAX_SIZE the clamp on how much of the index buffer can be read.
  • Mesa 26.2.2 RADV does the same for DRAW_INDEX_2: "Hardware uses this information to return 0 for out-of-bounds reads."

The change:

  • Validate no longer refuses INDEX_COUNT > MAX_SIZE.
  • ResolveDraw draws min(INDEX_COUNT, MAX_SIZE) indices, so it reads and range-checks only the indices inside MAX_SIZE. That is what the CPU indirect path already does for a record that runs past INDEX_BUFFER_SIZE (Driver/Draw/Draw.cpp: std::min(arguments.count, drawParameters.indexCount - arguments.firstVertexOrIndex)).

The hardware reads the indices past MAX_SIZE as 0 instead of dropping them, so a triangle that straddles MAX_SIZE, or the extra points of a point list, would still be drawn on the console. The debt fragment records that difference.

Tested

Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release, -DBUILD_TESTING=ON, RTX 4070 Ti on NVIDIA 615.71:

  • 11-27, head 85bad3a7eb (main f73d791b3d merged in): guest_indexed_draw, whose refusal checks main added after this branch was opened (8b3c360), still expected the refusal; its four checks now expect the clamp (a DRAW_INDEX_2 with MAX_SIZE 3 and a count of 4 or 0xffffffff resolves to 3 indices; a DRAW_INDEX_OFFSET_2 with MAX_SIZE 2 and a count of 3 or 0xffffffff resolves to the 2 indices of the in-range offset draw). guest_indexed_draw and the 30 agc_driver_pm4* tests pass in each of 3 runs.
  • testIndexedDraw in tests/Pm4.cpp gains three checks: a DRAW_INDEX_OFFSET_2 with MAX_SIZE 3, offset 2 and count 6 resolves to 3 indices at the offset; a DRAW_INDEX_2 with MAX_SIZE 2 and count 5 resolves to 2 indices; a MAX_SIZE of 0 resolves to an empty draw. The check that expected the refusal is removed.
  • ctest -R '^agc_driver_pm4' (30 tests) passes 3 runs on e74ccb2d9c, which has the same code as the head (284d1c3184 only shortens the debt entry).
  • Negative control: with main's Pm4.cpp and the new checks, agc_driver_pm4 fails.
  • git merge-tree against main d7be67833e is clean. tools/check_conventions.py passes on the branch and this body, and tools/debt_fold.py --check reports nothing for this fragment.

Windows 11 (VM, GeForce GT 1030, WinLibs GCC 16.2, Ninja, Release, ANYPS5_REQUIRE_VULKAN=1), ctest -R '^agc_driver_pm4' on 284d1c3184, 3 runs: 29 of 30 pass in each, agc_driver_pm4 included. agc_driver_pm4_order_signal fails in all 3, and fails the same way on this PR's base a8b2a7d2a5 (3 of 3): on this 4-vCPU VM a held submission starts 0.37-0.44 ms (median) after queue 0 begins to wait, and the test wants it at once. So that failure is not from this change.

  • kiwe147's batch run of 284d1c3184, applied with other PRs onto main fd4aeed483 (one combined tree, not this PR alone), Windows 11, WinLibs GCC 15.2.0 r7, Release, no validation layer: the 6 agc_driver_pm4* tests pass on an NVIDIA GeForce RTX 3080 (driver 621.24) and on the AMD Radeon iGPU of a Ryzen 7 9800X3D (gfx1036).

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. The stop is intermittent (one of nine 600 s runs of PPSA21564 on main). A 600 s run with this change plus a temporary log line for clamped draws reached the same point without meeting such a packet, so it shows nothing either way. If the stop shows up again, I'll post the packet's MAX_SIZE, offset and count here (discussion #357).
  • GPUs tested: NVIDIA RTX 4070 Ti (Linux), NVIDIA GeForce GT 1030 (Windows); NVIDIA RTX 3080 (Windows, kiwe147, batch tree); Ryzen 7 9800X3D iGPU (Windows, kiwe147, batch tree)
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Indexed draws with a requested index count above the available maximum are now accepted and resolved using the smaller count. This prevents excess indices from being processed while preserving the draw’s index address.
    • A maximum index count of zero now resolves to an empty draw.

milescoviello and others added 3 commits November 19, 2026 11:07
DRAW_INDEX_2 and DRAW_INDEX_OFFSET_2 refused a packet whose INDEX_COUNT
is larger than its MAX_SIZE. AMD PAL and Mesa RADV write MAX_SIZE as the
indices left in the bound buffer and INDEX_COUNT as the draw's own
count, and the hardware returns 0 for the reads past MAX_SIZE. Draw the
first MAX_SIZE indices, as the CPU indirect path already does for a
record past INDEX_BUFFER_SIZE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed_draw

guest_indexed_draw still expected Pm4::Validate to refuse a DRAW_INDEX_2 or DRAW_INDEX_OFFSET_2 whose INDEX_COUNT exceeds MAX_SIZE. With this change those packets validate and resolve to MAX_SIZE indices, so the four checks now assert that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Indexed draw handling

Layer / File(s) Summary
Validation and resolved index counts
core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp, core/libs/prx/libSceAgcDriver/tests/Pm4.cpp, core/libs/tests/GuestIndexedDraw.cpp, docs/dev/debt/changes/fix-agc-index-count-past-max-size.md
Validation no longer rejects indexed draws when the count exceeds the maximum-size field. ResolveDraw caps the resolved count. Tests cover oversized counts and zero maximum size. The debt document records PAL and CPU indirect-path limits and notes that the behavior was not measured on a console.

Suggested reviewers: slopgobblercobbler, boykopovar

Change: Bug fix

Merge Risk: Minimal · up to 85bad

Indexed draws whose count exceeds MAX_SIZE are now clamped instead of refused. No concrete merge-blocking defect was found, and the remaining hardware-behavior difference is documented.

Security architecture risk: Minimal · up to 85bad

The AGC driver now accepts oversized indexed-draw counts without using the excess count to read more indices. The newly accepted packets resolve to draws the driver could already execute, with the same guest-memory checks. No material increase in security or failure-containment risk was identified.

Security review details

Security Blast Radius

  • inferred — The change broadens which guest packet encodings reach direct draw execution, not which effective index addresses and counts a guest can request through those opcodes. The base allowed the same address and capped count with the other packet fields held fixed.

Trust Boundaries and Controls

  • observed — The driver checks and copies the guest command buffer before submission validation. The two opcodes retain their queue, packet-size, and flag checks, as well as the explicit-address bit check where applicable. Resolution then checks the capped guest index range before passing parameters to the draw path.

Resilience and Maintainability Implications

  • inferred — Passing preflight does not guarantee execution will succeed. A runtime draw error follows the existing worker failure path rather than the preflight rejection path. This distinction now applies to the newly accepted encoding, but was already reachable with the equivalent capped request.

Rollout and rollback

  • observed — The AGC driver still controls admission. It no longer stops a submission solely for this count mismatch, but it can still stop a draw that fails resolution checks. The behavior changes with the driver build; the patch adds no operator setting or migration step.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 4
  • Waived: $1.00 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b7632adc-5798-47db-9a6f-23c8743c703d
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and 85bad3a.

Files selected for processing: 4

  • core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
  • core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
  • core/libs/tests/GuestIndexedDraw.cpp
  • docs/dev/debt/changes/fix-agc-index-count-past-max-size.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants