mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-29 00:21:34 +00:00
Add Pages and Bytes newtypes
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
use crate::{
|
||||
memory::{WASM_MAX_PAGES, WASM_PAGE_SIZE},
|
||||
error::CreationError,
|
||||
sys,
|
||||
types::MemoryDescriptor,
|
||||
units::{Bytes, Pages},
|
||||
vm,
|
||||
};
|
||||
|
||||
@ -22,24 +23,24 @@ pub const DYNAMIC_GUARD_SIZE: usize = 4096;
|
||||
/// us to add a guard-page at the end to help elide some bounds-checks.
|
||||
pub struct DynamicMemory {
|
||||
memory: sys::Memory,
|
||||
current: u32,
|
||||
max: Option<u32>,
|
||||
current: Pages,
|
||||
max: Option<Pages>,
|
||||
}
|
||||
|
||||
impl DynamicMemory {
|
||||
pub(super) fn new(desc: MemoryDescriptor, local: &mut vm::LocalMemory) -> Option<Box<Self>> {
|
||||
pub(super) fn new(
|
||||
desc: MemoryDescriptor,
|
||||
local: &mut vm::LocalMemory,
|
||||
) -> Result<Box<Self>, CreationError> {
|
||||
let min_bytes: Bytes = desc.minimum.into();
|
||||
let memory = {
|
||||
let mut memory =
|
||||
sys::Memory::with_size((desc.minimum as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
|
||||
.ok()?;
|
||||
if desc.minimum != 0 {
|
||||
let mut memory = sys::Memory::with_size(min_bytes.0 + DYNAMIC_GUARD_SIZE)
|
||||
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||
if desc.minimum != Pages(0) {
|
||||
unsafe {
|
||||
memory
|
||||
.protect(
|
||||
0..(desc.minimum as usize * WASM_PAGE_SIZE),
|
||||
sys::Protect::ReadWrite,
|
||||
)
|
||||
.ok()?;
|
||||
.protect(0..min_bytes.0, sys::Protect::ReadWrite)
|
||||
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,18 +55,18 @@ impl DynamicMemory {
|
||||
let storage_ptr: *mut DynamicMemory = &mut *storage;
|
||||
|
||||
local.base = storage.memory.as_ptr();
|
||||
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
|
||||
local.bound = min_bytes.0;
|
||||
local.memory = storage_ptr as *mut ();
|
||||
|
||||
Some(storage)
|
||||
Ok(storage)
|
||||
}
|
||||
|
||||
pub fn current(&self) -> u32 {
|
||||
pub fn size(&self) -> Pages {
|
||||
self.current
|
||||
}
|
||||
|
||||
pub fn grow(&mut self, delta: u32, local: &mut vm::LocalMemory) -> Option<u32> {
|
||||
if delta == 0 {
|
||||
pub fn grow(&mut self, delta: Pages, local: &mut vm::LocalMemory) -> Option<Pages> {
|
||||
if delta == Pages(0) {
|
||||
return Some(self.current);
|
||||
}
|
||||
|
||||
@ -77,30 +78,22 @@ impl DynamicMemory {
|
||||
}
|
||||
}
|
||||
|
||||
if new_pages as usize > WASM_MAX_PAGES {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut new_memory =
|
||||
sys::Memory::with_size((new_pages as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
|
||||
.ok()?;
|
||||
sys::Memory::with_size(new_pages.bytes().0 + DYNAMIC_GUARD_SIZE).ok()?;
|
||||
|
||||
unsafe {
|
||||
new_memory
|
||||
.protect(
|
||||
0..(new_pages as usize * WASM_PAGE_SIZE),
|
||||
sys::Protect::ReadWrite,
|
||||
)
|
||||
.protect(0..new_pages.bytes().0, sys::Protect::ReadWrite)
|
||||
.ok()?;
|
||||
|
||||
new_memory.as_slice_mut()[..self.current as usize * WASM_PAGE_SIZE]
|
||||
.copy_from_slice(&self.memory.as_slice()[..self.current as usize * WASM_PAGE_SIZE]);
|
||||
new_memory.as_slice_mut()[..self.current.bytes().0]
|
||||
.copy_from_slice(&self.memory.as_slice()[..self.current.bytes().0]);
|
||||
}
|
||||
|
||||
self.memory = new_memory; //The old memory gets dropped.
|
||||
|
||||
local.base = self.memory.as_ptr();
|
||||
local.bound = new_pages as usize * WASM_PAGE_SIZE;
|
||||
local.bound = new_pages.bytes().0;
|
||||
|
||||
let old_pages = self.current;
|
||||
self.current = new_pages;
|
||||
@ -108,10 +101,10 @@ impl DynamicMemory {
|
||||
}
|
||||
|
||||
pub fn as_slice(&self) -> &[u8] {
|
||||
unsafe { &self.memory.as_slice()[0..self.current as usize * WASM_PAGE_SIZE] }
|
||||
unsafe { &self.memory.as_slice()[0..self.current.bytes().0] }
|
||||
}
|
||||
|
||||
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current as usize * WASM_PAGE_SIZE] }
|
||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current.bytes().0] }
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
use crate::{
|
||||
error::CreationError,
|
||||
export::Export,
|
||||
import::IsExport,
|
||||
memory::dynamic::DYNAMIC_GUARD_SIZE,
|
||||
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
||||
types::{MemoryDescriptor, ValueType},
|
||||
units::Pages,
|
||||
vm,
|
||||
};
|
||||
use std::{cell::RefCell, fmt, mem, ptr, rc::Rc, slice};
|
||||
@ -14,9 +16,6 @@ pub use self::static_::{SharedStaticMemory, StaticMemory};
|
||||
mod dynamic;
|
||||
mod static_;
|
||||
|
||||
pub const WASM_PAGE_SIZE: usize = 65_536;
|
||||
pub const WASM_MAX_PAGES: usize = 65_536;
|
||||
|
||||
pub struct Memory {
|
||||
desc: MemoryDescriptor,
|
||||
storage: Rc<RefCell<(MemoryStorage, Box<vm::LocalMemory>)>>,
|
||||
@ -24,29 +23,28 @@ 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;
|
||||
///
|
||||
/// # use wasmer_runtime_core::units::Pages;
|
||||
/// # fn create_memory() -> Result<()> {
|
||||
/// let descriptor = MemoryDescriptor {
|
||||
/// minimum: 10,
|
||||
/// minimum: Pages(10),
|
||||
/// maximum: None,
|
||||
/// shared: false,
|
||||
/// };
|
||||
///
|
||||
///
|
||||
/// let memory = Memory::new(descriptor)?;
|
||||
///
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn new(desc: MemoryDescriptor) -> Option<Self> {
|
||||
pub fn new(desc: MemoryDescriptor) -> Result<Self, CreationError> {
|
||||
let mut vm_local_memory = Box::new(vm::LocalMemory {
|
||||
base: ptr::null_mut(),
|
||||
bound: 0,
|
||||
@ -63,7 +61,7 @@ impl Memory {
|
||||
MemoryType::SharedStatic => unimplemented!("shared memories are not yet implemented"),
|
||||
};
|
||||
|
||||
Some(Memory {
|
||||
Ok(Memory {
|
||||
desc,
|
||||
storage: Rc::new(RefCell::new((memory_storage, vm_local_memory))),
|
||||
})
|
||||
@ -71,14 +69,14 @@ 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> {
|
||||
pub fn grow(&mut self, delta: Pages) -> Option<Pages> {
|
||||
match &mut *self.storage.borrow_mut() {
|
||||
(MemoryStorage::Dynamic(ref mut dynamic_memory), ref mut local) => {
|
||||
dynamic_memory.grow(delta, local)
|
||||
@ -90,16 +88,11 @@ impl Memory {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
pub fn size(&self) -> Pages {
|
||||
match &*self.storage.borrow() {
|
||||
(MemoryStorage::Dynamic(ref dynamic_memory), _) => dynamic_memory.current(),
|
||||
(MemoryStorage::Static(ref static_memory), _) => static_memory.current(),
|
||||
(MemoryStorage::Dynamic(ref dynamic_memory), _) => dynamic_memory.size(),
|
||||
(MemoryStorage::Static(ref static_memory), _) => static_memory.size(),
|
||||
(MemoryStorage::SharedStatic(_), _) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
@ -304,7 +297,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.bytes())
|
||||
.field("size", &self.size())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
use crate::{
|
||||
memory::{
|
||||
static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
||||
WASM_MAX_PAGES, WASM_PAGE_SIZE,
|
||||
},
|
||||
error::CreationError,
|
||||
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
||||
sys,
|
||||
types::MemoryDescriptor,
|
||||
units::Pages,
|
||||
vm,
|
||||
};
|
||||
|
||||
@ -21,26 +20,23 @@ use crate::{
|
||||
/// allows them to select the type of memory used however.
|
||||
pub struct StaticMemory {
|
||||
memory: sys::Memory,
|
||||
current: u32,
|
||||
max: Option<u32>,
|
||||
current: Pages,
|
||||
max: Option<Pages>,
|
||||
}
|
||||
|
||||
impl StaticMemory {
|
||||
pub(in crate::memory) fn new(
|
||||
desc: MemoryDescriptor,
|
||||
local: &mut vm::LocalMemory,
|
||||
) -> Option<Box<Self>> {
|
||||
) -> Result<Box<Self>, CreationError> {
|
||||
let memory = {
|
||||
let mut memory =
|
||||
sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE).ok()?;
|
||||
if desc.minimum != 0 {
|
||||
let mut memory = sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE)
|
||||
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||
if desc.minimum != Pages(0) {
|
||||
unsafe {
|
||||
memory
|
||||
.protect(
|
||||
0..(desc.minimum as usize * WASM_PAGE_SIZE),
|
||||
sys::Protect::ReadWrite,
|
||||
)
|
||||
.ok()?;
|
||||
.protect(0..desc.minimum.bytes().0, sys::Protect::ReadWrite)
|
||||
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,18 +51,18 @@ impl StaticMemory {
|
||||
let storage_ptr: *mut StaticMemory = &mut *storage;
|
||||
|
||||
local.base = storage.memory.as_ptr();
|
||||
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
|
||||
local.bound = desc.minimum.bytes().0;
|
||||
local.memory = storage_ptr as *mut ();
|
||||
|
||||
Some(storage)
|
||||
Ok(storage)
|
||||
}
|
||||
|
||||
pub fn current(&self) -> u32 {
|
||||
pub fn size(&self) -> Pages {
|
||||
self.current
|
||||
}
|
||||
|
||||
pub fn grow(&mut self, delta: u32, local: &mut vm::LocalMemory) -> Option<u32> {
|
||||
if delta == 0 {
|
||||
pub fn grow(&mut self, delta: Pages, local: &mut vm::LocalMemory) -> Option<Pages> {
|
||||
if delta == Pages(0) {
|
||||
return Some(self.current);
|
||||
}
|
||||
|
||||
@ -78,20 +74,16 @@ impl StaticMemory {
|
||||
}
|
||||
}
|
||||
|
||||
if new_pages as usize > WASM_MAX_PAGES {
|
||||
return None;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
self.memory
|
||||
.protect(
|
||||
self.current as usize * WASM_PAGE_SIZE..new_pages as usize * WASM_PAGE_SIZE,
|
||||
self.current.bytes().0..new_pages.bytes().0,
|
||||
sys::Protect::ReadWrite,
|
||||
)
|
||||
.ok()?;
|
||||
}
|
||||
|
||||
local.bound = new_pages as usize * WASM_PAGE_SIZE;
|
||||
local.bound = new_pages.bytes().0;
|
||||
|
||||
let old_pages = self.current;
|
||||
|
||||
@ -101,10 +93,10 @@ impl StaticMemory {
|
||||
}
|
||||
|
||||
pub fn as_slice(&self) -> &[u8] {
|
||||
unsafe { &self.memory.as_slice()[0..self.current as usize * WASM_PAGE_SIZE] }
|
||||
unsafe { &self.memory.as_slice()[0..self.current.bytes().0] }
|
||||
}
|
||||
|
||||
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current as usize * WASM_PAGE_SIZE] }
|
||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current.bytes().0] }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user