Skip to content

Shaders: accept loaded buffer descriptors rewritten with base intact (#815) - #2388

Closed
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-rewritten-buffer-descriptors-815
Closed

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-rewritten-buffer-descriptors-815

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Summary

Allows non-format 4-dword V# buffer descriptors (Fields::Buffer and Fields::Smem) whose stride, record-count, or flag fields were rewritten in place via pure SCC-free scalar ALU after s_load_* to resolve to Source::Loaded, provided the 48-bit virtual base address (w0 and w1[15:0]) is provably preserved (and w1 is untouched when idxen = true).

In the native compute pipeline measurements on main reported in #815 (#issuecomment-7160878577 world-run breakdown), "a descriptor changed after loading" is the single largest refusal reason (316 distinct compute programs), where the shader compiler converts structured buffer descriptors to raw views or adjusts record counts/flags with scalar ALU sequences outside made_raw's single 6-instruction FidelityFX pattern.

Soundness Guards

  • Non-format 4-dword buffer accesses only: Scoped strictly to Fields::Smem and Fields::Buffer with !inst.name().contains("format") && dwords == 4 (threaded through classify, phi, select, per_lane, lane_select, and lane_phi). 8-dword T# image descriptors, 4-dword S# samplers, and formatted tbuffer_*_format_* / buffer_*_format_* accesses remain rejected if modified.
  • 48-bit Base Address & Indexed Stride Preservation:
    • w0 (BASE_ADDRESS[31:0]) must be the unmodified Value inst.writes[first].1 of the s_load_*.
    • For unindexed accesses (Fields::Smem or Fields::Buffer { idxen: false, .. }), preserves_low16 proves w1 retains bits [15:0] (BASE_ADDRESS[47:32]) of inst.writes[first + 1].1 (e.g. s_and_b32 with mask & 0xFFFF == 0xFFFF, s_or_b32/s_xor_b32/s_andn2_b32 with low16_is_zero, s_bitset0/1_b32 on bit >= 16).
    • For stride-indexed accesses (Fields::Buffer { idxen: true, .. }), w1 must equal inst.writes[first + 1].1 unchanged so STRIDE (w1[29:16]) and SWIZZLE_ENABLE match the host view.
  • Pure Single-Descriptor Derivation: w1, w2, and w3 must derive via pure SCC-free scalar ALU exclusively from constants and inst.writes[first..first + 4] of that descriptor's load (rejecting cross-descriptor dwords from wider s_load_dwordx8/x16 loads or external SGPRs).

Verification

  • resource::tests::rewritten_loaded_buffer_descriptors_keep_base_and_reject_unsafe_edits: covers valid MUBUF, SMEM, idxen w2/w3, and phi-joined rewrites, and verifies refusals for modified w0, modified w1[15:0], modified w1 stride with idxen = true, tbuffer_load_format_x, cross-descriptor s_load_dwordx8 mixing, and rewritten 4-dword samplers.
  • emit::valu_tests::rewritten_loaded_buffer_descriptor_executes_on_gpu_and_warp: validates DXIL with dxil.dll and executes on both hardware GPU and WARP.
  • cargo clippy -p victory-shader --lib --tests --all-features --locked -- -D warnings & cargo xtask policy pass.

Refs #815

@catminou4 catminou4 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.

Tested locally: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — loaded buffer descriptors rewritten with base accepted.

@welisonzika14-jpg

Copy link
Copy Markdown

First run on NVIDIA hardware here, as far as I can see. RTX 5070 (Blackwell, driver 621.24), Ryzen 7 5700X, Windows 11, Rust 1.98.1 MSVC, no game files. Head a276e8b merged onto main 940d32f, cargo test -p victory-gpu -p victory-shader --lib --no-fail-fast.

The PR's new test(s) pass on this card: emit::valu_tests::rewritten_loaded_buffer_descriptor_executes_on_gpu_and_warp, resource::tests::rewritten_loaded_buffer_descriptors_keep_base_and_reject_unsafe_edits. The rest is the same as main on this card: the 5 known NVIDIA failures from #1744 (2 view heap cap tests in victory-gpu, 3 in victory-shader), nothing new.

One run, unit tests only, title not run.

Written with AI help (Claude/Kiro); the numbers are from runs on my machine.

@RomainKH RomainKH 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.

I read the tracing change and ran a probe on the head (a276e8b) on macOS arm64, CPU tracking only (cargo test -p victory-shader --lib).

derived_from_load accepts any SCC-free rewrite of dword 3, and of dword 1 above bit 15, as the loaded descriptor. Some of those bits change the addresses a MUBUF access reaches. The access then goes through the host view made from the descriptor in memory, which still has the old bits.

Here are four rewrites that main refuses with a descriptor changed after loading and that this head returns as Source::Loaded. Each comes after the test's s_load_dwordx4 s[8:11] and before buffer_store_dwordx4 v[0:3], v7, s[8:11], 0 offen:

rewrite field (GFX10 V#) on hardware
s_or_b32 s11, s11, 0x00800000 dword 3 bit 23, ADD_TID_ENABLE the lane id is added to the index, so each lane addresses its own record
s_or_b32 s11, s11, 0x00600000 dword 3 bits 22:21, INDEX_STRIDE changes how lanes are interleaved under swizzle or TID addressing
s_or_b32 s9, s9, 0x80000000 dword 1 bit 31, SWIZZLE_ENABLE the byte offset is interleaved with the index (swizzled addressing)
s_min_u32 s10, s10, 16 dword 2, NUM_RECORDS stores past byte 16 are dropped; on the host they land

I don't know whether any of the 316 programs does this. The patterns the PR targets (stride cleared, records scaled, OOB_SELECT set) don't. Still, the first three now translate into a wrong access where main refuses by name.

A narrow fix: require dword 1 bit 31 and dword 3 bits 23:21 to come through from the load unchanged, as preserves_low16 already does for the base. For example, generalise it to a mask, with 0x8000_FFFF on dword 1 and 0x00E0_0000 on dword 3. The record count could then get a // shortcut: note saying the host view's bound is the one in memory.

The probe below passes on main and fails on this head (accepted [880bff0b, 00800000]), so it can go into resource::tests as is:

#[test]
fn rewrites_that_change_addressing_are_refused() {
    let run = |extra: &[u32]| {
        let mut w = vec![
            0xBE88_0304, // s_mov_b32 s8, s4
            0xBE89_0380, // s_mov_b32 s9, 0
            0xF408_0204,
            0xFA00_0160, // s_load_dwordx4 s[8:11], s[8:9], 0x160
            0xBF8C_C07F, // s_waitcnt lgkmcnt(0)
        ];
        w.extend_from_slice(extra);
        // buffer_store_dwordx4 v[0:3], v7, s[8:11], 0 offen
        w.extend_from_slice(&[0xE078_1000, 0x8002_0007, S_ENDPGM]);
        let code: Vec<u8> = w.iter().flat_map(|x| x.to_le_bytes()).collect();
        let p = crate::ir::build(&code, false, 256).unwrap();
        track(&p, &[param(Table, 4, 1)], HwStage::Cs, lanes(&p).as_ref())
    };
    for extra in [
        [0x880B_FF0B, 0x0080_0000], // s_or_b32 s11, s11, 0x00800000 (ADD_TID_ENABLE)
        [0x880B_FF0B, 0x0060_0000], // s_or_b32 s11, s11, 0x00600000 (INDEX_STRIDE)
        [0x8809_FF09, 0x8000_0000], // s_or_b32 s9, s9, 0x80000000 (SWIZZLE_ENABLE)
    ] {
        assert!(run(&extra).is_err(), "accepted {extra:08x?}");
    }
}

Unrelated to the above: this head merges cleanly with #2324, #2046, #2078 and #2342 together on top of main 940d32f. cargo test -p victory-shader --locked on that merge: 158 passed, 0 failed, 2 ignored (macOS, so the Windows-only GPU checks did not run).

AI-assisted (Claude Code); the results are from runs on my machine.

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks @RomainKH — great catch on the MUBUF addressing bits! Rebased onto da82e444 and updated in 4e642d2e:

  • Generalized preserves_low16 to preserves_bits(mask) / bits_zero(mask).
  • For Mubuf { idxen: false }, w1 now requires 0x8000_FFFF (SWIZZLE_ENABLE bit 31 plus base bits 15:0) and w3 requires 0x00E0_0000 (ADD_TID_ENABLE bit 23 and INDEX_STRIDE bits 22:21) unchanged from the load; Mubuf { idxen: true } keeps all of w1 unchanged plus 0x00E0_0000 on w3; Smem keeps 0x0000_FFFF on w1.
  • Added a // shortcut: note on w2 (NUM_RECORDS) and included rewrites_that_change_addressing_are_refused in resource::tests (with Co-authored-by credit).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. Tracer::rewritten_buffer in crates/victory-shader/src/resource.rs lets a loaded 4-dword non-format buffer descriptor resolve to Source::Loaded after scalar-ALU rewrites of w1 to w3. The conditions are that the base (w0, w1[15:0]) and the addressing bits (SWIZZLE_ENABLE, ADD_TID_ENABLE/INDEX_STRIDE) are provably kept (preserves_bits/bits_zero), and that every word comes only from that load's four dwords (derived_from_load). The rebase onto 67e8ae1 (d135178) has dropped the unrelated rustfmt hunks, so the diff now shows only the change and its tests. CI (check): passing.

What still blocks it is evidence for the semantics. By the PR's own // shortcut: note, a rewritten NUM_RECORDS (w2) is ignored: the host view keeps the bound of the descriptor in memory. For unindexed access, a rewritten stride is ignored too. That is safe only if the title's rewrites never shrink the range or rely on the new bound. Otherwise reads the console would clamp to zero go past the intended range on the host.

On your reply: understood that you have no shader corpus. Either of these would move it forward:


Generated by Claude Code

…arvisak#815)

Co-authored-by: Machad3x <Machad3x@users.noreply.github.com>
Co-authored-by: Romain Khanoyan <32813610+RomainKH@users.noreply.github.com>
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Rebased onto 67e8ae14 (d135178f) and stripped the unrelated rustfmt hunks from crates/victory-shader/src/resource.rs (track, phi, etc.) and crates/victory-shader/src/emit/valu_tests.rs so the diff contains only the descriptor rewrite change and its tests.

Regarding the offline translate --validate count and opcode breakdown on the 316 programs from #815: I do not have the title shader packs locally (all testing here uses synthetic shaders only), so I cannot run that corpus measurement myself — if a tester with the #815 shader set can run before/after translate --validate and share the rewrite counts/patterns, I am happy to tighten or adjust the guard accordingly.

@fuzeofficlal
fuzeofficlal force-pushed the fix/shader-rewritten-buffer-descriptors-815 branch from 4e642d2 to d135178 Compare November 25, 2026 16:51
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I agree that keeping the 48-bit base intact is not sufficient when lowering through the original loaded host view: changes to NUM_RECORDS (w2), STRIDE (w1[29:16]), or OOB_SELECT (w3[29:28]) can alter guest bounds or addressing semantics relative to the unmodified host descriptor. Since all of the positive rewrite cases in this PR rely on those fields and there is no demonstrated hint-only shader case here, I'm closing this PR and will revisit if we have a specific title rewrite with proven bounds equivalence or explicit lowered bounds checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants