|
| 1 | +//! This test program takes argument(s) that determine which WASI feature to |
| 2 | +//! exercise and returns an exit code of 0 for success, 1 for WASI interface |
| 3 | +//! failure (which is sometimes expected in a test), and some other code on |
| 4 | +//! invalid argument(s). |
| 5 | +
|
| 6 | +#[link(wasm_import_module = "multiplier")] |
| 7 | +extern "C" { |
| 8 | + fn multiply(n: i32) -> i32; |
| 9 | +} |
| 10 | + |
| 11 | +type Result = std::result::Result<(), Box<dyn std::error::Error>>; |
| 12 | + |
| 13 | +fn main() -> Result { |
| 14 | + let mut args = std::env::args(); |
| 15 | + let cmd = args.next().expect("cmd"); |
| 16 | + match cmd.as_str() { |
| 17 | + "noop" => (), |
| 18 | + "echo" => { |
| 19 | + eprintln!("echo"); |
| 20 | + std::io::copy(&mut std::io::stdin(), &mut std::io::stdout())?; |
| 21 | + } |
| 22 | + "alloc" => { |
| 23 | + let size: usize = args.next().expect("size").parse().expect("size"); |
| 24 | + eprintln!("alloc {size}"); |
| 25 | + let layout = std::alloc::Layout::from_size_align(size, 8).expect("layout"); |
| 26 | + unsafe { |
| 27 | + let p = std::alloc::alloc(layout); |
| 28 | + if p.is_null() { |
| 29 | + return Err("allocation failed".into()); |
| 30 | + } |
| 31 | + // Force allocation to actually happen |
| 32 | + p.read_volatile(); |
| 33 | + } |
| 34 | + } |
| 35 | + "read" => { |
| 36 | + let path = args.next().expect("path"); |
| 37 | + eprintln!("read {path}"); |
| 38 | + std::fs::read(path)?; |
| 39 | + } |
| 40 | + "write" => { |
| 41 | + let path = args.next().expect("path"); |
| 42 | + eprintln!("write {path}"); |
| 43 | + std::fs::write(path, "content")?; |
| 44 | + } |
| 45 | + "multiply" => { |
| 46 | + let input: i32 = args.next().expect("input").parse().expect("i32"); |
| 47 | + eprintln!("multiply {input}"); |
| 48 | + let output = unsafe { multiply(input) }; |
| 49 | + println!("{output}"); |
| 50 | + } |
| 51 | + "panic" => { |
| 52 | + eprintln!("panic"); |
| 53 | + panic!("intentional panic"); |
| 54 | + } |
| 55 | + cmd => panic!("unknown cmd {cmd}"), |
| 56 | + }; |
| 57 | + Ok(()) |
| 58 | +} |
0 commit comments