mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-15 18:01:22 +00:00
fix 653 panic in memoryDescriptor
This commit is contained in:
@ -473,11 +473,7 @@ impl EmscriptenGlobals {
|
|||||||
let (memory_min, memory_max, shared) = get_emscripten_memory_size(&module)?;
|
let (memory_min, memory_max, shared) = get_emscripten_memory_size(&module)?;
|
||||||
|
|
||||||
// Memory initialization
|
// Memory initialization
|
||||||
let memory_type = MemoryDescriptor {
|
let memory_type = MemoryDescriptor::new(memory_min, memory_max, shared)?;
|
||||||
minimum: memory_min,
|
|
||||||
maximum: memory_max,
|
|
||||||
shared: shared,
|
|
||||||
};
|
|
||||||
let memory = Memory::new(memory_type).unwrap();
|
let memory = Memory::new(memory_type).unwrap();
|
||||||
|
|
||||||
let table_type = TableDescriptor {
|
let table_type = TableDescriptor {
|
||||||
|
@ -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 {
|
pub enum MemoryType {
|
||||||
Dynamic,
|
Dynamic,
|
||||||
Static,
|
Static,
|
||||||
|
@ -136,11 +136,13 @@ pub fn read_module<
|
|||||||
.push((import_name, table_desc));
|
.push((import_name, table_desc));
|
||||||
}
|
}
|
||||||
ImportSectionEntryType::Memory(memory_ty) => {
|
ImportSectionEntryType::Memory(memory_ty) => {
|
||||||
let mem_desc = MemoryDescriptor {
|
let mem_desc = MemoryDescriptor::new(
|
||||||
minimum: Pages(memory_ty.limits.initial),
|
Pages(memory_ty.limits.initial),
|
||||||
maximum: memory_ty.limits.maximum.map(|max| Pages(max)),
|
memory_ty.limits.maximum.map(|max| Pages(max)),
|
||||||
shared: memory_ty.shared,
|
memory_ty.shared,
|
||||||
};
|
)
|
||||||
|
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
|
||||||
|
|
||||||
info.write()
|
info.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.imported_memories
|
.imported_memories
|
||||||
@ -172,11 +174,12 @@ pub fn read_module<
|
|||||||
info.write().unwrap().tables.push(table_desc);
|
info.write().unwrap().tables.push(table_desc);
|
||||||
}
|
}
|
||||||
ParserState::MemorySectionEntry(memory_ty) => {
|
ParserState::MemorySectionEntry(memory_ty) => {
|
||||||
let mem_desc = MemoryDescriptor {
|
let mem_desc = MemoryDescriptor::new(
|
||||||
minimum: Pages(memory_ty.limits.initial),
|
Pages(memory_ty.limits.initial),
|
||||||
maximum: memory_ty.limits.maximum.map(|max| Pages(max)),
|
memory_ty.limits.maximum.map(|max| Pages(max)),
|
||||||
shared: memory_ty.shared,
|
memory_ty.shared,
|
||||||
};
|
)
|
||||||
|
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
|
||||||
|
|
||||||
info.write().unwrap().memories.push(mem_desc);
|
info.write().unwrap().memories.push(mem_desc);
|
||||||
}
|
}
|
||||||
|
@ -326,7 +326,7 @@ pub struct GlobalInit {
|
|||||||
pub init: Initializer,
|
pub init: Initializer,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A wasm memory.
|
/// A wasm memory descriptor.
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct MemoryDescriptor {
|
pub struct MemoryDescriptor {
|
||||||
/// The minimum number of allowed pages.
|
/// The minimum number of allowed pages.
|
||||||
@ -335,16 +335,30 @@ pub struct MemoryDescriptor {
|
|||||||
pub maximum: Option<Pages>,
|
pub maximum: Option<Pages>,
|
||||||
/// This memory can be shared between wasm threads.
|
/// This memory can be shared between wasm threads.
|
||||||
pub shared: bool,
|
pub shared: bool,
|
||||||
|
/// The type of the memory
|
||||||
|
pub memory_type: MemoryType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryDescriptor {
|
impl MemoryDescriptor {
|
||||||
pub fn memory_type(self) -> MemoryType {
|
pub fn new(minimum: Pages, maximum: Option<Pages>, shared: bool) -> Result<Self, String> {
|
||||||
match (self.maximum.is_some(), self.shared) {
|
let memory_type = match (maximum.is_some(), shared) {
|
||||||
(true, true) => MemoryType::SharedStatic,
|
(true, true) => MemoryType::SharedStatic,
|
||||||
(true, false) => MemoryType::Static,
|
(true, false) => MemoryType::Static,
|
||||||
(false, false) => MemoryType::Dynamic,
|
(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 {
|
pub(crate) fn fits_in_imported(&self, imported: MemoryDescriptor) -> bool {
|
||||||
|
Reference in New Issue
Block a user