d3d12: guard biased raw twins against sub-dword offsets and 32-bit wrap (D3D12-TWIN-BIAS-RDNA3) #2757
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2757"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-30T15:52:48Z.
Summary
Fixes #2702 (
D3D12-TWIN-BIAS-RDNA3).Commit
1bb188cf5(LEONIDA_TWIN_BIAS, active on AMD adapters whereraw_views()allocates raw twins) kept raw twins starting at the 16-byte boundary before an unaligned buffer view and recorded the byte deltaby(1..15) in bits24..27(VIEW_BIAS_MASK) ofstrides[slot]for translated shaders to add to raw byte offsets. On RDNA 3 (RX 7900 XT), that exposed three bugs that vanish underLEONIDA_TWIN_BIAS=0:Sub-dword
by % 4 != 0twins accepted bysettle_twin(d3d12/crates/leonida-gpu/src/descriptor/host.rs):shader.rs(VIEW_BIAS_MASKdoc) andlower.rs(rawBufferLoad/rawBufferStorewithalignment = 4, buffer atomics,cbuffer_dword,format_load, andnarrowwithin = byte & 3) assumebiasis a multiple of 4 (0, 4, 8, 12).twin_windowproducesbyin1..15for sub-dword typed views (R16_*at oddFirstElementgivesby = 2, 6, 10, 14;R8_*gives oddby), andsettle_twinonly checkedby < 16 && by < bytes, neverby.is_multiple_of(4). On GFX11/RDNA 3, misaligned byte offsets fed to 4-byte-aligned raw loads/stores/atomics truncate& !3or misalign, splicing adjacent half/snorm/index values.settle_twinnow requiresby.is_multiple_of(4), keeping the primary typed view intact while nulling sub-dword raw twins just asLEONIDA_TWIN_BIAS=0does.32-bit unsigned wrap-around on
offset + bias(d3d12/crates/leonida-shader/src/emit/lower.rs):u32::MAX * stride + field(0xFFFF_FFF4..0xFFFF_FFFCforstride = 4, 8, 12, 24, 48, ...) orx - 4atx = 0(0xFFFF_FFFC).bias == 0, these offsets remain near4 GiBout of bounds (0on D3D12 raw loads, dropped on stores/atomics). Addingbiasin{4, 8, 12}wrapped modulo2^32into[0, 12)— in bounds of the biased twin's 16-byte pre-view prefix, which holds bytes belonging to the preceding suballocation in the ring/heap. Vertex/geometry loads at sentinel-1read arbitrary preceding bytes as positions (stretching triangles across clip space and spiking rasterizer cost), while UAV stores/atomics at sentinel offsets overwrote preceding buffer allocations.Lower::add_biasnow clampsoffset > 0xFFFF_FFD0to out-of-bounds0xFFFF_FFE0(oob + 16 = 0xFFFF_FFF0, non-wrapping across 16-bytedwordx4accesses and constant-folded away byopt.rson literal offsets), andLower::narrowguardsaligned == 0xFFFF_FFFCbefore readingaligned + 4.Stride-word hygiene and ordering (
d3d12/crates/leonida-gpu/src/descriptor/host.rs):HostHeap::constant_raw_at(LEONIDA_PORT_CBV) now clears staleVIEW_BIAS_MASKbits instrides[index]when recycling a dynamic slot.HostHeap::{make_at, mirror, make_listed}publishstrides[index]before exposing twin descriptors (andmake_listedrefreshesexisting.stride_wordonstale_at), andtwin_windowboundsend - fromtou32soNUM_RECORDSbyte counts cannot wrap.Verification
Tested on Windows 11 (
10.0.26200), NVIDIA GeForce RTX 3090 (driver610.88), and D3D12 WARP withLEONIDA_D3D12_DEBUG=1and Windows SDKdxil.dllvalidation:leonida-shader:emit::twin_bias_tests::sentinel_and_wrapped_offsets_stay_out_of_bounds_on_biased_twins(includingbuffer_load_dword,buffer_load_dwordx4,buffer_load_ushortatu32::MAX,buffer_store_dword, andbuffer_atomic_addagainst non-zero pre-view canary bytes) andemit::twin_bias_tests::resource_heap_structured_twins_validate_and_guard_wrapped_elements.leonida-gpu:descriptor::host::listed_raw_tests::sub_dword_views_null_twins_and_recycled_raw_cbv_clears_bias(default andLEONIDA_TWIN_BIAS=0negative control) andlisted_off_alignment_raw_twin_readback.cargo test -p leonida-shader(387passed,0failed)cargo test -p leonida-gpu --lib descriptor::andport_cbv::(all passed)cargo clippy -p leonida-shader -p leonida-gpu --tests(0warnings in touched files)python -I scripts/check_no_game_data.py(rc=0)AI-assisted: authored with Gemini 4 Carbon (High) and independently reviewed with Gemini 4 Neon (XHigh).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T15:56:19Z.
Verified by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup),
main3e74b0d7dmerged with headfe7c76d3b. A partial check: the new tests cannot run here.cargo test -p leonida-shaderon macOS: 228 passed, 0 failed, 2 ignored (the same count asmain: the change toemit/lower.rsbreaks none of the tests that build on macOS).emit/twin_bias_tests.rsanddescriptor/host/listed_raw_tests.rsare under#[cfg(all(test, windows))]modules, so they are not run here.cargo check --target x86_64-pc-windows-msvcfrom macOS, C build scripts given a stand-in compiler, nothing linked):-p leonida-shader --tests,-p leonida-gpu --tests --features diagnosticsand-p leonida-gpu: no errors. The two new test files compile for Windows.AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T16:35:06Z.
Independent check on Windows (not the author), at
fe7c76d3b.cargo test --locked -p leonida-gpu -p leonida-shader --lib(shared warm target): 958 tests, 0 failed —leonida-gpu571 passed / 52 ignored (13.8 s),leonida-shader387 passed / 4 ignored (267.8 s).Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned.
No regressions in either crate. I did not run the title, so the biased-twin picture behaviour itself needs a game run.
AI-assisted: yes (opencode).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:24:34Z.
RUN · VERIFY-2757-RTX4060 · by MoonEyes (another person than the author) on head
fe7c76d3b, together with #2684 (bb68c6e19), #2685 (903e3731e) and #2757 (fe7c76d3b) merged intomain99167bcaf. Windows 11 Pro 26300, RTX 4060 8 GB,-SeriesS -FrameGen 2, diag build.cargo test -p leonida-shader --locked: 392 passed, 0 failed.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:28:23Z.
Merging. Head
fe7c76d3bmerges cleanly withmain4da779252and does not force a rebase of #2566. Two runs in the game (RTX 4060, the second 425 s steady, no device loss) with #2684, #2685 and #2757 together; cargo test 392 passed; checked by other people too. The shaders are translated again at the first start. Merged by MoonEyes.AI-assisted (Claude Code).