Check if memory description fits imported memory

This commit is contained in:
Lachlan Sneff
2019-01-12 16:45:32 -05:00
parent 80a1fce329
commit 60201ea7a6
4 changed files with 35 additions and 28 deletions

View File

@@ -195,30 +195,37 @@ fn import_memories(
vmctx: *mut vm::Ctx,
) -> Result<Box<[vm::ImportedMemory]>, String> {
let mut memories = Vec::with_capacity(module.imported_memories.len());
for (_index, (ImportName { namespace, name }, _memory)) in &module.imported_memories {
for (_index, (ImportName { namespace, name }, expected_memory_desc)) in &module.imported_memories {
let memory_import = imports.get(namespace, name);
match memory_import {
Some(Export::Memory {
local,
ctx,
memory: _,
memory: memory_desc,
}) => {
memories.push(vm::ImportedMemory {
memory: local,
vmctx: match ctx {
Context::External(ctx) => ctx,
Context::Internal => vmctx,
},
});
if expected_memory_desc.fits_in_imported(&memory_desc) {
memories.push(vm::ImportedMemory {
memory: local,
vmctx: match ctx {
Context::External(ctx) => ctx,
Context::Internal => vmctx,
},
});
} else {
return Err(format!(
"incorrect memory description for {}:{}",
namespace, name,
));
}
}
Some(_) => {
return Err(format!(
"incorrect import memory type for {}:{}",
"incorrect import type for {}:{}",
namespace, name
));
}
None => {
return Err(format!("memory not found: {}:{}", namespace, name));
return Err(format!("import not found: {}:{}", namespace, name));
}
}
}

View File

@@ -105,7 +105,7 @@ pub struct Global {
}
/// A wasm memory.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Memory {
/// The minimum number of allowed pages.
pub min: u32,
@@ -119,6 +119,12 @@ impl Memory {
pub fn is_static_heap(&self) -> bool {
self.max.is_some()
}
pub(crate) fn fits_in_imported(&self, imported: &Memory) -> bool {
self.shared == imported.shared
&& self.max == imported.max
&& self.min <= imported.min
}
}
/// A wasm func.