nordisk/heimdall
crates/heimdall-wasm/tests/rust-guest/src/lib.rs
// SPDX-License-Identifier: AGPL-3.0-only
//! A Rust guest for heimdall-wasm's tests (Heimdall is a defensive sandbox):
//! 'T…' input is refused, 'M…' panics (a trap), 'L…' loops forever, anything
//! else is emitted as `echo.bin` plus a `params.txt`, and accepted.
wit_bindgen::generate!({ path: "../../wit/heimdall.wit", world: "job" });
struct G;
impl Guest for G {
fn run(input: Vec<u8>, params: Vec<Param>) -> Result<Output, Refusal> {
match input.first() {
Some(b'T') => {
return Err(Refusal {
reason: "tampered input".to_string(),
})
}
Some(b'M') => panic!("malformed input"),
Some(b'L') => {
let mut x: u64 = 0;
loop {
x = std::hint::black_box(x.wrapping_add(1));
}
}
_ => {}
}
emit_file("echo.bin", &input);
let listed: Vec<String> = params
.iter()
.map(|p| format!("{}={}", p.key, p.value))
.collect();
emit_file("params.txt", listed.join("\n").as_bytes());
Ok(Output {
note: format!("accepted {} bytes", input.len()),
})
}
}
export!(G);