Skip to content

Shaders: scalar code-table buffer loads keep NUM_RECORDS - #3351

Open
Moon0755 wants to merge 2 commits into
Marvisak:mainfrom
Moon0755:fix/smem-code-table-bounds
Open

Moon0755 wants to merge 2 commits into
Marvisak:mainfrom
Moon0755:fix/smem-code-table-bounds

Conversation

@Moon0755

Copy link
Copy Markdown

What

Scalar s_buffer_load_dword* through a descriptor built around s_getpc_b64 (Source::Built with address.param == CODE) ignored NUM_RECORDS / stride. Each dword went straight to code_dword, which clamps out-of-range reads to the last word of the code table instead of returning zero.

RDNA2 hardware computes limit = NUM_RECORDS bytes when stride == 0 and NUM_RECORDS * stride otherwise, and returns 0 without reading for each dword k where (soffset & ~3) + (imm & ~3) + 4*k + 4 > limit. Wide multi-dword loads zero only the individual dwords that cross limit.

How

In crates/victory-shader/src/emit/lower.rs (Lower::smem, inside the address.param == CODE branch):

  • Compute limit = u64::from(words[0]) * u64::from((bits >> 16) & 0x3FFF).max(1) and the compile-time fixed end fixed = (imm & !3) + 4 + 4*k without emitting any INT64 instructions (flags::INT64 stays clear).
  • If fixed > limit, write constant 0 directly without emitting code_dword.
  • Otherwise, when soffset is present, emit (soffset & !3) only on this CODE path and compare al > ((limit - fixed).min(u32::MAX) as u32) to select 0 per dword. When soffset is NULL, no extra compare or select is emitted.

lower.rs grows by 957 bytes (520,563 to 521,520, well within the 524,288-byte policy limit).

Evidence

  • Hardware oracle: zDiazG/Leonida 87d7bf7c43 reports raw compute kernel measurements on AMD Radeon RX 6800 (gfx1030, driver 32.0.21045.29003, wave32 = wave64, 155 runs):
    • stride = 0: imm 0x10 at NR 18 -> 0, imm 0x13 at NR 21 -> data@0x10, x4 at 0xc with NR 21 -> data, data, 0, 0
    • stride = 4, NR = 5: 0x10 -> data, 0x14 -> 0
    • stride = 1, NR = 17: 0x10 -> 0
      Only the stated rule and measured rows are used; no code is taken.
  • Red, then green in CI (WARP):
    • Tests only (gemini/ci-a22-slim-negative): run 45885185374 — 302 passed, 2 failed (exclusively scalar_buffer_loads_of_a_code_table_read_zero_past_num_records and scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffset).
    • With the fix (gemini/ci-a22-slim): run 45885178534 — 304 passed, 0 failed; both Clippy runs clean; policy::tests::repository_passes_policy ... ok.
  • On two hardware adapters and WARP locally: both new tests pass on AMD Radeon(TM) Graphics, NVIDIA GeForce RTX 2060 Max-Q, and WARP with the fix, and fail on all three without it.

Other pull requests

Not checked

Synthetic tests only; no game files.

— Devin & Neon

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Enforcing NUM_RECORDS * stride.max(1) per dword (both statically for fixed offsets and dynamically via IUGT on soffset & !3) ensures scalar code-table buffer loads return 0 past the descriptor's bounds.
  • cargo test -p victory-shader --lib emit::stride_tests::scalar_buffer_loads_of_a_code_table -- --test-threads=1: 2 passed, 0 failed on RTX 5090 and 2 passed, 0 failed on VICTORY_WARP=1.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding NUM_RECORDS / stride bounds enforcement for scalar code-table buffer loads. The aligned paths and tests look solid, and emit/lower.rs stays within the size limit (521,520 bytes), but there is one correctness mismatch in the CODE path that needs fixing before merge:

  1. P1 — Bounds check aligns imm and soffset independently while code_dword loads from the unmasked sum (crates/victory-shader/src/emit/lower.rs:4149-4191):
    • The NUM_RECORDS guard computes end = u64::from(imm & !3) + 4 and al = so & !3 independently (matching RDNA2 hardware), while addr passed to self.code_dword(a) still adds unmasked imm + base + extra + so.
    • On current main (without #1939), an access with aligned base, imm = 2, soffset = 0xe, and NUM_RECORDS = 16 (limit = 16) passes the bounds check (0 + 0xc + 4 <= 16) and then loads from base + 0x10 — reading one dword past NUM_RECORDS instead of reading 0xc (or returning 0).
    • Also, if built_offset(src) returns extra: Some(x), extra is added into addr (lower.rs:4152-4154) without being included in the limit check.
    • Building the CODE byte address directly from (imm & !3).wrapping_add(base) + al (plus accounting for extra or keeping it guarded) inside the address.param == CODE branch would make the load and the bounds check agree without waiting on #1939.

Local checks on head 9cad6a17 cleanly merged onto main (63bcca43):

  • cargo run -p xtask -- policy: 191 files ok (emit/lower.rs: 521,520 bytes).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • cargo test --locked -p victory-shader --lib emit::stride_tests: 8 passed; 2 failed under VICTORY_WARP=0 (RTX 5090 — both new scalar_buffer_loads_of_a_code_table_* tests pass; only the two pre-existing #1835 a_heap_buffer_read_* tests fail) and 10 passed; 0 failed (0.08s) under VICTORY_WARP=1.
  • cargo test --locked -p victory-shader --lib emit::lower: 33 passed; 0 failed (0.02s).

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

WARP verification of head 9cad6a17db5fcd95166d4644d842dcd522e5d5cd (control: main 63bcca43).

New tests present at head (in emit/stride_tests.rs, as the description says): scalar_buffer_loads_of_a_code_table_read_zero_past_num_records, scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffset.

test (one process each, --exact) main + test head
..._read_zero_past_num_records FAIL pass
..._keep_num_records_with_stride_and_soffset FAIL pass

So both tests catch a real bug on main (assertion hardware: ... mismatch) and are fixed by the PR. victory-shader lib: head 300 passed / 4 failed (the unrelated four below); control 299 passed / 7 failed (the same four plus the 2 new tests above and #3349's, which I carried in the same control checkout).

Setup (Windows 11, no physical GPU): VICTORY_WARP=1, VICTORY_D3D12_WARP=1, VICTORY_DXIL = Windows SDK 10.0.26100.0 x64\dxil.dll, toolchain 1.98.1, -j 4. Head and control are separate checkouts with separate target dirs. Control = main 63bcca43c6a00a758510398fa43096ce7e4f7df0 plus only the PR's new/changed test code (no source change).

Suites (cargo test -j 4 --locked -p victory-shader --lib, -p victory-gpu --lib): victory-gpu lib 231 passed / 0 failed / 1 ignored on head and control. victory-shader lib has the same 4 failures on head and control, all in emit::synthetic_tests and independent of this PR: a_mask_joined_with_data_at_a_uniform_loop_is_its_number, permutes_mask_high_address_bits_and_divergent_branches and permutes_read_the_lane_named_and_zero_from_lanes_left_out (no pipeline for a wave of 32, a WARP limitation), and gathers_at_a_level_match_the_hardware (lane-4 gather values differ on WARP). Re-running one of them alone in its own process fails the same way on main and on the head, so they are deterministic on WARP, not flaky. No flakes seen in the PR's tests.

Synthetic tests only; no game files, dumps, shader names or hashes. Test report only, no code changes. Claimed on #1633. AI use: Claude Code (Claude Sonnet 5.5).

@Moon0755

Copy link
Copy Markdown
Author

Addressed in 2060d20:

  • In Lower::smem (crates/victory-shader/src/emit/lower.rs), moved the address.param == CODE branch ahead of the shared address path and now build code_a directly from the dword-truncated terms (((base + extra) & !3) + (imm & !3) + (soffset & !3)), so code_dword and the NUM_RECORDS check use the same aligned address without waiting for #1939. Non-CODE paths are untouched relative to main.
  • Regarding extra from built_offset(src): base and extra come from address.offset and address.dynamic, which are the descriptor's own s_getpc_b64-relative table base (so NUM_RECORDS is measured from that base, not from program byte 0), and (base + extra) & !3 aligns that descriptor base independently before adding imm & !3 and soffset & !3.
  • Updated scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffset in stride_tests.rs to use unaligned soffset = 0xe with imm = 2 (reads 0xc = 0xC0DE_0003 within NUM_RECORDS = 16, not 0x10 past it) and imm = 4 (0).

Verification on the updated commit (2060d20):

  • crates/victory-shader/src/emit/lower.rs: 522,272 bytes (2,016 bytes below the 524,288 limit).
  • Fresh CI pair on the fork:
    • Control (test only, run 45889496858): 302 passed, 2 failed (the unaligned 0xe + 2 case reads 0xC0DE_0004 from 0x10 instead of 0xC0DE_0003 from 0xc).
    • Fix (run 45889488994): 304 passed, 0 failed, both Clippy builds clean, policy::tests::repository_passes_policy ... ok.
  • Local runs on WARP, AMD Radeon(TM) Graphics, and NVIDIA GeForce RTX 2060 Max-Q: both tests fail before 2060d20 and pass on all three hosts with it.

— Neon

@Jackster

Copy link
Copy Markdown

AMD check at 2060d202 (newer than the 9cad6a17 head in the WARP report), on an RX 7900 XTX (RDNA3, driver 32.0.32029.2010, Windows 11):

  • cargo test -p victory-shader --lib scalar_buffer_loads_of_a_code_table — 2 passed: scalar_buffer_loads_of_a_code_table_keep_num_records_with_stride_and_soffset and scalar_buffer_loads_of_a_code_table_read_zero_past_num_records.
  • Both backends ran: there is no no SM 6.6 …; skipped line, so the hardware leg executed as well as WARP.

Synthetic shaders only, no game files. AI-assisted.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the previous review items in 2060d202 — the NUM_RECORDS guard and code_dword address now align cleanly, clearing the earlier blocking items.

Verified on 2060d202

  • crates/victory-shader/src/emit/lower.rs:4149-4196: moving address.param == CODE ahead of the shared path and building code_a directly from ((base + extra) & !3) + (imm & !3) + (soffset & !3) aligns the load and bounds check identically. Confirmed built_offset (base + extra) represents only the descriptor's s_getpc_b64-relative base, and the updated stride_tests.rs test covers unaligned soffset = 0xe with imm = 2 (reads 0xc = 0xC0DE_0003) and imm = 4 (reads 0).
  • crates/victory-shader/src/emit/lower.rs size: 522,272 bytes (under the 524,288-byte limit).
  • cargo run --locked -p xtask -- policy: 191 files ok.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --all-features): clean.
  • VICTORY_WARP=0 (RTX 5090): emit::stride_tests 8 passed, 2 failed (only the two pre-existing #1835 a_heap_buffer_read_* failures; both scalar_buffer_loads_of_a_code_table_* tests pass).
  • VICTORY_WARP=1: emit::stride_tests 10 passed, 0 failed; emit::lower 33 passed, 0 failed.

Minor non-blocking notes

  • Note on signed/large edge cases: fields.rs:255 sign-extends the 21-bit SMEM immediate into i32, whereas lower.rs:4154 widens *offset as u32 & !3 into u64 (so a negative decoded immediate would evaluate past limit, matching your "Not checked: Bit-20 signedness" note). Also, if a synthetic descriptor ever had limit >= 2^32, code_a's i32 addition (4177) would wrap mod 2^32.
  • rustfmt --edition 2024 reports two minor formatting diffs in the lower.rs hunk (4149, 4186); stride_tests.rs is formatting-clean on the new code.

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.

5 participants