Skip to content

Commit bb32803

Browse files
authored
memory::grow(), memory::size() (#1)
1 parent 6ef113e commit bb32803

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

primitives/sandbox/embedded_executor.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ impl Memory {
5252
self.memref.set(ptr, value).map_err(|_| Error::OutOfBounds)?;
5353
Ok(())
5454
}
55+
56+
pub fn grow(&self, pages: u32) -> Result<u32, Error> {
57+
self.memref
58+
.grow(Pages(pages as usize))
59+
.map(|prev| (prev.0 as u32))
60+
.map_err(|_| Error::MemoryGrow)
61+
}
62+
63+
pub fn size(&self) -> u32 {
64+
self.memref.current_size().0 as u32
65+
}
5566
}
5667

5768
struct HostFuncIndex(usize);

primitives/sandbox/src/lib.rs

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

70+
/// Trying to grow memory by more than maximum limit.
71+
MemoryGrow,
72+
7073
/// Failed to invoke the start function or an exported function for some reason.
7174
Execution,
7275
}
@@ -121,6 +124,20 @@ impl Memory {
121124
pub fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error> {
122125
self.inner.set(ptr, value)
123126
}
127+
128+
/// Grow memory with provided number of pages.
129+
///
130+
/// Returns `Err` if attempted to allocate more memory than permited by the limit.
131+
pub fn grow(&self, pages: u32) -> Result<u32, Error> {
132+
self.inner.grow(pages)
133+
}
134+
135+
/// Returns current memory size.
136+
///
137+
/// Maximum memory size cannot exceed 65536 pages or 4GiB.
138+
pub fn size(&self) -> u32 {
139+
self.inner.size()
140+
}
124141
}
125142

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

0 commit comments

Comments
 (0)