[pr-fix][shaders] shader: clamp cbufferLoadLegacy row index to 4096 to prevent Ada LDC device removal #223

Open
hingeowl wants to merge 2 commits from hingeowl/Darner:pr/clamp-cbuffer-load-row into main
Contributor

What it changes

Clamps dynamic dx.op.cbufferLoadLegacy.i32 row indices in cbuffer_dword (and guards constant offsets >= 64 KiB in smem) to < 4096, selecting row 0 for the load and masking out-of-range results to 0, preventing NVIDIA Ada (LDC) invalid-constant-address warp exceptions past D3D12's 64 KiB CBV limit. Includes a unit test covering OOB dynamic CBV offsets.

Notes

  • Rebased cleanly onto main (f3840d7).
  • Note: the primary CS_RaytracingResolve 0x887A0005 crash on RTX 4090 was separately root-caused to NVIDIA's 512 MiB shader-code heap limit (see PR #187), but this clamp remains a clean defensive correctness guard against OOB dynamic CBV row indices on Ada. Standalone A/B on current main has not been run yet.
### What it changes Clamps dynamic `dx.op.cbufferLoadLegacy.i32` row indices in `cbuffer_dword` (and guards constant offsets `>= 64 KiB` in `smem`) to `< 4096`, selecting row 0 for the load and masking out-of-range results to 0, preventing NVIDIA Ada (`LDC`) invalid-constant-address warp exceptions past D3D12's 64 KiB CBV limit. Includes a unit test covering OOB dynamic CBV offsets. ### Notes - Rebased cleanly onto `main` (`f3840d7`). - Note: the primary `CS_RaytracingResolve` `0x887A0005` crash on RTX 4090 was separately root-caused to NVIDIA's 512 MiB shader-code heap limit (see PR #187), but this clamp remains a clean defensive correctness guard against OOB dynamic CBV row indices on Ada. Standalone A/B on current `main` has not been run yet.
On NVIDIA Ada GPUs (RTX 40-series), a dynamic dx.op.cbufferLoadLegacy.i32
(compiled to SASS LDC) with row index >= 4096 (past D3D12's 64 KiB CBV limit)
raises an instant hardware warp exception (Invalid Constant Address LDC ->
DXGI_ERROR_DEVICE_REMOVED 0x887A0005 with no page fault, no TDR event, and
empty DRED breadcrumbs) instead of returning zero. This triggered reliably
inside CS_RaytracingResolve shaders under both Hardware RT and Software RT
(and even with DARNER_HW_RT=0 + DARNER_RT_RESOLVE=1).

Clamp dynamic row indices in cbuffer_dword (and guard constant offsets >= 64 KiB
in smem) to < 4096, selecting row 0 for the load and masking out-of-range
results to 0. Add a hardware + WARP unit test covering OOB dynamic CBV offsets.
First-time contributor

Independent Windows build, red/green test, and code-read review of 0f5138d48 (based on main f3840d73d). Not run in-game.

Environment

  • Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (Ampere, driver 621.47), Rust 1.98.1 (x86_64-pc-windows-msvc), MSVC 19.44.35230.

Measured results

  • Gate 1 (cargo clippy --workspace --all-targets --locked -- -D warnings): PASS
  • Gate 2 (cargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASS
  • cargo xtask policy: PASS (asset policy: 555 files ok)
  • cargo test --locked -p darner-shader --lib emit::valu_tests::cbv -- --nocapture: PASS (3 passed; 0 failed in 1.85 s)
  • Red-on-main check (f3840d73d + PR's valu_tests.rs only):
    • cargo test --locked -p darner-shader --lib emit::valu_tests::cbv_dynamic_oob_load_returns_zero_without_faulting -- --exact --nocapture: PASSED on unmodified main (1 passed; 0 failed in 1.02 s).

Findings

  1. Wrap-around bypass in the static smem path (crates/darner-shader/src/emit/lower.rs:8160-8162):
    • let byte = base.wrapping_add(4 * k); if (byte >> 4) >= 4096 wraps back into valid rows when base is near u32::MAX (for example, Fields::Smem.offset is signed 21-bit at gfx10/fields.rs:267 and base is (*offset as u32).wrapping_add(built_base) at lower.rs:7990). For a multi-dword load with base = 0xFFFF_FFFC (-4) and count = 4, k = 0 returns 0 while k = 1..3 wrap to byte = 0, 4, 8 and read cbv[0..3] instead of 0.
    • Using base.checked_add(4 * k).filter(|&b| (b >> 4) < 4096) (returning 0 on None) avoids the wrap.
  2. Same 32-bit wrap-around in dynamic callers of cbuffer_dword (note):
    • smem dynamic (lower.rs:8181-8183), picked-heap CBV (lower.rs:8080-8082), vector buffer_load (lower.rs:13862), and 16-bit load_subword (lower.rs:8351, byte + 1) add 4 * k / 1 in i32 before calling cbuffer_dword. cbuffer_dword's clamp still keeps the row index < 4096 (so Ada LDC will not fault), though dynamic OOB addresses near u32::MAX can wrap into row 0 instead of returning 0.
  3. New test does not catch removing the clamp on non-Ada GPUs / WARP, and does not cover the static path (valu_tests.rs:2735-2776):
    • As measured above, cbv_dynamic_oob_load_returns_zero_without_faulting passes on unmodified main on both Ampere (RTX A2000) and WARP because they already return 0 for OOB cbufferLoadLegacy.
    • Adding a structural IR assertion checking that cbuffer_dword emits the IULT 4096 / select clamp (and a static s_buffer_load_dword test at >= 0x1_0000 verifying zero cbufferLoadLegacy calls / zero result, plus the 0xFFFF_FFFC wrap case) would make the regression test fail on main across all hardware.
  • Checked opt.rs interaction: simplify_guarded_uses (opt.rs:2405-2423, 2982-2989) and LICM (opt.rs:2465-2468) do not hoist or break the dynamic clamp; the only other cbufferLoadLegacy.i32 emitter (user_at, lower.rs:3385) is bounded by user-data dwords.

Verdict: Changes requested (checked_add on the static path + IR-level test assertions so the test goes red on main without Ada hardware).

AI-assisted: yes (Gemini 4 Neon).

Independent Windows build, red/green test, and code-read review of `0f5138d48` (based on `main` `f3840d73d`). Not run in-game. ### Environment - Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (Ampere, driver 621.47), Rust 1.98.1 (`x86_64-pc-windows-msvc`), MSVC 19.44.35230. ### Measured results - Gate 1 (`cargo clippy --workspace --all-targets --locked -- -D warnings`): **PASS** - Gate 2 (`cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`): **PASS** - `cargo xtask policy`: **PASS** (`asset policy: 555 files ok`) - `cargo test --locked -p darner-shader --lib emit::valu_tests::cbv -- --nocapture`: **PASS** (`3 passed; 0 failed` in 1.85 s) - **Red-on-`main` check (`f3840d73d` + PR's `valu_tests.rs` only):** - `cargo test --locked -p darner-shader --lib emit::valu_tests::cbv_dynamic_oob_load_returns_zero_without_faulting -- --exact --nocapture`: **PASSED on unmodified `main`** (`1 passed; 0 failed` in 1.02 s). ### Findings 1. **Wrap-around bypass in the static `smem` path (`crates/darner-shader/src/emit/lower.rs:8160-8162`):** - `let byte = base.wrapping_add(4 * k); if (byte >> 4) >= 4096` wraps back into valid rows when `base` is near `u32::MAX` (for example, `Fields::Smem.offset` is signed 21-bit at `gfx10/fields.rs:267` and `base` is `(*offset as u32).wrapping_add(built_base)` at `lower.rs:7990`). For a multi-dword load with `base = 0xFFFF_FFFC` (`-4`) and `count = 4`, `k = 0` returns `0` while `k = 1..3` wrap to `byte = 0, 4, 8` and read `cbv[0..3]` instead of `0`. - Using `base.checked_add(4 * k).filter(|&b| (b >> 4) < 4096)` (returning `0` on `None`) avoids the wrap. 2. **Same 32-bit wrap-around in dynamic callers of `cbuffer_dword` (note):** - `smem` dynamic (`lower.rs:8181-8183`), picked-heap CBV (`lower.rs:8080-8082`), vector `buffer_load` (`lower.rs:13862`), and 16-bit `load_subword` (`lower.rs:8351`, `byte + 1`) add `4 * k` / `1` in `i32` before calling `cbuffer_dword`. `cbuffer_dword`'s clamp still keeps the row index `< 4096` (so Ada `LDC` will not fault), though dynamic OOB addresses near `u32::MAX` can wrap into row 0 instead of returning `0`. 3. **New test does not catch removing the clamp on non-Ada GPUs / WARP, and does not cover the static path (`valu_tests.rs:2735-2776`):** - As measured above, `cbv_dynamic_oob_load_returns_zero_without_faulting` passes on unmodified `main` on both Ampere (RTX A2000) and WARP because they already return `0` for OOB `cbufferLoadLegacy`. - Adding a structural IR assertion checking that `cbuffer_dword` emits the `IULT 4096` / `select` clamp (and a static `s_buffer_load_dword` test at `>= 0x1_0000` verifying zero `cbufferLoadLegacy` calls / zero result, plus the `0xFFFF_FFFC` wrap case) would make the regression test fail on `main` across all hardware. - Checked `opt.rs` interaction: `simplify_guarded_uses` (`opt.rs:2405-2423, 2982-2989`) and LICM (`opt.rs:2465-2468`) do not hoist or break the dynamic clamp; the only other `cbufferLoadLegacy.i32` emitter (`user_at`, `lower.rs:3385`) is bounded by user-data dwords. **Verdict:** Changes requested (`checked_add` on the static path + IR-level test assertions so the test goes red on `main` without Ada hardware). AI-assisted: yes (Gemini 4 Neon).
Author
Contributor

Fixed in eb0ce54:

  1. Switched the static smem CBV path in crates/darner-shader/src/emit/lower.rs to base.checked_add(4 * k).filter(|&b| (b >> 4) < 4096) so signed offsets near u32::MAX (e.g. -4 = 0xFFFF_FFFC with count = 4) return 0 for every dword instead of wrapping into row 0.
  2. Added structural IR assertions (IULT bounds check, selects >= 5, cbv_loads == 1) to cbv_dynamic_oob_load_returns_zero_without_faulting so it fails on unmodified main regardless of GPU vendor/WARP, and added cbv_static_oob_and_wraparound_loads_emit_no_cbuffer_load covering both 0x1_0000 and -4 (0xFFFF_FFFC, 4 dwords) with cbv_loads == 0 and GPU/WARP readback [0, 0].

Validation on eb0ce54:

  • cargo test -p darner-shader --lib --locked -- emit::valu_tests::cbv --nocapture — 4 passed, 0 failed.
  • cargo clippy -p darner-shader --all-targets --all-features --locked -- -D warnings — passed.

Ready for re-review.

Fixed in `eb0ce54`: 1. Switched the static `smem` CBV path in `crates/darner-shader/src/emit/lower.rs` to `base.checked_add(4 * k).filter(|&b| (b >> 4) < 4096)` so signed offsets near `u32::MAX` (e.g. `-4` = `0xFFFF_FFFC` with `count = 4`) return `0` for every dword instead of wrapping into row 0. 2. Added structural IR assertions (`IULT` bounds check, `selects >= 5`, `cbv_loads == 1`) to `cbv_dynamic_oob_load_returns_zero_without_faulting` so it fails on unmodified `main` regardless of GPU vendor/WARP, and added `cbv_static_oob_and_wraparound_loads_emit_no_cbuffer_load` covering both `0x1_0000` and `-4` (`0xFFFF_FFFC`, 4 dwords) with `cbv_loads == 0` and GPU/WARP readback `[0, 0]`. Validation on `eb0ce54`: - `cargo test -p darner-shader --lib --locked -- emit::valu_tests::cbv --nocapture` — 4 passed, 0 failed. - `cargo clippy -p darner-shader --all-targets --all-features --locked -- -D warnings` — passed. Ready for re-review.
Contributor

Code review only (read the diff at eb0ce543), nothing built or run. lewis499's static-path and test points are fixed in eb0ce54. Their point 2 (dynamic paths wrap past u32::MAX before the clamp) is still open, though it can't fault.

  • Superseded by #292, which changes the same cbuffer_dword and static row code (the two conflict) and does this clamp behind DARNER_CB_ROWS.
  • The clamp at 4096 likely misses the fault seen in the field. NVIDIA checks the view's own size, not 64 KiB. #292's shadow-cascade dump faulted on row 4095 of a 768-byte view, read by an inactive lane. That is below 4096, so this clamp lets it through. #292 reads row 0 for inactive lanes and handles the wrap too.
  • The new code comment says LDC gives 0x887A0005. #292's runs report 0x887A0006 for the LDC kind, and the description here traces the 0x887A0005 resolve crash to the shader heap (#187). Worth wording it as "device removed" without a code.

Suggest merging one of the two, #292 for its wider coverage.

Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.

Code review only (read the diff at `eb0ce543`), nothing built or run. lewis499's static-path and test points are fixed in `eb0ce54`. Their point 2 (dynamic paths wrap past `u32::MAX` before the clamp) is still open, though it can't fault. - **Superseded by #292**, which changes the same `cbuffer_dword` and static row code (the two conflict) and does this clamp behind `DARNER_CB_ROWS`. - **The clamp at 4096 likely misses the fault seen in the field.** NVIDIA checks the view's own size, not 64 KiB. #292's shadow-cascade dump faulted on row 4095 of a 768-byte view, read by an inactive lane. That is below 4096, so this clamp lets it through. #292 reads row 0 for inactive lanes and handles the wrap too. - The new code comment says LDC gives `0x887A0005`. #292's runs report `0x887A0006` for the LDC kind, and the description here traces the 0x887A0005 resolve crash to the shader heap (#187). Worth wording it as "device removed" without a code. Suggest merging one of the two, #292 for its wider coverage. Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
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 'pr/clamp-cbuffer-load-row:hingeowl-pr/clamp-cbuffer-load-row'
git switch 'hingeowl-pr/clamp-cbuffer-load-row'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'hingeowl-pr/clamp-cbuffer-load-row'
git switch 'hingeowl-pr/clamp-cbuffer-load-row'
git rebase 'main'
git switch 'main'
git merge --ff-only 'hingeowl-pr/clamp-cbuffer-load-row'
git switch 'hingeowl-pr/clamp-cbuffer-load-row'
git rebase 'main'
git switch 'main'
git merge --no-ff 'hingeowl-pr/clamp-cbuffer-load-row'
git switch 'main'
git merge --squash 'hingeowl-pr/clamp-cbuffer-load-row'
git switch 'main'
git merge --ff-only 'hingeowl-pr/clamp-cbuffer-load-row'
git switch 'main'
git merge 'hingeowl-pr/clamp-cbuffer-load-row'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!223
No description provided.