From 9e43be57c295938915ae2dd4e458e113d1d3e89c Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:25:16 +0000 Subject: [PATCH] Recompiler: widen 1-window raw MUBUF bounds checks to 64-bit to prevent wrap In mubuf, buffer_atomic, and mubuf_d16, the mem_windows == 1 path computed t_off = voff + OFFSET and checked t_off + bytes <= d2 in 32-bit uint, unlike the multi-window path which already uses uint64_t. A large/negative voff (e.g. 0xFFFFFFFC with bytes=4, or 0xFFFFFFF0 with OFFSET=16) wrapped past 2^32 to 0 <= d2 and performed an out-of-bounds load, store, or atomic instead of treating the access as out of bounds. Compute t_off and the raw bounds comparison in uint64_t for both single- and multi-window modes, with a GPU regression test covering BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, BUFFER_ATOMIC_ADD, and BUFFER_LOAD_SHORT_D16. Assisted-by: OpenCode (Gemini 4 Neon) --- crates/xcl/src/gpu/recomp/emit.rs | 31 ++++++-------------- crates/xcl/src/gpu/recomp/tests.rs | 46 +++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..121c873ea1 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -1702,8 +1702,8 @@ impl Em { let t = self.t(); let is_store = small.map_or(matches!(kind, BufKind::Store | BufKind::FormatStore), |(_, _, s)| s); self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); + self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET"))); if self.opt.mem_windows > 1 { - self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET"))); self.line(&format!( "uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_off + (uint64_t){t}_stride * (uint64_t){index};" )); @@ -1714,17 +1714,11 @@ impl Em { } self.line(&format!("uint {t}_a = (uint){t}_ea;")); } else { - self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET"))); - self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_off + {t}_stride * {index};")); + self.line(&format!("uint {t}_a = {d0} + {soff} + (uint){t}_off + {t}_stride * {index};")); } let act = self.act(); - let multi = self.opt.mem_windows > 1; let in_range = |bytes: String| { - if multi { - format!("select({t}_stride == 0u, {t}_off + (uint64_t)({bytes}) <= (uint64_t){d2}, {index} < {d2})") - } else { - format!("select({t}_stride == 0u, {t}_off + {bytes} <= {d2}, {index} < {d2})") - } + format!("select({t}_stride == 0u, {t}_off + (uint64_t)({bytes}) <= (uint64_t){d2}, {index} < {d2})") }; if let Some((n, signed, store)) = small { let ok = in_range(format!("{n}u")); @@ -3692,19 +3686,17 @@ impl Em { let op = op.trim_end_matches("_x2"); let bytes = if x2 { 8 } else { 4 }; self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); + self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET"))); if self.opt.mem_windows > 1 { - self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET"))); self.line(&format!( "uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_off + (uint64_t){t}_stride * (uint64_t){index};" )); self.window_write(&format!("(uint)({t}_ea >> 32)")); self.line(&format!("uint {t}_a = (uint){t}_ea & ~{}u;", bytes - 1)); - self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + (uint64_t){bytes}u <= (uint64_t){d2}, {index} < {d2});")); } else { - self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET"))); - self.line(&format!("uint {t}_a = ({d0} + {soff} + {t}_off + {t}_stride * {index}) & ~{}u;", bytes - 1)); - self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + {bytes}u <= {d2}, {index} < {d2});")); + self.line(&format!("uint {t}_a = ({d0} + {soff} + (uint){t}_off + {t}_stride * {index}) & ~{}u;", bytes - 1)); } + self.line(&format!("bool {t}_ok = select({t}_stride == 0u, {t}_off + (uint64_t){bytes}u <= (uint64_t){d2}, {index} < {d2});")); let act = self.act(); if x2 { return self.atomic64(op, &format!("{t}_a"), vdata, vdata + 2, f("GLC") == 1, vdata, &format!("and({act}, {t}_ok)")); @@ -3747,8 +3739,8 @@ impl Em { let voff = if offen { self.vreg(vaddr + idxen as u64) } else { "0u".into() }; let t = self.t(); self.line(&format!("uint {t}_stride = ({d1} >> 16) & 0x3FFFu;")); + self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET"))); if self.opt.mem_windows > 1 { - self.line(&format!("uint64_t {t}_off = (uint64_t){voff} + (uint64_t){}u;", f("OFFSET"))); self.line(&format!( "uint64_t {t}_ea = ((((uint64_t)({d1} & 0xFFFFu)) << 32) | (uint64_t){d0}) + (uint64_t){soff} + {t}_off + (uint64_t){t}_stride * (uint64_t){index};" )); @@ -3759,14 +3751,9 @@ impl Em { } self.line(&format!("uint {t}_a = (uint){t}_ea;")); } else { - self.line(&format!("uint {t}_off = {voff} + {}u;", f("OFFSET"))); - self.line(&format!("uint {t}_a = {d0} + {soff} + {t}_off + {t}_stride * {index};")); + self.line(&format!("uint {t}_a = {d0} + {soff} + (uint){t}_off + {t}_stride * {index};")); } - let ok = if self.opt.mem_windows > 1 { - format!("select({t}_stride == 0u, {t}_off + (uint64_t){n}u <= (uint64_t){d2}, {index} < {d2})") - } else { - format!("select({t}_stride == 0u, {t}_off + {n}u <= {d2}, {index} < {d2})") - }; + let ok = format!("select({t}_stride == 0u, {t}_off + (uint64_t){n}u <= (uint64_t){d2}, {index} < {d2})"); let act = self.act(); let v = self.vreg(vdata); if store { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d1..fc265281e9 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -391,7 +391,7 @@ fn atomic_fcmpswap_x2_is_a_bitwise_64_bit_cas() { assert!(t.hlsl.contains("_cmp = (uint64_t)v26 | ((uint64_t)v27 << 32);"), "{}", t.hlsl); // 8-byte alignment of all three cells; the buffer form's bounds check reads records in dwords assert_eq!(t.hlsl.matches("& ~7u;").count(), 3, "{}", t.hlsl); - assert!(t.hlsl.contains("_off + 8u <= s10"), "{}", t.hlsl); + assert!(t.hlsl.contains("_off + (uint64_t)8u <= (uint64_t)s10"), "{}", t.hlsl); // GLC returns the old u64 as a low / high dword pair – in place in VDATA for the buffer // form, into VDST for flat / global assert_eq!(t.hlsl.matches("_old >> 32").count(), 3, "{}", t.hlsl); @@ -7306,6 +7306,50 @@ fn gpu_sdwa_f16_inline_constants() { } } +#[test] +fn gpu_mubuf_raw_bounds_do_not_wrap_32bit() { + if !gpu_ready() { + return; + } + let p = prog(&[ + // voff = 0xFFFF_FFFC, OFFSET = 0: t_off + 4 wraps to 0 in 32-bit uint + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None), + // voff = 0xFFFF_FFF0, OFFSET = 16: voff + OFFSET wraps to 0 in 32-bit uint + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 11), ("VADDR", 1), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 16)], None), + // wrapping store must not touch mem[base - 4] + ("BUFFER_STORE_DWORD", "ENC_MUBUF", &[("VDATA", 4), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None), + // wrapping atomic must not touch mem[base - 4] + ("BUFFER_ATOMIC_ADD", "ENC_MUBUF", &[("VDATA", 5), ("VADDR", 0), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None), + // wrapping d16 load (voff = 0xFFFF_FFFE, bytes = 2) must return 0 in low 16 bits + ("BUFFER_LOAD_SHORT_D16", "ENC_MUBUF", &[("VDATA", 12), ("VADDR", 2), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None), + // control: in-bounds load at offset 0 still reads mem[base] + ("BUFFER_LOAD_DWORD", "ENC_MUBUF", &[("VDATA", 13), ("VADDR", 3), ("SRSRC", 4), ("SOFFSET", 128), ("OFFEN", 1), ("OFFSET", 0)], None), + ]); + let mut st = KernelState::default(); + // V# at s[16:19]: base = 0x100, stride = 0 (raw), num_records = 64 + st.sgpr[16..20].copy_from_slice(&[0x100, 0, 64, 0]); + for r in st.vgpr.iter_mut() { + r[0] = 0xFFFF_FFFC; + r[1] = 0xFFFF_FFF0; + r[2] = 0xFFFF_FFFE; + r[3] = 0; + r[4] = 0x1111_2222; + r[5] = 1; + r[10] = 0xAAAA_AAAA; + r[11] = 0xBBBB_BBBB; + r[12] = 0x1234_5678; + } + let mut mem = vec![0u32; 0x80]; + mem[0xFC / 4] = 0xDEAD_BEEF; + mem[0x100 / 4] = 0xCAFE_BABE; + let r = runner::run_code(&p, &st, &mem).unwrap(); + assert_eq!(r.vgpr[0][10], 0, "BUFFER_LOAD_DWORD with voff=0xFFFFFFFC must be OOB (0)"); + assert_eq!(r.vgpr[0][11], 0, "BUFFER_LOAD_DWORD with voff+OFFSET wrapping to 0 must be OOB (0)"); + assert_eq!(r.vgpr[0][12], 0x1234_0000, "BUFFER_LOAD_USHORT_D16 with voff=0xFFFFFFFE must be OOB (0 in low half)"); + assert_eq!(r.vgpr[0][13], 0xCAFE_BABE, "in-bounds control load"); + assert_eq!(r.mem[0xFC / 4], 0xDEAD_BEEF, "wrapping store/atomic must not modify mem[base - 4]"); +} + #[test] fn fspx_compute_user_sgprs_from_rsrc2() { use crate::gpu::xbox::tests::container;