From e59c33046bfae7c3f8ed5a412ea1db058f8b0cbf Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Mon, 1 Aug 2022 22:25:32 +0200 Subject: [PATCH] Validate against the maximum binding index (#2892) * Validate binding indices in create_bind_group_layout. * Add an entry in the changelog --- CHANGELOG.md | 1 + wgpu-core/src/binding_model.rs | 2 ++ wgpu-core/src/device/mod.rs | 9 +++++++++ 3 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index df77e009c9..9674311e85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,7 @@ the same every time it is rendered, we now warn if it is missing. - Fix bugs when mapping/unmapping zero-sized buffers and ranges by @nical in [#2877](https://github.com/gfx-rs/wgpu/pull/2877) - Fix out-of-bound write in `map_buffer` with non-zero offset by @nical in [#2916](https://github.com/gfx-rs/wgpu/pull/2916) - Validate the number of color attachments in `create_render_pipeline` by @nical in [#2913](https://github.com/gfx-rs/wgpu/pull/2913) +- Validate against the maximum binding index in `create_bind_group_layout` by @nical in [#2892] #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 458723a3bf..ff09999ad5 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -48,6 +48,8 @@ pub enum CreateBindGroupLayoutError { }, #[error(transparent)] TooManyBindings(BindingTypeMaxCountError), + #[error("Binding index {binding} is greater than the maximum index {maximum}")] + InvalidBindingIndex { binding: u32, maximum: u32 }, } //TODO: refactor this to move out `enum BindingError`. diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index dfb30d8607..895d7e068f 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -31,6 +31,9 @@ pub mod queue; #[cfg(any(feature = "trace", feature = "replay"))] pub mod trace; +// Per WebGPU specification. +pub const MAX_BINDING_INDEX: u32 = 65535; + pub const SHADER_STAGE_COUNT: usize = 3; // Should be large enough for the largest possible texture row. This value is enough for a 16k texture with float4 format. pub(crate) const ZERO_BUFFER_SIZE: BufferAddress = 512 << 10; @@ -4083,6 +4086,12 @@ impl Global { let mut entry_map = FastHashMap::default(); for entry in desc.entries.iter() { + if entry.binding > MAX_BINDING_INDEX { + break 'outer binding_model::CreateBindGroupLayoutError::InvalidBindingIndex { + binding: entry.binding, + maximum: MAX_BINDING_INDEX, + }; + } if entry_map.insert(entry.binding, *entry).is_some() { break 'outer binding_model::CreateBindGroupLayoutError::ConflictBinding( entry.binding,