wgpu-core: a destroyed buffer that is mapped keeps its raw buffer until it drops

`Buffer::destroy` freed the raw buffer of a buffer that was still mapped. So did
`release_gpu_resources`, which runs `destroy` on every buffer when a device is lost or destroyed.
The mapped pointer, and a `BufferView` over it, then read freed memory.

A buffer that is mapped when it is destroyed now keeps what `destroy` took from it, and frees it
when the buffer drops. The buffer is destroyed in every other way: it has no raw buffer to use, and
`unmap` still reports it as destroyed.
This commit is contained in:
Abel Lucas authored and Teodor Tanasoaia committed 2026-10-26 19:17:54 +01:00
1 parent a0bd44d7ff
commit 3302d5072d
3 files changed
+15 -3

No files matched your search

+1
View File
@@ -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)
+2
View File
@@ -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()),
+12 -3
View File
@@ -515,6 +515,9 @@ pub struct Buffer {
pub(crate) label: String,
pub(crate) tracking_data: TrackingData,
pub(crate) map_state: Mutex<BufferMapState>,
/// 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<DestroyedBuffer>,
// Bind groups that reference this buffer. May contain duplicates.
pub(crate) bind_groups: Mutex<WeakVec<BindGroup>>,
pub(crate) timestamp_normalization_bind_group: Snatchable<TimestampNormalizationBindGroup>,
@@ -1074,7 +1077,7 @@ impl Buffer {
pub fn destroy(self: &Arc<Self>) {
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;
};