From c660aa9fce1a15b62d12ac59738c3d030a804b4f Mon Sep 17 00:00:00 2001 From: Patrick Ventuzelo Date: Tue, 17 Sep 2019 17:42:06 +0200 Subject: [PATCH] fix 653 panic in memoryDescriptor --- lib/emscripten/src/lib.rs | 6 +----- lib/runtime-core/src/memory/mod.rs | 2 +- lib/runtime-core/src/parse.rs | 23 +++++++++++++---------- lib/runtime-core/src/types.rs | 24 +++++++++++++++++++----- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index ba25b4be8..0d30c0b0b 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -473,11 +473,7 @@ impl EmscriptenGlobals { let (memory_min, memory_max, shared) = get_emscripten_memory_size(&module)?; // Memory initialization - let memory_type = MemoryDescriptor { - minimum: memory_min, - maximum: memory_max, - shared: shared, - }; + let memory_type = MemoryDescriptor::new(memory_min, memory_max, shared)?; let memory = Memory::new(memory_type).unwrap(); let table_type = TableDescriptor { diff --git a/lib/runtime-core/src/memory/mod.rs b/lib/runtime-core/src/memory/mod.rs index 75e9ea007..6f6af5c65 100644 --- a/lib/runtime-core/src/memory/mod.rs +++ b/lib/runtime-core/src/memory/mod.rs @@ -177,7 +177,7 @@ impl fmt::Debug for Memory { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum MemoryType { Dynamic, Static, diff --git a/lib/runtime-core/src/parse.rs b/lib/runtime-core/src/parse.rs index 259a628dd..9162c5169 100644 --- a/lib/runtime-core/src/parse.rs +++ b/lib/runtime-core/src/parse.rs @@ -136,11 +136,13 @@ pub fn read_module< .push((import_name, table_desc)); } ImportSectionEntryType::Memory(memory_ty) => { - let mem_desc = MemoryDescriptor { - minimum: Pages(memory_ty.limits.initial), - maximum: memory_ty.limits.maximum.map(|max| Pages(max)), - shared: memory_ty.shared, - }; + let mem_desc = MemoryDescriptor::new( + Pages(memory_ty.limits.initial), + memory_ty.limits.maximum.map(|max| Pages(max)), + memory_ty.shared, + ) + .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; + info.write() .unwrap() .imported_memories @@ -172,11 +174,12 @@ pub fn read_module< info.write().unwrap().tables.push(table_desc); } ParserState::MemorySectionEntry(memory_ty) => { - let mem_desc = MemoryDescriptor { - minimum: Pages(memory_ty.limits.initial), - maximum: memory_ty.limits.maximum.map(|max| Pages(max)), - shared: memory_ty.shared, - }; + let mem_desc = MemoryDescriptor::new( + Pages(memory_ty.limits.initial), + memory_ty.limits.maximum.map(|max| Pages(max)), + memory_ty.shared, + ) + .map_err(|x| LoadError::Codegen(format!("{:?}", x)))?; info.write().unwrap().memories.push(mem_desc); } diff --git a/lib/runtime-core/src/types.rs b/lib/runtime-core/src/types.rs index ab7c023a8..bea89ffe3 100644 --- a/lib/runtime-core/src/types.rs +++ b/lib/runtime-core/src/types.rs @@ -326,7 +326,7 @@ pub struct GlobalInit { pub init: Initializer, } -/// A wasm memory. +/// A wasm memory descriptor. #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] pub struct MemoryDescriptor { /// The minimum number of allowed pages. @@ -335,16 +335,30 @@ pub struct MemoryDescriptor { pub maximum: Option, /// This memory can be shared between wasm threads. pub shared: bool, + /// The type of the memory + pub memory_type: MemoryType, } impl MemoryDescriptor { - pub fn memory_type(self) -> MemoryType { - match (self.maximum.is_some(), self.shared) { + pub fn new(minimum: Pages, maximum: Option, shared: bool) -> Result { + let memory_type = match (maximum.is_some(), shared) { (true, true) => MemoryType::SharedStatic, (true, false) => MemoryType::Static, (false, false) => MemoryType::Dynamic, - (false, true) => panic!("shared memory without a max is not allowed"), - } + (false, true) => { + return Err("Max number of pages is required for shared memory".to_string()); + } + }; + Ok(MemoryDescriptor { + minimum, + maximum, + shared, + memory_type, + }) + } + + pub fn memory_type(&self) -> MemoryType { + self.memory_type } pub(crate) fn fits_in_imported(&self, imported: MemoryDescriptor) -> bool {