From c97d22fff4411228546f37bc7c532c158a61dc6e Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Tue, 21 Jul 2026 04:36:40 -0700 Subject: [PATCH] fix(core): Add some missing validity checks (#9913) --- tests/tests/wgpu-gpu/resource_error.rs | 54 ++++++++++++++++++++++++-- wgpu-core/src/device/queue.rs | 4 ++ wgpu-core/src/device/resource.rs | 1 + wgpu-core/src/resource.rs | 2 +- wgpu-types/src/origin_extent.rs | 8 ++-- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/tests/tests/wgpu-gpu/resource_error.rs b/tests/tests/wgpu-gpu/resource_error.rs index e8f923549..7d35adaa5 100644 --- a/tests/tests/wgpu-gpu/resource_error.rs +++ b/tests/tests/wgpu-gpu/resource_error.rs @@ -1,11 +1,11 @@ use wgpu_test::{fail, gpu_test, valid, GpuTestConfiguration, GpuTestInitializer, TestParameters}; pub fn all_tests(vec: &mut Vec) { - vec.extend([BAD_BUFFER, BAD_TEXTURE]); + vec.extend([BAD_BUFFER_MAP, BAD_TEXTURE_CREATE_VIEW, BAD_TEXTURE_WRITE]); } #[gpu_test] -static BAD_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new() +static BAD_BUFFER_MAP: GpuTestConfiguration = GpuTestConfiguration::new() .parameters(TestParameters::default().enable_noop()) .run_sync(|ctx| { // Create a buffer with bad parameters and call a few methods. @@ -38,7 +38,7 @@ static BAD_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new() }); #[gpu_test] -static BAD_TEXTURE: GpuTestConfiguration = GpuTestConfiguration::new() +static BAD_TEXTURE_CREATE_VIEW: GpuTestConfiguration = GpuTestConfiguration::new() .parameters(TestParameters::default().enable_noop()) .run_sync(|ctx| { let texture = fail( @@ -72,3 +72,51 @@ static BAD_TEXTURE: GpuTestConfiguration = GpuTestConfiguration::new() valid(&ctx.device, || texture.destroy()); valid(&ctx.device, || texture.destroy()); }); + +#[gpu_test] +static BAD_TEXTURE_WRITE: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(TestParameters::default().enable_noop()) + .run_sync(|ctx| { + let texture = fail( + &ctx.device, + || { + ctx.device.create_texture(&wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { + width: 256, + height: 256, + depth_or_array_layers: 1, + }, + mip_level_count: 5888, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }) + }, + Some("mip level count"), + ); + + fail( + &ctx.device, + || { + ctx.queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &texture, + mip_level: 255, + origin: wgpu::Origin3d::ZERO, + aspect: wgpu::TextureAspect::All, + }, + &[0u8; 4], + wgpu::TexelCopyBufferLayout::default(), + wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: 1, + }, + ); + }, + Some("Texture with '' label is invalid"), + ); + }); diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index f069784ea..e29290898 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -922,6 +922,8 @@ impl Queue { self.same_device_as(dst.as_ref())?; + dst.check_valid()?; + dst.check_usage(wgt::TextureUsages::COPY_DST) .map_err(TransferError::MissingTextureUsage)?; @@ -1171,6 +1173,8 @@ impl Queue { aspect: destination.aspect, }; + dst.check_valid()?; + if !conv::is_valid_external_image_copy_dst_texture_format(dst.desc.format) { return Err( TransferError::ExternalCopyToForbiddenTextureFormat(dst.desc.format).into(), diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 7bd7c3c7b..efc77fe94 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -3818,6 +3818,7 @@ impl Device { use crate::binding_model::CreateBindGroupError as Error; view.same_device(self)?; + view.check_valid()?; let internal_use = self.texture_use_parameters(binding, decl, view, "SampledTexture")?; used.views.insert_single(view.clone(), internal_use); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 234acab0e..59c7193b9 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -2645,7 +2645,7 @@ impl QuerySet { desc: desc.clone().map_label(|_| ()), initialized_slots: Mutex::new( rank::QUERY_SET_INITIALIZED_SLOTS, - bit_vec::BitVec::from_elem(desc.count as usize, false), + bit_vec::BitVec::new(), ), device, }) diff --git a/wgpu-types/src/origin_extent.rs b/wgpu-types/src/origin_extent.rs index 5f871c3ab..d3d9a5b72 100644 --- a/wgpu-types/src/origin_extent.rs +++ b/wgpu-types/src/origin_extent.rs @@ -177,15 +177,17 @@ impl Extent3d { #[must_use] pub fn mip_level_size(&self, level: u32, dim: TextureDimension) -> Self { Self { - width: u32::max(1, self.width >> level), + width: u32::max(1, self.width.unbounded_shr(level)), height: match dim { TextureDimension::D1 => 1, - _ => u32::max(1, self.height >> level), + _ => u32::max(1, self.height.unbounded_shr(level)), }, depth_or_array_layers: match dim { TextureDimension::D1 => 1, TextureDimension::D2 => self.depth_or_array_layers, - TextureDimension::D3 => u32::max(1, self.depth_or_array_layers >> level), + TextureDimension::D3 => { + u32::max(1, self.depth_or_array_layers.unbounded_shr(level)) + } }, } }