瀏覽代碼

shader: a lane mask that needs its own value fails instead of overflowing the stack

In a wave64 compute program, writing only EXEC's high dword (for
example s_mov_b32 exec_hi, 0) and then running any vector instruction
crashed the whole process with a stack overflow. The new mask is opaque
and keyed by the unchanged low dword, so lowering the instruction's guard
asked for that dword's value, the value is a lane mask read as data, its
ballot needs the mask's lane bit, and that bit needs the value again:
mask_dword -> boolean -> atom -> get -> mask_dword, without end.

mask_dword now notes the masks whose ballot it is building, and a mask
reached again on the way fails with a reason. The instruction then fails
like any other that cannot be expressed: its result is missing, and only
a side effect that uses it stops the translation. A shader the translator
cannot handle must not take the title down at pipeline creation.

Found with a random-input harness over the whole translation (ir,
resource, io, structure, emit) of random decodable compute programs. The
new test overflows the stack without the fix. One million further
programs (about 160,000 fully translated) gave no panic, hang or
overflow.
9hvn9yt2vk-netizen 3 天之前
父節點
當前提交
ea24ea109c
共有 2 個文件被更改,包括 48 次插入 和 1 次删除
  1. 11 1
      crates/victory-shader/src/emit/lower.rs
  2. 37 0
      crates/victory-shader/src/emit/mod.rs

+ 11 - 1
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<Value, (V, V)>,
+    /// 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<Value>,
     /// Block writing each value.
     def_block: HashMap<Value, usize>,
     /// 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

+ 37 - 0
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<u8> {