You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
436 lines
11 KiB
436 lines
11 KiB
5 years ago
|
use anyhow::Result;
|
||
5 years ago
|
use std::panic::{self, AssertUnwindSafe};
|
||
5 years ago
|
use wasmtime::*;
|
||
|
|
||
|
#[test]
|
||
5 years ago
|
fn test_trap_return() -> Result<()> {
|
||
5 years ago
|
let store = Store::default();
|
||
5 years ago
|
let wat = r#"
|
||
|
(module
|
||
|
(func $hello (import "" "hello"))
|
||
|
(func (export "run") (call $hello))
|
||
|
)
|
||
|
"#;
|
||
|
|
||
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let hello_type = FuncType::new(Box::new([]), Box::new([]));
|
||
5 years ago
|
let hello_func = Func::new(&store, hello_type, |_, _, _| Err(Trap::new("test 123")));
|
||
5 years ago
|
|
||
5 years ago
|
let instance = Instance::new(&module, &[hello_func.into()])?;
|
||
5 years ago
|
let run_func = instance.exports()[0]
|
||
|
.func()
|
||
|
.expect("expected function export");
|
||
|
|
||
5 years ago
|
let e = run_func
|
||
|
.call(&[])
|
||
|
.err()
|
||
|
.expect("error calling function")
|
||
|
.downcast::<Trap>()?;
|
||
5 years ago
|
|
||
5 years ago
|
assert_eq!(e.message(), "test 123");
|
||
5 years ago
|
|
||
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
5 years ago
|
fn test_trap_trace() -> Result<()> {
|
||
5 years ago
|
let store = Store::default();
|
||
5 years ago
|
let wat = r#"
|
||
|
(module $hello_mod
|
||
|
(func (export "run") (call $hello))
|
||
|
(func $hello (unreachable))
|
||
|
)
|
||
|
"#;
|
||
|
|
||
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let instance = Instance::new(&module, &[])?;
|
||
5 years ago
|
let run_func = instance.exports()[0]
|
||
|
.func()
|
||
|
.expect("expected function export");
|
||
|
|
||
5 years ago
|
let e = run_func
|
||
|
.call(&[])
|
||
|
.err()
|
||
|
.expect("error calling function")
|
||
|
.downcast::<Trap>()?;
|
||
5 years ago
|
|
||
|
let trace = e.trace();
|
||
|
assert_eq!(trace.len(), 2);
|
||
|
assert_eq!(trace[0].module_name().unwrap(), "hello_mod");
|
||
|
assert_eq!(trace[0].func_index(), 1);
|
||
5 years ago
|
assert_eq!(trace[0].func_name(), Some("hello"));
|
||
5 years ago
|
assert_eq!(trace[0].func_offset(), 1);
|
||
|
assert_eq!(trace[0].module_offset(), 0x26);
|
||
5 years ago
|
assert_eq!(trace[1].module_name().unwrap(), "hello_mod");
|
||
|
assert_eq!(trace[1].func_index(), 0);
|
||
5 years ago
|
assert_eq!(trace[1].func_name(), None);
|
||
5 years ago
|
assert_eq!(trace[1].func_offset(), 1);
|
||
|
assert_eq!(trace[1].module_offset(), 0x21);
|
||
5 years ago
|
assert!(
|
||
|
e.message().contains("unreachable"),
|
||
|
"wrong message: {}",
|
||
|
e.message()
|
||
|
);
|
||
5 years ago
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
5 years ago
|
fn test_trap_trace_cb() -> Result<()> {
|
||
5 years ago
|
let store = Store::default();
|
||
5 years ago
|
let wat = r#"
|
||
|
(module $hello_mod
|
||
|
(import "" "throw" (func $throw))
|
||
|
(func (export "run") (call $hello))
|
||
|
(func $hello (call $throw))
|
||
|
)
|
||
|
"#;
|
||
5 years ago
|
|
||
|
let fn_type = FuncType::new(Box::new([]), Box::new([]));
|
||
5 years ago
|
let fn_func = Func::new(&store, fn_type, |_, _, _| Err(Trap::new("cb throw")));
|
||
5 years ago
|
|
||
5 years ago
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let instance = Instance::new(&module, &[fn_func.into()])?;
|
||
5 years ago
|
let run_func = instance.exports()[0]
|
||
|
.func()
|
||
|
.expect("expected function export");
|
||
|
|
||
5 years ago
|
let e = run_func
|
||
|
.call(&[])
|
||
|
.err()
|
||
|
.expect("error calling function")
|
||
|
.downcast::<Trap>()?;
|
||
5 years ago
|
|
||
|
let trace = e.trace();
|
||
|
assert_eq!(trace.len(), 2);
|
||
|
assert_eq!(trace[0].module_name().unwrap(), "hello_mod");
|
||
5 years ago
|
assert_eq!(trace[0].func_index(), 2);
|
||
5 years ago
|
assert_eq!(trace[1].module_name().unwrap(), "hello_mod");
|
||
5 years ago
|
assert_eq!(trace[1].func_index(), 1);
|
||
5 years ago
|
assert_eq!(e.message(), "cb throw");
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
5 years ago
|
fn test_trap_stack_overflow() -> Result<()> {
|
||
5 years ago
|
let store = Store::default();
|
||
5 years ago
|
let wat = r#"
|
||
|
(module $rec_mod
|
||
|
(func $run (export "run") (call $run))
|
||
|
)
|
||
|
"#;
|
||
|
|
||
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let instance = Instance::new(&module, &[])?;
|
||
5 years ago
|
let run_func = instance.exports()[0]
|
||
|
.func()
|
||
|
.expect("expected function export");
|
||
|
|
||
5 years ago
|
let e = run_func
|
||
|
.call(&[])
|
||
|
.err()
|
||
|
.expect("error calling function")
|
||
|
.downcast::<Trap>()?;
|
||
5 years ago
|
|
||
|
let trace = e.trace();
|
||
|
assert!(trace.len() >= 32);
|
||
|
for i in 0..trace.len() {
|
||
|
assert_eq!(trace[i].module_name().unwrap(), "rec_mod");
|
||
|
assert_eq!(trace[i].func_index(), 0);
|
||
5 years ago
|
assert_eq!(trace[i].func_name(), Some("run"));
|
||
5 years ago
|
}
|
||
|
assert!(e.message().contains("call stack exhausted"));
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
|
fn trap_display_pretty() -> Result<()> {
|
||
|
let store = Store::default();
|
||
5 years ago
|
let wat = r#"
|
||
|
(module $m
|
||
|
(func $die unreachable)
|
||
|
(func call $die)
|
||
|
(func $foo call 1)
|
||
|
(func (export "bar") call $foo)
|
||
|
)
|
||
|
"#;
|
||
|
|
||
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let instance = Instance::new(&module, &[])?;
|
||
|
let run_func = instance.exports()[0]
|
||
|
.func()
|
||
|
.expect("expected function export");
|
||
|
|
||
|
let e = run_func.call(&[]).err().expect("error calling function");
|
||
|
assert_eq!(
|
||
|
e.to_string(),
|
||
|
"\
|
||
5 years ago
|
wasm trap: unreachable
|
||
5 years ago
|
wasm backtrace:
|
||
5 years ago
|
0: 0x23 - m!die
|
||
|
1: 0x27 - m!<wasm function 1>
|
||
|
2: 0x2c - m!foo
|
||
|
3: 0x31 - m!<wasm function 3>
|
||
5 years ago
|
"
|
||
|
);
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn trap_display_multi_module() -> Result<()> {
|
||
|
let store = Store::default();
|
||
5 years ago
|
let wat = r#"
|
||
|
(module $a
|
||
|
(func $die unreachable)
|
||
|
(func call $die)
|
||
|
(func $foo call 1)
|
||
|
(func (export "bar") call $foo)
|
||
|
)
|
||
|
"#;
|
||
|
|
||
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let instance = Instance::new(&module, &[])?;
|
||
|
let bar = instance.exports()[0].clone();
|
||
|
|
||
5 years ago
|
let wat = r#"
|
||
|
(module $b
|
||
|
(import "" "" (func $bar))
|
||
|
(func $middle call $bar)
|
||
|
(func (export "bar2") call $middle)
|
||
|
)
|
||
|
"#;
|
||
|
let module = Module::new(&store, wat)?;
|
||
5 years ago
|
let instance = Instance::new(&module, &[bar])?;
|
||
|
let bar2 = instance.exports()[0]
|
||
|
.func()
|
||
|
.expect("expected function export");
|
||
|
|
||
|
let e = bar2.call(&[]).err().expect("error calling function");
|
||
|
assert_eq!(
|
||
|
e.to_string(),
|
||
|
"\
|
||
5 years ago
|
wasm trap: unreachable
|
||
5 years ago
|
wasm backtrace:
|
||
5 years ago
|
0: 0x23 - a!die
|
||
|
1: 0x27 - a!<wasm function 1>
|
||
|
2: 0x2c - a!foo
|
||
|
3: 0x31 - a!<wasm function 3>
|
||
|
4: 0x29 - b!middle
|
||
|
5: 0x2e - b!<wasm function 2>
|
||
5 years ago
|
"
|
||
|
);
|
||
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
|
fn trap_start_function_import() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let binary = wat::parse_str(
|
||
|
r#"
|
||
|
(module $a
|
||
|
(import "" "" (func $foo))
|
||
|
(start $foo)
|
||
|
)
|
||
|
"#,
|
||
|
)?;
|
||
|
|
||
|
let module = Module::new(&store, &binary)?;
|
||
|
let sig = FuncType::new(Box::new([]), Box::new([]));
|
||
5 years ago
|
let func = Func::new(&store, sig, |_, _, _| Err(Trap::new("user trap")));
|
||
5 years ago
|
let err = Instance::new(&module, &[func.into()]).err().unwrap();
|
||
|
assert_eq!(err.downcast_ref::<Trap>().unwrap().message(), "user trap");
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn rust_panic_import() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let binary = wat::parse_str(
|
||
|
r#"
|
||
|
(module $a
|
||
|
(import "" "" (func $foo))
|
||
5 years ago
|
(import "" "" (func $bar))
|
||
5 years ago
|
(func (export "foo") call $foo)
|
||
5 years ago
|
(func (export "bar") call $bar)
|
||
5 years ago
|
)
|
||
|
"#,
|
||
|
)?;
|
||
|
|
||
|
let module = Module::new(&store, &binary)?;
|
||
|
let sig = FuncType::new(Box::new([]), Box::new([]));
|
||
5 years ago
|
let func = Func::new(&store, sig, |_, _, _| panic!("this is a panic"));
|
||
5 years ago
|
let instance = Instance::new(
|
||
|
&module,
|
||
|
&[
|
||
|
func.into(),
|
||
5 years ago
|
Func::wrap(&store, || panic!("this is another panic")).into(),
|
||
5 years ago
|
],
|
||
|
)?;
|
||
5 years ago
|
let func = instance.exports()[0].func().unwrap().clone();
|
||
|
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||
|
drop(func.call(&[]));
|
||
|
}))
|
||
|
.unwrap_err();
|
||
|
assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic"));
|
||
5 years ago
|
|
||
|
let func = instance.exports()[1].func().unwrap().clone();
|
||
|
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||
|
drop(func.call(&[]));
|
||
|
}))
|
||
|
.unwrap_err();
|
||
|
assert_eq!(
|
||
|
err.downcast_ref::<&'static str>(),
|
||
|
Some(&"this is another panic")
|
||
|
);
|
||
5 years ago
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn rust_panic_start_function() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let binary = wat::parse_str(
|
||
|
r#"
|
||
|
(module $a
|
||
|
(import "" "" (func $foo))
|
||
|
(start $foo)
|
||
|
)
|
||
|
"#,
|
||
|
)?;
|
||
|
|
||
|
let module = Module::new(&store, &binary)?;
|
||
|
let sig = FuncType::new(Box::new([]), Box::new([]));
|
||
5 years ago
|
let func = Func::new(&store, sig, |_, _, _| panic!("this is a panic"));
|
||
5 years ago
|
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||
|
drop(Instance::new(&module, &[func.into()]));
|
||
|
}))
|
||
|
.unwrap_err();
|
||
|
assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic"));
|
||
5 years ago
|
|
||
5 years ago
|
let func = Func::wrap(&store, || panic!("this is another panic"));
|
||
5 years ago
|
let err = panic::catch_unwind(AssertUnwindSafe(|| {
|
||
|
drop(Instance::new(&module, &[func.into()]));
|
||
|
}))
|
||
|
.unwrap_err();
|
||
|
assert_eq!(
|
||
|
err.downcast_ref::<&'static str>(),
|
||
|
Some(&"this is another panic")
|
||
|
);
|
||
5 years ago
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
|
fn mismatched_arguments() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let binary = wat::parse_str(
|
||
|
r#"
|
||
|
(module $a
|
||
|
(func (export "foo") (param i32))
|
||
|
)
|
||
|
"#,
|
||
|
)?;
|
||
|
|
||
|
let module = Module::new(&store, &binary)?;
|
||
|
let instance = Instance::new(&module, &[])?;
|
||
|
let func = instance.exports()[0].func().unwrap().clone();
|
||
|
assert_eq!(
|
||
5 years ago
|
func.call(&[]).unwrap_err().to_string(),
|
||
5 years ago
|
"expected 1 arguments, got 0"
|
||
|
);
|
||
|
assert_eq!(
|
||
5 years ago
|
func.call(&[Val::F32(0)]).unwrap_err().to_string(),
|
||
5 years ago
|
"argument type mismatch",
|
||
|
);
|
||
|
assert_eq!(
|
||
|
func.call(&[Val::I32(0), Val::I32(1)])
|
||
|
.unwrap_err()
|
||
5 years ago
|
.to_string(),
|
||
5 years ago
|
"expected 1 arguments, got 2"
|
||
|
);
|
||
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
|
fn call_signature_mismatch() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let binary = wat::parse_str(
|
||
|
r#"
|
||
|
(module $a
|
||
|
(func $foo
|
||
|
i32.const 0
|
||
|
call_indirect)
|
||
|
(func $bar (param i32))
|
||
|
(start $foo)
|
||
|
|
||
|
(table 1 anyfunc)
|
||
|
(elem (i32.const 0) 1)
|
||
|
)
|
||
|
"#,
|
||
|
)?;
|
||
|
|
||
|
let module = Module::new(&store, &binary)?;
|
||
|
let err = Instance::new(&module, &[])
|
||
|
.err()
|
||
|
.unwrap()
|
||
|
.downcast::<Trap>()
|
||
|
.unwrap();
|
||
5 years ago
|
assert_eq!(err.message(), "wasm trap: indirect call type mismatch");
|
||
5 years ago
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
|
fn start_trap_pretty() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let wat = r#"
|
||
|
(module $m
|
||
|
(func $die unreachable)
|
||
|
(func call $die)
|
||
|
(func $foo call 1)
|
||
|
(func $start call $foo)
|
||
|
(start $start)
|
||
|
)
|
||
|
"#;
|
||
|
|
||
|
let module = Module::new(&store, wat)?;
|
||
|
let e = match Instance::new(&module, &[]) {
|
||
|
Ok(_) => panic!("expected failure"),
|
||
|
Err(e) => e.downcast::<Trap>()?,
|
||
|
};
|
||
|
|
||
|
assert_eq!(
|
||
|
e.to_string(),
|
||
|
"\
|
||
5 years ago
|
wasm trap: unreachable
|
||
5 years ago
|
wasm backtrace:
|
||
5 years ago
|
0: 0x1d - m!die
|
||
|
1: 0x21 - m!<wasm function 1>
|
||
|
2: 0x26 - m!foo
|
||
|
3: 0x2b - m!start
|
||
5 years ago
|
"
|
||
|
);
|
||
|
Ok(())
|
||
|
}
|
||
5 years ago
|
|
||
|
#[test]
|
||
|
fn present_after_module_drop() -> Result<()> {
|
||
|
let store = Store::default();
|
||
|
let module = Module::new(&store, r#"(func (export "foo") unreachable)"#)?;
|
||
|
let instance = Instance::new(&module, &[])?;
|
||
|
let func = instance.exports()[0].func().unwrap().clone();
|
||
|
|
||
|
println!("asserting before we drop modules");
|
||
|
assert_trap(func.call(&[]).unwrap_err().downcast()?);
|
||
|
drop((instance, module));
|
||
|
|
||
|
println!("asserting after drop");
|
||
|
assert_trap(func.call(&[]).unwrap_err().downcast()?);
|
||
|
return Ok(());
|
||
|
|
||
|
fn assert_trap(t: Trap) {
|
||
|
println!("{}", t);
|
||
|
assert_eq!(t.trace().len(), 1);
|
||
|
assert_eq!(t.trace()[0].func_index(), 0);
|
||
|
}
|
||
|
}
|