From cdc5ca5ed9e4fe6f4d5f8464e488c3c103d4ce54 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 9 Apr 2026 14:21:20 -0700 Subject: [PATCH] fix(core): Use more checked arithmetic, part 2 (#9363) --- .../wgpu-gpu/dispatch_workgroups_indirect.rs | 2 +- wgpu-core/src/command/bundle.rs | 3 + wgpu-core/src/command/compute.rs | 6 +- wgpu-core/src/command/ray_tracing.rs | 115 +++++++++++------- wgpu-core/src/command/render.rs | 62 +++++----- wgpu-core/src/device/queue.rs | 8 +- wgpu-core/src/device/ray_tracing.rs | 8 +- wgpu-core/src/device/resource.rs | 15 ++- wgpu-core/src/ray_tracing.rs | 11 +- wgpu-hal/src/dx12/adapter.rs | 5 +- wgpu-hal/src/lib.rs | 2 +- wgpu-hal/src/metal/adapter.rs | 6 +- wgpu-types/src/texture.rs | 4 +- 13 files changed, 153 insertions(+), 94 deletions(-) diff --git a/tests/tests/wgpu-gpu/dispatch_workgroups_indirect.rs b/tests/tests/wgpu-gpu/dispatch_workgroups_indirect.rs index 23be840cc..8b042c2b4 100644 --- a/tests/tests/wgpu-gpu/dispatch_workgroups_indirect.rs +++ b/tests/tests/wgpu-gpu/dispatch_workgroups_indirect.rs @@ -144,7 +144,7 @@ static ZERO_SIZED_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new() let error = pollster::block_on(scope.pop()); assert!(error.is_some_and(|error| { format!("{error}").contains( - "Indirect buffer uses bytes 0..12 which overruns indirect buffer of size 0", + "Indirect buffer of 12 bytes starting at offset 0 would overrun buffer of size 0", ) })); }); diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 7b4e382b8..6a4c68c02 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -838,6 +838,9 @@ fn multi_draw_indirect( let vertex_limits = super::VertexLimits::new(state.vertex_buffer_sizes(), &pipeline.steps); let stride = super::get_stride_of_indirect_args(family); + // TODO(https://github.com/gfx-rs/wgpu/issues/8051): It would be better to report this + // as a validation error, but it's pathological, so let's do the simpler thing for now + // and do the better thing as part of eliminating pass/bundle duplication. assert!(offset <= wgt::BufferAddress::MAX - stride); state .buffer_memory_init_actions diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 474dd4b13..19db52d72 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -154,10 +154,10 @@ pub enum ComputePassErrorInner { DestroyedResource(#[from] DestroyedResourceError), #[error("Indirect buffer offset {0:?} is not a multiple of 4")] UnalignedIndirectBufferOffset(BufferAddress), - #[error("Indirect buffer uses bytes {offset}..{end_offset} which overruns indirect buffer of size {buffer_size}")] + #[error("Indirect buffer of {args_size} bytes starting at offset {offset} would overrun buffer of size {buffer_size}")] IndirectBufferOverrun { + args_size: u64, offset: u64, - end_offset: u64, buffer_size: u64, }, #[error(transparent)] @@ -888,8 +888,8 @@ fn dispatch_indirect( let args_size = size_of::() as u64; if buffer.size < args_size || buffer.size - args_size < offset { return Err(ComputePassErrorInner::IndirectBufferOverrun { + args_size, offset, - end_offset: offset + args_size, buffer_size: buffer.size, }); } diff --git a/wgpu-core/src/command/ray_tracing.rs b/wgpu-core/src/command/ray_tracing.rs index 705d65a68..591ec6fd5 100644 --- a/wgpu-core/src/command/ray_tracing.rs +++ b/wgpu-core/src/command/ray_tracing.rs @@ -209,10 +209,15 @@ pub(crate) fn build_acceleration_structures( state.tracker.tlas_s.insert_single(tlas.clone()); let scratch_buffer_offset = scratch_buffer_tlas_size; + assert!( + tlas.size_info.build_scratch_size + <= u64::MAX + - u64::from(state.device.alignments.ray_tracing_scratch_buffer_alignment) + ); scratch_buffer_tlas_size += align_to( - tlas.size_info.build_scratch_size as u32, - state.device.alignments.ray_tracing_scratch_buffer_alignment, - ) as u64; + tlas.size_info.build_scratch_size, + u64::from(state.device.alignments.ray_tracing_scratch_buffer_alignment), + ); let first_byte_index = instance_buffer_staging_source.len(); @@ -293,6 +298,8 @@ pub(crate) fn build_acceleration_structures( return Ok(()); }; + assert!(scratch_size.get() != u64::MAX); + let scratch_buffer = ScratchBuffer::new(state.device, scratch_size)?; let scratch_buffer_barrier = hal::BufferBarrier:: { @@ -705,15 +712,17 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>( } if u64::from(mesh.size.vertex_count) .checked_add(u64::from(mesh.first_vertex)) - .and_then(|end_index| end_index.checked_mul(mesh.vertex_stride)) + .and_then(|end_vertex| end_vertex.checked_mul(mesh.vertex_stride)) .is_none_or(|end| vertex_buffer.size < end) { - return Err(BuildAccelerationStructureError::InsufficientBufferSize( - vertex_buffer.error_ident(), - vertex_buffer.size, - (mesh.size.vertex_count + mesh.first_vertex) as u64 - * mesh.vertex_stride, - )); + return Err(BuildAccelerationStructureError::InsufficientBufferSize { + buffer_ident: vertex_buffer.error_ident(), + offset: u64::from(mesh.first_vertex) + .saturating_mul(mesh.vertex_stride), + region_size: u64::from(mesh.size.vertex_count) + .saturating_mul(mesh.vertex_stride), + buffer_size: vertex_buffer.size, + }); } let vertex_buffer_offset = mesh.first_vertex as u64 * mesh.vertex_stride; state.buffer_memory_init_actions.extend( @@ -748,10 +757,11 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>( }) { input_barriers.push(barrier); } - let index_stride = mesh.size.index_format.unwrap().byte_size() as u64; - let offset = mesh.first_index.unwrap() as u64 * index_stride; - let index_buffer_size = - mesh.size.index_count.unwrap() as u64 * index_stride; + let index_stride = mesh.size.index_format.unwrap().byte_size() as u32; + // `hal::AccelerationStructureTriangleIndices` accepts only `u32` offset + let vertex_offset = mesh.first_index.unwrap().saturating_mul(index_stride); + let indexes_size = + mesh.size.index_count.unwrap().saturating_mul(index_stride); if mesh.size.index_count.unwrap() % 3 != 0 { return Err(BuildAccelerationStructureError::InvalidIndexCount( @@ -759,24 +769,29 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>( mesh.size.index_count.unwrap(), )); } - if index_buffer.size - < mesh.size.index_count.unwrap() as u64 * index_stride + offset + if indexes_size == u32::MAX + || vertex_offset == u32::MAX + || index_buffer.size < u64::from(vertex_offset) + || index_buffer.size - u64::from(vertex_offset) + < u64::from(indexes_size) { - return Err(BuildAccelerationStructureError::InsufficientBufferSize( - index_buffer.error_ident(), - index_buffer.size, - mesh.size.index_count.unwrap() as u64 * index_stride + offset, - )); + return Err(BuildAccelerationStructureError::InsufficientBufferSize { + buffer_ident: index_buffer.error_ident(), + offset: u64::from(vertex_offset), + region_size: u64::from(indexes_size), + buffer_size: index_buffer.size, + }); } state.buffer_memory_init_actions.extend( index_buffer.initialization_status.read().create_action( index_buffer, - offset..(offset + index_buffer_size), + u64::from(vertex_offset) + ..(u64::from(vertex_offset) + u64::from(indexes_size)), MemoryInitKind::NeedsInitializedMemory, ), ); - Some(index_raw) + Some((index_raw, vertex_offset)) } else { None }; @@ -813,30 +828,36 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>( input_barriers.push(barrier); } - let offset = mesh.transform_buffer_offset.unwrap(); + // `hal::AccelerationStructureTriangleTransform` accepts only `u32` offset + let offset = u32::try_from(mesh.transform_buffer_offset.unwrap()) + .unwrap_or(u32::MAX); - if offset % wgt::TRANSFORM_BUFFER_ALIGNMENT != 0 { + if offset % wgt::TRANSFORM_BUFFER_ALIGNMENT as u32 != 0 { return Err( BuildAccelerationStructureError::UnalignedTransformBufferOffset( transform_buffer.error_ident(), ), ); } - if transform_buffer.size < 48 + offset { - return Err(BuildAccelerationStructureError::InsufficientBufferSize( - transform_buffer.error_ident(), - transform_buffer.size, - 48 + offset, - )); + if offset == u32::MAX + || transform_buffer.size < 48 + || transform_buffer.size - 48 < u64::from(offset) + { + return Err(BuildAccelerationStructureError::InsufficientBufferSize { + buffer_ident: transform_buffer.error_ident(), + region_size: 48, + offset: u64::from(offset), + buffer_size: transform_buffer.size, + }); } state.buffer_memory_init_actions.extend( transform_buffer.initialization_status.read().create_action( transform_buffer, - offset..(offset + 48), + u64::from(offset)..(u64::from(offset) + 48), MemoryInitKind::NeedsInitializedMemory, ), ); - Some(transform_raw) + Some((transform_raw, offset)) } else { if blas .flags @@ -855,19 +876,18 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>( first_vertex: mesh.first_vertex, vertex_count: mesh.size.vertex_count, vertex_stride: mesh.vertex_stride, - indices: index_buffer.map(|index_buffer| { - let index_stride = mesh.size.index_format.unwrap().byte_size() as u32; + indices: index_buffer.map(|(index_raw, vertex_offset)| { hal::AccelerationStructureTriangleIndices:: { format: mesh.size.index_format.unwrap(), - buffer: Some(index_buffer), - offset: mesh.first_index.unwrap() * index_stride, + buffer: Some(index_raw), + offset: vertex_offset, count: mesh.size.index_count.unwrap(), } }), - transform: transform_buffer.map(|transform_buffer| { + transform: transform_buffer.map(|(transform_raw, offset)| { hal::AccelerationStructureTriangleTransform { - buffer: transform_buffer, - offset: mesh.transform_buffer_offset.unwrap() as u32, + buffer: transform_raw, + offset, } }), flags: mesh.size.flags, @@ -877,10 +897,17 @@ fn iter_blas<'snatch_guard: 'buffers, 'buffers>( { let scratch_buffer_offset = *scratch_buffer_blas_size; - *scratch_buffer_blas_size += align_to( - blas.size_info.build_scratch_size as u32, - state.device.alignments.ray_tracing_scratch_buffer_alignment, - ) as u64; + assert!( + blas.size_info.build_scratch_size + <= u64::MAX + - u64::from( + state.device.alignments.ray_tracing_scratch_buffer_alignment + ) + ); + *scratch_buffer_blas_size = scratch_buffer_blas_size.saturating_add(align_to( + blas.size_info.build_scratch_size, + u64::from(state.device.alignments.ray_tracing_scratch_buffer_alignment), + )); blas_storage.push(BlasStore { blas: blas.clone(), diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index d8b150b9b..bfc166b3e 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -757,17 +757,17 @@ pub enum RenderPassErrorInner { MissingDownlevelFlags(#[from] MissingDownlevelFlags), #[error("Indirect buffer offset {0:?} is not a multiple of 4")] UnalignedIndirectBufferOffset(BufferAddress), - #[error("Indirect draw uses bytes {offset}..{end_offset} using count {count} which overruns indirect buffer of size {buffer_size}")] + #[error("Indirect draw arguments of {args_size} bytes (count = {count}) starting at {offset} would overrun buffer size of {buffer_size}")] IndirectBufferOverrun { count: u32, offset: u64, - end_offset: u64, + args_size: u64, buffer_size: u64, }, - #[error("Indirect draw uses bytes {begin_count_offset}..{end_count_offset} which overruns indirect buffer of size {count_buffer_size}")] + #[error("Indirect draw count of {count_bytes} bytes starting at {begin_count_offset} would overrun buffer of size {count_buffer_size}")] IndirectCountBufferOverrun { + count_bytes: u64, begin_count_offset: u64, - end_count_offset: u64, count_buffer_size: u64, }, #[error(transparent)] @@ -2789,21 +2789,22 @@ fn multi_draw_indirect( } let stride = get_stride_of_indirect_args(family); - - let end_offset = offset + stride * count as u64; - if end_offset > indirect_buffer.size { - return Err(RenderPassErrorInner::IndirectBufferOverrun { - count, - offset, - end_offset, - buffer_size: indirect_buffer.size, - }); - } + let args_size = match stride.checked_mul(u64::from(count)) { + Some(sz) if sz <= indirect_buffer.size && indirect_buffer.size - sz >= offset => sz, + args_size => { + return Err(RenderPassErrorInner::IndirectBufferOverrun { + count, + offset, + args_size: args_size.unwrap_or(u64::MAX), + buffer_size: indirect_buffer.size, + }); + } + }; state.pass.base.buffer_memory_init_actions.extend( indirect_buffer.initialization_status.read().create_action( &indirect_buffer, - offset..end_offset, + offset..offset + args_size, MemoryInitKind::NeedsInitializedMemory, ), ); @@ -2998,36 +2999,39 @@ fn multi_draw_indirect_count( return Err(RenderPassErrorInner::UnalignedIndirectBufferOffset(offset)); } - let end_offset = offset + stride * max_count as u64; - if end_offset > indirect_buffer.size { - return Err(RenderPassErrorInner::IndirectBufferOverrun { - count: 1, - offset, - end_offset, - buffer_size: indirect_buffer.size, - }); - } + let args_size = match stride.checked_mul(u64::from(max_count)) { + Some(sz) if sz <= indirect_buffer.size && indirect_buffer.size - sz >= offset => sz, + args_size => { + return Err(RenderPassErrorInner::IndirectBufferOverrun { + count: 1, + offset, + args_size: args_size.unwrap_or(u64::MAX), + buffer_size: indirect_buffer.size, + }); + } + }; + state.pass.base.buffer_memory_init_actions.extend( indirect_buffer.initialization_status.read().create_action( &indirect_buffer, - offset..end_offset, + offset..offset + args_size, MemoryInitKind::NeedsInitializedMemory, ), ); let begin_count_offset = count_buffer_offset; - let end_count_offset = count_buffer_offset + 4; - if end_count_offset > count_buffer.size { + let count_bytes = 4; + if count_buffer.size < count_bytes || count_buffer.size - count_bytes < count_buffer_offset { return Err(RenderPassErrorInner::IndirectCountBufferOverrun { begin_count_offset, - end_count_offset, + count_bytes: 4, count_buffer_size: count_buffer.size, }); } state.pass.base.buffer_memory_init_actions.extend( count_buffer.initialization_status.read().create_action( &count_buffer, - count_buffer_offset..end_count_offset, + count_buffer_offset..count_buffer_offset + count_bytes, MemoryInitKind::NeedsInitializedMemory, ), ); diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index c74be0377..eb5340af7 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1021,20 +1021,20 @@ impl Queue { .into()); } - if source.origin.x + size.width > src_width { + if source.origin.x > src_width || src_width - source.origin.x < size.width { return Err(TransferError::TextureOverrun { start_offset: source.origin.x, - end_offset: source.origin.x + size.width, + end_offset: source.origin.x.saturating_add(size.width), texture_size: src_width, dimension: crate::resource::TextureErrorDimension::X, side: CopySide::Source, } .into()); } - if source.origin.y + size.height > src_height { + if source.origin.y > src_height || src_height - source.origin.y < size.height { return Err(TransferError::TextureOverrun { start_offset: source.origin.y, - end_offset: source.origin.y + size.height, + end_offset: source.origin.y.saturating_add(size.height), texture_size: src_height, dimension: crate::resource::TextureErrorDimension::Y, side: CopySide::Source, diff --git a/wgpu-core/src/device/ray_tracing.rs b/wgpu-core/src/device/ray_tracing.rs index f70e3517d..010f2a6f8 100644 --- a/wgpu-core/src/device/ray_tracing.rs +++ b/wgpu-core/src/device/ray_tracing.rs @@ -228,12 +228,14 @@ impl Device { } .map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?; - let instance_buffer_size = - self.alignments.raw_tlas_instance_size * desc.max_instances.max(1) as usize; + let instance_buffer_size = self + .alignments + .raw_tlas_instance_size + .saturating_mul(desc.max_instances.max(1)); let instance_buffer = unsafe { self.raw().create_buffer(&hal::BufferDescriptor { label: hal_label(Some("(wgpu-core) instances_buffer"), self.instance_flags), - size: instance_buffer_size as u64, + size: u64::from(instance_buffer_size), usage: wgt::BufferUses::COPY_DST | wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT, memory_flags: hal::MemoryFlags::PREFER_COHERENT, diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 47aab4241..dd20d6c24 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -1171,10 +1171,23 @@ impl Device { let snatch_guard = device.snatchable_lock.read(); let raw_buf = buffer.try_raw(&snatch_guard)?; + if offset > buffer.size { + return Err(resource::BufferAccessError::OutOfBoundsStartOffsetOverrun { + index: offset, + max: buffer.size, + }); + } else if buffer.size - offset < u64::try_from(data.len()).unwrap() { + return Err(resource::BufferAccessError::OutOfBoundsEndOffsetOverrun { + index: offset, + size: u64::try_from(data.len()).unwrap(), + max: buffer.size, + }); + } + let mapping = unsafe { device .raw() - .map_buffer(raw_buf, offset..offset + data.len() as u64) + .map_buffer(raw_buf, offset..offset + u64::try_from(data.len()).unwrap()) } .map_err(|e| device.handle_hal_error(e))?; diff --git a/wgpu-core/src/ray_tracing.rs b/wgpu-core/src/ray_tracing.rs index 279884d66..a8a6644a1 100644 --- a/wgpu-core/src/ray_tracing.rs +++ b/wgpu-core/src/ray_tracing.rs @@ -107,9 +107,14 @@ pub enum BuildAccelerationStructureError { MissingFeatures(#[from] MissingFeatures), #[error( - "Buffer {0:?} size is insufficient for provided size information (size: {1}, required: {2}" + "Data range of {region_size} B starting at offset {offset} would overrun the size {buffer_size} of buffer {buffer_ident:?}" )] - InsufficientBufferSize(ResourceErrorIdent, u64, u64), + InsufficientBufferSize { + buffer_ident: ResourceErrorIdent, + offset: BufferAddress, + region_size: BufferAddress, + buffer_size: BufferAddress, + }, #[error("Buffer {0:?} associated offset doesn't align with the index type")] UnalignedIndexBufferOffset(ResourceErrorIdent), @@ -192,7 +197,7 @@ impl WebGpuError for BuildAccelerationStructureError { Self::DestroyedResource(e) => e.webgpu_error_type(), Self::MissingBufferUsage(e) => e.webgpu_error_type(), Self::MissingFeatures(e) => e.webgpu_error_type(), - Self::InsufficientBufferSize(..) + Self::InsufficientBufferSize { .. } | Self::UnalignedIndexBufferOffset(..) | Self::UnalignedTransformBufferOffset(..) | Self::InvalidIndexCount(..) diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 62598fc3e..c5bdba20e 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -930,7 +930,10 @@ impl super::Adapter { // Direct3D correctly bounds-checks all array accesses: // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.8.2%20Device%20Memory%20Reads uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), - raw_tlas_instance_size: size_of::(), + raw_tlas_instance_size: u32::try_from(size_of::< + Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC, + >()) + .unwrap(), ray_tracing_scratch_buffer_alignment: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT, }, diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 3ce87a2c7..15be63536 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -1921,7 +1921,7 @@ pub struct Alignments { pub uniform_bounds_check_alignment: wgt::BufferSize, /// The size of the raw TLAS instance - pub raw_tlas_instance_size: usize, + pub raw_tlas_instance_size: u32, /// What the scratch buffer for building an acceleration structure must be aligned to pub ray_tracing_scratch_buffer_alignment: u32, diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index c14f90421..8c6f81d8c 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -1251,8 +1251,10 @@ impl super::PrivateCapabilities { // Metal Shading Language it generates, so from `wgpu_hal`'s // users' point of view, references are tightly checked. uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), - raw_tlas_instance_size: size_of::( - ), + raw_tlas_instance_size: u32::try_from(size_of::< + MTLIndirectAccelerationStructureInstanceDescriptor, + >()) + .unwrap(), ray_tracing_scratch_buffer_alignment: 1, }, downlevel, diff --git a/wgpu-types/src/texture.rs b/wgpu-types/src/texture.rs index a28abe2f9..7e23d58cf 100644 --- a/wgpu-types/src/texture.rs +++ b/wgpu-types/src/texture.rs @@ -976,7 +976,7 @@ impl ImageSubresourceRange { #[must_use] pub fn mip_range(&self, mip_level_count: u32) -> Range { self.base_mip_level..match self.mip_level_count { - Some(mip_level_count) => self.base_mip_level + mip_level_count, + Some(mip_level_count) => self.base_mip_level.saturating_add(mip_level_count), None => mip_level_count, } } @@ -985,7 +985,7 @@ impl ImageSubresourceRange { #[must_use] pub fn layer_range(&self, array_layer_count: u32) -> Range { self.base_array_layer..match self.array_layer_count { - Some(array_layer_count) => self.base_array_layer + array_layer_count, + Some(array_layer_count) => self.base_array_layer.saturating_add(array_layer_count), None => array_layer_count, } }