fix(core)!: properly handle end offset overrun for immediate data

This commit is contained in:
Erich Gubler committed 2026-03-18 12:16:00 -06:00
1 parent d5c0b137e0
commit 2a3dd23b03
4 files changed
+40 -20

No files matched your search

+3 -1
View File
@@ -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).
+32 -11
View File
@@ -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(())
}
}
+1 -3
View File
@@ -700,14 +700,12 @@ fn set_immediates(
size_bytes: u32,
values_offset: Option<u32>,
) -> 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,
+4 -5
View File
@@ -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);