Files
wasmtime/crates/api/tests/traps.rs
Alex Crichton c417d4b587 Improve trap error messages (#831)
* Improve trap error messages

The new trap error message for the issue #828 looks like:

```
thread 'main' panicked at 'a', /proc/self/fd/11:1:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Error: failed to run main module `test.wasm`

Caused by:
    0: failed to invoke `_start`
    1: wasm trap: unreachable, source location: @6cea
       wasm backtrace:
         0: __rust_start_panic
         1: rust_panic
         2: std::panicking::rust_panic_with_hook::h57f0cff11449798f
         3: std::panicking::begin_panic::hd620695467c5dd1f
         4: test::main::ha54db001eabbde1b
         5: std::rt::lang_start::{{closure}}::h5acfb82693695869
         6: std::sys_common::backtrace::__rust_begin_short_backtrace::h39e8b9420da241f9
         7: std::panicking::try::do_call::hb7ebfcd70d5f703e
         8: __rust_maybe_catch_panic
         9: std::rt::lang_start_internal::hd5f64f52a5c5315c
         10: std::rt::lang_start::h2a51d79994dd0c4b
         11: __original_main
         12: _start
```

Closes #828

* Tidy up the style of the traps tests

* Add some tests and module names
2020-01-16 17:39:52 -06:00

232 lines
6.1 KiB
Rust

use anyhow::Result;
use std::rc::Rc;
use wasmtime::*;
#[test]
fn test_trap_return() -> Result<()> {
struct HelloCallback;
impl Callable for HelloCallback {
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), Trap> {
Err(Trap::new("test 123"))
}
}
let store = Store::default();
let binary = wat::parse_str(
r#"
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
"#,
)?;
let module = Module::new(&store, &binary)?;
let hello_type = FuncType::new(Box::new([]), Box::new([]));
let hello_func = Func::new(&store, hello_type, Rc::new(HelloCallback));
let instance = Instance::new(&module, &[hello_func.into()])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
assert_eq!(e.message(), "test 123");
Ok(())
}
#[test]
fn test_trap_trace() -> Result<()> {
let store = Store::default();
let binary = wat::parse_str(
r#"
(module $hello_mod
(func (export "run") (call $hello))
(func $hello (unreachable))
)
"#,
)?;
let module = Module::new(&store, &binary)?;
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");
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);
assert_eq!(trace[0].func_name(), Some("hello"));
assert_eq!(trace[1].module_name().unwrap(), "hello_mod");
assert_eq!(trace[1].func_index(), 0);
assert_eq!(trace[1].func_name(), None);
assert!(e.message().contains("unreachable"));
Ok(())
}
#[test]
fn test_trap_trace_cb() -> Result<()> {
struct ThrowCallback;
impl Callable for ThrowCallback {
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), Trap> {
Err(Trap::new("cb throw"))
}
}
let store = Store::default();
let binary = wat::parse_str(
r#"
(module $hello_mod
(import "" "throw" (func $throw))
(func (export "run") (call $hello))
(func $hello (call $throw))
)
"#,
)?;
let fn_type = FuncType::new(Box::new([]), Box::new([]));
let fn_func = Func::new(&store, fn_type, Rc::new(ThrowCallback));
let module = Module::new(&store, &binary)?;
let instance = Instance::new(&module, &[fn_func.into()])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
let trace = e.trace();
assert_eq!(trace.len(), 2);
assert_eq!(trace[0].module_name().unwrap(), "hello_mod");
assert_eq!(trace[0].func_index(), 2);
assert_eq!(trace[1].module_name().unwrap(), "hello_mod");
assert_eq!(trace[1].func_index(), 1);
assert_eq!(e.message(), "cb throw");
Ok(())
}
#[test]
fn test_trap_stack_overflow() -> Result<()> {
let store = Store::default();
let binary = wat::parse_str(
r#"
(module $rec_mod
(func $run (export "run") (call $run))
)
"#,
)?;
let module = Module::new(&store, &binary)?;
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");
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);
assert_eq!(trace[i].func_name(), Some("run"));
}
assert!(e.message().contains("call stack exhausted"));
Ok(())
}
#[test]
fn trap_display_pretty() -> Result<()> {
let store = Store::default();
let binary = wat::parse_str(
r#"
(module $m
(func $die unreachable)
(func call $die)
(func $foo call 1)
(func (export "bar") call $foo)
)
"#,
)?;
let module = Module::new(&store, &binary)?;
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(),
"\
wasm trap: unreachable, source location: @0023
wasm backtrace:
0: m!die
1: m!<wasm function 1>
2: m!foo
3: m!<wasm function 3>
"
);
Ok(())
}
#[test]
fn trap_display_multi_module() -> Result<()> {
let store = Store::default();
let binary = wat::parse_str(
r#"
(module $a
(func $die unreachable)
(func call $die)
(func $foo call 1)
(func (export "bar") call $foo)
)
"#,
)?;
let module = Module::new(&store, &binary)?;
let instance = Instance::new(&module, &[])?;
let bar = instance.exports()[0].clone();
let binary = wat::parse_str(
r#"
(module $b
(import "" "" (func $bar))
(func $middle call $bar)
(func (export "bar2") call $middle)
)
"#,
)?;
let module = Module::new(&store, &binary)?;
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(),
"\
wasm trap: unreachable, source location: @0023
wasm backtrace:
0: a!die
1: a!<wasm function 1>
2: a!foo
3: a!<wasm function 3>
4: b!middle
5: b!<wasm function 2>
"
);
Ok(())
}