wasmer/lib/singlepass-backend/src/protect_unix.rs

54 lines
1.7 KiB
Rust
Raw Normal View History

//! Installing signal handlers allows us to handle traps and out-of-bounds memory
2019-05-13 18:18:57 +00:00
//! accesses that occur when runniing WebAssembly.
//!
//! This code is inspired by: https://github.com/pepyakin/wasmtime/commit/625a2b6c0815b21996e111da51b9664feb174622
//!
//! When a WebAssembly module triggers any traps, we perform recovery here.
//!
//! This module uses TLS (thread-local storage) to track recovery information. Since the four signals we're handling
//! are very special, the async signal unsafety of Rust's TLS implementation generally does not affect the correctness here
//! unless you have memory unsafety elsewhere in your code.
//!
2019-04-11 03:53:26 +00:00
use std::any::Any;
2019-06-27 07:49:43 +00:00
use std::cell::Cell;
2019-07-03 17:45:06 +00:00
use wasmer_runtime_core::codegen::BreakpointMap;
2019-07-03 17:45:54 +00:00
use wasmer_runtime_core::fault::{begin_unsafe_unwind, catch_unsafe_unwind, ensure_sighandler};
2019-04-22 22:06:40 +00:00
use wasmer_runtime_core::typed_func::WasmTrapInfo;
thread_local! {
2019-04-11 03:52:57 +00:00
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None);
}
pub unsafe fn trigger_trap() -> ! {
begin_unsafe_unwind(Box::new(()));
}
pub enum CallProtError {
2019-04-22 22:06:40 +00:00
Trap(WasmTrapInfo),
Error(Box<dyn Any>),
}
2019-06-27 07:49:43 +00:00
pub fn call_protected<T>(
f: impl FnOnce() -> T,
2019-07-03 17:45:06 +00:00
breakpoints: Option<BreakpointMap>,
2019-06-27 07:49:43 +00:00
) -> Result<T, CallProtError> {
ensure_sighandler();
unsafe {
2019-06-27 07:49:43 +00:00
let ret = catch_unsafe_unwind(|| f(), breakpoints);
match ret {
Ok(x) => Ok(x),
2019-06-27 07:49:43 +00:00
Err(e) => {
if let Some(data) = TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
Err(CallProtError::Error(data))
} else {
2019-06-27 07:49:43 +00:00
Err(CallProtError::Error(e))
}
}
}
}
}
pub unsafe fn throw(payload: Box<dyn Any>) -> ! {
begin_unsafe_unwind(payload);
}