Skip to content

Commit 854ed79

Browse files
fix(core)!: validate set_immediate's values_offset param.
1 parent f165d20 commit 854ed79

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ By @kpreid in [#9042](https://github.com/gfx-rs/wgpu/pull/9042).
227227
- `ImmediateUploadError`:
228228
- Removed the `TooLarge` variant in favor of new `StartOffsetOverrun` and `EndOffsetOverrun` variants.
229229
- Removed the `Unaligned` variant in favor of new `StartOffsetUnaligned` and `SizeUnaligned` variants.
230+
- Added the `ValueStartIndexOverrun` and `ValueEndIndexOverrun` invariants
230231
- 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).
231232
- 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).
232233
- The main 3 native backends now report their limits properly. By @teoxoy in [#9196](https://github.com/gfx-rs/wgpu/pull/9196).

wgpu-core/src/binding_model.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,20 @@ pub enum ImmediateUploadError {
889889
size: u32,
890890
immediate_size: u32,
891891
},
892+
#[error("Start index {start_index} overruns the value data range with {data_size} element(s)")]
893+
ValueStartIndexOverrun { start_index: u32, data_size: usize },
894+
#[error(
895+
"Start index {} + count of {} overruns the value data range \
896+
with {} element(s)",
897+
start_index,
898+
count,
899+
data_size
900+
)]
901+
ValueEndIndexOverrun {
902+
start_index: u32,
903+
count: u32,
904+
data_size: usize,
905+
},
892906
}
893907

894908
impl WebGpuError for ImmediateUploadError {

wgpu-core/src/command/pass.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,30 @@ where
240240

241241
pipeline_layout.validate_immediates_ranges(offset, size_bytes)?;
242242

243-
let values_end_offset = (values_offset + size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize;
244-
let data_slice = &immediates_data[(values_offset as usize)..values_end_offset];
243+
let values_offset_usize = values_offset as usize;
244+
if values_offset_usize > immediates_data.len() {
245+
return Err(ImmediateUploadError::ValueStartIndexOverrun {
246+
start_index: values_offset,
247+
data_size: immediates_data.len(),
248+
}
249+
.into());
250+
}
251+
252+
// NOTE: The `validate_immediates_ranges` call above validates `size_bytes` is aligned.
253+
let size_immediate_elements = size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT;
254+
let size_immediate_elements_usize = size_immediate_elements as usize;
255+
if size_immediate_elements_usize > immediates_data.len() - values_offset_usize {
256+
return Err(ImmediateUploadError::ValueEndIndexOverrun {
257+
start_index: values_offset,
258+
count: size_immediate_elements,
259+
data_size: immediates_data.len(),
260+
}
261+
.into());
262+
}
263+
264+
// NOTE: These additions are will not overflow, because we've validated the range above.
265+
let values_end_offset = values_offset_usize + size_immediate_elements_usize;
266+
let data_slice = &immediates_data[(values_offset_usize)..values_end_offset];
245267

246268
f(data_slice);
247269

0 commit comments

Comments
 (0)