shader: leave planning-only reads out of the empty-table and zero-stride folds (Ghost of Yōtei) #2230

Open
github-bot wants to merge 1 commit from github-bot/gh-2230 into main AGit
Owner

@⁠Nawid3333 on GitHub · 2026-11-12 16:02 UTC · original
Nawid3333:fix/planning-reads-specialization → main · +39 −2 · 2 files · 1 commits · open

Compiling Ghost of Yōtei's compute shader 386c6a44da561c0d reads past the end of the buffer vectors in ApplyResourceSpecialization: the empty scalar-table fold indexes specialization.buffers and program.info.buffers with memory.resource 17, past the end of both. Usually that reads whatever follows the vectors on the heap; in an offline recompile of the shader it crashed with an access violation in 3 of 100 runs (ApplyResourceSpecialization ← CompileProgram).

Cause. Resource tracking assigns no buffer to planning-only memory (it returns before AddMemoryPatch), so resource keeps the translator's value, the descriptor's SGPR index divided by 4. The emitter skips planning-only scalar reads, and ResourceControlFlow and CollectShaderInfo skip planning-only memory; the empty scalar-table fold and the zero-stride fold in ApplyResourceSpecialization do not. With a larger index they read out of bounds; with a smaller one they read an unrelated buffer and can fold the planning reads through it.

In 172 Ghost of Yōtei programs (a capture recompiled offline, guest memory read as zeros), 4 have planning-only scalar reads: 2 index out of bounds on every compile (386c6a44da561c0d, b920ac9e14cb5fb8), and 2 name an unrelated buffer: in 69ba1a07c7a95510 the index 5 is an empty scalar table, so all 8 planning reads are folded to zero there; in 3d82c2913cac198d the index 3 is not empty, so nothing is folded.

Change. Both folds skip planning-only memory, as ResourceControlFlow and CollectShaderInfo do. A planning read is not a resource, so neither fold can tell which table it reads.

Tests.

  • TestPlanningReadsSkipBufferFolds (a planning scalar-table read, and a planning LoadBufferU32x4 of a descriptor) runs with the raw index 0, which names an unrelated empty scalar table / zero-stride buffer, and with 2, past the end of the one-entry buffer lists. On main it fails with planning-only table read was folded through an unrelated empty scalar table; without either of the two checks it fails with that check's own message; with the fix all cases pass.
  • With and without the fix the programs compile to byte-identical SPIR-V: all 172 with the AMD device profile, and the 167 the default profile can emit (the other 5 need maintenance8 on both builds); in one series of 60 offline recompiles of 386c6a44da561c0d each, main crashed once and the fix not at all.
  • In a build with AddressSanitizer and the MSVC STL's bounds checks (_MSVC_STL_HARDENING=1), with the AMD device profile, main stops at the bounds check on 386c6a44da561c0d and b920ac9e14cb5fb8 on every compile, and nowhere else in the 172 programs; with the fix all 172 compile without a report.
  • My fork's CI (run 43229985446): Linux and macOS build and pass their tests. Windows stops at main's own tests/BvhMirrorGpuTests.inc(154,25): error: expected unqualified-id (#2229 fixes that).

Written with Claude Code (AI): it found the crash in an offline recompile of the game's shaders, traced it to this read, wrote the change and the test, and ran them on my PC; reviewed by me.

🤖 Generated with Claude Code

**@⁠Nawid3333** on GitHub · 2026-11-12 16:02 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2230) `Nawid3333:fix/planning-reads-specialization` → `main` · +39 −2 · 2 files · 1 commits · open Compiling Ghost of Yōtei's compute shader `386c6a44da561c0d` reads past the end of the buffer vectors in `ApplyResourceSpecialization`: the empty scalar-table fold indexes `specialization.buffers` and `program.info.buffers` with `memory.resource` 17, past the end of both. Usually that reads whatever follows the vectors on the heap; in an offline recompile of the shader it crashed with an access violation in 3 of 100 runs (`ApplyResourceSpecialization` ← `CompileProgram`). **Cause.** Resource tracking assigns no buffer to planning-only memory (it returns before `AddMemoryPatch`), so `resource` keeps the translator's value, the descriptor's SGPR index divided by 4. The emitter skips planning-only scalar reads, and `ResourceControlFlow` and `CollectShaderInfo` skip planning-only memory; the empty scalar-table fold and the zero-stride fold in `ApplyResourceSpecialization` do not. With a larger index they read out of bounds; with a smaller one they read an unrelated buffer and can fold the planning reads through it. In 172 Ghost of Yōtei programs (a capture recompiled offline, guest memory read as zeros), 4 have planning-only scalar reads: 2 index out of bounds on every compile (`386c6a44da561c0d`, `b920ac9e14cb5fb8`), and 2 name an unrelated buffer: in `69ba1a07c7a95510` the index 5 is an empty scalar table, so all 8 planning reads are folded to zero there; in `3d82c2913cac198d` the index 3 is not empty, so nothing is folded. **Change.** Both folds skip planning-only memory, as `ResourceControlFlow` and `CollectShaderInfo` do. A planning read is not a resource, so neither fold can tell which table it reads. **Tests.** - `TestPlanningReadsSkipBufferFolds` (a planning scalar-table read, and a planning `LoadBufferU32x4` of a descriptor) runs with the raw index 0, which names an unrelated empty scalar table / zero-stride buffer, and with 2, past the end of the one-entry buffer lists. On main it fails with `planning-only table read was folded through an unrelated empty scalar table`; without either of the two checks it fails with that check's own message; with the fix all cases pass. - With and without the fix the programs compile to byte-identical SPIR-V: all 172 with the AMD device profile, and the 167 the default profile can emit (the other 5 need maintenance8 on both builds); in one series of 60 offline recompiles of `386c6a44da561c0d` each, main crashed once and the fix not at all. - In a build with AddressSanitizer and the MSVC STL's bounds checks (`_MSVC_STL_HARDENING=1`), with the AMD device profile, main stops at the bounds check on `386c6a44da561c0d` and `b920ac9e14cb5fb8` on every compile, and nowhere else in the 172 programs; with the fix all 172 compile without a report. - My fork's CI (run 43229985446): Linux and macOS build and pass their tests. Windows stops at main's own `tests/BvhMirrorGpuTests.inc(154,25): error: expected unqualified-id` (#2229 fixes that). Written with Claude Code (AI): it found the crash in an offline recompile of the game's shaders, traced it to this read, wrote the change and the test, and ran them on my PC; reviewed by me. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Resource tracking does not assign a buffer to planning-only memory, so its
`resource` keeps the descriptor's SGPR quad index from translation. The
scalar empty-table fold and the zero-stride fold in
ApplyResourceSpecialization indexed the buffer specialization with it: past
the end of the buffers when the index is larger (an out-of-bounds read on
every compile of such a program), and an unrelated buffer when it is
smaller, which folds the planning reads through that buffer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:17:29 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-12 16:05 UTC · original

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

ApplyResourceSpecialization now skips empty-scalar-buffer replacement and native zero-stride out-of-bounds handling for planning-only resources. Tests check that planning-only scalar and vector buffer reads remain after specialization across both table types and raw indices 0 and 2.

Suggested reviewers: nmzik, claxten10

Change: Bug fix

Merge Risk: Minimal · up to bf55c

The change stops planning-only buffer reads from being folded using an unrelated or out-of-range buffer, which can cause crashes during shader compilation. No merge-blocking risk is evident from the supplied review context.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: planning-only reads are excluded from the empty scalar-table and zero-stride folds in ApplyResourceSpecialization. The raw summary and objectives both describe this ch…
Description check Passed The description explains the out-of-bounds read in ApplyResourceSpecialization, the cause, the change to both folds, and the new tests. It relates directly to the changeset.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: planning-only reads are excluded from the empty scalar-table and zero-stride folds in ApplyResourceSpecialization. The raw summary and objectives both describe this change, so the title matches the changeset.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0a016e91a0 and bf55cb7e7f.

Files selected for processing: 2

  • src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpp
  • tests/ResourceMaterializationTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-12 16:05 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2230#issuecomment-6828812675) <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/2230?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_status_start --> No actionable comments were generated in the recent review. <!-- recent_review_status_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> <br> `ApplyResourceSpecialization` now skips empty-scalar-buffer replacement and native zero-stride out-of-bounds handling for planning-only resources. Tests check that planning-only scalar and vector buffer reads remain after specialization across both table types and raw indices 0 and 2. **Suggested reviewers:** `nmzik`, `claxten10` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"bf55cb7e7f823c9d8e05f8f92ddfc28571f40382" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>bf55c</code></summary> <dl> <dd> <br> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `bf55c` --> <!-- final_review_risk_coverage:{"sourceCommitId":"bf55cb7e7f823c9d8e05f8f92ddfc28571f40382","coveredCommitId":"bf55cb7e7f823c9d8e05f8f92ddfc28571f40382","kind":"reviewed"} --> The change stops planning-only buffer reads from being folded using an unrelated or out-of-range buffer, which can cause crashes during shader compilation. No merge-blocking risk is evident from the supplied review context. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4</strong></summary> <dl> <dd> <br> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title names the main change: planning-only reads are excluded from the empty scalar-table and zero-stride folds in ApplyResourceSpecialization. The raw summary and objectives both describe this ch… | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description explains the out-of-bounds read in ApplyResourceSpecialization, the cause, the change to both folds, and the new tests. It relates directly to the changeset. | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | <details> <summary><strong>Full details: Title check</strong></summary> <dl> <dd> <br> **Explanation** The title names the main change: planning-only reads are excluded from the empty scalar-table and zero-stride folds in ApplyResourceSpecialization. The raw summary and objectives both describe this change, so the title matches the changeset. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=2230)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>Share</strong></summary> <dl> <dd> <br> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <!-- tips_end --> <!-- recent_review_start --> <h6> <details> <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary> <dl> <dd> <br> <!-- review_info_content --> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary> <dl> <dd> <br> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/0a016e91a07906e927cee7cd00b13173f40da91b/.coderabbit.yaml>) - **Commit**: [0a016e9](<https://github.com/KytyPS5/KytyPS5/commit/0a016e91a07906e927cee7cd00b13173f40da91b>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `2533bc42-d1a8-48ea-9586-a7db0aae6116` </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary> <dl> <dd> <br> Reviewing files that changed from the base of the PR and between 0a016e91a07906e927cee7cd00b13173f40da91b and bf55cb7e7f823c9d8e05f8f92ddfc28571f40382. </dd> </dl> </details> <details> <summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</strong></summary> <dl> <dd> <br> * `src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpp` * `tests/ResourceMaterializationTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review. </dd> </dl> </details> </h6> <!-- recent_review_end -->
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2230/head:github-bot/gh-2230'
git switch 'github-bot/gh-2230'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2230'
git switch 'github-bot/gh-2230'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2230'
git switch 'github-bot/gh-2230'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2230'
git switch 'main'
git merge --squash 'github-bot/gh-2230'
git switch 'main'
git merge --ff-only 'github-bot/gh-2230'
git switch 'main'
git merge 'github-bot/gh-2230'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.