Shaders: LDS accesses past the program's allocation read 0 and drop writes #336

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

@⁠DiosET3RN0 on GitHub · 2026-11-26 17:28 UTC · original
DiosET3RN0:lds-bounds → main · +93 −12 · 4 files · 1 commits · open

What

ds lowering assumed every LDS address was in bounds
(shortcut: LDS addresses are taken as dword-aligned and in bounds). On the
console an access past the program's LDS reads 0 and drops the write; in DXIL
an out-of-range groupshared index is undefined behaviour (garbage, memory
corruption or a device hang, depending on the driver).

It is worse than plain UB here: the translator places its own dwords right
after the program's LDS (the wave64-on-32 lane exchange and the wave_base
counter), so a title overflow silently corrupted the wave64 emulation. The
BVH traversal keeps a per-thread stack in LDS, which is exactly where such an
overflow would come from.

How

  • Shader::lds_title_dwords: the LDS size the console program declares (set
    where lds_dwords is set for CS and MS, before the layer's own dwords are
    added).
  • In ds, each dword access computes index < lds_title_dwords, uses an
    index clamped into range (always valid), and:
    • reads (ds_read_*, bytes/halves, d16) give 0 out of range;
    • writes (ds_write_*, write2) are guarded per dword;
    • atomics (ds_add…, cmpst, min/max_f32) do not run and return 0; lanes
      switched off by EXEC keep their value, as before.
    • Only the dword-alignment assumption stays as a shortcut:.
  • TRANSLATOR_VERSION 1 → 2: the DXIL of every program using LDS changes, so
    cached translations must not be reused (per the rule in lib.rs).
  • Cost: one compare and one select per LDS access; constant addresses fold.

Tests

New GPU test ds_past_the_program_lds_reads_zero_and_drops_writes
(512-byte LDS; addresses 4 and 508 in bounds, 512, 0x10000 and 0xFFFFFFFC out).

Run on NVIDIA RTX 4060 8 GB (32-lane waves; driver 621.24, Windows 11), Rust 1.98.1:

  • cargo test -p victory-shader --locked ds_: all pass, the new test included
    (it runs on the hardware adapter; WARP refuses its WaveSize(32)).
  • cargo test --workspace --locked: 1029 passed, 0 failed, 45 ignored.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.
  • cargo clippy --workspace ... fails on main already, before this change, in
    victory-loader/src/descriptor_sites.rs:277 (transmute used without annotations, Rust 1.98.1); this PR does not touch that crate.
  • Control: with the bounds check disabled on purpose the new test fails at
    byte 0x200 (the first out-of-range access), so it does catch the bug.

Not run in the title (no dump on this machine): it should be a no-op for well-
behaved programs; a run of victory shaders will report how many translations
changed.

Originally written for a fork of fxceless/victory-fork and ported here.

🤖 Generated with Claude Code

**@⁠DiosET3RN0** on GitHub · 2026-11-26 17:28 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/336) `DiosET3RN0:lds-bounds` → `main` · +93 −12 · 4 files · 1 commits · open ## What `ds` lowering assumed every LDS address was in bounds (`shortcut: LDS addresses are taken as dword-aligned and in bounds`). On the console an access past the program's LDS reads 0 and drops the write; in DXIL an out-of-range `groupshared` index is undefined behaviour (garbage, memory corruption or a device hang, depending on the driver). It is worse than plain UB here: the translator places its own dwords right after the program's LDS (the wave64-on-32 lane exchange and the `wave_base` counter), so a title overflow silently corrupted the wave64 emulation. The BVH traversal keeps a per-thread stack in LDS, which is exactly where such an overflow would come from. ## How - `Shader::lds_title_dwords`: the LDS size the console program declares (set where `lds_dwords` is set for CS and MS, before the layer's own dwords are added). - In `ds`, each dword access computes `index < lds_title_dwords`, uses an index clamped into range (always valid), and: - reads (`ds_read_*`, bytes/halves, d16) give 0 out of range; - writes (`ds_write_*`, `write2`) are guarded per dword; - atomics (`ds_add…`, `cmpst`, `min/max_f32`) do not run and return 0; lanes switched off by EXEC keep their value, as before. - Only the dword-alignment assumption stays as a `shortcut:`. - `TRANSLATOR_VERSION` 1 → 2: the DXIL of every program using LDS changes, so cached translations must not be reused (per the rule in `lib.rs`). - Cost: one compare and one select per LDS access; constant addresses fold. ## Tests New GPU test `ds_past_the_program_lds_reads_zero_and_drops_writes` (512-byte LDS; addresses 4 and 508 in bounds, 512, 0x10000 and 0xFFFFFFFC out). Run on **NVIDIA RTX 4060 8 GB** (32-lane waves; driver 621.24, Windows 11), Rust 1.98.1: - `cargo test -p victory-shader --locked ds_`: all pass, the new test included (it runs on the hardware adapter; WARP refuses its `WaveSize(32)`). - `cargo test --workspace --locked`: 1029 passed, 0 failed, 45 ignored. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean. - `cargo clippy --workspace ...` fails on `main` already, before this change, in `victory-loader/src/descriptor_sites.rs:277` (`transmute used without annotations`, Rust 1.98.1); this PR does not touch that crate. - Control: with the bounds check disabled on purpose the new test fails at byte 0x200 (the first out-of-range access), so it does catch the bug. Not run in the title (no dump on this machine): it should be a no-op for well- behaved programs; a run of `victory shaders` will report how many translations changed. Originally written for a fork of fxceless/victory-fork and ported here. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On the console an LDS access past the program's allocation reads 0 and
drops the write; in DXIL an out-of-range groupshared index is undefined
behaviour, and the dwords right after the program's are the layer's own
(wave64 lane exchange, wave_base counter), so an overflow corrupted them.

Each LDS dword access now checks the index against the size the program
declares (lds_title_dwords), uses a clamped index, reads 0 out of range,
guards writes per dword, and skips atomics (returning 0). Only the
dword-alignment shortcut remains. TRANSLATOR_VERSION 1 -> 2, since the
DXIL of every program using LDS changes.

New GPU test ds_past_the_program_lds_reads_zero_and_drops_writes; it
fails without the check (at byte 0x200) and passes with it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:39:08 +00:00
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/336/head:github-bot/gh-336'
git switch 'github-bot/gh-336'

Merge

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