gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.13k stars 884 forks source link

WGSL attempt to multiply with overflow #4339

Open kkristof opened 3 years ago

kkristof commented 3 years ago

Hi, I am still working on my fuzz testing using fuzzinator framework and the grammarinator test generator mentioned in gfx-rs/naga#631. I'm not sure which component is failing on the following wgsl test case and if it is considered to be an error or not :)

The following test case is failing. I know the generated test is far from following the standard (I'm trying to improve it).

type F_ = array < mat3x4 < f32  >  , 292814780 >   ;
  ;
  struct QZ9 { n_ : mat2x2 < f32  >   ;
  }   ;
  [[ override ( 0x88c  )  , override ( 239  )  ]]  let W : u32    ;
  struct F3gk2 { }   ;
  [[ override ( 193470765  )  , override ( 0xC0D  )  ]]  let _ : mat4x4 < f32  >    ;
  [[ block  , block  ]]  struct __ { u : [[ access ( f  )  , access ( K  )  ]]  vec4 < bool  >   ;
  [[ stride ( 0xD  )  , stride ( 78  )  ]]  _ : [[ access ( QKh  )  , access ( d_  )  ]]  mat4x3 < f32  >   ;
  }   ;
  type _ = f32   ;
  type y96p = mat4x3 < f32  >   ;
  var qB : ptr < private  , u32  >     ;

Result:

$ cargo run --features wgsl-in,spv-out -- /tmp/60a7012eb869c37454eb1c5c.wgsl test.spv
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/naga /tmp/60a7012eb869c37454eb1c5c.wgsl test.spv`
thread 'main' panicked at 'attempt to multiply with overflow', src/proc/mod.rs:105:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
kvark commented 3 years ago

This is great! We'd love to run this on our CI.

hcsch commented 3 years ago

Minimal reproducer seems to be

type T = array<u32, 1073741824>; // 4 bytes * ⌈(2³² - 1) / 4.0⌉ count

The error occurs at the multiplication at the end of this match arm: https://github.com/gfx-rs/naga/blob/35f27cfe7ee09588add02e3c8ce9e3b391953711/src/proc/mod.rs#L114-L128

This should probably use a checked_mul and return a Result.

teoxoy commented 2 years ago

Related spec issue: https://github.com/gpuweb/gpuweb/issues/2118