fix(naga): Validate swizzles in lowering (#8949)

This commit is contained in:
Andy Leiserson authored and GitHub committed 2026-02-03 01:38:28 +00:00
1 parent a5116520b0
commit 28bc72f5bc
4 files changed
+53 -1

No files matched your search

+1
View File
@@ -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<i32>(); let r = v.xyz`. By @andyleiserson in [#8949](https://github.com/gfx-rs/wgpu/pull/8949).
#### GLES
+4
View File
@@ -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`]
+9 -1
View File
@@ -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 })
}
}
+39
View File
@@ -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<i32>();
let r : vec3<i32> = v.xyz;
}
",
r###"error: invalid field accessor `xyz`
┌─ wgsl:5:35
│
5 │ let r : vec3<i32> = v.xyz;
│ ^^^ invalid accessor
"###,
);
// 4-component swizzle from non-const vec3
check(
"
@compute @workgroup_size(1)
fn main() {
var v = vec3<i32>();
let r : vec4<i32> = v.xyzw;
}
",
r###"error: invalid field accessor `xyzw`
┌─ wgsl:5:35
│
5 │ let r : vec4<i32> = v.xyzw;
│ ^^^^ invalid accessor
"###,
);
}
#[test]
fn const_assert_must_be_const() {
check(