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 committed Dec 9, 2022
1 parent 2dff067 commit 698020c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions primitives/sandbox/src/embedded_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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
13 changes: 13 additions & 0 deletions primitives/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 @@ -113,6 +116,16 @@ pub trait SandboxMemory: Sized + Clone {
///
/// Returns `Err` if the range is out-of-bounds.
fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error>;

/// 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>;

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

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

0 comments on commit 698020c

Please sign in to comment.