diff --git a/src/webassembly/instance.rs b/src/webassembly/instance.rs index fc218ceb5..4b82d3ee5 100644 --- a/src/webassembly/instance.rs +++ b/src/webassembly/instance.rs @@ -392,12 +392,20 @@ impl Instance { // Instantiate memories { // Allocate the underlying memory and initialize it to all zeros. - memories.reserve_exact(module.info.memories.len()); - for memory in &module.info.memories { - let memory = memory.entity; - let v = - LinearMemory::new(memory.pages_count as u32, memory.maximum.map(|m| m as u32)); - memories.push(v); + let total_memories = module.info.memories.len(); + if total_memories > 0 { + memories.reserve_exact(total_memories); + for memory in &module.info.memories { + let memory = memory.entity; + let v = LinearMemory::new( + memory.pages_count as u32, + memory.maximum.map(|m| m as u32), + ); + memories.push(v); + } + } else { + memories.reserve_exact(1); + memories.push(LinearMemory::new(0, None)); } for init in &module.info.data_initializers { debug_assert!(init.base.is_none(), "globalvar base not supported yet"); diff --git a/src/webassembly/memory.rs b/src/webassembly/memory.rs index 68e44291b..dcc5b816e 100644 --- a/src/webassembly/memory.rs +++ b/src/webassembly/memory.rs @@ -40,7 +40,7 @@ impl LinearMemory { Some(val) => val as u64, None => initial as u64, }; - let len = if len == 0 { 1 } else { len }; + let len = if len == 0 { PAGE_SIZE as u64 } else { len }; let mmap = MmapMut::map_anon(len as usize).unwrap(); debug!("LinearMemory instantiated");