Improve panics/traps from imported functions (#857)
* Improve panics/traps from imported functions This commit performs a few refactorings and fixes a bug as well. The changes here are: * The `thread_local!` in the `wasmtime` crate for trap information is removed. The thread local in the `wasmtime_runtime` crate is now leveraged to transmit trap information. * Panics in user-provided functions are now caught explicitly to be carried across JIT code manually. Getting Rust panics unwinding through JIT code is pretty likely to be super tricky and difficult to do, so in the meantime we can get by with catching panics and resuming the panic once we've resumed in Rust code. * Various take/record trap apis have all been removed in favor of working directly with `Trap` objects, where the internal trap object has been expanded slightly to encompass user-provided errors as well. This borrows a bit #839 and otherwise will... Closes #848 * Rename `r#return` to `ret`
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
// TODO complete the C API
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use std::rc::Rc;
|
||||
use std::{mem, ptr, slice};
|
||||
use wasmtime::{
|
||||
@@ -488,15 +489,34 @@ pub unsafe extern "C" fn wasm_func_call(
|
||||
let val = &(*args.add(i));
|
||||
params.push(val.val());
|
||||
}
|
||||
match func.call(¶ms) {
|
||||
Ok(out) => {
|
||||
|
||||
// We're calling arbitrary code here most of the time, and we in general
|
||||
// want to try to insulate callers against bugs in wasmtime/wasi/etc if we
|
||||
// can. As a result we catch panics here and transform them to traps to
|
||||
// allow the caller to have any insulation possible against Rust panics.
|
||||
let result = panic::catch_unwind(AssertUnwindSafe(|| func.call(¶ms)));
|
||||
match result {
|
||||
Ok(Ok(out)) => {
|
||||
for i in 0..func.result_arity() {
|
||||
let val = &mut (*results.add(i));
|
||||
*val = wasm_val_t::from_val(&out[i]);
|
||||
}
|
||||
ptr::null_mut()
|
||||
}
|
||||
Err(trap) => {
|
||||
Ok(Err(trap)) => {
|
||||
let trap = Box::new(wasm_trap_t {
|
||||
trap: HostRef::new(trap),
|
||||
});
|
||||
Box::into_raw(trap)
|
||||
}
|
||||
Err(panic) => {
|
||||
let trap = if let Some(msg) = panic.downcast_ref::<String>() {
|
||||
Trap::new(msg)
|
||||
} else if let Some(msg) = panic.downcast_ref::<&'static str>() {
|
||||
Trap::new(*msg)
|
||||
} else {
|
||||
Trap::new("rust panic happened")
|
||||
};
|
||||
let trap = Box::new(wasm_trap_t {
|
||||
trap: HostRef::new(trap),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user