2019-02-23 06:18:59 +00:00
|
|
|
use crate::error::GrowError;
|
2019-01-25 23:28:54 +00:00
|
|
|
use crate::{
|
2019-01-29 23:44:15 +00:00
|
|
|
error::CreationError,
|
2019-01-25 23:28:54 +00:00
|
|
|
sys,
|
2019-01-29 21:04:42 +00:00
|
|
|
types::MemoryDescriptor,
|
2019-01-29 23:44:15 +00:00
|
|
|
units::{Bytes, Pages},
|
2019-01-25 23:28:54 +00:00
|
|
|
vm,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const DYNAMIC_GUARD_SIZE: usize = 4096;
|
|
|
|
|
2019-01-29 20:49:51 +00:00
|
|
|
/// This is an internal-only api.
|
|
|
|
///
|
|
|
|
/// A Dynamic memory allocates only the minimum amount of memory
|
|
|
|
/// when first created. Over time, as it grows, it may reallocate to
|
|
|
|
/// a different location and size.
|
|
|
|
///
|
2019-02-23 06:18:59 +00:00
|
|
|
/// Dynamic memories are significantly faster to create than static
|
2019-01-29 20:49:51 +00:00
|
|
|
/// memories and use much less virtual memory, however, they require
|
2019-02-23 06:18:59 +00:00
|
|
|
/// the WebAssembly module to bounds-check memory accesses.
|
2019-01-29 20:49:51 +00:00
|
|
|
///
|
|
|
|
/// While, a dynamic memory could use a vector of some sort as its
|
|
|
|
/// backing memory, we use mmap (or the platform-equivalent) to allow
|
|
|
|
/// us to add a guard-page at the end to help elide some bounds-checks.
|
2019-01-25 23:28:54 +00:00
|
|
|
pub struct DynamicMemory {
|
|
|
|
memory: sys::Memory,
|
2019-01-29 23:44:15 +00:00
|
|
|
current: Pages,
|
|
|
|
max: Option<Pages>,
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DynamicMemory {
|
2019-01-29 23:44:15 +00:00
|
|
|
pub(super) fn new(
|
|
|
|
desc: MemoryDescriptor,
|
|
|
|
local: &mut vm::LocalMemory,
|
|
|
|
) -> Result<Box<Self>, CreationError> {
|
|
|
|
let min_bytes: Bytes = desc.minimum.into();
|
2019-01-25 23:28:54 +00:00
|
|
|
let memory = {
|
2019-01-29 23:44:15 +00:00
|
|
|
let mut memory = sys::Memory::with_size(min_bytes.0 + DYNAMIC_GUARD_SIZE)
|
|
|
|
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
|
|
|
if desc.minimum != Pages(0) {
|
2019-01-25 23:28:54 +00:00
|
|
|
unsafe {
|
|
|
|
memory
|
2019-01-29 23:44:15 +00:00
|
|
|
.protect(0..min_bytes.0, sys::Protect::ReadWrite)
|
|
|
|
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memory
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut storage = Box::new(DynamicMemory {
|
|
|
|
memory,
|
2019-01-29 22:15:59 +00:00
|
|
|
current: desc.minimum,
|
|
|
|
max: desc.maximum,
|
2019-01-25 23:28:54 +00:00
|
|
|
});
|
|
|
|
let storage_ptr: *mut DynamicMemory = &mut *storage;
|
|
|
|
|
|
|
|
local.base = storage.memory.as_ptr();
|
2019-01-29 23:44:15 +00:00
|
|
|
local.bound = min_bytes.0;
|
2019-01-25 23:28:54 +00:00
|
|
|
local.memory = storage_ptr as *mut ();
|
|
|
|
|
2019-01-29 23:44:15 +00:00
|
|
|
Ok(storage)
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 23:44:15 +00:00
|
|
|
pub fn size(&self) -> Pages {
|
2019-01-25 23:28:54 +00:00
|
|
|
self.current
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:18:59 +00:00
|
|
|
pub fn grow(&mut self, delta: Pages, local: &mut vm::LocalMemory) -> Result<Pages, GrowError> {
|
2019-01-29 23:44:15 +00:00
|
|
|
if delta == Pages(0) {
|
2019-02-23 06:18:59 +00:00
|
|
|
return Ok(self.current);
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:18:59 +00:00
|
|
|
let new_pages = self.current.checked_add(delta).map_err(|e| e.into())?;
|
2019-01-25 23:28:54 +00:00
|
|
|
|
|
|
|
if let Some(max) = self.max {
|
|
|
|
if new_pages > max {
|
2019-02-23 06:18:59 +00:00
|
|
|
return Err(GrowError::ExceededMaxPagesForMemory(
|
|
|
|
new_pages.0 as usize,
|
|
|
|
max.0 as usize,
|
|
|
|
));
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:18:59 +00:00
|
|
|
let mut new_memory = sys::Memory::with_size(new_pages.bytes().0 + DYNAMIC_GUARD_SIZE)
|
|
|
|
.map_err(|e| e.into())?;
|
2019-01-25 23:28:54 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
new_memory
|
2019-01-29 23:44:15 +00:00
|
|
|
.protect(0..new_pages.bytes().0, sys::Protect::ReadWrite)
|
2019-02-23 06:18:59 +00:00
|
|
|
.map_err(|e| e.into())?;
|
2019-01-25 23:28:54 +00:00
|
|
|
|
2019-01-29 23:44:15 +00:00
|
|
|
new_memory.as_slice_mut()[..self.current.bytes().0]
|
|
|
|
.copy_from_slice(&self.memory.as_slice()[..self.current.bytes().0]);
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.memory = new_memory; //The old memory gets dropped.
|
|
|
|
|
|
|
|
local.base = self.memory.as_ptr();
|
2019-01-29 23:44:15 +00:00
|
|
|
local.bound = new_pages.bytes().0;
|
2019-01-25 23:28:54 +00:00
|
|
|
|
|
|
|
let old_pages = self.current;
|
|
|
|
self.current = new_pages;
|
2019-02-23 06:18:59 +00:00
|
|
|
Ok(old_pages)
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice(&self) -> &[u8] {
|
2019-01-29 23:44:15 +00:00
|
|
|
unsafe { &self.memory.as_slice()[0..self.current.bytes().0] }
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
2019-01-29 23:44:15 +00:00
|
|
|
unsafe { &mut self.memory.as_slice_mut()[0..self.current.bytes().0] }
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
}
|