diff --git a/CHANGELOG.md b/CHANGELOG.md index c27699365..1188ef4ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,12 @@ We have merged the acceleration structure feature into the `RayQuery` feature. T By @Vecvec in [#7913](https://github.com/gfx-rs/wgpu/pull/7913). +### Changes + +#### Naga + +Naga now requires that no type be larger than 1 GB. This limit may be lowered in the future; feedback on an appropriate value for the limit is welcome. By @andyleiserson in [#7950](https://github.com/gfx-rs/wgpu/pull/7950). + ## v26.0.1 (2025-07-10) ### Bug Fixes diff --git a/naga/src/front/wgsl/error.rs b/naga/src/front/wgsl/error.rs index 7f2c81812..7101eb6fc 100644 --- a/naga/src/front/wgsl/error.rs +++ b/naga/src/front/wgsl/error.rs @@ -409,6 +409,12 @@ pub(crate) enum Error<'a> { accept_span: Span, accept_type: String, }, + StructMemberTooLarge { + member_name_span: Span, + }, + TypeTooLarge { + span: Span, + }, } impl From for Error<'_> { @@ -1367,6 +1373,22 @@ impl<'a> Error<'a> { ], notes: vec![], }, + Error::StructMemberTooLarge { member_name_span } => ParseError { + message: "struct member is too large".into(), + labels: vec![(member_name_span, "this member exceeds the maximum size".into())], + notes: vec![format!( + "the maximum size is {} bytes", + crate::valid::MAX_TYPE_SIZE + )], + }, + Error::TypeTooLarge { span } => ParseError { + message: "type is too large".into(), + labels: vec![(span, "this type exceeds the maximum size".into())], + notes: vec![format!( + "the maximum size is {} bytes", + crate::valid::MAX_TYPE_SIZE + )], + }, } } } diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 7f702b82b..a59bdad1e 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -7,7 +7,6 @@ use alloc::{ }; use core::num::NonZeroU32; -use crate::common::ForDebugWithTypes; use crate::front::wgsl::error::{Error, ExpectedToken, InvalidAssignmentType}; use crate::front::wgsl::index::Index; use crate::front::wgsl::parse::number::Number; @@ -18,6 +17,7 @@ use crate::{ common::wgsl::{TryToWgsl, TypeContext}, compact::KeepUnused, }; +use crate::{common::ForDebugWithTypes, proc::LayoutErrorInner}; use crate::{ir, proc}; use crate::{Arena, FastHashMap, FastIndexMap, Handle, Span}; @@ -3709,7 +3709,27 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { for member in s.members.iter() { let ty = self.resolve_ast_type(member.ty, &mut ctx.as_const())?; - ctx.layouter.update(ctx.module.to_ctx()).unwrap(); + ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| { + let LayoutErrorInner::TooLarge = err.inner else { + unreachable!("unexpected layout error: {err:?}"); + }; + // Since anonymous types of struct members don't get a span, + // associate the error with the member. The layouter could have + // failed on any type that was pending layout, but if it wasn't + // the current struct member, it wasn't a struct member at all, + // because we resolve struct members one-by-one. + if ty == err.ty { + Box::new(Error::StructMemberTooLarge { + member_name_span: member.name.span, + }) + } else { + // Lots of type definitions don't get spans, so this error + // message may not be very useful. + Box::new(Error::TypeTooLarge { + span: ctx.module.types.get_span(err.ty), + }) + } + })?; let member_min_size = ctx.layouter[ty].size; let member_min_alignment = ctx.layouter[ty].alignment; @@ -3761,6 +3781,9 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { }); offset += member_size; + if offset > crate::valid::MAX_TYPE_SIZE { + return Err(Box::new(Error::TypeTooLarge { span })); + } } let size = struct_alignment.round_up(offset); @@ -3938,7 +3961,17 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { let base = self.resolve_ast_type(base, &mut ctx.as_const())?; let size = self.array_size(size, ctx)?; - ctx.layouter.update(ctx.module.to_ctx()).unwrap(); + // Determine the size of the base type, if needed. + ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| { + let LayoutErrorInner::TooLarge = err.inner else { + unreachable!("unexpected layout error: {err:?}"); + }; + // Lots of type definitions don't get spans, so this error + // message may not be very useful. + Box::new(Error::TypeTooLarge { + span: ctx.module.types.get_span(err.ty), + }) + })?; let stride = ctx.layouter[base].to_stride(); ir::TypeInner::Array { base, size, stride } diff --git a/naga/src/ir/mod.rs b/naga/src/ir/mod.rs index 943dbc245..125ba818c 100644 --- a/naga/src/ir/mod.rs +++ b/naga/src/ir/mod.rs @@ -411,6 +411,18 @@ impl VectorSize { pub const MAX: usize = Self::Quad as usize; } +impl From for u8 { + fn from(size: VectorSize) -> u8 { + size as u8 + } +} + +impl From for u32 { + fn from(size: VectorSize) -> u32 { + size as u32 + } +} + /// Primitive type for a scalar. #[repr(u8)] #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)] diff --git a/naga/src/proc/layouter.rs b/naga/src/proc/layouter.rs index 0001d92f2..204a523c9 100644 --- a/naga/src/proc/layouter.rs +++ b/naga/src/proc/layouter.rs @@ -1,6 +1,9 @@ use core::{fmt::Display, num::NonZeroU32, ops}; -use crate::arena::{Handle, HandleVec}; +use crate::{ + arena::{Handle, HandleVec}, + valid::MAX_TYPE_SIZE, +}; /// A newtype struct where its only valid values are powers of 2 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] @@ -121,6 +124,12 @@ impl ops::Index> for Layouter { } } +/// Errors generated by the `Layouter`. +/// +/// All of these errors can be produced when validating an arbitrary module. +/// When processing WGSL source, only the `TooLarge` error should be +/// produced by the `Layouter`, as the front-end should not produce IR +/// that would result in the other errors. #[derive(Clone, Copy, Debug, PartialEq, thiserror::Error)] pub enum LayoutErrorInner { #[error("Array element type {0:?} doesn't exist")] @@ -129,6 +138,8 @@ pub enum LayoutErrorInner { InvalidStructMemberType(u32, Handle), #[error("Type width must be a power of two")] NonPowerOfTwoWidth, + #[error("Size exceeds limit of {MAX_TYPE_SIZE} bytes")] + TooLarge, } #[derive(Clone, Copy, Debug, PartialEq, thiserror::Error)] @@ -168,7 +179,10 @@ impl Layouter { use crate::TypeInner as Ti; for (ty_handle, ty) in gctx.types.iter().skip(self.layouts.len()) { - let size = ty.inner.size(gctx); + let size = ty + .inner + .try_size(gctx) + .ok_or_else(|| LayoutErrorInner::TooLarge.with(ty_handle))?; let layout = match ty.inner { Ti::Scalar(scalar) | Ti::Atomic(scalar) => { let alignment = Alignment::new(scalar.width as u32) diff --git a/naga/src/proc/type_methods.rs b/naga/src/proc/type_methods.rs index 289756a67..c59d524f1 100644 --- a/naga/src/proc/type_methods.rs +++ b/naga/src/proc/type_methods.rs @@ -4,7 +4,7 @@ //! [`Scalar`]: crate::Scalar //! [`ScalarKind`]: crate::ScalarKind -use crate::ir; +use crate::{ir, valid::MAX_TYPE_SIZE}; use super::TypeResolution; @@ -190,18 +190,19 @@ impl crate::TypeInner { } } - /// Get the size of this type. - pub fn size(&self, gctx: super::GlobalCtx) -> u32 { + /// Attempt to calculate the size of this type. Returns `None` if the size + /// exceeds the limit of [`crate::valid::MAX_TYPE_SIZE`]. + pub fn try_size(&self, gctx: super::GlobalCtx) -> Option { match *self { - Self::Scalar(scalar) | Self::Atomic(scalar) => scalar.width as u32, - Self::Vector { size, scalar } => size as u32 * scalar.width as u32, + Self::Scalar(scalar) | Self::Atomic(scalar) => Some(scalar.width as u32), + Self::Vector { size, scalar } => Some(size as u32 * scalar.width as u32), // matrices are treated as arrays of aligned columns Self::Matrix { columns, rows, scalar, - } => super::Alignment::from(rows) * scalar.width as u32 * columns as u32, - Self::Pointer { .. } | Self::ValuePointer { .. } => POINTER_SPAN, + } => Some(super::Alignment::from(rows) * scalar.width as u32 * columns as u32), + Self::Pointer { .. } | Self::ValuePointer { .. } => Some(POINTER_SPAN), Self::Array { base: _, size, @@ -215,17 +216,35 @@ impl crate::TypeInner { // A dynamically-sized array has to have at least one element Ok(crate::proc::IndexableLength::Dynamic) => 1, }; - count * stride + if count > MAX_TYPE_SIZE { + // It shouldn't be possible to have an array of a zero-sized type, but + // let's check just in case. + None + } else { + count + .checked_mul(stride) + .filter(|size| *size <= MAX_TYPE_SIZE) + } } - Self::Struct { span, .. } => span, + Self::Struct { span, .. } => Some(span), Self::Image { .. } | Self::Sampler { .. } | Self::AccelerationStructure { .. } | Self::RayQuery { .. } - | Self::BindingArray { .. } => 0, + | Self::BindingArray { .. } => Some(0), } } + /// Get the size of this type. + /// + /// Panics if the size exceeds the limit of [`crate::valid::MAX_TYPE_SIZE`]. + /// Validated modules should not contain such types. Code working with + /// modules prior to validation should use [`Self::try_size`] and handle the + /// error appropriately. + pub fn size(&self, gctx: super::GlobalCtx) -> u32 { + self.try_size(gctx).expect("type is too large") + } + /// Return the canonical form of `self`, or `None` if it's already in /// canonical form. /// diff --git a/naga/src/valid/mod.rs b/naga/src/valid/mod.rs index aef6a2416..cee5b7420 100644 --- a/naga/src/valid/mod.rs +++ b/naga/src/valid/mod.rs @@ -35,6 +35,9 @@ pub use r#type::{Disalignment, PushConstantError, TypeError, TypeFlags, WidthErr use self::handles::InvalidHandleError; +/// Maximum size of a type, in bytes. +pub const MAX_TYPE_SIZE: u32 = 0x4000_0000; // 1GB + bitflags::bitflags! { /// Validation flags. /// diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 5784192ef..79ffe2a49 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -7,7 +7,10 @@ Tests for the WGSL front end. clippy::result_large_err )] -use naga::{compact::KeepUnused, valid::Capabilities}; +use naga::{ + compact::KeepUnused, + valid::{self, Capabilities}, +}; #[track_caller] fn check(input: &str, snapshot: &str) { @@ -3746,3 +3749,141 @@ fn invalid_clip_distances() { naga::valid::Capabilities::CLIP_DISTANCE } } + +#[cfg(feature = "wgsl-in")] +#[test] +fn max_type_size_large_array() { + // The total size of an array is not resolved until validation. Type aliases + // don't get spans so the error isn't very helpful. + check_validation! { + "alias LargeArray = array;": + Err(naga::valid::ValidationError::Layouter( + naga::proc::LayoutError { + inner: naga::proc::LayoutErrorInner::TooLarge, + .. + } + )) + } +} + +#[cfg(feature = "wgsl-in")] +#[test] +fn max_type_size_array_of_arrays() { + // If the size of the base type of an array is oversize, the error is raised + // during lowering. Anonymous types don't get spans so this error isn't very + // helpful. + check( + "alias ArrayOfArrays = array, 22>;", + r#"error: type is too large + = note: the maximum size is 1073741824 bytes + +"#, + ); +} + +#[cfg(feature = "wgsl-in")] +#[test] +fn max_type_size_override_array() { + // The validation that occurs after override processing should reject any + // arrays that were overridden to be larger than the maximum size. Type + // aliases don't get spans so the error isn't very helpful. + let source = r#" + override SIZE: u32 = 1; + alias ArrayOfOverrideArrays = array; + + var global: ArrayOfOverrideArrays; + + @compute @workgroup_size(64) + fn main() { + let used = &global; + } + "#; + let module = naga::front::wgsl::parse_str(source).expect("module should parse"); + let info = valid::Validator::new(Default::default(), valid::Capabilities::all()) + .validate(&module) + .expect("module should validate"); + + let overrides = hashbrown::HashMap::from([(String::from("SIZE"), f64::from((1 << 28) + 1))]); + let err = naga::back::pipeline_constants::process_overrides(&module, &info, None, &overrides) + .unwrap_err(); + let naga::back::pipeline_constants::PipelineConstantError::ValidationError(err) = err else { + panic!("expected a validation error, got {err:?}"); + }; + assert!(matches!( + err.into_inner(), + naga::valid::ValidationError::Layouter(naga::proc::LayoutError { + inner: naga::proc::LayoutErrorInner::TooLarge, + .. + }), + )); +} + +#[cfg(feature = "wgsl-in")] +#[test] +fn max_type_size_array_in_struct() { + // If a struct member is oversize, the error is raised during lowering. + // For struct members we can associate the error with the member. + check( + r#" + struct ContainsLargeArray { + arr: array, + } + "#, + r#"error: struct member is too large + ┌─ wgsl:3:17 + │ +3 │ arr: array, + │ ^^^ this member exceeds the maximum size + │ + = note: the maximum size is 1073741824 bytes + +"#, + ); +} + +#[cfg(feature = "wgsl-in")] +#[test] +fn max_type_size_two_arrays_in_struct() { + // The total size of a struct is checked during lowering. For a struct, + // we can associate the error with the struct itself. + check( + r#" + struct TwoArrays { + arr1: array, + arr2: array, + } + "#, + "error: type is too large + ┌─ wgsl:2:13 + │\x20\x20 +2 │ ╭ struct TwoArrays { +3 │ │ arr1: array, +4 │ │ arr2: array, + │ ╰───────────────────────────────────────────────^ this type exceeds the maximum size + │\x20\x20 + = note: the maximum size is 1073741824 bytes + +", + ); +} + +#[cfg(feature = "wgsl-in")] +#[test] +fn max_type_size_array_of_structs() { + // The total size of an array is not resolved until validation. Type aliases + // don't get spans so the error isn't very helpful. + check_validation! { + r#" + struct NotVeryBigStruct { + data: u32, + } + alias BigArrayOfStructs = array; + "#: + Err(naga::valid::ValidationError::Layouter( + naga::proc::LayoutError { + inner: naga::proc::LayoutErrorInner::TooLarge, + .. + } + )) + } +}