A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix `MemoryType::default_value` with shared memories (#12029)
Alex Crichton874da6779518
diff --git a/crates/fuzzing/src/oracles/dummy.rs b/crates/fuzzing/src/oracles/dummy.rsindex c5790e759e..16a66e7834 100644--- a/crates/fuzzing/src/oracles/dummy.rs+++ b/crates/fuzzing/src/oracles/dummy.rs@@ -59,7 +59,11 @@ mod tests {fn dummy_memory_import() {let mut store = store();let memory_type = MemoryType::new(1, None);- let memory = memory_type.default_value(&mut store).unwrap();+ let memory = memory_type+ .default_value(&mut store)+ .unwrap()+ .into_memory()+ .unwrap();assert_eq!(memory.size(&store), 1);}diff --git a/crates/wasmtime/src/runtime/types.rs b/crates/wasmtime/src/runtime/types.rsindex f8826b92d5..4a67e6668f 100644--- a/crates/wasmtime/src/runtime/types.rs+++ b/crates/wasmtime/src/runtime/types.rs@@ -1,5 +1,4 @@use crate::prelude::*;-use crate::runtime::Memory as RuntimeMemory;use crate::runtime::externals::Global as RuntimeGlobal;use crate::runtime::externals::Table as RuntimeTable;use crate::runtime::externals::Tag as RuntimeTag;@@ -1526,7 +1525,7 @@ impl ExternType {ExternType::Func(func_ty) => func_ty.default_value(store).map(Extern::Func),ExternType::Global(global_ty) => global_ty.default_value(store).map(Extern::Global),ExternType::Table(table_ty) => table_ty.default_value(store).map(Extern::Table),- ExternType::Memory(mem_ty) => mem_ty.default_value(store).map(Extern::Memory),+ ExternType::Memory(mem_ty) => mem_ty.default_value(store),ExternType::Tag(tag_ty) => tag_ty.default_value(store).map(Extern::Tag),}}@@ -3494,12 +3493,26 @@ impl MemoryType {pub(crate) fn wasmtime_memory(&self) -> &Memory {&self.ty}- /// Construct a new memory import initialized to this memory type’s default state+ /// Construct a new memory import initialized to this memory type’s default+ /// state.///- /// Returns a host `Memory` in the given store with the configured initial- /// page size and zeroed contents.- pub fn default_value(&self, store: impl AsContextMut) -> Result<RuntimeMemory> {- RuntimeMemory::new(store, self.clone())+ /// Returns a host `Memory` or `SharedMemory` depending on if this is a+ /// shared memory type or not. The memory's type will have the same type as+ /// `self` and the initial contents of the memory, if any, will be all zero.+ pub fn default_value(&self, store: impl AsContextMut) -> Result<Extern> {+ Ok(if self.is_shared() {+ #[cfg(feature = "threads")]+ {+ let store = store.as_context();+ Extern::SharedMemory(crate::SharedMemory::new(store.engine(), self.clone())?)+ }+ #[cfg(not(feature = "threads"))]+ {+ bail!("creation of shared memories disabled at compile time")+ }+ } else {+ Extern::Memory(crate::Memory::new(store, self.clone())?)+ })}}