Skip to content

Commit c97d22f

Browse files
fix(core): Add some missing validity checks (#9913)
1 parent 0eb5b62 commit c97d22f

5 files changed

Lines changed: 62 additions & 7 deletions

File tree

tests/tests/wgpu-gpu/resource_error.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use wgpu_test::{fail, gpu_test, valid, GpuTestConfiguration, GpuTestInitializer, TestParameters};
22

33
pub fn all_tests(vec: &mut Vec<GpuTestInitializer>) {
4-
vec.extend([BAD_BUFFER, BAD_TEXTURE]);
4+
vec.extend([BAD_BUFFER_MAP, BAD_TEXTURE_CREATE_VIEW, BAD_TEXTURE_WRITE]);
55
}
66

77
#[gpu_test]
8-
static BAD_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new()
8+
static BAD_BUFFER_MAP: GpuTestConfiguration = GpuTestConfiguration::new()
99
.parameters(TestParameters::default().enable_noop())
1010
.run_sync(|ctx| {
1111
// Create a buffer with bad parameters and call a few methods.
@@ -38,7 +38,7 @@ static BAD_BUFFER: GpuTestConfiguration = GpuTestConfiguration::new()
3838
});
3939

4040
#[gpu_test]
41-
static BAD_TEXTURE: GpuTestConfiguration = GpuTestConfiguration::new()
41+
static BAD_TEXTURE_CREATE_VIEW: GpuTestConfiguration = GpuTestConfiguration::new()
4242
.parameters(TestParameters::default().enable_noop())
4343
.run_sync(|ctx| {
4444
let texture = fail(
@@ -72,3 +72,51 @@ static BAD_TEXTURE: GpuTestConfiguration = GpuTestConfiguration::new()
7272
valid(&ctx.device, || texture.destroy());
7373
valid(&ctx.device, || texture.destroy());
7474
});
75+
76+
#[gpu_test]
77+
static BAD_TEXTURE_WRITE: GpuTestConfiguration = GpuTestConfiguration::new()
78+
.parameters(TestParameters::default().enable_noop())
79+
.run_sync(|ctx| {
80+
let texture = fail(
81+
&ctx.device,
82+
|| {
83+
ctx.device.create_texture(&wgpu::TextureDescriptor {
84+
label: None,
85+
size: wgpu::Extent3d {
86+
width: 256,
87+
height: 256,
88+
depth_or_array_layers: 1,
89+
},
90+
mip_level_count: 5888,
91+
sample_count: 1,
92+
dimension: wgpu::TextureDimension::D2,
93+
format: wgpu::TextureFormat::Rgba8Unorm,
94+
usage: wgpu::TextureUsages::COPY_DST,
95+
view_formats: &[],
96+
})
97+
},
98+
Some("mip level count"),
99+
);
100+
101+
fail(
102+
&ctx.device,
103+
|| {
104+
ctx.queue.write_texture(
105+
wgpu::TexelCopyTextureInfo {
106+
texture: &texture,
107+
mip_level: 255,
108+
origin: wgpu::Origin3d::ZERO,
109+
aspect: wgpu::TextureAspect::All,
110+
},
111+
&[0u8; 4],
112+
wgpu::TexelCopyBufferLayout::default(),
113+
wgpu::Extent3d {
114+
width: 1,
115+
height: 1,
116+
depth_or_array_layers: 1,
117+
},
118+
);
119+
},
120+
Some("Texture with '' label is invalid"),
121+
);
122+
});

wgpu-core/src/device/queue.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,8 @@ impl Queue {
922922

923923
self.same_device_as(dst.as_ref())?;
924924

925+
dst.check_valid()?;
926+
925927
dst.check_usage(wgt::TextureUsages::COPY_DST)
926928
.map_err(TransferError::MissingTextureUsage)?;
927929

@@ -1171,6 +1173,8 @@ impl Queue {
11711173
aspect: destination.aspect,
11721174
};
11731175

1176+
dst.check_valid()?;
1177+
11741178
if !conv::is_valid_external_image_copy_dst_texture_format(dst.desc.format) {
11751179
return Err(
11761180
TransferError::ExternalCopyToForbiddenTextureFormat(dst.desc.format).into(),

wgpu-core/src/device/resource.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,6 +3818,7 @@ impl Device {
38183818
use crate::binding_model::CreateBindGroupError as Error;
38193819

38203820
view.same_device(self)?;
3821+
view.check_valid()?;
38213822

38223823
let internal_use = self.texture_use_parameters(binding, decl, view, "SampledTexture")?;
38233824
used.views.insert_single(view.clone(), internal_use);

wgpu-core/src/resource.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2645,7 +2645,7 @@ impl QuerySet {
26452645
desc: desc.clone().map_label(|_| ()),
26462646
initialized_slots: Mutex::new(
26472647
rank::QUERY_SET_INITIALIZED_SLOTS,
2648-
bit_vec::BitVec::from_elem(desc.count as usize, false),
2648+
bit_vec::BitVec::new(),
26492649
),
26502650
device,
26512651
})

wgpu-types/src/origin_extent.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,17 @@ impl Extent3d {
177177
#[must_use]
178178
pub fn mip_level_size(&self, level: u32, dim: TextureDimension) -> Self {
179179
Self {
180-
width: u32::max(1, self.width >> level),
180+
width: u32::max(1, self.width.unbounded_shr(level)),
181181
height: match dim {
182182
TextureDimension::D1 => 1,
183-
_ => u32::max(1, self.height >> level),
183+
_ => u32::max(1, self.height.unbounded_shr(level)),
184184
},
185185
depth_or_array_layers: match dim {
186186
TextureDimension::D1 => 1,
187187
TextureDimension::D2 => self.depth_or_array_layers,
188-
TextureDimension::D3 => u32::max(1, self.depth_or_array_layers >> level),
188+
TextureDimension::D3 => {
189+
u32::max(1, self.depth_or_array_layers.unbounded_shr(level))
190+
}
189191
},
190192
}
191193
}

0 commit comments

Comments
 (0)