Skip to content

Commit

Permalink
memory::grow(), memory::size() (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs authored and grishasobol committed Mar 28, 2022
1 parent 5c3984a commit a42262f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions primitives/sandbox/src/embedded_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ impl super::SandboxMemory for Memory {
self.memref.set(ptr, value).map_err(|_| Error::OutOfBounds)?;
Ok(())
}

pub fn grow(&self, pages: u32) -> Result<u32, Error> {
self.memref
.grow(Pages(pages as usize))
.map(|prev| (prev.0 as u32))
.map_err(|_| Error::MemoryGrow)
}

pub fn size(&self) -> u32 {
self.memref.current_size().0 as u32
}
}

struct HostFuncIndex(usize);
Expand Down
21 changes: 20 additions & 1 deletion primitives/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub enum Error {
/// Note that if wasm module makes an out-of-bounds access then trap will occur.
OutOfBounds,

/// Trying to grow memory by more than maximum limit.
MemoryGrow,

/// Failed to invoke the start function or an exported function for some reason.
Execution,
}
Expand Down Expand Up @@ -111,7 +114,23 @@ pub trait SandboxMemory: Sized + Clone {
/// Write a memory area at the address `ptr` with contents of the provided slice `buf`.
///
/// Returns `Err` if the range is out-of-bounds.
fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error>;
pub fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error> {
self.inner.set(ptr, value)
}

/// Grow memory with provided number of pages.
///
/// Returns `Err` if attempted to allocate more memory than permited by the limit.
pub fn grow(&self, pages: u32) -> Result<u32, Error> {
self.inner.grow(pages)
}

/// Returns current memory size.
///
/// Maximum memory size cannot exceed 65536 pages or 4GiB.
pub fn size(&self) -> u32 {
self.inner.size()
}
}

/// Struct that can be used for defining an environment for a sandboxed module.
Expand Down

0 comments on commit a42262f

Please sign in to comment.