oc_wasm_safe/execute.rs
1//! Functions for loading and executing a new WASM binary.
2
3use super::error::{Error, Result};
4use oc_wasm_sys::execute as sys;
5
6/// Clears the execution buffer.
7///
8/// At the start of a program’s execution, the execution buffer is empty, so loading can commence
9/// without invoking this syscall first. However, if the program starts loading a binary then needs
10/// to abort and load a different binary, this syscall can be used to discard the first binary.
11pub fn clear() {
12 // SAFETY: clear is unconditionally safe.
13 unsafe { sys::clear() }
14}
15
16/// Writes data to the execution buffer.
17///
18/// The `data` parameter is the portion of the Wasm binary to write into the buffer.
19///
20/// # Errors
21/// * [`Other`](Error::Other) is returned if this call would make the contents of the buffer larger
22/// than the computer’s installed RAM.
23pub fn add(data: &[u8]) -> Result<()> {
24 Error::from_i32(
25 // SAFETY: add permits a readable pointer/length pair.
26 unsafe { sys::add(data.as_ptr(), data.len()) },
27 )?;
28 Ok(())
29}
30
31/// Executes the Wasm binary contained in the execution buffer.
32pub fn execute() -> ! {
33 // SAFETY: execute is unconditionally safe.
34 unsafe { sys::execute() }
35}