Skip to content

Commit 4218e3d

Browse files
committed
memory::grow(), memory::size() (#1)
1 parent 08434db commit 4218e3d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

primitives/sandbox/src/embedded_executor.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ impl super::SandboxMemory for Memory {
5757
self.memref.set(ptr, value).map_err(|_| Error::OutOfBounds)?;
5858
Ok(())
5959
}
60+
61+
pub fn grow(&self, pages: u32) -> Result<u32, Error> {
62+
self.memref
63+
.grow(Pages(pages as usize))
64+
.map(|prev| (prev.0 as u32))
65+
.map_err(|_| Error::MemoryGrow)
66+
}
67+
68+
pub fn size(&self) -> u32 {
69+
self.memref.current_size().0 as u32
70+
}
6071
}
6172

6273
struct HostFuncIndex(usize);

primitives/sandbox/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub enum Error {
7070
/// Note that if wasm module makes an out-of-bounds access then trap will occur.
7171
OutOfBounds,
7272

73+
/// Trying to grow memory by more than maximum limit.
74+
MemoryGrow,
75+
7376
/// Failed to invoke the start function or an exported function for some reason.
7477
Execution,
7578
}
@@ -113,6 +116,16 @@ pub trait SandboxMemory: Sized + Clone {
113116
///
114117
/// Returns `Err` if the range is out-of-bounds.
115118
fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error>;
119+
120+
/// Grow memory with provided number of pages.
121+
///
122+
/// Returns `Err` if attempted to allocate more memory than permited by the limit.
123+
pub fn grow(&self, pages: u32) -> Result<u32, Error>;
124+
125+
/// Returns current memory size.
126+
///
127+
/// Maximum memory size cannot exceed 65536 pages or 4GiB.
128+
pub fn size(&self) -> u32;
116129
}
117130

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

0 commit comments

Comments
 (0)