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).
This commit is contained in:
Andrew Brown
2022-06-21 12:55:27 -07:00
committed by GitHub
parent 651f40855f
commit b2e03ae873

View File

@@ -470,7 +470,7 @@ impl SharedMemory {
pub fn new(plan: MemoryPlan) -> Result<Self> { pub fn new(plan: MemoryPlan) -> Result<Self> {
let (minimum_bytes, maximum_bytes) = Memory::limit_new(&plan, None)?; let (minimum_bytes, maximum_bytes) = Memory::limit_new(&plan, None)?;
let mmap_memory = MmapMemory::new(&plan, minimum_bytes, maximum_bytes, 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]. /// Wrap an existing [Memory] with the locking provided by a [SharedMemory].
@@ -478,19 +478,23 @@ impl SharedMemory {
plan: &MemoryPlan, plan: &MemoryPlan,
mut memory: Box<dyn RuntimeLinearMemory>, mut memory: Box<dyn RuntimeLinearMemory>,
ty: wasmtime_environ::Memory, ty: wasmtime_environ::Memory,
) -> Self { ) -> Result<Self> {
assert!(ty.shared); if !ty.shared {
assert!(matches!(plan.style, MemoryStyle::Static { .. })); 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!( assert!(
memory.as_any_mut().type_id() != std::any::TypeId::of::<SharedMemory>(), memory.as_any_mut().type_id() != std::any::TypeId::of::<SharedMemory>(),
"cannot re-wrap a shared memory" "cannot re-wrap a shared memory"
); );
let def = LongTermVMMemoryDefinition(memory.vmmemory()); let def = LongTermVMMemoryDefinition(memory.vmmemory());
Self(Arc::new(RwLock::new(SharedMemoryInner { Ok(Self(Arc::new(RwLock::new(SharedMemoryInner {
memory: memory, memory: memory,
ty, ty,
def, def,
}))) }))))
} }
/// Return the memory type for this [`SharedMemory`]. /// Return the memory type for this [`SharedMemory`].
@@ -613,7 +617,7 @@ impl Memory {
let (minimum, maximum) = Self::limit_new(plan, Some(store))?; let (minimum, maximum) = Self::limit_new(plan, Some(store))?;
let allocation = creator.new_memory(plan, minimum, maximum, memory_image)?; let allocation = creator.new_memory(plan, minimum, maximum, memory_image)?;
let allocation = if plan.memory.shared { let allocation = if plan.memory.shared {
Box::new(SharedMemory::wrap(plan, allocation, plan.memory)) Box::new(SharedMemory::wrap(plan, allocation, plan.memory)?)
} else { } else {
allocation allocation
}; };