use std::process::Command;
fn file(name: &str, bytes: &[u8]) -> std::path::PathBuf {
let d = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("wasmrt_reports_offsets_{}", std::process::id()));
std::fs::create_dir_all(&d).expect("scratch dir");
let p = d.join(name);
std::fs::write(&p, bytes).expect("write fixture");
p
}
fn stderr_of(args: &[&str]) -> String {
let out = Command::new(env!("CARGO_BIN_EXE_wasmrt")).args(args).output().expect("run wasmrt");
assert!(!out.status.success(), "{args:?} must fail");
String::from_utf8_lossy(&out.stdout).to_string() + &String::from_utf8_lossy(&out.stderr)
}
#[test]
fn a_call_indirect_table_byte_is_reported_at_that_byte() {
let m = file(
"call_indirect_overlong.wasm",
&[
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02, 0x01,
0x00, 0x04, 0x04, 0x01, 0x70, 0x00, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0a, 0x0a, 0x01, 0x08, 0x00,
0x41, 0x00, 0x11, 0x00, 0x80, 0x00, 0x0b,
],
);
let text = stderr_of(&["--features", "all,-reference-types", m.to_str().unwrap()]);
assert!(
text.contains("invalid module at offset 38 (function 0): zero byte expected"),
"as wasmtime: `Invalid input WebAssembly code at offset 38: zero byte expected`; got:\n{text}"
);
}
#[test]
fn a_truncated_module_is_reported_where_the_next_byte_was_needed() {
let m = file("truncated.wasm", &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x80]);
let text = stderr_of(&[m.to_str().unwrap()]);
assert!(
text.contains("decode failed at offset 10: unexpected end-of-file"),
"as wasmtime: `unexpected end-of-file (at offset 0xa)`; got:\n{text}"
);
}
#[test]
fn the_wasi_path_reports_the_offset_too() {
let m = file("truncated_wasi.wasm", &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x80]);
let text = stderr_of(&["wasi", m.to_str().unwrap()]);
assert!(text.contains("decode failed at offset 10: unexpected end-of-file"), "got:\n{text}");
}