Skip to content

Commit f25e07b

Browse files
nicalteoxoy
authored andcommitted
Fix soundness issue with Snatchable
The code was written with the incorrect assumption that if no lifetime is specified in a method definition, then all lifetimes are elided to the lifetime of self. In fact only lifetimes in the returned value are elided to the lifetime of self, and other parameters get their own lifetimes. Kudos to @teoxoy for catching the issue!
1 parent 0a76c0f commit f25e07b

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

wgpu-core/src/device/queue.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -1472,23 +1472,25 @@ impl Global {
14721472
)
14731473
.collect::<Vec<_>>();
14741474

1475-
let mut submit_surface_textures =
1476-
SmallVec::<[_; 2]>::with_capacity(submit_surface_textures_owned.len());
1477-
1478-
for texture in submit_surface_textures_owned.values() {
1479-
submit_surface_textures.extend(match texture.inner.get(&snatch_guard) {
1480-
Some(TextureInner::Surface { raw, .. }) => raw.as_ref(),
1481-
_ => None,
1482-
});
1483-
}
1475+
{
1476+
let mut submit_surface_textures =
1477+
SmallVec::<[_; 2]>::with_capacity(submit_surface_textures_owned.len());
1478+
1479+
for texture in submit_surface_textures_owned.values() {
1480+
submit_surface_textures.extend(match texture.inner.get(&snatch_guard) {
1481+
Some(TextureInner::Surface { raw, .. }) => raw.as_ref(),
1482+
_ => None,
1483+
});
1484+
}
14841485

1485-
unsafe {
1486-
queue
1487-
.raw
1488-
.as_ref()
1489-
.unwrap()
1490-
.submit(&refs, &submit_surface_textures, (fence, submit_index))
1491-
.map_err(DeviceError::from)?;
1486+
unsafe {
1487+
queue
1488+
.raw
1489+
.as_ref()
1490+
.unwrap()
1491+
.submit(&refs, &submit_surface_textures, (fence, submit_index))
1492+
.map_err(DeviceError::from)?;
1493+
}
14921494
}
14931495

14941496
profiling::scope!("cleanup");

wgpu-core/src/resource.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<A: HalApi> Drop for Buffer<A> {
467467
}
468468

469469
impl<A: HalApi> Buffer<A> {
470-
pub(crate) fn raw(&self, guard: &SnatchGuard) -> Option<&A::Buffer> {
470+
pub(crate) fn raw<'a>(&'a self, guard: &'a SnatchGuard) -> Option<&'a A::Buffer> {
471471
self.raw.get(guard)
472472
}
473473

@@ -1054,7 +1054,7 @@ impl<A: HalApi> Texture<A> {
10541054

10551055
pub(crate) fn inner_mut<'a>(
10561056
&'a self,
1057-
guard: &mut ExclusiveSnatchGuard,
1057+
guard: &'a mut ExclusiveSnatchGuard,
10581058
) -> Option<&'a mut TextureInner<A>> {
10591059
self.inner.get_mut(guard)
10601060
}
@@ -1153,10 +1153,8 @@ impl Global {
11531153
let buffer_opt = { hub.buffers.try_get(id).ok().flatten() };
11541154
let buffer = buffer_opt.as_ref().unwrap();
11551155

1156-
let hal_buffer = {
1157-
let snatch_guard = buffer.device.snatchable_lock.read();
1158-
buffer.raw(&snatch_guard)
1159-
};
1156+
let snatch_guard = buffer.device.snatchable_lock.read();
1157+
let hal_buffer = buffer.raw(&snatch_guard);
11601158

11611159
hal_buffer_callback(hal_buffer)
11621160
}

wgpu-core/src/snatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ impl<T> Snatchable<T> {
3333
}
3434

3535
/// Get read access to the value. Requires a the snatchable lock's read guard.
36-
pub fn get(&self, _guard: &SnatchGuard) -> Option<&T> {
36+
pub fn get<'a>(&'a self, _guard: &'a SnatchGuard) -> Option<&'a T> {
3737
unsafe { (*self.value.get()).as_ref() }
3838
}
3939

4040
/// Get write access to the value. Requires a the snatchable lock's write guard.
41-
pub fn get_mut(&self, _guard: &mut ExclusiveSnatchGuard) -> Option<&mut T> {
41+
pub fn get_mut<'a>(&'a self, _guard: &'a mut ExclusiveSnatchGuard) -> Option<&'a mut T> {
4242
unsafe { (*self.value.get()).as_mut() }
4343
}
4444

0 commit comments

Comments
 (0)