fix 653 panic in memoryDescriptor

This commit is contained in:
Patrick Ventuzelo
2019-09-17 17:42:06 +02:00
parent 90dfb0f7a7
commit c660aa9fce
4 changed files with 34 additions and 21 deletions

View File

@ -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 {

View File

@ -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,

View File

@ -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);
}

View File

@ -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<Pages>,
/// 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<Pages>, shared: bool) -> Result<Self, String> {
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 {