From b2e03ae873cce1cfc70736e865e2d88a781316da Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 21 Jun 2022 12:55:27 -0700 Subject: [PATCH] shared memory: change some assertions to returned errors (#4292) Previously, @alexcrichton had mentioned that some of these assertions should be bubbled up as errors. This change re-factors two such assertions, leaving others in this file as assertions since they represent code paths that we should avoid internally (not by external users). --- crates/runtime/src/memory.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/runtime/src/memory.rs b/crates/runtime/src/memory.rs index 6fb822e153..90a0ae9f48 100644 --- a/crates/runtime/src/memory.rs +++ b/crates/runtime/src/memory.rs @@ -470,7 +470,7 @@ impl SharedMemory { pub fn new(plan: MemoryPlan) -> Result { let (minimum_bytes, maximum_bytes) = Memory::limit_new(&plan, None)?; let mmap_memory = MmapMemory::new(&plan, minimum_bytes, maximum_bytes, None)?; - Ok(Self::wrap(&plan, Box::new(mmap_memory), plan.memory)) + Self::wrap(&plan, Box::new(mmap_memory), plan.memory) } /// Wrap an existing [Memory] with the locking provided by a [SharedMemory]. @@ -478,19 +478,23 @@ impl SharedMemory { plan: &MemoryPlan, mut memory: Box, ty: wasmtime_environ::Memory, - ) -> Self { - assert!(ty.shared); - assert!(matches!(plan.style, MemoryStyle::Static { .. })); + ) -> Result { + if !ty.shared { + bail!("shared memory must have a `shared` memory type"); + } + if !matches!(plan.style, MemoryStyle::Static { .. }) { + bail!("shared memory can only be built from a static memory allocation") + } assert!( memory.as_any_mut().type_id() != std::any::TypeId::of::(), "cannot re-wrap a shared memory" ); let def = LongTermVMMemoryDefinition(memory.vmmemory()); - Self(Arc::new(RwLock::new(SharedMemoryInner { + Ok(Self(Arc::new(RwLock::new(SharedMemoryInner { memory: memory, ty, def, - }))) + })))) } /// Return the memory type for this [`SharedMemory`]. @@ -613,7 +617,7 @@ impl Memory { let (minimum, maximum) = Self::limit_new(plan, Some(store))?; let allocation = creator.new_memory(plan, minimum, maximum, memory_image)?; let allocation = if plan.memory.shared { - Box::new(SharedMemory::wrap(plan, allocation, plan.memory)) + Box::new(SharedMemory::wrap(plan, allocation, plan.memory)?) } else { allocation };