victory-shader: clamp LDS indexes and drop out-of-bounds writes #618

Open
github-bot wants to merge 2 commits from github-bot/gh-618 into a-publier AGit
Owner

@⁠SalmanBarkat on GitHub · 2026-11-27 14:17 UTC · original
SalmanBarkat:fix/ds-oob-lds-clamp → a-publier · +177 −22 · 2 files · 2 commits · open

Out-of-bounds local data share (LDS) byte offsets previously wrapped or accessed outside the declared groupshared array bounds in emitted DXIL.

Summary

  • In \crates/victory-shader/src/emit/lower.rs, compute \inside\ bounds predicate on the LDS byte address against the declared \lds_bytes\ window and clamp out-of-bounds dword indexes to slot \
**@⁠SalmanBarkat** on GitHub · 2026-11-27 14:17 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/618) `SalmanBarkat:fix/ds-oob-lds-clamp` → `a-publier` · +177 −22 · 2 files · 2 commits · open Out-of-bounds local data share (LDS) byte offsets previously wrapped or accessed outside the declared groupshared array bounds in emitted DXIL. ## Summary - In \crates/victory-shader/src/emit/lower.rs\, compute \inside\ bounds predicate on the LDS byte address against the declared \lds_bytes\ window and clamp out-of-bounds dword indexes to slot \
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:18:59 +00:00
Author
Owner

@⁠gut0to on GitHub · 2026-11-27 15:00 UTC · original · reviewed

AI-assisted static review; I traced the LDS address calculation and bounds predicate in this diff. I did not run the test suite.

**@⁠gut0to** on GitHub · 2026-11-27 15:00 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/618#pullrequestreview-6090031921) · **reviewed** AI-assisted static review; I traced the LDS address calculation and bounds predicate in this diff. I did not run the test suite.
@ -5592,3 +5591,4 @@
let dword = |l: &mut Self, bytes: u32| -> (V, V) {
let c = l.cur;
let o = l.s.m.i32(bytes);
let at = l.s.m.bin(c, Bin::Add, a, o);
Author
Owner

@⁠gut0to on GitHub · 2026-11-27 15:00 UTC · original

AI-assisted review: the bounds check runs after a + bytes has already wrapped in 32-bit arithmetic. For a multi-dword access starting at 0xFFFF_FFFC, the second dword wraps to index 0 and passes inside, so an out-of-range read can return LDS[0] and a write can clobber it. Could the code detect addition overflow (or widen the sum) before clamping? Please add a boundary case for a multi-dword access at this address.

**@⁠gut0to** on GitHub · 2026-11-27 15:00 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/618#discussion_r4720891493) AI-assisted review: the bounds check runs after `a + bytes` has already wrapped in 32-bit arithmetic. For a multi-dword access starting at `0xFFFF_FFFC`, the second dword wraps to index 0 and passes `inside`, so an out-of-range read can return LDS[0] and a write can clobber it. Could the code detect addition overflow (or widen the sum) before clamping? Please add a boundary case for a multi-dword access at this address.
Author
Owner

@⁠lvndrpup on GitHub · 2026-11-27 15:38 UTC · original · reviewed

Tested on an RTX 3060 Ti (Windows 11, debug build), base 4eb6cfd against head 501fb5d.

base 4eb6cfd head 501fb5d
lds_out_of_bounds_reads_zero_and_drops_writes, copied onto base passes on the card and on WARP passes
probe: ds_write_b32 then ds_read_b32 at one address (64 threads, 512 bytes of LDS) 0 at all 9 addresses from 0x200 to 0xFFFFFFFC, on the card and on WARP same
probe below: a console wave64 program of 32 threads with 512 bytes of LDS, lowered for host waves of 32 (as on NVIDIA), ds_read_b32 at 0x200 0x20 (WARP skips it: no 32-lane waves) 0x20
cargo test -p victory-shader --lib 304 passed, 4 ignored
cargo clippy -p victory-shader --all-targets -- -D warnings, with and without --all-features clean

Two things:

  • On this card the new test doesn't fail on the base. The NVIDIA driver and WARP already read 0 and drop writes past the end of the shared array, so the cases the test covers already match the console without the clamp. The clamp still guarantees it on drivers that don't do this, since an out-of-bounds groupshared index is undefined in DXIL. Has anyone seen it fail on the base, on AMD perhaps?
  • The bound includes the translator's own dwords. The description says the bound is the declared lds_bytes, but inside compares with s.lds_dwords. For a console wave64 program in host waves of 32, lower.rs adds dwords after the title's LDS: the wave_base counter (halves, line 1948) and the exchange pair (line 1902). An access at the title's lds_bytes is then "inside" and reaches them. The third row shows it: the read at 0x200 returns 32, the counter after one host wave's atomic add, where the console gives 0. A write there would land on those dwords instead of being dropped. Bounding against the title's own size (cs.lds_bytes() / 4, before those dwords are added) would cover it. NVIDIA runs wave64 programs this way, so this is the case that matters on that hardware.

The probe, in valu_tests.rs. build_cs64 is a copy of build_cs with wave64: true:

#[test]
fn past_the_titles_lds_on_32_lane_hosts() {
    let body: Vec<u32> = [
        ds(0x35, 6, 1, 0).to_vec(),         // ds_swizzle_b32 v6, v1: keeps console lanes
        vec![vop1(0x001, 7, vgpr(0))],      // v_mov_b32 v7, v0: the thread index
        ds(DS_READ_B32, 0, 1, 0).to_vec(),  // ds_read_b32 v0, v1
    ]
    .concat();
    let was = lower::TEST_HOST_WAVE.replace(Some(32));
    let shader = build_cs64(&body, 32, 512);
    lower::TEST_HOST_WAVE.set(was);
    run_cases(&shader, &[[512, 0, 0, 0]], |backend, _, _, got| {
        assert_eq!(got, 0, "{backend}: read past the title's LDS");
    });
}

It conflicts with #623, #611, #578 and #502, all in valu_tests.rs. With #623, both PRs add a test at the end of the file.

**@⁠lvndrpup** on GitHub · 2026-11-27 15:38 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/618#pullrequestreview-6090464957) · **reviewed** Tested on an RTX 3060 Ti (Windows 11, debug build), base `4eb6cfd` against head `501fb5d`. | | base `4eb6cfd` | head `501fb5d` | |---|---|---| | `lds_out_of_bounds_reads_zero_and_drops_writes`, copied onto base | passes on the card and on WARP | passes | | probe: `ds_write_b32` then `ds_read_b32` at one address (64 threads, 512 bytes of LDS) | 0 at all 9 addresses from `0x200` to `0xFFFFFFFC`, on the card and on WARP | same | | probe below: a console wave64 program of 32 threads with 512 bytes of LDS, lowered for host waves of 32 (as on NVIDIA), `ds_read_b32` at `0x200` | `0x20` (WARP skips it: no 32-lane waves) | `0x20` | | `cargo test -p victory-shader --lib` | | 304 passed, 4 ignored | | `cargo clippy -p victory-shader --all-targets -- -D warnings`, with and without `--all-features` | | clean | Two things: - **On this card the new test doesn't fail on the base.** The NVIDIA driver and WARP already read 0 and drop writes past the end of the shared array, so the cases the test covers already match the console without the clamp. The clamp still guarantees it on drivers that don't do this, since an out-of-bounds groupshared index is undefined in DXIL. Has anyone seen it fail on the base, on AMD perhaps? - **The bound includes the translator's own dwords.** The description says the bound is the declared `lds_bytes`, but `inside` compares with `s.lds_dwords`. For a console wave64 program in host waves of 32, `lower.rs` adds dwords after the title's LDS: the `wave_base` counter (`halves`, line 1948) and the exchange pair (line 1902). An access at the title's `lds_bytes` is then "inside" and reaches them. The third row shows it: the read at `0x200` returns 32, the counter after one host wave's atomic add, where the console gives 0. A write there would land on those dwords instead of being dropped. Bounding against the title's own size (`cs.lds_bytes() / 4`, before those dwords are added) would cover it. NVIDIA runs wave64 programs this way, so this is the case that matters on that hardware. The probe, in `valu_tests.rs`. `build_cs64` is a copy of `build_cs` with `wave64: true`: ```rust #[test] fn past_the_titles_lds_on_32_lane_hosts() { let body: Vec<u32> = [ ds(0x35, 6, 1, 0).to_vec(), // ds_swizzle_b32 v6, v1: keeps console lanes vec![vop1(0x001, 7, vgpr(0))], // v_mov_b32 v7, v0: the thread index ds(DS_READ_B32, 0, 1, 0).to_vec(), // ds_read_b32 v0, v1 ] .concat(); let was = lower::TEST_HOST_WAVE.replace(Some(32)); let shader = build_cs64(&body, 32, 512); lower::TEST_HOST_WAVE.set(was); run_cases(&shader, &[[512, 0, 0, 0]], |backend, _, _, got| { assert_eq!(got, 0, "{backend}: read past the title's LDS"); }); } ``` It conflicts with #623, #611, #578 and #502, all in `valu_tests.rs`. With #623, both PRs add a test at the end of the file.
This pull request can be merged automatically.
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/618/head:github-bot/gh-618'
git switch 'github-bot/gh-618'

Merge

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