Direct DXIL: 32-bit unsigned wraparound in raw-buffer bounds checks bypasses OOB clamping into adjacent UMA windows #1893

Open
opened 2026-11-26 05:08:37 +00:00 by github-bot · 1 comment
Owner

@⁠microwave805 on GitHub · 2026-11-24 22:37 UTC · original

Static audit on main @ 689f2db found 32-bit unsigned integer wraparound in Direct DXIL's raw-buffer bounds checks (s_buffer_load_*, mubuf, and buffer_atomic_*), which lets out-of-bounds accesses pass the check and — combined with 64-bit buffer_window() address arithmetic — hit adjacent 4 GiB UMA windows instead of returning 0 / dropping writes.

Where

  • crates/xcl/src/gpu/dxil/lower_mem.rs:1054-1082 (smem / s_buffer_load_*)
  • crates/xcl/src/gpu/dxil/lower_mem.rs:1156-1170 (mubuf raw-buffer in_range)
  • crates/xcl/src/gpu/dxil/lower_mem.rs:1699-1710 (buffer_atomic)

Root cause

  1. mubuf (lower_mem.rs:1156-1169) and buffer_atomic (lower_mem.rs:1699-1710):

    let off = self.addc(voff, f("OFFSET") as u32);
    ...
    let e = lw.add(off, bytes);
    let ok = lw.cmp(Pred::ULe, e, d2);
    

    Both off = voff + OFFSET and e = off + bytes wrap modulo 2^{32}.

    • For example, with stride == 0, d2 = 0x100, voff = 0xFFFF_FFFC, OFFSET = 0, bytes = 4:
      • off = 0xFFFF_FFFC
      • e = off.wrapping_add(4) = 0 <= d2 → ok == true!
    • Or with voff = 0xFFFF_FFF0, OFFSET = 0x20, bytes = 4:
      • off = 0x10, e = 0x14 <= d2 → ok == true!
    • Meanwhile, buffer_window(d0, d1, &[Ea::U(soff), Ea::U(voff), Ea::K(OFFSET), ...], ...) adds the components in 64-bit unsigned arithmetic, carrying +1 into the upper 32 bits and selecting the next 4 GiB UMA window, so an OOB buffer store or atomic writes into an adjacent UMA window instead of being masked off!
    • Note: mubuf also does not check off + soff when soff is used (RDNA2 hardware OOB compares the unsigned byte offset voff + OFFSET + soff against num_records when stride == 0; soff is omitted from in_range here too).
  2. s_buffer_load_* (lower_mem.rs:1054-1082):

    let off = self.add(k, soff);
    ...
    let lim = self.mul(d2, st);
    ...
    let o = self.addc(off, 4 * i);
    let ok = self.cmp(Pred::ULt, o, lim);
    
    • off = imm + soff and o = off + 4 * i both wrap in 32 bits before cmp(Pred::ULt, o, lim) (while buffer_window computes the window in 64 bits).
    • When st != 0, lim = d2 * st also wraps in 32 bits if d2 * st >= 2^32.

Fix direction

Compute raw-buffer offset checks without 32-bit wraparound — e.g. check overflow/carry on voff + OFFSET (and soff where applicable) and off <= d2 && d2 - off >= bytes (or compare in i64 / u64 when already widened, or carry-check in i32 to avoid requiring Int64Ops needlessly: sum >= a for additions and off < d2 && bytes <= d2 - off). Add unit/GPU tests with voff = 0xFFFF_FFFC and voff = 0xFFFF_FFF0, OFFSET = 0x20 verifying OOB reads return 0 and OOB stores/atomics are dropped.

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ 689f2db and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-24 22:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1893) Static audit on `main @ 689f2db` found 32-bit unsigned integer wraparound in Direct DXIL's raw-buffer bounds checks (`s_buffer_load_*`, `mubuf`, and `buffer_atomic_*`), which lets out-of-bounds accesses pass the check and — combined with 64-bit `buffer_window()` address arithmetic — hit adjacent 4 GiB UMA windows instead of returning `0` / dropping writes. ### Where - `crates/xcl/src/gpu/dxil/lower_mem.rs:1054-1082` (`smem` / `s_buffer_load_*`) - `crates/xcl/src/gpu/dxil/lower_mem.rs:1156-1170` (`mubuf` raw-buffer `in_range`) - `crates/xcl/src/gpu/dxil/lower_mem.rs:1699-1710` (`buffer_atomic`) ### Root cause 1. **`mubuf` (`lower_mem.rs:1156-1169`) and `buffer_atomic` (`lower_mem.rs:1699-1710`):** ```rust let off = self.addc(voff, f("OFFSET") as u32); ... let e = lw.add(off, bytes); let ok = lw.cmp(Pred::ULe, e, d2); ``` Both `off = voff + OFFSET` and `e = off + bytes` wrap modulo $2^{32}$. - For example, with `stride == 0`, `d2 = 0x100`, `voff = 0xFFFF_FFFC`, `OFFSET = 0`, `bytes = 4`: - `off = 0xFFFF_FFFC` - `e = off.wrapping_add(4) = 0 <= d2` → `ok == true`! - Or with `voff = 0xFFFF_FFF0`, `OFFSET = 0x20`, `bytes = 4`: - `off = 0x10`, `e = 0x14 <= d2` → `ok == true`! - Meanwhile, `buffer_window(d0, d1, &[Ea::U(soff), Ea::U(voff), Ea::K(OFFSET), ...], ...)` adds the components in **64-bit** unsigned arithmetic, carrying `+1` into the upper 32 bits and selecting the **next 4 GiB UMA window**, so an OOB buffer store or atomic writes into an adjacent UMA window instead of being masked off! - Note: `mubuf` also does not check `off + soff` when `soff` is used (RDNA2 hardware OOB compares the unsigned byte offset `voff + OFFSET + soff` against `num_records` when `stride == 0`; `soff` is omitted from `in_range` here too). 2. **`s_buffer_load_*` (`lower_mem.rs:1054-1082`):** ```rust let off = self.add(k, soff); ... let lim = self.mul(d2, st); ... let o = self.addc(off, 4 * i); let ok = self.cmp(Pred::ULt, o, lim); ``` - `off = imm + soff` and `o = off + 4 * i` both wrap in 32 bits before `cmp(Pred::ULt, o, lim)` (while `buffer_window` computes the window in 64 bits). - When `st != 0`, `lim = d2 * st` also wraps in 32 bits if `d2 * st >= 2^32`. ### Fix direction Compute raw-buffer offset checks without 32-bit wraparound — e.g. check overflow/carry on `voff + OFFSET` (and `soff` where applicable) and `off <= d2 && d2 - off >= bytes` (or compare in `i64` / `u64` when already widened, or carry-check in `i32` to avoid requiring `Int64Ops` needlessly: `sum >= a` for additions and `off < d2 && bytes <= d2 - off`). Add unit/GPU tests with `voff = 0xFFFF_FFFC` and `voff = 0xFFFF_FFF0, OFFSET = 0x20` verifying OOB reads return `0` and OOB stores/atomics are dropped. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ 689f2db` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:37 +00:00
Author
Owner

@⁠LucasHoepfner on GitHub · 2026-11-24 23:53 UTC · original

Confirmed on a GPU with two memory windows, where the wrapped access lands 4 GiB above the buffer. The HLSL path gets every case right, so this is a difference between the two translators. #1933 fixes the MUBUF and atomic cases here; the s_buffer_load case it leaves out is one line (below).

What happens on main 20cf130. One kernel-mode program, a raw V# (stride 0) of 0x100 bytes at window 0 + 0x1000, markers at the same low addresses in window 1:

access HLSL path direct DXIL
buffer_load_dword, voff 0xFFFF_FFFC 0 the dword at window 1 + 0xFFC
buffer_load_dword, voff 0xFFFF_FFF0, OFFSET 0x20 0 the dword at window 1 + 0x1010
buffer_store_dword, voff 0xFFFF_FFF0, OFFSET 0x40 dropped written at window 1 + 0x1030
buffer_atomic_add (GLC), voff 0xFFFF_FFF0, OFFSET 0x60 dropped, returns 0 64 adds at window 1 + 0x1050, returns the old value
s_buffer_load_dword, OFFSET 0x20, soff 0xFFFF_FFF0 buffer + 0x10 the dword at window 1 + 0x1010

The same with the windows as a switch and from the heap. The range check passes on the wrapped 32-bit offset, the low address wraps, and buffer_window takes the window from the 64-bit sum. The HLSL path has the MUBUF offset and its end in 64 bits with several windows (emit.rs:1706, 1724, since #792).

#1933 (@⁠PracticalStudios, head bb5e0b7): with its lower_mem.rs on 20cf130, the first four rows come out as on the HLSL path, in both window forms. Its own test runs with one window, so this adds the two-window case.

What is left: s_buffer_load. The HLSL path uses the 32-bit OFFSET + soff for the check, the address and the window (emit.rs:1498–1504). The direct path takes the window from the two added in 64 bits, so a sum that wraps reads the right low address in the next window. This makes it agree:

--- a/crates/xcl/src/gpu/dxil/lower_mem.rs
+++ b/crates/xcl/src/gpu/dxil/lower_mem.rs
@@ -1054,5 +1054,7 @@ impl Lw {
             let k = self.c(imm);
             let off = self.add(k, soff);
-            self.buffer_window(d0, d1, &[Ea::U(k), Ea::U(soff)], false);
+            // the window of the 32-bit offset the range check and the address below use, as in
+            // the HLSL kernel (not of `k + soff` in 64 bits, which can be the next window's)
+            self.buffer_window(d0, d1, &[Ea::U(off)], false);
             let st = self.lshrc(d1, 16);
             let st = self.andc(st, 0x3FFF);

I have it with a GPU test (s_buffer_load_dword and dwordx4 through both translators and both window forms; fails on main for the direct path, passes with the line). @⁠PracticalStudios, take the line into #1933 if you like, so one smoke pair covers the issue, and I'll send you the test. Otherwise I'll open it as its own small PR; I have no retail title, so it would need someone's smoke run.

Not confirmed, and left alone

  • The issue's two other remarks (soff is not part of the MUBUF check; num_records * stride can wrap): the HLSL path does the same, and I don't know what the console does there.
  • Whether a retail title ever has such an offset. This is from synthetic programs.

Tested

  • NVIDIA GeForce RTX 3060 Ti 8 GB (32-lane waves, split wave64), driver 621.01; Intel Core i7-14700KF, 32 GB; Windows 11 Pro 26200; rustc 1.99.1 MSVC; DXC 1.8.2502.11; debug. No retail title, AMD or native wave64.

AI use: Claude Code (Claude Fable 5.2) ran the experiments and wrote the fix, the test and this comment.

**@⁠LucasHoepfner** on GitHub · 2026-11-24 23:53 UTC · [original](https://github.com/xcl-dev/xcl/issues/1893#issuecomment-7147909991) Confirmed on a GPU with two memory windows, where the wrapped access lands 4 GiB above the buffer. The HLSL path gets every case right, so this is a difference between the two translators. #1933 fixes the MUBUF and atomic cases here; the `s_buffer_load` case it leaves out is one line (below). **What happens** on `main` `20cf130`. One kernel-mode program, a raw V# (stride 0) of 0x100 bytes at window 0 + 0x1000, markers at the same low addresses in window 1: | access | HLSL path | direct DXIL | |---|---|---| | `buffer_load_dword`, voff `0xFFFF_FFFC` | 0 | the dword at window 1 + 0xFFC | | `buffer_load_dword`, voff `0xFFFF_FFF0`, OFFSET 0x20 | 0 | the dword at window 1 + 0x1010 | | `buffer_store_dword`, voff `0xFFFF_FFF0`, OFFSET 0x40 | dropped | written at window 1 + 0x1030 | | `buffer_atomic_add` (GLC), voff `0xFFFF_FFF0`, OFFSET 0x60 | dropped, returns 0 | 64 adds at window 1 + 0x1050, returns the old value | | `s_buffer_load_dword`, OFFSET 0x20, soff `0xFFFF_FFF0` | buffer + 0x10 | the dword at window 1 + 0x1010 | The same with the windows as a switch and from the heap. The range check passes on the wrapped 32-bit offset, the low address wraps, and `buffer_window` takes the window from the 64-bit sum. The HLSL path has the MUBUF offset and its end in 64 bits with several windows (`emit.rs:1706`, `1724`, since #792). **#1933** (@⁠PracticalStudios, head `bb5e0b7`): with its `lower_mem.rs` on `20cf130`, the first four rows come out as on the HLSL path, in both window forms. Its own test runs with one window, so this adds the two-window case. **What is left: `s_buffer_load`.** The HLSL path uses the 32-bit `OFFSET + soff` for the check, the address and the window (`emit.rs:1498`–`1504`). The direct path takes the window from the two added in 64 bits, so a sum that wraps reads the right low address in the next window. This makes it agree: ```diff --- a/crates/xcl/src/gpu/dxil/lower_mem.rs +++ b/crates/xcl/src/gpu/dxil/lower_mem.rs @@ -1054,5 +1054,7 @@ impl Lw { let k = self.c(imm); let off = self.add(k, soff); - self.buffer_window(d0, d1, &[Ea::U(k), Ea::U(soff)], false); + // the window of the 32-bit offset the range check and the address below use, as in + // the HLSL kernel (not of `k + soff` in 64 bits, which can be the next window's) + self.buffer_window(d0, d1, &[Ea::U(off)], false); let st = self.lshrc(d1, 16); let st = self.andc(st, 0x3FFF); ``` I have it with a GPU test (`s_buffer_load_dword` and `dwordx4` through both translators and both window forms; fails on `main` for the direct path, passes with the line). @⁠PracticalStudios, take the line into #1933 if you like, so one smoke pair covers the issue, and I'll send you the test. Otherwise I'll open it as its own small PR; I have no retail title, so it would need someone's smoke run. **Not confirmed, and left alone** - The issue's two other remarks (`soff` is not part of the MUBUF check; `num_records * stride` can wrap): the HLSL path does the same, and I don't know what the console does there. - Whether a retail title ever has such an offset. This is from synthetic programs. **Tested** - NVIDIA GeForce RTX 3060 Ti 8 GB (32-lane waves, split wave64), driver 621.01; Intel Core i7-14700KF, 32 GB; Windows 11 Pro 26200; rustc 1.99.1 MSVC; DXC 1.8.2502.11; debug. No retail title, AMD or native wave64. AI use: Claude Code (Claude Fable 5.2) ran the experiments and wrote the fix, the test and this comment.
This discussion has been locked. Commenting is limited to contributors.
No description provided.