Skip to content

Commit 874da67

Browse files
authored
Fix MemoryType::default_value with shared memories (#12029)
* Fix `MemoryType::default_value` with shared memories This fixes fallout from #12022 which was detected during fuzzing which tried creating a shared memory. * Fix tests
1 parent 3be0757 commit 874da67

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

crates/fuzzing/src/oracles/dummy.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ mod tests {
5959
fn dummy_memory_import() {
6060
let mut store = store();
6161
let memory_type = MemoryType::new(1, None);
62-
let memory = memory_type.default_value(&mut store).unwrap();
62+
let memory = memory_type
63+
.default_value(&mut store)
64+
.unwrap()
65+
.into_memory()
66+
.unwrap();
6367
assert_eq!(memory.size(&store), 1);
6468
}
6569

crates/wasmtime/src/runtime/types.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::prelude::*;
2-
use crate::runtime::Memory as RuntimeMemory;
32
use crate::runtime::externals::Global as RuntimeGlobal;
43
use crate::runtime::externals::Table as RuntimeTable;
54
use crate::runtime::externals::Tag as RuntimeTag;
@@ -1526,7 +1525,7 @@ impl ExternType {
15261525
ExternType::Func(func_ty) => func_ty.default_value(store).map(Extern::Func),
15271526
ExternType::Global(global_ty) => global_ty.default_value(store).map(Extern::Global),
15281527
ExternType::Table(table_ty) => table_ty.default_value(store).map(Extern::Table),
1529-
ExternType::Memory(mem_ty) => mem_ty.default_value(store).map(Extern::Memory),
1528+
ExternType::Memory(mem_ty) => mem_ty.default_value(store),
15301529
ExternType::Tag(tag_ty) => tag_ty.default_value(store).map(Extern::Tag),
15311530
}
15321531
}
@@ -3494,12 +3493,26 @@ impl MemoryType {
34943493
pub(crate) fn wasmtime_memory(&self) -> &Memory {
34953494
&self.ty
34963495
}
3497-
/// Construct a new memory import initialized to this memory type’s default state
3496+
/// Construct a new memory import initialized to this memory type’s default
3497+
/// state.
34983498
///
3499-
/// Returns a host `Memory` in the given store with the configured initial
3500-
/// page size and zeroed contents.
3501-
pub fn default_value(&self, store: impl AsContextMut) -> Result<RuntimeMemory> {
3502-
RuntimeMemory::new(store, self.clone())
3499+
/// Returns a host `Memory` or `SharedMemory` depending on if this is a
3500+
/// shared memory type or not. The memory's type will have the same type as
3501+
/// `self` and the initial contents of the memory, if any, will be all zero.
3502+
pub fn default_value(&self, store: impl AsContextMut) -> Result<Extern> {
3503+
Ok(if self.is_shared() {
3504+
#[cfg(feature = "threads")]
3505+
{
3506+
let store = store.as_context();
3507+
Extern::SharedMemory(crate::SharedMemory::new(store.engine(), self.clone())?)
3508+
}
3509+
#[cfg(not(feature = "threads"))]
3510+
{
3511+
bail!("creation of shared memories disabled at compile time")
3512+
}
3513+
} else {
3514+
Extern::Memory(crate::Memory::new(store, self.clone())?)
3515+
})
35033516
}
35043517
}
35053518

0 commit comments

Comments
 (0)