diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 1f6ab71..0edc5dd 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -183,6 +183,10 @@ struct Lower<'a> { mask_ok: bool, /// Ballots of masks read as data, by low dword: (low, high). ballots: HashMap, + /// Masks whose ballot is being built: a mask whose lane bit needs its + /// own value as a number (an opaque mask keyed by a dword it shares + /// with another) would otherwise recurse until the stack overflows. + balloting: HashSet, /// Block writing each value. def_block: HashMap, /// IR block being lowered. @@ -472,6 +476,7 @@ impl<'a> Lower<'a> { }, mask_ok: false, ballots: HashMap::new(), + balloting: HashSet::new(), def_block: p .blocks .iter() @@ -1161,7 +1166,12 @@ impl<'a> Lower<'a> { Some(&b) if b == self.cur_ir => self.cur, Some(&b) => self.tail[b].ok_or("a lane mask written after its use")?, }; - let bit = self.boolean(m, at)?; + if !self.balloting.insert(lo) { + return Err("a lane mask whose lanes depend on its own value"); + } + let bit = self.boolean(m, at); + self.balloting.remove(&lo); + let bit = bit?; let i32t = self.s.t.i32; let four = self.s.m.named_struct("dx.types.fouri32", vec![i32t; 4]); let r = self diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index a283545..a922e1b 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -355,6 +355,43 @@ mod tests { use super::bitcode::Attr; use super::container::SigElement; use super::shader::Shader; + use super::*; + use crate::native::{ComputeIo, StageIo}; + + /// A wave64 compute program that writes only EXEC's high dword + /// (`s_mov_b32 exec_hi, 0`) before a vector instruction. The new mask is + /// opaque and keyed by the unchanged low dword, so building its lane bit + /// needed the mask's own value: lowering recursed until the stack + /// overflowed, which takes the whole process down (found by + /// random-input testing). Now the instruction fails like any other that + /// cannot be expressed: its result is missing, and only a side effect + /// that uses it would stop the translation. + #[test] + fn an_exec_high_write_does_not_overflow_the_stack() { + // s_mov_b32 exec_hi, 0; v_mov_b32 v1, v0; s_endpgm + let words = [0xBEFF_0380u32, 0x7E02_0300, 0xBF81_0000]; + let native = NativeProgram { + stage: HwStage::Cs, + wave64: true, + vgprs: 2, + layout: vec![], + used: vec![], + io: StageIo::Cs(ComputeIo { + rsrc2: 0, + threads: [64, 1, 1], + }), + code: words.iter().flat_map(|w| w.to_le_bytes()).collect(), + }; + let p = ir::build(&native.patched_code(), native.wave64, native.vgprs).unwrap(); + let res = resource::track(&p, &native.layout, native.stage).unwrap(); + let io = io::raise(&p, &native).unwrap(); + let st = structure::structure(&p).unwrap(); + let container = crate::dxbc::build(&[]); + let c = Container::parse(&container).unwrap(); + if let Err(e) = emit(&c, &native, &p, &st, &io, &res) { + assert!(e.to_string().contains("its own value"), "{e}"); + } + } /// A pixel shader writing a constant colour. fn constant_color() -> Vec {