From 2a3dd23b0369f2d34b488c83c39f0c788644c3ce Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 11 Mar 2026 17:24:32 -0600 Subject: [PATCH] fix(core)!: properly handle end offset overrun for immediate data --- CHANGELOG.md | 4 ++- wgpu-core/src/binding_model.rs | 43 ++++++++++++++++++++++++--------- wgpu-core/src/command/bundle.rs | 4 +-- wgpu-core/src/command/pass.rs | 9 +++---- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1384511c1..ca859bf00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -219,11 +219,13 @@ By @kpreid in [#9042](https://github.com/gfx-rs/wgpu/pull/9042). #### General - Tracing now uses the `.metal` extension for metal source files, instead of `.msl`. By @inner-daemons in [#8880](https://github.com/gfx-rs/wgpu/pull/8880). -- Several error APIs were changed by @ErichDonGubler in [#9073](https://github.com/gfx-rs/wgpu/pull/9073): +- BREAKING: Several error APIs were changed by @ErichDonGubler in [#9073](https://github.com/gfx-rs/wgpu/pull/9073) and [#????](https://github.com/gfx-rs/wgpu/pull/????): - `BufferAccessError`: - Split the `OutOfBoundsOverrun` variant into new `OutOfBoundsStartOffsetOverrun` and `OutOfBoundsEndOffsetOverrun` variants. - Removed the `NegativeRange` variant in favor of new `MapStartOffsetUnderrun` and `MapStartOffsetOverrun` variants. - Split the `TransferError::BufferOverrun` variant into new `BufferStartOffsetOverrun` and `BufferEndOffsetOverrun` variants. + - `ImmediateUploadError`: + - Removed the `TooLarge` variant in favor of new `StartOffsetOverrun` and `EndOffsetOverrun` variants. - The various "max resources per stage" limits are now capped at 100, so that their total remains below `max_bindings_per_bind_group`, as required by WebGPU. By @andyleiserson in [#9118](https://github.com/gfx-rs/wgpu/pull/9118). - The `max_uniform_buffer_binding_size` and `max_storage_buffer_binding_size` limits are now `u64` instead of `u32`, to match WebGPU. By @wingertge in [#9146](https://github.com/gfx-rs/wgpu/pull/9146). - The main 3 native backends now report their limits properly. By @teoxoy in [#9196](https://github.com/gfx-rs/wgpu/pull/9196). diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 9db83a720..5c8316d4c 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -858,17 +858,30 @@ impl WebGpuError for CreatePipelineLayoutError { #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum ImmediateUploadError { - #[error("Provided immediate data written to offset {offset}..{end_offset} overruns the immediate data range with a size of {size}")] - TooLarge { - offset: u32, - end_offset: u32, - size: u32, + #[error( + "Start offset {start_offset} overruns the immediate data range with a size of {immediate_size}" + )] + StartOffsetOverrun { + start_offset: u32, + immediate_size: u32, }, #[error( "Provided immediate data offset {0} does not respect `IMMEDIATE_DATA_ALIGNMENT` ({ida})", ida = wgt::IMMEDIATE_DATA_ALIGNMENT )] Unaligned(u32), + #[error( + "Provided immediate data start offset {} + size {} overruns the immediate data range \ + with a size of {}", + start_offset, + size, + immediate_size + )] + EndOffsetOverrun { + start_offset: u32, + size: u32, + immediate_size: u32, + }, } impl WebGpuError for ImmediateUploadError { @@ -970,7 +983,7 @@ impl PipelineLayout { pub(crate) fn validate_immediates_ranges( &self, offset: u32, - end_offset: u32, + size_bytes: u32, ) -> Result<(), ImmediateUploadError> { // Don't need to validate size against the immediate data size limit here, // as immediate data ranges are already validated to be within bounds, @@ -980,13 +993,21 @@ impl PipelineLayout { return Err(ImmediateUploadError::Unaligned(offset)); } - if end_offset > self.immediate_size { - return Err(ImmediateUploadError::TooLarge { - offset, - end_offset, - size: self.immediate_size, + if offset > self.immediate_size { + return Err(ImmediateUploadError::StartOffsetOverrun { + start_offset: offset, + immediate_size: self.immediate_size, }); } + + if size_bytes > self.immediate_size - offset { + return Err(ImmediateUploadError::EndOffsetOverrun { + start_offset: offset, + size: size_bytes, + immediate_size: self.immediate_size, + }); + } + Ok(()) } } diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 0b3dbf951..00bb94271 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -700,14 +700,12 @@ fn set_immediates( size_bytes: u32, values_offset: Option, ) -> Result<(), RenderBundleErrorInner> { - let end_offset = offset + size_bytes; - let pipeline_state = state.pipeline()?; pipeline_state .pipeline .layout - .validate_immediates_ranges(offset, end_offset)?; + .validate_immediates_ranges(offset, size_bytes)?; state.commands.push(ArcRenderCommand::SetImmediate { offset, diff --git a/wgpu-core/src/command/pass.rs b/wgpu-core/src/command/pass.rs index bfa72aa91..15d2e241d 100644 --- a/wgpu-core/src/command/pass.rs +++ b/wgpu-core/src/command/pass.rs @@ -232,17 +232,16 @@ where let values_offset = values_offset.ok_or(InvalidValuesOffset)?; - let end_offset_bytes = offset + size_bytes; - let values_end_offset = (values_offset + size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize; - let data_slice = &immediates_data[(values_offset as usize)..values_end_offset]; - let pipeline_layout = state .binder .pipeline_layout .as_ref() .ok_or(MissingPipeline)?; - pipeline_layout.validate_immediates_ranges(offset, end_offset_bytes)?; + pipeline_layout.validate_immediates_ranges(offset, size_bytes)?; + + let values_end_offset = (values_offset + size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize; + let data_slice = &immediates_data[(values_offset as usize)..values_end_offset]; f(data_slice);