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 Oct 18, 2021
1 parent 6ef113e commit bb32803
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions primitives/sandbox/embedded_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ impl 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
17 changes: 17 additions & 0 deletions primitives/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 @@ -121,6 +124,20 @@ impl Memory {
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 bb32803

Please sign in to comment.