From 28bc72f5bcf1d2b3625b01035098ef59360194bd Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Mon, 2 Feb 2026 17:38:28 -0800 Subject: [PATCH] fix(naga): Validate swizzles in lowering (#8949) --- CHANGELOG.md | 1 + naga/src/back/pipeline_constants.rs | 4 +++ naga/src/front/wgsl/lower/mod.rs | 10 +++++++- naga/tests/naga/wgsl_errors.rs | 39 +++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ed59bed4..0dec13c8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,7 @@ Bottom level categories: - Fixed validation of the texture format in GPUDepthStencilState when neither depth nor stencil is actually enabled. By @andyleiserson in [#8766](https://github.com/gfx-rs/wgpu/pull/8766). - Check that depth bias is not used with non-triangle topologies. By @andyleiserson in [#8856](https://github.com/gfx-rs/wgpu/pull/8856). - Check that if the shader outputs `frag_depth`, then the pipeline must have a depth attachment. By @andyleiserson in [#8856](https://github.com/gfx-rs/wgpu/pull/8856). +- Fix incorrect acceptance of some swizzle selectors that are not valid for their operand, e.g. `const v = vec2(); let r = v.xyz`. By @andyleiserson in [#8949](https://github.com/gfx-rs/wgpu/pull/8949). #### GLES diff --git a/naga/src/back/pipeline_constants.rs b/naga/src/back/pipeline_constants.rs index 79eaeeb32..b66d4d28f 100644 --- a/naga/src/back/pipeline_constants.rs +++ b/naga/src/back/pipeline_constants.rs @@ -46,6 +46,10 @@ pub enum PipelineConstantError { /// Compact `module` and replace all overrides with constants. /// +/// `module` must be valid. Both compaction and constant evaluation may produce +/// invalid results (e.g. replace an invalid expression with a constant) for +/// invalid modules. +/// /// If no changes are needed, this just returns `Cow::Borrowed` references to /// `module` and `module_info`. Otherwise, it clones `module`, retains only the /// selected entry point, compacts the module, edits its [`global_expressions`] diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index ee0bd08a1..58a106dd4 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -2490,9 +2490,14 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { lowered_base.map(|base| ir::Expression::AccessIndex { base, index }) } - ir::TypeInner::Vector { .. } => { + ir::TypeInner::Vector { size: vec_size, .. } => { match Components::new(field.name, field.span)? { Components::Swizzle { size, pattern } => { + for &component in pattern[..size as usize].iter() { + if component as u8 >= vec_size as u8 { + return Err(Box::new(Error::BadAccessor(field.span))); + } + } Typed::Plain(ir::Expression::Swizzle { size, vector: ctx.apply_load_rule(lowered_base)?, @@ -2500,6 +2505,9 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { }) } Components::Single(index) => { + if index >= vec_size as u32 { + return Err(Box::new(Error::BadAccessor(field.span))); + } lowered_base.map(|base| ir::Expression::AccessIndex { base, index }) } } diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 3a9fb18d4..f9d6047c4 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -3499,6 +3499,45 @@ fn only_one_swizzle_type() { ); } +#[test] +fn swizzle_oob() { + // 3-component swizzle from const vec2 + check( + " + @compute @workgroup_size(1) + fn main() { + const v = vec2(); + let r : vec3 = v.xyz; + } + ", + r###"error: invalid field accessor `xyz` + ┌─ wgsl:5:35 + │ +5 │ let r : vec3 = v.xyz; + │ ^^^ invalid accessor + +"###, + ); + + // 4-component swizzle from non-const vec3 + check( + " + @compute @workgroup_size(1) + fn main() { + var v = vec3(); + let r : vec4 = v.xyzw; + } + ", + r###"error: invalid field accessor `xyzw` + ┌─ wgsl:5:35 + │ +5 │ let r : vec4 = v.xyzw; + │ ^^^^ invalid accessor + +"###, + ); +} + #[test] fn const_assert_must_be_const() { check(