Skip to content

Commit 47d20d9

Browse files
authored
Fixes crash when there's a missing texture argument (#6486)
1 parent 9b47b06 commit 47d20d9

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ Bottom level categories:
6464

6565
### Bug Fixes
6666

67+
#### Naga
68+
69+
- Fix crash when a texture argument is missing. By @aedm in [#6486](https://github.com/gfx-rs/wgpu/pull/6486)
70+
6771
#### General
6872

6973
- Ensure that `Features::TIMESTAMP_QUERY` is set when using timestamp writes in render and compute passes. By @ErichDonGubler in [#6497](https://github.com/gfx-rs/wgpu/pull/6497).

naga/src/valid/analyzer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,10 @@ impl FunctionInfo {
421421
let image_storage = match sampling.image {
422422
GlobalOrArgument::Global(var) => GlobalOrArgument::Global(var),
423423
GlobalOrArgument::Argument(i) => {
424-
let handle = arguments[i as usize];
424+
let Some(handle) = arguments.get(i as usize).cloned() else {
425+
// Argument count mismatch, will be reported later by validate_call
426+
break;
427+
};
425428
GlobalOrArgument::from_expression(expression_arena, handle).map_err(
426429
|source| {
427430
FunctionError::Expression { handle, source }
@@ -434,7 +437,10 @@ impl FunctionInfo {
434437
let sampler_storage = match sampling.sampler {
435438
GlobalOrArgument::Global(var) => GlobalOrArgument::Global(var),
436439
GlobalOrArgument::Argument(i) => {
437-
let handle = arguments[i as usize];
440+
let Some(handle) = arguments.get(i as usize).cloned() else {
441+
// Argument count mismatch, will be reported later by validate_call
442+
break;
443+
};
438444
GlobalOrArgument::from_expression(expression_arena, handle).map_err(
439445
|source| {
440446
FunctionError::Expression { handle, source }

naga/tests/validation.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,40 @@ fn binding_arrays_cannot_hold_scalars() {
606606

607607
assert!(t.validator.validate(&t.module).is_err());
608608
}
609+
610+
#[cfg(feature = "wgsl-in")]
611+
#[test]
612+
fn validation_error_messages() {
613+
let cases = [(
614+
r#"@group(0) @binding(0) var my_sampler: sampler;
615+
616+
fn foo(tex: texture_2d<f32>) -> vec4<f32> {
617+
return textureSampleLevel(tex, my_sampler, vec2f(0, 0), 0.0);
618+
}
619+
620+
fn main() {
621+
foo();
622+
}
623+
"#,
624+
"\
625+
error: Function [1] 'main' is invalid
626+
┌─ wgsl:7:17
627+
\n7 │ ╭ fn main() {
628+
8 │ │ foo();
629+
│ │ ^^^^ invalid function call
630+
│ ╰──────────────────────────^ naga::Function [1]
631+
\n = Call to [0] is invalid
632+
= Requires 1 arguments, but 0 are provided
633+
634+
",
635+
)];
636+
637+
for (source, expected_err) in cases {
638+
let module = naga::front::wgsl::parse_str(source).unwrap();
639+
let err = valid::Validator::new(Default::default(), valid::Capabilities::all())
640+
.validate_no_overrides(&module)
641+
.expect_err("module should be invalid");
642+
println!("{}", err.emit_to_string(source));
643+
assert_eq!(err.emit_to_string(source), expected_err);
644+
}
645+
}

0 commit comments

Comments
 (0)