Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend sandbox memory. #1

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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