mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-30 01:54:31 +01:00
[naga wgsl-in] Reject vector constructors with incorrect number of components during const evaluation.
A vector constructor with multiple arguments is represented in IR as a Compose expression. These can be nested, for example due to code such as this: `vec4(1, vec3(2, vec2(3, 4)))`. The total number of components of each argument must match the vector size, meaning `vec4(1, 2, 3, 4, 5)` is invalid. The validation pass already catches such occurences. However, validation runs after constant evaluation, meaning this can cause issues if the expression is evaluated as part of a const expression. When applying an operation to a vector compose during const evaluation, we typically "flatten" the vector using the function `proc::flatten_compose()`. This silently truncates the list of components to the size implied by the type, meaning invalid code is accepted. This patch validates that the total number of components in a Compose expression, once flattened, matches the size implied by the type. We do so once as each expression is registered to avoid needing to handle this each time the expression is used in a const expression. For nested compose expressions, the inner expressions will be registed before the outer ones. This means we can trust that the size implied by the inner expression's type is correct, as it will have already been validated, and can therefore avoid recursing through each nested expression when registering the outer expression.
This commit is contained in:
1 parent
4687973c9a
commit
187e1a6967
2 files changed
+96
-3
No files matched your search
@@ -565,6 +565,10 @@ pub enum ConstantEvaluatorError {
|
||||
InvalidRelationalArg(RelationalFunction),
|
||||
#[error("value of `low` is greater than `high` for clamp built-in function")]
|
||||
InvalidClamp,
|
||||
#[error("Constructor expects {expected} components, found {actual}")]
|
||||
InvalidVectorComposeLength { expected: usize, actual: usize },
|
||||
#[error("Constructor must only contain vector or scalar arguments")]
|
||||
InvalidVectorComposeComponent,
|
||||
#[error("Splat is defined only on scalar values")]
|
||||
SplatScalarOnly,
|
||||
#[error("Can only swizzle vector constants")]
|
||||
@@ -2244,18 +2248,55 @@ impl<'a> ConstantEvaluator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the total number of components, after flattening, of a vector compose expression.
|
||||
fn vector_compose_flattened_size(
|
||||
&self,
|
||||
components: &[Handle<Expression>],
|
||||
) -> Result<usize, ConstantEvaluatorError> {
|
||||
components
|
||||
.iter()
|
||||
.try_fold(0, |acc, c| -> Result<_, ConstantEvaluatorError> {
|
||||
let size = match *self.resolve_type(*c)?.inner_with(self.types) {
|
||||
TypeInner::Scalar(_) => 1,
|
||||
// We trust that the vector size of `component` is correct,
|
||||
// as it will have already been validated when `component`
|
||||
// was registered.
|
||||
TypeInner::Vector { size, .. } => size as usize,
|
||||
_ => return Err(ConstantEvaluatorError::InvalidVectorComposeComponent),
|
||||
};
|
||||
Ok(acc + size)
|
||||
})
|
||||
}
|
||||
|
||||
fn register_evaluated_expr(
|
||||
&mut self,
|
||||
expr: Expression,
|
||||
span: Span,
|
||||
) -> Result<Handle<Expression>, ConstantEvaluatorError> {
|
||||
// It suffices to only check literals, since we only register one
|
||||
// expression at a time, `Compose` expressions can only refer to other
|
||||
// expressions, and `ZeroValue` expressions are always okay.
|
||||
// It suffices to only check_literal_value() for `Literal` expressions,
|
||||
// since we only register one expression at a time, `Compose`
|
||||
// expressions can only refer to other expressions, and `ZeroValue`
|
||||
// expressions are always okay.
|
||||
if let Expression::Literal(literal) = expr {
|
||||
crate::valid::check_literal_value(literal)?;
|
||||
}
|
||||
|
||||
// Ensure vector composes contain the correct number of components. We
|
||||
// do so here when each compose is registered to avoid having to deal
|
||||
// with the mess each time the compose is used in another expression.
|
||||
if let Expression::Compose { ty, ref components } = expr {
|
||||
if let TypeInner::Vector { size, scalar: _ } = self.types[ty].inner {
|
||||
let expected = size as usize;
|
||||
let actual = self.vector_compose_flattened_size(components)?;
|
||||
if expected != actual {
|
||||
return Err(ConstantEvaluatorError::InvalidVectorComposeLength {
|
||||
expected,
|
||||
actual,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self.append_expr(expr, span, ExpressionKind::Const))
|
||||
}
|
||||
|
||||
|
||||
@@ -253,6 +253,58 @@ fn constructor_parameter_type_mismatch() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vector_constructor_incorrect_component_count() {
|
||||
// Too few components
|
||||
check(
|
||||
r#"
|
||||
fn x() {
|
||||
_ = vec4(1, 2, 3);
|
||||
}
|
||||
"#,
|
||||
r#"error: Constructor expects 4 components, found 3
|
||||
┌─ wgsl:3:21
|
||||
│
|
||||
3 │ _ = vec4(1, 2, 3);
|
||||
│ ^^^^^^^^^^^^^ see msg
|
||||
|
||||
"#,
|
||||
);
|
||||
|
||||
// Too many components
|
||||
check(
|
||||
r#"
|
||||
fn x() {
|
||||
_ = vec4(1, 2, 3, 4, 5);
|
||||
}
|
||||
"#,
|
||||
r#"error: Constructor expects 4 components, found 5
|
||||
┌─ wgsl:3:21
|
||||
│
|
||||
3 │ _ = vec4(1, 2, 3, 4, 5);
|
||||
│ ^^^^^^^^^^^^^^^^^^^ see msg
|
||||
|
||||
"#,
|
||||
);
|
||||
|
||||
// The outer constructor has the correct number of components, but only
|
||||
// because the inner constructor has too many.
|
||||
check(
|
||||
r#"
|
||||
fn x() {
|
||||
_ = vec4(1, vec2(2, 3, 4));
|
||||
}
|
||||
"#,
|
||||
r#"error: Constructor expects 2 components, found 3
|
||||
┌─ wgsl:3:29
|
||||
│
|
||||
3 │ _ = vec4(1, vec2(2, 3, 4));
|
||||
│ ^^^^^^^^^^^^^ see msg
|
||||
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_texture_sample_type() {
|
||||
check(
|
||||
|
||||
Reference in new issue
Block a user