Skip to content

Commit a42262f

Browse files
ukint-vsgrishasobol
authored andcommitted
memory::grow(), memory::size() (#1)
1 parent 5c3984a commit a42262f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

primitives/sandbox/src/embedded_executor.rs

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

6071
struct HostFuncIndex(usize);

primitives/sandbox/src/lib.rs

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

72+
/// Trying to grow memory by more than maximum limit.
73+
MemoryGrow,
74+
7275
/// Failed to invoke the start function or an exported function for some reason.
7376
Execution,
7477
}
@@ -111,7 +114,23 @@ pub trait SandboxMemory: Sized + Clone {
111114
/// Write a memory area at the address `ptr` with contents of the provided slice `buf`.
112115
///
113116
/// Returns `Err` if the range is out-of-bounds.
114-
fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error>;
117+
pub fn set(&self, ptr: u32, value: &[u8]) -> Result<(), Error> {
118+
self.inner.set(ptr, value)
119+
}
120+
121+
/// Grow memory with provided number of pages.
122+
///
123+
/// Returns `Err` if attempted to allocate more memory than permited by the limit.
124+
pub fn grow(&self, pages: u32) -> Result<u32, Error> {
125+
self.inner.grow(pages)
126+
}
127+
128+
/// Returns current memory size.
129+
///
130+
/// Maximum memory size cannot exceed 65536 pages or 4GiB.
131+
pub fn size(&self) -> u32 {
132+
self.inner.size()
133+
}
115134
}
116135

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

0 commit comments

Comments
 (0)