diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c4d1004..70085bf63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Bottom level categories: - Explicitly implement `Send` and `Sync` in `wgpu-core` to reduce the risk of `recursion_limit` overflow. By @nazar-pc in [#9953](https://github.com/gfx-rs/wgpu/pull/9953) - Fix a texture id leak when surface acquire returns no texture. By @jnb in [#10184](https://github.com/gfx-rs/wgpu/pull/10184). +- Fix a use after free of a mapped buffer when its device is lost or destroyed. By @uael in [#10593](https://github.com/gfx-rs/wgpu/pull/10593). ## v30.0.1 (2026-08-21) diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 2e09f25f3..dc4de79ed 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -1130,6 +1130,7 @@ impl Device { BufferInitTracker::new(aligned_size), ), map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), + destroyed_while_mapped: OnceCellOrLock::new(), label: desc.label.to_string(), tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()), @@ -1327,6 +1328,7 @@ impl Device { BufferInitTracker::new(0), ), map_state: Mutex::new(rank::BUFFER_MAP_STATE, resource::BufferMapState::Idle), + destroyed_while_mapped: OnceCellOrLock::new(), label: desc.label.to_string(), tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()), bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()), diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index b97bbdf52..fc7150ac6 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -515,6 +515,9 @@ pub struct Buffer { pub(crate) label: String, pub(crate) tracking_data: TrackingData, pub(crate) map_state: Mutex, + /// What [`Buffer::destroy`] took from a buffer that was mapped. It is freed when the buffer + /// drops, since the mapped pointer, and the views over it, read the raw buffer until then. + pub(crate) destroyed_while_mapped: crate::OnceCellOrLock, // Bind groups that reference this buffer. May contain duplicates. pub(crate) bind_groups: Mutex>, pub(crate) timestamp_normalization_bind_group: Snatchable, @@ -1074,7 +1077,7 @@ impl Buffer { pub fn destroy(self: &Arc) { let device = &self.device; - let temp = { + let destroyed = { let mut snatch_guard = device.snatchable_lock.write(); let raw = match self.raw.snatch(&mut snatch_guard) { @@ -1100,16 +1103,22 @@ impl Buffer { mem::take(&mut *guard) }; - queue::TempResource::DestroyedBuffer(DestroyedBuffer { + DestroyedBuffer { raw: ManuallyDrop::new(raw), device: Arc::clone(&self.device), label: self.label().to_owned(), bind_groups, timestamp_normalization_bind_group, indirect_validation_bind_groups, - }) + } }; + if matches!(*self.map_state.lock(), BufferMapState::Active { .. }) { + let _ = self.destroyed_while_mapped.set(destroyed); + return; + } + let temp = queue::TempResource::DestroyedBuffer(destroyed); + let Some(queue) = device.get_queue() else { return; };