mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-29 12:22:18 +00:00
Merge #381
381: Hook up error propagation r=lachlansneff a=lachlansneff You can now retrieve the error from a panicking or error-returning imported function. Example: ```rust #[derive(Debug)] struct ExitCode { code: i32, } fn do_panic(_ctx: &mut Ctx) -> Result<i32, ExitCode> { Err(ExitCode { code: 42 }) } // This wasm functions calls `do_panic`. let foo: Func<(), i32> = instance.func(...)?; let result = foo.call(); println!("result: {:?}", result); if let Err(RuntimeError::Error { data }) = result { if let Ok(exit_code) = data.downcast::<ExitCode>() { println!("exit code: {:?}", exit_code); } } ``` outputs: ``` result: Err(unknown error) exit code: ExitCode { code: 42 } ``` Co-authored-by: Lachlan Sneff <lachlan.sneff@gmail.com>
This commit is contained in:
commit
b7f98c8401
@ -5,6 +5,7 @@ All PRs to the Wasmer repository must add to this file.
|
|||||||
Blocks of changes will separated by version increments.
|
Blocks of changes will separated by version increments.
|
||||||
|
|
||||||
## **[Unreleased]**
|
## **[Unreleased]**
|
||||||
|
- [#381](https://github.com/wasmerio/wasmer/pull/381) Allow retrieving propagated user errors.
|
||||||
- [#379](https://github.com/wasmerio/wasmer/pull/379) Fix small return types from imported functions.
|
- [#379](https://github.com/wasmerio/wasmer/pull/379) Fix small return types from imported functions.
|
||||||
- [#371](https://github.com/wasmerio/wasmer/pull/371) Add more Debug impl for WASI types
|
- [#371](https://github.com/wasmerio/wasmer/pull/371) Add more Debug impl for WASI types
|
||||||
- [#368](https://github.com/wasmerio/wasmer/pull/368) Fix issue with write buffering
|
- [#368](https://github.com/wasmerio/wasmer/pull/368) Fix issue with write buffering
|
||||||
|
@ -27,6 +27,11 @@ thread_local! {
|
|||||||
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None);
|
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum CallProtError {
|
||||||
|
Trap(WasmTrapInfo),
|
||||||
|
Error(Box<dyn Any>),
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Caller {
|
pub struct Caller {
|
||||||
handler_data: HandlerData,
|
handler_data: HandlerData,
|
||||||
trampolines: Arc<Trampolines>,
|
trampolines: Arc<Trampolines>,
|
||||||
@ -59,7 +64,8 @@ impl RunnableModule for Caller {
|
|||||||
func: NonNull<vm::Func>,
|
func: NonNull<vm::Func>,
|
||||||
args: *const u64,
|
args: *const u64,
|
||||||
rets: *mut u64,
|
rets: *mut u64,
|
||||||
_trap_info: *mut WasmTrapInfo,
|
trap_info: *mut WasmTrapInfo,
|
||||||
|
user_error: *mut Option<Box<dyn Any>>,
|
||||||
invoke_env: Option<NonNull<c_void>>,
|
invoke_env: Option<NonNull<c_void>>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let handler_data = &*invoke_env.unwrap().cast().as_ptr();
|
let handler_data = &*invoke_env.unwrap().cast().as_ptr();
|
||||||
@ -68,14 +74,22 @@ impl RunnableModule for Caller {
|
|||||||
let res = call_protected(handler_data, || {
|
let res = call_protected(handler_data, || {
|
||||||
// Leap of faith.
|
// Leap of faith.
|
||||||
trampoline(ctx, func, args, rets);
|
trampoline(ctx, func, args, rets);
|
||||||
})
|
});
|
||||||
.is_ok();
|
|
||||||
|
|
||||||
// the trampoline is called from C on windows
|
// the trampoline is called from C on windows
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
let res = call_protected(handler_data, trampoline, ctx, func, args, rets).is_ok();
|
let res = call_protected(handler_data, trampoline, ctx, func, args, rets);
|
||||||
|
|
||||||
res
|
match res {
|
||||||
|
Err(err) => {
|
||||||
|
match err {
|
||||||
|
CallProtError::Trap(info) => *trap_info = info,
|
||||||
|
CallProtError::Error(data) => *user_error = Some(data),
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Ok(()) => true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let trampoline = self
|
let trampoline = self
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
//! unless you have memory unsafety elsewhere in your code.
|
//! unless you have memory unsafety elsewhere in your code.
|
||||||
//!
|
//!
|
||||||
use crate::relocation::{TrapCode, TrapData};
|
use crate::relocation::{TrapCode, TrapData};
|
||||||
use crate::signal::HandlerData;
|
use crate::signal::{CallProtError, HandlerData};
|
||||||
use libc::{c_int, c_void, siginfo_t};
|
use libc::{c_int, c_void, siginfo_t};
|
||||||
use nix::sys::signal::{
|
use nix::sys::signal::{
|
||||||
sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
|
sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
|
||||||
@ -18,7 +18,7 @@ use nix::sys::signal::{
|
|||||||
use std::cell::{Cell, UnsafeCell};
|
use std::cell::{Cell, UnsafeCell};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::Once;
|
use std::sync::Once;
|
||||||
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
|
use wasmer_runtime_core::typed_func::WasmTrapInfo;
|
||||||
|
|
||||||
extern "C" fn signal_trap_handler(
|
extern "C" fn signal_trap_handler(
|
||||||
signum: ::nix::libc::c_int,
|
signum: ::nix::libc::c_int,
|
||||||
@ -62,7 +62,10 @@ pub unsafe fn trigger_trap() -> ! {
|
|||||||
longjmp(jmp_buf as *mut c_void, 0)
|
longjmp(jmp_buf as *mut c_void, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> RuntimeResult<T> {
|
pub fn call_protected<T>(
|
||||||
|
handler_data: &HandlerData,
|
||||||
|
f: impl FnOnce() -> T,
|
||||||
|
) -> Result<T, CallProtError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
|
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
|
||||||
let prev_jmp_buf = *jmp_buf;
|
let prev_jmp_buf = *jmp_buf;
|
||||||
@ -76,7 +79,7 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
|
|||||||
*jmp_buf = prev_jmp_buf;
|
*jmp_buf = prev_jmp_buf;
|
||||||
|
|
||||||
if let Some(data) = super::TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
if let Some(data) = super::TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
||||||
Err(RuntimeError::Error { data })
|
Err(CallProtError::Error(data))
|
||||||
} else {
|
} else {
|
||||||
let (faulting_addr, inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
|
let (faulting_addr, inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
|
||||||
|
|
||||||
@ -85,33 +88,18 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
|
|||||||
srcloc: _,
|
srcloc: _,
|
||||||
}) = handler_data.lookup(inst_ptr)
|
}) = handler_data.lookup(inst_ptr)
|
||||||
{
|
{
|
||||||
Err(match Signal::from_c_int(signum) {
|
Err(CallProtError::Trap(match Signal::from_c_int(signum) {
|
||||||
Ok(SIGILL) => match trapcode {
|
Ok(SIGILL) => match trapcode {
|
||||||
TrapCode::BadSignature => RuntimeError::Trap {
|
TrapCode::BadSignature => WasmTrapInfo::IncorrectCallIndirectSignature,
|
||||||
msg: "incorrect call_indirect signature".into(),
|
TrapCode::IndirectCallToNull => WasmTrapInfo::CallIndirectOOB,
|
||||||
},
|
TrapCode::HeapOutOfBounds => WasmTrapInfo::MemoryOutOfBounds,
|
||||||
TrapCode::IndirectCallToNull => RuntimeError::Trap {
|
TrapCode::TableOutOfBounds => WasmTrapInfo::CallIndirectOOB,
|
||||||
msg: "indirect call to null".into(),
|
_ => WasmTrapInfo::Unknown,
|
||||||
},
|
|
||||||
TrapCode::HeapOutOfBounds => RuntimeError::Trap {
|
|
||||||
msg: "memory out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
TrapCode::TableOutOfBounds => RuntimeError::Trap {
|
|
||||||
msg: "table out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
_ => RuntimeError::Trap {
|
|
||||||
msg: "unknown trap".into(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Ok(SIGSEGV) | Ok(SIGBUS) => RuntimeError::Trap {
|
|
||||||
msg: "memory out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
Ok(SIGFPE) => RuntimeError::Trap {
|
|
||||||
msg: "illegal arithmetic operation".into(),
|
|
||||||
},
|
},
|
||||||
|
Ok(SIGSEGV) | Ok(SIGBUS) => WasmTrapInfo::MemoryOutOfBounds,
|
||||||
|
Ok(SIGFPE) => WasmTrapInfo::IllegalArithmetic,
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}))
|
||||||
.into())
|
|
||||||
} else {
|
} else {
|
||||||
let signal = match Signal::from_c_int(signum) {
|
let signal = match Signal::from_c_int(signum) {
|
||||||
Ok(SIGFPE) => "floating-point exception",
|
Ok(SIGFPE) => "floating-point exception",
|
||||||
@ -122,10 +110,8 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
|
|||||||
_ => "unkown trapped signal",
|
_ => "unkown trapped signal",
|
||||||
};
|
};
|
||||||
// When the trap-handler is fully implemented, this will return more information.
|
// When the trap-handler is fully implemented, this will return more information.
|
||||||
Err(RuntimeError::Trap {
|
let s = format!("unknown trap at {:p} - {}", faulting_addr, signal);
|
||||||
msg: format!("unknown trap at {:p} - {}", faulting_addr, signal).into(),
|
Err(CallProtError::Error(Box::new(s)))
|
||||||
}
|
|
||||||
.into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use crate::relocation::{TrapCode, TrapData};
|
use crate::relocation::{TrapCode, TrapData};
|
||||||
use crate::signal::HandlerData;
|
use crate::signal::{CallProtError, HandlerData};
|
||||||
use crate::trampoline::Trampoline;
|
use crate::trampoline::Trampoline;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
use std::ptr::{self, NonNull};
|
use std::ptr::{self, NonNull};
|
||||||
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
|
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
|
||||||
|
use wasmer_runtime_core::typed_func::WasmTrapInfo;
|
||||||
use wasmer_runtime_core::vm::Ctx;
|
use wasmer_runtime_core::vm::Ctx;
|
||||||
use wasmer_runtime_core::vm::Func;
|
use wasmer_runtime_core::vm::Func;
|
||||||
use wasmer_win_exception_handler::CallProtectedData;
|
use wasmer_win_exception_handler::CallProtectedData;
|
||||||
@ -28,7 +29,7 @@ pub fn call_protected(
|
|||||||
func: NonNull<Func>,
|
func: NonNull<Func>,
|
||||||
param_vec: *const u64,
|
param_vec: *const u64,
|
||||||
return_vec: *mut u64,
|
return_vec: *mut u64,
|
||||||
) -> RuntimeResult<()> {
|
) -> Result<(), CallProtError> {
|
||||||
// TODO: trap early
|
// TODO: trap early
|
||||||
// user code error
|
// user code error
|
||||||
// if let Some(msg) = super::TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
// if let Some(msg) = super::TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
||||||
@ -52,38 +53,22 @@ pub fn call_protected(
|
|||||||
srcloc: _,
|
srcloc: _,
|
||||||
}) = handler_data.lookup(instruction_pointer as _)
|
}) = handler_data.lookup(instruction_pointer as _)
|
||||||
{
|
{
|
||||||
Err(match signum as DWORD {
|
Err(CallProtError::Trap(match signum as DWORD {
|
||||||
EXCEPTION_ACCESS_VIOLATION => RuntimeError::Trap {
|
EXCEPTION_ACCESS_VIOLATION => WasmTrapInfo::MemoryOutOfBounds,
|
||||||
msg: "memory out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
EXCEPTION_ILLEGAL_INSTRUCTION => match trapcode {
|
EXCEPTION_ILLEGAL_INSTRUCTION => match trapcode {
|
||||||
TrapCode::BadSignature => RuntimeError::Trap {
|
TrapCode::BadSignature => WasmTrapInfo::IncorrectCallIndirectSignature,
|
||||||
msg: "incorrect call_indirect signature".into(),
|
TrapCode::IndirectCallToNull => WasmTrapInfo::CallIndirectOOB,
|
||||||
},
|
TrapCode::HeapOutOfBounds => WasmTrapInfo::MemoryOutOfBounds,
|
||||||
TrapCode::IndirectCallToNull => RuntimeError::Trap {
|
TrapCode::TableOutOfBounds => WasmTrapInfo::CallIndirectOOB,
|
||||||
msg: "indirect call to null".into(),
|
TrapCode::UnreachableCodeReached => WasmTrapInfo::Unreachable,
|
||||||
},
|
_ => WasmTrapInfo::Unknown,
|
||||||
TrapCode::HeapOutOfBounds => RuntimeError::Trap {
|
|
||||||
msg: "memory out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
TrapCode::TableOutOfBounds => RuntimeError::Trap {
|
|
||||||
msg: "table out-of-bounds access".into(),
|
|
||||||
},
|
|
||||||
_ => RuntimeError::Trap {
|
|
||||||
msg: "unknown trap".into(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
EXCEPTION_STACK_OVERFLOW => RuntimeError::Trap {
|
|
||||||
msg: "stack overflow trap".into(),
|
|
||||||
},
|
|
||||||
EXCEPTION_INT_DIVIDE_BY_ZERO | EXCEPTION_INT_OVERFLOW => RuntimeError::Trap {
|
|
||||||
msg: "illegal arithmetic operation".into(),
|
|
||||||
},
|
|
||||||
_ => RuntimeError::Trap {
|
|
||||||
msg: "unknown trap".into(),
|
|
||||||
},
|
},
|
||||||
|
EXCEPTION_STACK_OVERFLOW => WasmTrapInfo::Unknown,
|
||||||
|
EXCEPTION_INT_DIVIDE_BY_ZERO | EXCEPTION_INT_OVERFLOW => {
|
||||||
|
WasmTrapInfo::IllegalArithmetic
|
||||||
}
|
}
|
||||||
.into())
|
_ => WasmTrapInfo::Unknown,
|
||||||
|
}))
|
||||||
} else {
|
} else {
|
||||||
let signal = match signum as DWORD {
|
let signal = match signum as DWORD {
|
||||||
EXCEPTION_FLT_DENORMAL_OPERAND
|
EXCEPTION_FLT_DENORMAL_OPERAND
|
||||||
@ -98,10 +83,9 @@ pub fn call_protected(
|
|||||||
_ => "unkown trapped signal",
|
_ => "unkown trapped signal",
|
||||||
};
|
};
|
||||||
|
|
||||||
Err(RuntimeError::Trap {
|
let s = format!("unknown trap at {} - {}", exception_address, signal);
|
||||||
msg: format!("unknown trap at {} - {}", exception_address, signal).into(),
|
|
||||||
}
|
Err(CallProtError::Error(Box::new(s)))
|
||||||
.into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,6 +213,8 @@ fn main() {
|
|||||||
|
|
||||||
println!("cargo:rustc-link-lib=static=llvm-backend");
|
println!("cargo:rustc-link-lib=static=llvm-backend");
|
||||||
println!("cargo:rerun-if-changed=build.rs");
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
println!("cargo:rerun-if-changed=cpp/object_loader.cpp");
|
||||||
|
println!("cargo:rerun-if-changed=cpp/object_loader.hh");
|
||||||
|
|
||||||
// Enable "nightly" cfg if the current compiler is nightly.
|
// Enable "nightly" cfg if the current compiler is nightly.
|
||||||
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {
|
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {
|
||||||
|
@ -43,6 +43,11 @@ typedef struct
|
|||||||
visit_fde_t visit_fde;
|
visit_fde_t visit_fde;
|
||||||
} callbacks_t;
|
} callbacks_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
size_t data, vtable;
|
||||||
|
} box_any_t;
|
||||||
|
|
||||||
struct WasmException
|
struct WasmException
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -61,7 +66,7 @@ struct UncatchableException : WasmException
|
|||||||
struct UserException : UncatchableException
|
struct UserException : UncatchableException
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UserException(size_t data, size_t vtable) : data(data), vtable(vtable) {}
|
UserException(size_t data, size_t vtable) : error_data({ data, vtable }) {}
|
||||||
|
|
||||||
virtual std::string description() const noexcept override
|
virtual std::string description() const noexcept override
|
||||||
{
|
{
|
||||||
@ -69,7 +74,7 @@ struct UserException : UncatchableException
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The parts of a `Box<dyn Any>`.
|
// The parts of a `Box<dyn Any>`.
|
||||||
size_t data, vtable;
|
box_any_t error_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WasmTrap : UncatchableException
|
struct WasmTrap : UncatchableException
|
||||||
@ -194,6 +199,7 @@ extern "C"
|
|||||||
void *params,
|
void *params,
|
||||||
void *results,
|
void *results,
|
||||||
WasmTrap::Type *trap_out,
|
WasmTrap::Type *trap_out,
|
||||||
|
box_any_t *user_error,
|
||||||
void *invoke_env) throw()
|
void *invoke_env) throw()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -206,6 +212,11 @@ extern "C"
|
|||||||
*trap_out = e.type;
|
*trap_out = e.type;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
catch (const UserException &e)
|
||||||
|
{
|
||||||
|
*user_error = e.error_data;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
catch (const WasmException &e)
|
catch (const WasmException &e)
|
||||||
{
|
{
|
||||||
*trap_out = WasmTrap::Type::Unknown;
|
*trap_out = WasmTrap::Type::Unknown;
|
||||||
|
@ -93,6 +93,7 @@ extern "C" {
|
|||||||
params: *const u64,
|
params: *const u64,
|
||||||
results: *mut u64,
|
results: *mut u64,
|
||||||
trap_out: *mut WasmTrapInfo,
|
trap_out: *mut WasmTrapInfo,
|
||||||
|
user_error: *mut Option<Box<dyn Any>>,
|
||||||
invoke_env: Option<NonNull<c_void>>,
|
invoke_env: Option<NonNull<c_void>>,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
}
|
}
|
||||||
|
@ -137,15 +137,13 @@ impl std::fmt::Display for RuntimeError {
|
|||||||
write!(f, "WebAssembly trap occured during runtime: {}", msg)
|
write!(f, "WebAssembly trap occured during runtime: {}", msg)
|
||||||
}
|
}
|
||||||
RuntimeError::Error { data } => {
|
RuntimeError::Error { data } => {
|
||||||
let msg = if let Some(s) = data.downcast_ref::<String>() {
|
if let Some(s) = data.downcast_ref::<String>() {
|
||||||
s
|
write!(f, "\"{}\"", s)
|
||||||
} else if let Some(s) = data.downcast_ref::<&str>() {
|
} else if let Some(s) = data.downcast_ref::<&str>() {
|
||||||
s
|
write!(f, "\"{}\"", s)
|
||||||
} else {
|
} else {
|
||||||
"user-defined, opaque"
|
write!(f, "unknown error")
|
||||||
};
|
}
|
||||||
|
|
||||||
write!(f, "{}", msg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -528,6 +528,7 @@ fn call_func_with_index(
|
|||||||
|
|
||||||
let run_wasm = |result_space: *mut u64| unsafe {
|
let run_wasm = |result_space: *mut u64| unsafe {
|
||||||
let mut trap_info = WasmTrapInfo::Unknown;
|
let mut trap_info = WasmTrapInfo::Unknown;
|
||||||
|
let mut user_error = None;
|
||||||
|
|
||||||
let success = invoke(
|
let success = invoke(
|
||||||
trampoline,
|
trampoline,
|
||||||
@ -536,16 +537,21 @@ fn call_func_with_index(
|
|||||||
raw_args.as_ptr(),
|
raw_args.as_ptr(),
|
||||||
result_space,
|
result_space,
|
||||||
&mut trap_info,
|
&mut trap_info,
|
||||||
|
&mut user_error,
|
||||||
invoke_env,
|
invoke_env,
|
||||||
);
|
);
|
||||||
|
|
||||||
if success {
|
if success {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
} else {
|
||||||
|
if let Some(data) = user_error {
|
||||||
|
Err(RuntimeError::Error { data })
|
||||||
} else {
|
} else {
|
||||||
Err(RuntimeError::Trap {
|
Err(RuntimeError::Trap {
|
||||||
msg: trap_info.to_string().into(),
|
msg: trap_info.to_string().into(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let raw_to_value = |raw, ty| match ty {
|
let raw_to_value = |raw, ty| match ty {
|
||||||
|
@ -58,6 +58,7 @@ pub type Invoke = unsafe extern "C" fn(
|
|||||||
*const u64,
|
*const u64,
|
||||||
*mut u64,
|
*mut u64,
|
||||||
*mut WasmTrapInfo,
|
*mut WasmTrapInfo,
|
||||||
|
*mut Option<Box<dyn Any>>,
|
||||||
Option<NonNull<c_void>>,
|
Option<NonNull<c_void>>,
|
||||||
) -> bool;
|
) -> bool;
|
||||||
|
|
||||||
@ -104,7 +105,7 @@ pub trait WasmTypeList {
|
|||||||
f: NonNull<vm::Func>,
|
f: NonNull<vm::Func>,
|
||||||
wasm: Wasm,
|
wasm: Wasm,
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
) -> Result<Rets, WasmTrapInfo>
|
) -> Result<Rets, RuntimeError>
|
||||||
where
|
where
|
||||||
Rets: WasmTypeList;
|
Rets: WasmTypeList;
|
||||||
}
|
}
|
||||||
@ -213,6 +214,35 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WasmTypeList for Infallible {
|
||||||
|
type CStruct = Infallible;
|
||||||
|
type RetArray = [u64; 0];
|
||||||
|
fn from_ret_array(_: Self::RetArray) -> Self {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
fn empty_ret_array() -> Self::RetArray {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
fn from_c_struct(_: Self::CStruct) -> Self {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
fn into_c_struct(self) -> Self::CStruct {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
fn types() -> &'static [Type] {
|
||||||
|
&[]
|
||||||
|
}
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
unsafe fn call<Rets: WasmTypeList>(
|
||||||
|
self,
|
||||||
|
_: NonNull<vm::Func>,
|
||||||
|
_: Wasm,
|
||||||
|
_: *mut Ctx,
|
||||||
|
) -> Result<Rets, RuntimeError> {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<A: WasmExternType> WasmTypeList for (A,) {
|
impl<A: WasmExternType> WasmTypeList for (A,) {
|
||||||
type CStruct = S1<A>;
|
type CStruct = S1<A>;
|
||||||
type RetArray = [u64; 1];
|
type RetArray = [u64; 1];
|
||||||
@ -242,11 +272,12 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
|
|||||||
f: NonNull<vm::Func>,
|
f: NonNull<vm::Func>,
|
||||||
wasm: Wasm,
|
wasm: Wasm,
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
) -> Result<Rets, WasmTrapInfo> {
|
) -> Result<Rets, RuntimeError> {
|
||||||
let (a,) = self;
|
let (a,) = self;
|
||||||
let args = [a.to_native().to_bits()];
|
let args = [a.to_native().to_bits()];
|
||||||
let mut rets = Rets::empty_ret_array();
|
let mut rets = Rets::empty_ret_array();
|
||||||
let mut trap = WasmTrapInfo::Unknown;
|
let mut trap = WasmTrapInfo::Unknown;
|
||||||
|
let mut user_error = None;
|
||||||
|
|
||||||
if (wasm.invoke)(
|
if (wasm.invoke)(
|
||||||
wasm.trampoline,
|
wasm.trampoline,
|
||||||
@ -255,11 +286,18 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
|
|||||||
args.as_ptr(),
|
args.as_ptr(),
|
||||||
rets.as_mut().as_mut_ptr(),
|
rets.as_mut().as_mut_ptr(),
|
||||||
&mut trap,
|
&mut trap,
|
||||||
|
&mut user_error,
|
||||||
wasm.invoke_env,
|
wasm.invoke_env,
|
||||||
) {
|
) {
|
||||||
Ok(Rets::from_ret_array(rets))
|
Ok(Rets::from_ret_array(rets))
|
||||||
} else {
|
} else {
|
||||||
Err(trap)
|
if let Some(data) = user_error {
|
||||||
|
Err(RuntimeError::Error { data })
|
||||||
|
} else {
|
||||||
|
Err(RuntimeError::Trap {
|
||||||
|
msg: trap.to_string().into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -269,11 +307,7 @@ where
|
|||||||
Rets: WasmTypeList,
|
Rets: WasmTypeList,
|
||||||
{
|
{
|
||||||
pub fn call(&self, a: A) -> Result<Rets, RuntimeError> {
|
pub fn call(&self, a: A) -> Result<Rets, RuntimeError> {
|
||||||
unsafe { <A as WasmTypeList>::call(a, self.f, self.inner, self.ctx) }.map_err(|e| {
|
unsafe { <A as WasmTypeList>::call(a, self.f, self.inner, self.ctx) }
|
||||||
RuntimeError::Trap {
|
|
||||||
msg: e.to_string().into(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,17 +341,22 @@ macro_rules! impl_traits {
|
|||||||
&[$( $x::Native::TYPE, )*]
|
&[$( $x::Native::TYPE, )*]
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, WasmTrapInfo> {
|
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, RuntimeError> {
|
||||||
#[allow(unused_parens)]
|
#[allow(unused_parens)]
|
||||||
let ( $( $x ),* ) = self;
|
let ( $( $x ),* ) = self;
|
||||||
let args = [ $( $x.to_native().to_bits() ),* ];
|
let args = [ $( $x.to_native().to_bits() ),* ];
|
||||||
let mut rets = Rets::empty_ret_array();
|
let mut rets = Rets::empty_ret_array();
|
||||||
let mut trap = WasmTrapInfo::Unknown;
|
let mut trap = WasmTrapInfo::Unknown;
|
||||||
|
let mut user_error = None;
|
||||||
|
|
||||||
if (wasm.invoke)(wasm.trampoline, ctx, f, args.as_ptr(), rets.as_mut().as_mut_ptr(), &mut trap, wasm.invoke_env) {
|
if (wasm.invoke)(wasm.trampoline, ctx, f, args.as_ptr(), rets.as_mut().as_mut_ptr(), &mut trap, &mut user_error, wasm.invoke_env) {
|
||||||
Ok(Rets::from_ret_array(rets))
|
Ok(Rets::from_ret_array(rets))
|
||||||
} else {
|
} else {
|
||||||
Err(trap)
|
if let Some(data) = user_error {
|
||||||
|
Err(RuntimeError::Error { data })
|
||||||
|
} else {
|
||||||
|
Err(RuntimeError::Trap { msg: trap.to_string().into() })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -359,11 +398,7 @@ macro_rules! impl_traits {
|
|||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub fn call(&self, $( $x: $x, )* ) -> Result<Rets, RuntimeError> {
|
pub fn call(&self, $( $x: $x, )* ) -> Result<Rets, RuntimeError> {
|
||||||
#[allow(unused_parens)]
|
#[allow(unused_parens)]
|
||||||
unsafe { <( $( $x ),* ) as WasmTypeList>::call(( $($x),* ), self.f, self.inner, self.ctx) }.map_err(|e| {
|
unsafe { <( $( $x ),* ) as WasmTypeList>::call(( $($x),* ), self.f, self.inner, self.ctx) }
|
||||||
RuntimeError::Trap {
|
|
||||||
msg: e.to_string().into(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use wasmer_runtime::{compile, error, imports, Ctx, Func, Value};
|
use wasmer_runtime::{compile, error, error::RuntimeError, imports, Ctx, Func, Value};
|
||||||
|
|
||||||
use wabt::wat2wasm;
|
use wabt::wat2wasm;
|
||||||
|
|
||||||
@ -7,6 +7,8 @@ static WAT: &'static str = r#"
|
|||||||
(type (;0;) (func (result i32)))
|
(type (;0;) (func (result i32)))
|
||||||
(import "env" "do_panic" (func $do_panic (type 0)))
|
(import "env" "do_panic" (func $do_panic (type 0)))
|
||||||
(func $dbz (result i32)
|
(func $dbz (result i32)
|
||||||
|
call $do_panic
|
||||||
|
drop
|
||||||
i32.const 42
|
i32.const 42
|
||||||
i32.const 0
|
i32.const 0
|
||||||
i32.div_u
|
i32.div_u
|
||||||
@ -34,8 +36,13 @@ fn foobar(_ctx: &mut Ctx) -> i32 {
|
|||||||
42
|
42
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_panic(_ctx: &mut Ctx) -> Result<i32, String> {
|
#[derive(Debug)]
|
||||||
Err("error".to_string())
|
struct ExitCode {
|
||||||
|
code: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn do_panic(_ctx: &mut Ctx) -> Result<i32, ExitCode> {
|
||||||
|
Err(ExitCode { code: 42 })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), error::Error> {
|
fn main() -> Result<(), error::Error> {
|
||||||
@ -63,5 +70,11 @@ fn main() -> Result<(), error::Error> {
|
|||||||
|
|
||||||
println!("result: {:?}", result);
|
println!("result: {:?}", result);
|
||||||
|
|
||||||
|
if let Err(RuntimeError::Error { data }) = result {
|
||||||
|
if let Ok(exit_code) = data.downcast::<ExitCode>() {
|
||||||
|
println!("exit code: {:?}", exit_code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
49
lib/runtime/tests/error_propagation.rs
Normal file
49
lib/runtime/tests/error_propagation.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#[test]
|
||||||
|
fn error_propagation() {
|
||||||
|
use std::convert::Infallible;
|
||||||
|
use wabt::wat2wasm;
|
||||||
|
use wasmer_runtime::{compile, error::RuntimeError, imports, Ctx, Func};
|
||||||
|
|
||||||
|
static WAT: &'static str = r#"
|
||||||
|
(module
|
||||||
|
(type (;0;) (func))
|
||||||
|
(import "env" "ret_err" (func $ret_err (type 0)))
|
||||||
|
(func $call_panic
|
||||||
|
call $ret_err
|
||||||
|
)
|
||||||
|
(export "call_err" (func $call_panic))
|
||||||
|
)
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ExitCode {
|
||||||
|
code: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ret_err(_ctx: &mut Ctx) -> Result<Infallible, ExitCode> {
|
||||||
|
Err(ExitCode { code: 42 })
|
||||||
|
}
|
||||||
|
|
||||||
|
let wasm = wat2wasm(WAT).unwrap();
|
||||||
|
|
||||||
|
let module = compile(&wasm).unwrap();
|
||||||
|
|
||||||
|
let instance = module
|
||||||
|
.instantiate(&imports! {
|
||||||
|
"env" => {
|
||||||
|
"ret_err" => Func::new(ret_err),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let foo: Func<(), ()> = instance.func("call_err").unwrap();
|
||||||
|
|
||||||
|
let result = foo.call();
|
||||||
|
|
||||||
|
if let Err(RuntimeError::Error { data }) = result {
|
||||||
|
let exit_code = data.downcast::<ExitCode>().unwrap();
|
||||||
|
assert_eq!(exit_code.code, 42);
|
||||||
|
} else {
|
||||||
|
panic!("didn't return RuntimeError::Error")
|
||||||
|
}
|
||||||
|
}
|
@ -205,7 +205,8 @@ impl RunnableModule for X64ExecutionContext {
|
|||||||
func: NonNull<vm::Func>,
|
func: NonNull<vm::Func>,
|
||||||
args: *const u64,
|
args: *const u64,
|
||||||
rets: *mut u64,
|
rets: *mut u64,
|
||||||
_trap_info: *mut WasmTrapInfo,
|
trap_info: *mut WasmTrapInfo,
|
||||||
|
user_error: *mut Option<Box<dyn Any>>,
|
||||||
num_params_plus_one: Option<NonNull<c_void>>,
|
num_params_plus_one: Option<NonNull<c_void>>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let args = ::std::slice::from_raw_parts(
|
let args = ::std::slice::from_raw_parts(
|
||||||
@ -227,7 +228,13 @@ impl RunnableModule for X64ExecutionContext {
|
|||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(_) => false,
|
Err(err) => {
|
||||||
|
match err {
|
||||||
|
protect_unix::CallProtError::Trap(info) => *trap_info = info,
|
||||||
|
protect_unix::CallProtError::Error(data) => *user_error = Some(data),
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ use std::any::Any;
|
|||||||
use std::cell::{Cell, UnsafeCell};
|
use std::cell::{Cell, UnsafeCell};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::Once;
|
use std::sync::Once;
|
||||||
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
|
use wasmer_runtime_core::typed_func::WasmTrapInfo;
|
||||||
|
|
||||||
extern "C" fn signal_trap_handler(
|
extern "C" fn signal_trap_handler(
|
||||||
signum: ::nix::libc::c_int,
|
signum: ::nix::libc::c_int,
|
||||||
@ -62,7 +62,12 @@ pub unsafe fn trigger_trap() -> ! {
|
|||||||
longjmp(jmp_buf as *mut c_void, 0)
|
longjmp(jmp_buf as *mut c_void, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn call_protected<T>(f: impl FnOnce() -> T) -> RuntimeResult<T> {
|
pub enum CallProtError {
|
||||||
|
Trap(WasmTrapInfo),
|
||||||
|
Error(Box<dyn Any>),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_protected<T>(f: impl FnOnce() -> T) -> Result<T, CallProtError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
|
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
|
||||||
let prev_jmp_buf = *jmp_buf;
|
let prev_jmp_buf = *jmp_buf;
|
||||||
@ -76,23 +81,24 @@ pub fn call_protected<T>(f: impl FnOnce() -> T) -> RuntimeResult<T> {
|
|||||||
*jmp_buf = prev_jmp_buf;
|
*jmp_buf = prev_jmp_buf;
|
||||||
|
|
||||||
if let Some(data) = TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
if let Some(data) = TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
||||||
Err(RuntimeError::Error { data })
|
Err(CallProtError::Error(data))
|
||||||
} else {
|
} else {
|
||||||
let (faulting_addr, _inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
|
// let (faulting_addr, _inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
|
||||||
|
|
||||||
let signal = match Signal::from_c_int(signum) {
|
// let signal = match Signal::from_c_int(signum) {
|
||||||
Ok(SIGFPE) => "floating-point exception",
|
// Ok(SIGFPE) => "floating-point exception",
|
||||||
Ok(SIGILL) => "illegal instruction",
|
// Ok(SIGILL) => "illegal instruction",
|
||||||
Ok(SIGSEGV) => "segmentation violation",
|
// Ok(SIGSEGV) => "segmentation violation",
|
||||||
Ok(SIGBUS) => "bus error",
|
// Ok(SIGBUS) => "bus error",
|
||||||
Err(_) => "error while getting the Signal",
|
// Err(_) => "error while getting the Signal",
|
||||||
_ => "unkown trapped signal",
|
// _ => "unkown trapped signal",
|
||||||
};
|
// };
|
||||||
// When the trap-handler is fully implemented, this will return more information.
|
// // When the trap-handler is fully implemented, this will return more information.
|
||||||
Err(RuntimeError::Trap {
|
// Err(RuntimeError::Trap {
|
||||||
msg: format!("unknown trap at {:p} - {}", faulting_addr, signal).into(),
|
// msg: format!("unknown trap at {:p} - {}", faulting_addr, signal).into(),
|
||||||
}
|
// }
|
||||||
.into())
|
// .into())
|
||||||
|
Err(CallProtError::Trap(WasmTrapInfo::Unknown))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let ret = f(); // TODO: Switch stack?
|
let ret = f(); // TODO: Switch stack?
|
||||||
|
@ -13,6 +13,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
use std::convert::Infallible;
|
||||||
use std::io::{self, Read, Seek, Write};
|
use std::io::{self, Read, Seek, Write};
|
||||||
use wasmer_runtime_core::{debug, memory::Memory, vm::Ctx};
|
use wasmer_runtime_core::{debug, memory::Memory, vm::Ctx};
|
||||||
|
|
||||||
@ -1431,7 +1432,7 @@ pub fn poll_oneoff(
|
|||||||
debug!("wasi::poll_oneoff");
|
debug!("wasi::poll_oneoff");
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
pub fn proc_exit(ctx: &mut Ctx, code: __wasi_exitcode_t) -> Result<(), ExitCode> {
|
pub fn proc_exit(ctx: &mut Ctx, code: __wasi_exitcode_t) -> Result<Infallible, ExitCode> {
|
||||||
debug!("wasi::proc_exit, {}", code);
|
debug!("wasi::proc_exit, {}", code);
|
||||||
Err(ExitCode { code })
|
Err(ExitCode { code })
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user