Skip to content

Commit e90b1fb

Browse files
authored
Make GcRuntime::take_memory a safe method (#11410)
This cannot be an `unsafe` method as it's not possible to provide the guarantee that the memory is placed back in the store (e.g. `forget`-ing futures). Instead make the method more strict and say that panics will happen if `replace_memory` isn't called. Existing implementations should already adhere to this.
1 parent 28f9783 commit e90b1fb

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

crates/wasmtime/src/runtime/store/gc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl StoreOpaque {
8383

8484
// Take the GC heap's underlying memory out of the GC heap, attempt to
8585
// grow it, then replace it.
86-
let mut memory = unsafe { self.unwrap_gc_store_mut().gc_heap.take_memory() };
86+
let mut memory = self.unwrap_gc_store_mut().gc_heap.take_memory();
8787
let mut delta_bytes_grown = 0;
8888
let grow_result: Result<()> = (|| {
8989
let page_size = self.engine().tunables().gc_heap_memory_type().page_size();

crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ unsafe impl GcHeap for DrcHeap {
935935
ptr.cast()
936936
}
937937

938-
unsafe fn take_memory(&mut self) -> crate::vm::Memory {
938+
fn take_memory(&mut self) -> crate::vm::Memory {
939939
debug_assert!(self.is_attached());
940940
self.vmmemory.take();
941941
self.memory.take().unwrap()

crates/wasmtime/src/runtime/vm/gc/enabled/null.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ unsafe impl GcHeap for NullHeap {
219219
self.no_gc_count -= 1;
220220
}
221221

222-
unsafe fn take_memory(&mut self) -> crate::vm::Memory {
222+
fn take_memory(&mut self) -> crate::vm::Memory {
223223
debug_assert!(self.is_attached());
224224
self.memory.take().unwrap()
225225
}

crates/wasmtime/src/runtime/vm/gc/gc_runtime.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,11 @@ pub unsafe trait GcHeap: 'static + Send + Sync {
423423

424424
/// Take the underlying memory storage out of this GC heap.
425425
///
426-
/// # Safety
426+
/// # Panics
427427
///
428-
/// You may not use this GC heap again until after you replace the memory.
429-
unsafe fn take_memory(&mut self) -> crate::vm::Memory;
428+
/// If this GC heap is used while the memory is taken then a panic will
429+
/// occur. This will also panic if the memory is already taken.
430+
fn take_memory(&mut self) -> crate::vm::Memory;
430431

431432
/// Replace this GC heap's underlying memory storage.
432433
///

0 commit comments

Comments
 (0)