Return anyhow::Error from host functions instead of Trap, redesign Trap (#5149)
* Return `anyhow::Error` from host functions instead of `Trap` This commit refactors how errors are modeled when returned from host functions and additionally refactors how custom errors work with `Trap`. At a high level functions in Wasmtime that previously worked with `Result<T, Trap>` now work with `Result<T>` instead where the error is `anyhow::Error`. This includes functions such as: * Host-defined functions in a `Linker<T>` * `TypedFunc::call` * Host-related callbacks like call hooks Errors are now modeled primarily as `anyhow::Error` throughout Wasmtime. This subsequently removes the need for `Trap` to have the ability to represent all host-defined errors as it previously did. Consequently the `From` implementations for any error into a `Trap` have been removed here and the only embedder-defined way to create a `Trap` is to use `Trap::new` with a custom string. After this commit the distinction between a `Trap` and a host error is the wasm backtrace that it contains. Previously all errors in host functions would flow through a `Trap` and get a wasm backtrace attached to them, but now this only happens if a `Trap` itself is created meaning that arbitrary host-defined errors flowing from a host import to the other side won't get backtraces attached. Some internals of Wasmtime itself were updated or preserved to use `Trap::new` to capture a backtrace where it seemed useful, such as when fuel runs out. The main motivation for this commit is that it now enables hosts to thread a concrete error type from a host function all the way through to where a wasm function was invoked. Previously this could not be done since the host error was wrapped in a `Trap` that didn't provide the ability to get at the internals. A consequence of this commit is that when a host error is returned that isn't a `Trap` we'll capture a backtrace and then won't have a `Trap` to attach it to. To avoid losing the contextual information this commit uses the `Error::context` method to attach the backtrace as contextual information to ensure that the backtrace is itself not lost. This is a breaking change for likely all users of Wasmtime, but it's hoped to be a relatively minor change to workaround. Most use cases can likely change `-> Result<T, Trap>` to `-> Result<T>` and otherwise explicit creation of a `Trap` is largely no longer necessary. * Fix some doc links * add some tests and make a backtrace type public (#55) * Trap: avoid a trailing newline in the Display impl which in turn ends up with three newlines between the end of the backtrace and the `Caused by` in the anyhow Debug impl * make BacktraceContext pub, and add tests showing downcasting behavior of anyhow::Error to traps or backtraces * Remove now-unnecesary `Trap` downcasts in `Linker::module` * Fix test output expectations * Remove `Trap::i32_exit` This commit removes special-handling in the `wasmtime::Trap` type for the i32 exit code required by WASI. This is now instead modeled as a specific `I32Exit` error type in the `wasmtime-wasi` crate which is returned by the `proc_exit` hostcall. Embedders which previously tested for i32 exits now downcast to the `I32Exit` value. * Remove the `Trap::new` constructor This commit removes the ability to create a trap with an arbitrary error message. The purpose of this commit is to continue the prior trend of leaning into the `anyhow::Error` type instead of trying to recreate it with `Trap`. A subsequent simplification to `Trap` after this commit is that `Trap` will simply be an `enum` of trap codes with no extra information. This commit is doubly-motivated by the desire to always use the new `BacktraceContext` type instead of sometimes using that and sometimes using `Trap`. Most of the changes here were around updating `Trap::new` calls to `bail!` calls instead. Tests which assert particular error messages additionally often needed to use the `:?` formatter instead of the `{}` formatter because the prior formats the whole `anyhow::Error` and the latter only formats the top-most error, which now contains the backtrace. * Merge `Trap` and `TrapCode` With prior refactorings there's no more need for `Trap` to be opaque or otherwise contain a backtrace. This commit parse down `Trap` to simply an `enum` which was the old `TrapCode`. All various tests and such were updated to handle this. The main consequence of this commit is that all errors have a `BacktraceContext` context attached to them. This unfortunately means that the backtrace is printed first before the error message or trap code, but given all the prior simplifications that seems worth it at this time. * Rename `BacktraceContext` to `WasmBacktrace` This feels like a better name given how this has turned out, and additionally this commit removes having both `WasmBacktrace` and `BacktraceContext`. * Soup up documentation for errors and traps * Fix build of the C API Co-authored-by: Pat Hickey <pat@moreproductive.org>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use anyhow::Result;
|
||||
use anyhow::{bail, Error, Result};
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use std::process::Command;
|
||||
use wasmtime::*;
|
||||
@@ -15,7 +15,75 @@ fn test_trap_return() -> Result<()> {
|
||||
|
||||
let module = Module::new(store.engine(), wat)?;
|
||||
let hello_type = FuncType::new(None, None);
|
||||
let hello_func = Func::new(&mut store, hello_type, |_, _, _| Err(Trap::new("test 123")));
|
||||
let hello_func = Func::new(&mut store, hello_type, |_, _, _| bail!("test 123"));
|
||||
|
||||
let instance = Instance::new(&mut store, &module, &[hello_func.into()])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
assert!(format!("{e:?}").contains("test 123"));
|
||||
|
||||
assert!(
|
||||
e.downcast_ref::<WasmBacktrace>().is_some(),
|
||||
"error should contain a WasmBacktrace"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anyhow_error_return() -> Result<()> {
|
||||
let mut store = Store::<()>::default();
|
||||
let wat = r#"
|
||||
(module
|
||||
(func $hello (import "" "hello"))
|
||||
(func (export "run") (call $hello))
|
||||
)
|
||||
"#;
|
||||
|
||||
let module = Module::new(store.engine(), wat)?;
|
||||
let hello_type = FuncType::new(None, None);
|
||||
let hello_func = Func::new(&mut store, hello_type, |_, _, _| {
|
||||
Err(anyhow::Error::msg("test 1234"))
|
||||
});
|
||||
|
||||
let instance = Instance::new(&mut store, &module, &[hello_func.into()])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
assert!(!e.to_string().contains("test 1234"));
|
||||
assert!(format!("{:?}", e).contains("Caused by:\n test 1234"));
|
||||
|
||||
assert!(e.downcast_ref::<Trap>().is_none());
|
||||
assert!(e.downcast_ref::<WasmBacktrace>().is_some());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trap_return_downcast() -> Result<()> {
|
||||
let mut store = Store::<()>::default();
|
||||
let wat = r#"
|
||||
(module
|
||||
(func $hello (import "" "hello"))
|
||||
(func (export "run") (call $hello))
|
||||
)
|
||||
"#;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MyTrap;
|
||||
impl std::fmt::Display for MyTrap {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "my trap")
|
||||
}
|
||||
}
|
||||
impl std::error::Error for MyTrap {}
|
||||
|
||||
let module = Module::new(store.engine(), wat)?;
|
||||
let hello_type = FuncType::new(None, None);
|
||||
let hello_func = Func::new(&mut store, hello_type, |_, _, _| {
|
||||
Err(anyhow::Error::from(MyTrap))
|
||||
});
|
||||
|
||||
let instance = Instance::new(&mut store, &module, &[hello_func.into()])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
@@ -24,7 +92,19 @@ fn test_trap_return() -> Result<()> {
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
assert!(e.to_string().contains("test 123"));
|
||||
let dbg = format!("{:?}", e);
|
||||
println!("{}", dbg);
|
||||
|
||||
assert!(!e.to_string().contains("my trap"));
|
||||
assert!(dbg.contains("Caused by:\n my trap"));
|
||||
|
||||
e.downcast_ref::<MyTrap>()
|
||||
.expect("error downcasts to MyTrap");
|
||||
let bt = e
|
||||
.downcast_ref::<WasmBacktrace>()
|
||||
.expect("error downcasts to WasmBacktrace");
|
||||
assert_eq!(bt.frames().len(), 1);
|
||||
println!("{:?}", bt);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -43,12 +123,9 @@ fn test_trap_trace() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
|
||||
let trace = e.trace().expect("backtrace is available");
|
||||
let trace = e.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
assert_eq!(trace.len(), 2);
|
||||
assert_eq!(trace[0].module_name().unwrap(), "hello_mod");
|
||||
assert_eq!(trace[0].func_index(), 1);
|
||||
@@ -60,11 +137,7 @@ fn test_trap_trace() -> Result<()> {
|
||||
assert_eq!(trace[1].func_name(), None);
|
||||
assert_eq!(trace[1].func_offset(), Some(1));
|
||||
assert_eq!(trace[1].module_offset(), Some(0x21));
|
||||
assert!(
|
||||
e.to_string().contains("unreachable"),
|
||||
"wrong message: {}",
|
||||
e.to_string()
|
||||
);
|
||||
assert_eq!(e.downcast::<Trap>()?, Trap::UnreachableCodeReached);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -123,11 +196,9 @@ fn test_trap_through_host() -> Result<()> {
|
||||
&module,
|
||||
&[host_func_a.into(), host_func_b.into()],
|
||||
)?;
|
||||
let a = instance
|
||||
.get_typed_func::<(), (), _>(&mut store, "a")
|
||||
.unwrap();
|
||||
let a = instance.get_typed_func::<(), (), _>(&mut store, "a")?;
|
||||
let err = a.call(&mut store, ()).unwrap_err();
|
||||
let trace = err.trace().expect("backtrace is available");
|
||||
let trace = err.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
assert_eq!(trace.len(), 3);
|
||||
assert_eq!(trace[0].func_name(), Some("c"));
|
||||
assert_eq!(trace[1].func_name(), Some("b"));
|
||||
@@ -153,12 +224,8 @@ fn test_trap_backtrace_disabled() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
|
||||
assert!(e.trace().is_none(), "backtraces should be disabled");
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
assert!(e.downcast_ref::<WasmBacktrace>().is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -174,24 +241,21 @@ fn test_trap_trace_cb() -> Result<()> {
|
||||
"#;
|
||||
|
||||
let fn_type = FuncType::new(None, None);
|
||||
let fn_func = Func::new(&mut store, fn_type, |_, _, _| Err(Trap::new("cb throw")));
|
||||
let fn_func = Func::new(&mut store, fn_type, |_, _, _| bail!("cb throw"));
|
||||
|
||||
let module = Module::new(store.engine(), wat)?;
|
||||
let instance = Instance::new(&mut store, &module, &[fn_func.into()])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
|
||||
let trace = e.trace().expect("backtrace is available");
|
||||
let trace = e.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
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!(e.to_string().contains("cb throw"));
|
||||
assert!(format!("{e:?}").contains("cb throw"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -209,19 +273,16 @@ fn test_trap_stack_overflow() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
|
||||
let trace = e.trace().expect("backtrace is available");
|
||||
let trace = e.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
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.to_string().contains("call stack exhausted"));
|
||||
assert_eq!(e.downcast::<Trap>()?, Trap::StackOverflow);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -242,19 +303,18 @@ fn trap_display_pretty() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "bar")?;
|
||||
|
||||
let e = run_func
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
assert_eq!(
|
||||
e.to_string(),
|
||||
format!("{:?}", e),
|
||||
"\
|
||||
wasm trap: wasm `unreachable` instruction executed
|
||||
wasm backtrace:
|
||||
error while executing at wasm backtrace:
|
||||
0: 0x23 - m!die
|
||||
1: 0x27 - m!<wasm function 1>
|
||||
2: 0x2c - m!foo
|
||||
3: 0x31 - m!<wasm function 3>
|
||||
|
||||
Caused by:
|
||||
wasm trap: wasm `unreachable` instruction executed\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
@@ -287,21 +347,20 @@ fn trap_display_multi_module() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[bar])?;
|
||||
let bar2 = instance.get_typed_func::<(), (), _>(&mut store, "bar2")?;
|
||||
|
||||
let e = bar2
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
let e = bar2.call(&mut store, ()).unwrap_err();
|
||||
assert_eq!(
|
||||
e.to_string(),
|
||||
format!("{e:?}"),
|
||||
"\
|
||||
wasm trap: wasm `unreachable` instruction executed
|
||||
wasm backtrace:
|
||||
error while executing at wasm backtrace:
|
||||
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>
|
||||
|
||||
Caused by:
|
||||
wasm trap: wasm `unreachable` instruction executed\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
@@ -321,15 +380,9 @@ fn trap_start_function_import() -> Result<()> {
|
||||
|
||||
let module = Module::new(store.engine(), &binary)?;
|
||||
let sig = FuncType::new(None, None);
|
||||
let func = Func::new(&mut store, sig, |_, _, _| Err(Trap::new("user trap")));
|
||||
let err = Instance::new(&mut store, &module, &[func.into()])
|
||||
.err()
|
||||
.unwrap();
|
||||
assert!(err
|
||||
.downcast_ref::<Trap>()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains("user trap"));
|
||||
let func = Func::new(&mut store, sig, |_, _, _| bail!("user trap"));
|
||||
let err = Instance::new(&mut store, &module, &[func.into()]).unwrap_err();
|
||||
assert!(format!("{err:?}").contains("user trap"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -417,7 +470,7 @@ fn rust_catch_panic_import() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[panic.into(), catch_panic.into()])?;
|
||||
let run = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
let trap = run.call(&mut store, ()).unwrap_err();
|
||||
let trace = trap.trace().unwrap();
|
||||
let trace = trap.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
assert_eq!(trace.len(), 1);
|
||||
assert_eq!(trace[0].func_index(), 3);
|
||||
assert_eq!(num_panics.load(std::sync::atomic::Ordering::SeqCst), 2);
|
||||
@@ -536,18 +589,20 @@ fn start_trap_pretty() -> Result<()> {
|
||||
let module = Module::new(store.engine(), wat)?;
|
||||
let e = match Instance::new(&mut store, &module, &[]) {
|
||||
Ok(_) => panic!("expected failure"),
|
||||
Err(e) => e.downcast::<Trap>()?,
|
||||
Err(e) => e,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
e.to_string(),
|
||||
format!("{e:?}"),
|
||||
"\
|
||||
wasm trap: wasm `unreachable` instruction executed
|
||||
wasm backtrace:
|
||||
error while executing at wasm backtrace:
|
||||
0: 0x1d - m!die
|
||||
1: 0x21 - m!<wasm function 1>
|
||||
2: 0x26 - m!foo
|
||||
3: 0x2b - m!start
|
||||
|
||||
Caused by:
|
||||
wasm trap: wasm `unreachable` instruction executed\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
@@ -568,15 +623,15 @@ fn present_after_module_drop() -> Result<()> {
|
||||
assert_trap(func.call(&mut store, ()).unwrap_err());
|
||||
return Ok(());
|
||||
|
||||
fn assert_trap(t: Trap) {
|
||||
println!("{}", t);
|
||||
let trace = t.trace().expect("backtrace is available");
|
||||
fn assert_trap(t: Error) {
|
||||
println!("{:?}", t);
|
||||
let trace = t.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
assert_eq!(trace.len(), 1);
|
||||
assert_eq!(trace[0].func_index(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_trap_code(wat: &str, code: wasmtime::TrapCode) {
|
||||
fn assert_trap_code(wat: &str, code: wasmtime::Trap) {
|
||||
let mut store = Store::<()>::default();
|
||||
let module = Module::new(store.engine(), wat).unwrap();
|
||||
|
||||
@@ -585,7 +640,7 @@ fn assert_trap_code(wat: &str, code: wasmtime::TrapCode) {
|
||||
Err(e) => e,
|
||||
};
|
||||
let trap = err.downcast_ref::<Trap>().unwrap();
|
||||
assert_eq!(trap.trap_code(), Some(code));
|
||||
assert_eq!(*trap, code);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -598,7 +653,7 @@ fn heap_out_of_bounds_trap() {
|
||||
(start $start)
|
||||
)
|
||||
"#,
|
||||
TrapCode::MemoryOutOfBounds,
|
||||
Trap::MemoryOutOfBounds,
|
||||
);
|
||||
|
||||
assert_trap_code(
|
||||
@@ -609,7 +664,7 @@ fn heap_out_of_bounds_trap() {
|
||||
(start $start)
|
||||
)
|
||||
"#,
|
||||
TrapCode::MemoryOutOfBounds,
|
||||
Trap::MemoryOutOfBounds,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -660,13 +715,11 @@ fn parse_dwarf_info() -> Result<()> {
|
||||
);
|
||||
linker.module(&mut store, "", &module)?;
|
||||
let run = linker.get_default(&mut store, "")?;
|
||||
let trap = run
|
||||
.call(&mut store, &[], &mut [])
|
||||
.unwrap_err()
|
||||
.downcast::<Trap>()?;
|
||||
let trap = run.call(&mut store, &[], &mut []).unwrap_err();
|
||||
|
||||
let mut found = false;
|
||||
for frame in trap.trace().expect("backtrace is available") {
|
||||
let frames = trap.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
for frame in frames {
|
||||
for symbol in frame.symbols() {
|
||||
if let Some(file) = symbol.file() {
|
||||
if file.ends_with("input.rs") {
|
||||
@@ -698,16 +751,15 @@ fn no_hint_even_with_dwarf_info() -> Result<()> {
|
||||
)
|
||||
"#,
|
||||
)?;
|
||||
let trap = Instance::new(&mut store, &module, &[])
|
||||
.err()
|
||||
.unwrap()
|
||||
.downcast::<Trap>()?;
|
||||
let trap = Instance::new(&mut store, &module, &[]).unwrap_err();
|
||||
assert_eq!(
|
||||
trap.to_string(),
|
||||
format!("{trap:?}"),
|
||||
"\
|
||||
wasm trap: wasm `unreachable` instruction executed
|
||||
wasm backtrace:
|
||||
error while executing at wasm backtrace:
|
||||
0: 0x1a - <unknown>!start
|
||||
|
||||
Caused by:
|
||||
wasm trap: wasm `unreachable` instruction executed\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
@@ -732,17 +784,16 @@ fn hint_with_dwarf_info() -> Result<()> {
|
||||
)
|
||||
"#,
|
||||
)?;
|
||||
let trap = Instance::new(&mut store, &module, &[])
|
||||
.err()
|
||||
.unwrap()
|
||||
.downcast::<Trap>()?;
|
||||
let trap = Instance::new(&mut store, &module, &[]).unwrap_err();
|
||||
assert_eq!(
|
||||
trap.to_string(),
|
||||
format!("{trap:?}"),
|
||||
"\
|
||||
wasm trap: wasm `unreachable` instruction executed
|
||||
wasm backtrace:
|
||||
error while executing at wasm backtrace:
|
||||
0: 0x1a - <unknown>!start
|
||||
note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable to may show more debugging information
|
||||
|
||||
Caused by:
|
||||
wasm trap: wasm `unreachable` instruction executed\
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
@@ -794,12 +845,9 @@ fn traps_without_address_map() -> Result<()> {
|
||||
let instance = Instance::new(&mut store, &module, &[])?;
|
||||
let run_func = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
|
||||
|
||||
let e = run_func
|
||||
.call(&mut store, ())
|
||||
.err()
|
||||
.expect("error calling function");
|
||||
let e = run_func.call(&mut store, ()).unwrap_err();
|
||||
|
||||
let trace = e.trace().expect("backtrace is available");
|
||||
let trace = e.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
assert_eq!(trace.len(), 2);
|
||||
assert_eq!(trace[0].func_name(), Some("hello"));
|
||||
assert_eq!(trace[0].func_index(), 1);
|
||||
@@ -846,16 +894,13 @@ fn catch_trap_calling_across_stores() -> Result<()> {
|
||||
.get_typed_func::<(), (), _>(&mut data.child_store, "trap")
|
||||
.expect("trap function should be exported");
|
||||
|
||||
let trap = func
|
||||
.call(&mut data.child_store, ())
|
||||
.err()
|
||||
.expect("should trap");
|
||||
let trap = func.call(&mut data.child_store, ()).unwrap_err();
|
||||
assert!(
|
||||
trap.to_string().contains("unreachable"),
|
||||
"trap should contain 'unreachable', got: {trap}"
|
||||
format!("{trap:?}").contains("unreachable"),
|
||||
"trap should contain 'unreachable', got: {trap:?}"
|
||||
);
|
||||
|
||||
let trace = trap.trace().unwrap();
|
||||
let trace = trap.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
|
||||
assert_eq!(trace.len(), 1);
|
||||
assert_eq!(trace[0].func_name(), Some("trap"));
|
||||
@@ -965,7 +1010,7 @@ async fn async_then_sync_trap() -> Result<()> {
|
||||
.unwrap();
|
||||
let trap = a.call_async(&mut async_store, ()).await.unwrap_err();
|
||||
|
||||
let trace = trap.trace().unwrap();
|
||||
let trace = trap.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
// We don't support cross-store or cross-engine symbolication currently, so
|
||||
// the other frames are ignored.
|
||||
assert_eq!(trace.len(), 1);
|
||||
@@ -1022,25 +1067,21 @@ async fn sync_then_async_trap() -> Result<()> {
|
||||
let sync_module = Module::new(sync_store.engine(), wat)?;
|
||||
|
||||
let mut sync_linker = Linker::new(sync_store.engine());
|
||||
sync_linker.func_wrap(
|
||||
"",
|
||||
"b",
|
||||
move |mut caller: Caller<SyncCtx>| -> Result<(), Trap> {
|
||||
log::info!("Called `b`...");
|
||||
let async_instance = caller.data().async_instance;
|
||||
let async_store = &mut caller.data_mut().async_store;
|
||||
sync_linker.func_wrap("", "b", move |mut caller: Caller<SyncCtx>| -> Result<()> {
|
||||
log::info!("Called `b`...");
|
||||
let async_instance = caller.data().async_instance;
|
||||
let async_store = &mut caller.data_mut().async_store;
|
||||
|
||||
log::info!("Calling `c`...");
|
||||
let c = async_instance
|
||||
.get_typed_func::<(), (), _>(&mut *async_store, "c")
|
||||
.unwrap();
|
||||
tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current()
|
||||
.block_on(async move { c.call_async(async_store, ()).await })
|
||||
})?;
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
log::info!("Calling `c`...");
|
||||
let c = async_instance
|
||||
.get_typed_func::<(), (), _>(&mut *async_store, "c")
|
||||
.unwrap();
|
||||
tokio::task::block_in_place(|| {
|
||||
tokio::runtime::Handle::current()
|
||||
.block_on(async move { c.call_async(async_store, ()).await })
|
||||
})?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let sync_instance = sync_linker.instantiate(&mut sync_store, &sync_module)?;
|
||||
|
||||
@@ -1050,7 +1091,7 @@ async fn sync_then_async_trap() -> Result<()> {
|
||||
.unwrap();
|
||||
let trap = a.call(&mut sync_store, ()).unwrap_err();
|
||||
|
||||
let trace = trap.trace().unwrap();
|
||||
let trace = trap.downcast_ref::<WasmBacktrace>().unwrap().frames();
|
||||
// We don't support cross-store or cross-engine symbolication currently, so
|
||||
// the other frames are ignored.
|
||||
assert_eq!(trace.len(), 1);
|
||||
|
||||
Reference in New Issue
Block a user