2019-03-17 10:27:14 +08:00
|
|
|
//! Installing signal handlers allows us to handle traps and out-of-bounds memory
|
2019-05-13 11:18:57 -07:00
|
|
|
//! accesses that occur when runniing WebAssembly.
|
2019-03-17 10:27:14 +08:00
|
|
|
//!
|
|
|
|
//! 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 11:53:26 +08:00
|
|
|
use std::any::Any;
|
2019-06-27 00:41:07 +08:00
|
|
|
use std::cell::{Cell, RefCell};
|
2019-04-27 16:31:47 +08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
2019-06-27 00:41:07 +08:00
|
|
|
use wasmer_runtime_core::alternative_stack::{
|
|
|
|
begin_unsafe_unwind, catch_unsafe_unwind, ensure_sighandler,
|
|
|
|
};
|
2019-04-27 16:31:47 +08:00
|
|
|
use wasmer_runtime_core::codegen::BkptInfo;
|
2019-04-22 15:06:40 -07:00
|
|
|
use wasmer_runtime_core::typed_func::WasmTrapInfo;
|
2019-03-17 10:27:14 +08:00
|
|
|
|
|
|
|
thread_local! {
|
2019-04-11 11:52:57 +08:00
|
|
|
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None);
|
2019-04-27 16:31:47 +08:00
|
|
|
pub static BKPT_MAP: RefCell<Vec<Arc<HashMap<usize, Box<Fn(BkptInfo) + Send + Sync + 'static>>>>> = RefCell::new(Vec::new());
|
2019-04-11 11:52:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn trigger_trap() -> ! {
|
2019-06-27 00:41:07 +08:00
|
|
|
begin_unsafe_unwind(Box::new(()));
|
2019-03-17 10:27:14 +08:00
|
|
|
}
|
|
|
|
|
2019-04-22 15:36:47 -07:00
|
|
|
pub enum CallProtError {
|
2019-04-22 15:06:40 -07:00
|
|
|
Trap(WasmTrapInfo),
|
|
|
|
Error(Box<dyn Any>),
|
|
|
|
}
|
|
|
|
|
2019-04-22 15:36:47 -07:00
|
|
|
pub fn call_protected<T>(f: impl FnOnce() -> T) -> Result<T, CallProtError> {
|
2019-06-27 00:41:07 +08:00
|
|
|
ensure_sighandler();
|
2019-03-17 10:27:14 +08:00
|
|
|
unsafe {
|
2019-06-27 00:41:07 +08:00
|
|
|
let ret = catch_unsafe_unwind(|| f());
|
|
|
|
match ret {
|
|
|
|
Ok(x) => Ok(x),
|
|
|
|
Err(_) => {
|
|
|
|
if let Some(data) = TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
|
|
|
|
Err(CallProtError::Error(data))
|
|
|
|
} else {
|
|
|
|
Err(CallProtError::Trap(WasmTrapInfo::Unknown))
|
|
|
|
}
|
2019-03-17 10:27:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 11:51:33 +08:00
|
|
|
pub unsafe fn throw(payload: Box<dyn Any>) -> ! {
|
2019-06-27 00:41:07 +08:00
|
|
|
begin_unsafe_unwind(payload);
|
2019-03-17 10:27:14 +08:00
|
|
|
}
|