This commit is contained in:
Lachlan Sneff 2019-01-29 14:15:59 -08:00
parent 581363119b
commit bc78738bb7
9 changed files with 96 additions and 40 deletions

View File

@ -219,7 +219,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
func.create_heap(ir::HeapData {
base: local_memory_base,
min_size: ((description.min as u64) * (WASM_PAGE_SIZE as u64)).into(),
min_size: ((description.minimum as u64) * (WASM_PAGE_SIZE as u64)).into(),
offset_guard_size: mem_type.guard_size().into(),
style: ir::HeapStyle::Dynamic {
bound_gv: local_memory_bound,
@ -230,7 +230,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
mem_type @ MemoryType::Static | mem_type @ MemoryType::SharedStatic => func
.create_heap(ir::HeapData {
base: local_memory_base,
min_size: ((description.min as u64) * (WASM_PAGE_SIZE as u64)).into(),
min_size: ((description.minimum as u64) * (WASM_PAGE_SIZE as u64)).into(),
offset_guard_size: mem_type.guard_size().into(),
style: ir::HeapStyle::Static {
bound: mem_type.bounds().unwrap().into(),
@ -327,7 +327,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
func.create_table(ir::TableData {
base_gv: table_base,
min_size: (description.min as u64).into(),
min_size: (description.minimum as u64).into(),
bound_gv: table_count,
element_size: (vm::Anyfunc::size() as u64).into(),
index_type: ir::types::I32,

View File

@ -180,8 +180,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
TableElementType::Func => ElementType::Anyfunc,
_ => unimplemented!(),
},
min: table.minimum,
max: table.maximum,
minimum: table.minimum,
maximum: table.maximum,
});
}
@ -204,8 +204,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
TableElementType::Func => ElementType::Anyfunc,
_ => unimplemented!(),
},
min: table.minimum,
max: table.maximum,
minimum: table.minimum,
maximum: table.maximum,
};
// Add import names to list of imported tables
@ -251,8 +251,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
/// Declares a memory to the environment
fn declare_memory(&mut self, memory: cranelift_wasm::Memory) {
self.module.memories.push(MemoryDescriptor {
min: memory.minimum,
max: memory.maximum,
minimum: memory.minimum,
maximum: memory.maximum,
shared: memory.shared,
});
}
@ -270,8 +270,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
};
let memory = MemoryDescriptor {
min: memory.minimum,
max: memory.maximum,
minimum: memory.minimum,
maximum: memory.maximum,
shared: memory.shared,
};

View File

@ -104,7 +104,7 @@ impl LocalBacking {
LocalOrImport::Local(local_memory_index) => {
let memory_desc = module.memories[local_memory_index];
let data_top = init_base + init.data.len();
assert!(memory_desc.min as usize * WASM_PAGE_SIZE >= data_top);
assert!(memory_desc.minimum as usize * WASM_PAGE_SIZE >= data_top);
let mem = &memories[local_memory_index];
mem.write_many(init_base as u32, &init.data).unwrap();

View File

@ -30,13 +30,13 @@ impl DynamicMemory {
pub(super) fn new(desc: MemoryDescriptor, local: &mut vm::LocalMemory) -> Option<Box<Self>> {
let memory = {
let mut memory =
sys::Memory::with_size((desc.min as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
sys::Memory::with_size((desc.minimum as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
.ok()?;
if desc.min != 0 {
if desc.minimum != 0 {
unsafe {
memory
.protect(
0..(desc.min as usize * WASM_PAGE_SIZE),
0..(desc.minimum as usize * WASM_PAGE_SIZE),
sys::Protect::ReadWrite,
)
.ok()?;
@ -48,13 +48,13 @@ impl DynamicMemory {
let mut storage = Box::new(DynamicMemory {
memory,
current: desc.min,
max: desc.max,
current: desc.minimum,
max: desc.maximum,
});
let storage_ptr: *mut DynamicMemory = &mut *storage;
local.base = storage.memory.as_ptr();
local.bound = desc.min as usize * WASM_PAGE_SIZE;
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
local.memory = storage_ptr as *mut ();
Some(storage)

View File

@ -23,6 +23,29 @@ pub struct Memory {
}
impl Memory {
/// Create a new `Memory` from a [`MemoryDescriptor`]
///
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
///
/// Usage:
///
/// ```
/// # use wasmer_runtime_core::types::MemoryDescriptor;
/// # use wasmer_runtime_core::memory::Memory;
/// # use wasmer_runtime_core::error::Result;
///
/// # fn create_memory() -> Result<()> {
/// let descriptor = MemoryDescriptor {
/// minimum: 10,
/// maximum: None,
/// shared: false,
/// };
///
/// let memory = Memory::new(descriptor)?;
///
/// # Ok(())
/// # }
/// ```
pub fn new(desc: MemoryDescriptor) -> Option<Self> {
let mut vm_local_memory = Box::new(vm::LocalMemory {
base: ptr::null_mut(),
@ -46,10 +69,15 @@ impl Memory {
})
}
/// Return the [`MemoryDescriptor`] that this memory
/// was created with.
///
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
pub fn descriptor(&self) -> MemoryDescriptor {
self.desc
}
/// Grow this memory by the specfied number of pages.
pub fn grow(&mut self, delta: u32) -> Option<u32> {
match &mut *self.storage.borrow_mut() {
(MemoryStorage::Dynamic(ref mut dynamic_memory), ref mut local) => {
@ -62,8 +90,13 @@ impl Memory {
}
}
/// This returns the number of pages in the memory.
pub fn current_pages(&self) -> u32 {
/// The size, in bytes, of this memory.
pub fn bytes(&self) -> usize {
(self.size() as usize) * WASM_PAGE_SIZE
}
/// The size, in wasm pages, of this memory.
pub fn size(&self) -> u32 {
match &*self.storage.borrow() {
(MemoryStorage::Dynamic(ref dynamic_memory), _) => dynamic_memory.current(),
(MemoryStorage::Static(ref static_memory), _) => static_memory.current(),
@ -271,7 +304,7 @@ impl fmt::Debug for Memory {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Memory")
.field("desc", &self.desc)
.field("size", &(self.current_pages() as usize * WASM_PAGE_SIZE))
.field("size", &self.bytes())
.finish()
}
}

View File

@ -33,11 +33,11 @@ impl StaticMemory {
let memory = {
let mut memory =
sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE).ok()?;
if desc.min != 0 {
if desc.minimum != 0 {
unsafe {
memory
.protect(
0..(desc.min as usize * WASM_PAGE_SIZE),
0..(desc.minimum as usize * WASM_PAGE_SIZE),
sys::Protect::ReadWrite,
)
.ok()?;
@ -49,13 +49,13 @@ impl StaticMemory {
let mut storage = Box::new(StaticMemory {
memory,
current: desc.min,
max: desc.max,
current: desc.minimum,
max: desc.maximum,
});
let storage_ptr: *mut StaticMemory = &mut *storage;
local.base = storage.memory.as_ptr();
local.bound = desc.min as usize * WASM_PAGE_SIZE;
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
local.memory = storage_ptr as *mut ();
Some(storage)

View File

@ -49,14 +49,14 @@ pub struct AnyfuncTable {
impl AnyfuncTable {
pub fn new(desc: TableDescriptor, local: &mut vm::LocalTable) -> Result<Box<Self>, ()> {
let initial_table_backing_len = match desc.max {
let initial_table_backing_len = match desc.maximum {
Some(max) => max,
None => desc.min,
None => desc.minimum,
} as usize;
let mut storage = Box::new(AnyfuncTable {
backing: vec![vm::Anyfunc::null(); initial_table_backing_len],
max: desc.max,
max: desc.maximum,
});
let storage_ptr: *mut AnyfuncTable = &mut *storage;

View File

@ -27,6 +27,29 @@ pub struct Table {
}
impl Table {
/// Create a new `Table` from a [`TableDescriptor`]
///
/// [`TableDescriptor`]: struct.TableDescriptor.html
///
/// Usage:
///
/// ```
/// # use wasmer_runtime_core::types::{TableDescriptor, ElementType};
/// # use wasmer_runtime_core::table::Table;
/// # use wasmer_runtime_core::error::Result;
///
/// # fn create_table() -> Result<()> {
/// let descriptor = TableDescriptor {
/// element: ElementType::Anyfunc,
/// minimum: 10,
/// maximum: None,
/// };
///
/// let table = Table::new(descriptor)?;
///
/// # Ok(())
/// # }
/// ```
pub fn new(desc: TableDescriptor) -> Result<Self, ()> {
let mut local = vm::LocalTable {
base: ptr::null_mut(),

View File

@ -132,17 +132,17 @@ pub struct TableDescriptor {
/// Type of data stored in this table.
pub element: ElementType,
/// The minimum number of elements that must be stored in this table.
pub min: u32,
pub minimum: u32,
/// The maximum number of elements in this table.
pub max: Option<u32>,
pub maximum: Option<u32>,
}
impl TableDescriptor {
pub(crate) fn fits_in_imported(&self, imported: TableDescriptor) -> bool {
// TODO: We should define implementation limits.
let imported_max = imported.max.unwrap_or(u32::max_value());
let self_max = self.max.unwrap_or(u32::max_value());
self.element == imported.element && imported_max <= self_max && self.min <= imported.min
let imported_max = imported.maximum.unwrap_or(u32::max_value());
let self_max = self.maximum.unwrap_or(u32::max_value());
self.element == imported.element && imported_max <= self_max && self.minimum <= imported.minimum
}
}
@ -174,16 +174,16 @@ pub struct GlobalInit {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemoryDescriptor {
/// The minimum number of allowed pages.
pub min: u32,
pub minimum: u32,
/// The maximum number of allowed pages.
pub max: Option<u32>,
pub maximum: Option<u32>,
/// This memory can be shared between wasm threads.
pub shared: bool,
}
impl MemoryDescriptor {
pub fn memory_type(self) -> MemoryType {
match (self.max.is_some(), self.shared) {
match (self.maximum.is_some(), self.shared) {
(true, true) => MemoryType::SharedStatic,
(true, false) => MemoryType::Static,
(false, false) => MemoryType::Dynamic,
@ -192,10 +192,10 @@ impl MemoryDescriptor {
}
pub(crate) fn fits_in_imported(&self, imported: MemoryDescriptor) -> bool {
let imported_max = imported.max.unwrap_or(65_536);
let self_max = self.max.unwrap_or(65_536);
let imported_max = imported.maximum.unwrap_or(65_536);
let self_max = self.maximum.unwrap_or(65_536);
self.shared == imported.shared && imported_max <= self_max && self.min <= imported.min
self.shared == imported.shared && imported_max <= self_max && self.minimum <= imported.minimum
}
}