Trap registry
This commit is contained in:
@@ -38,6 +38,7 @@ mod mmap;
|
||||
mod sig_registry;
|
||||
mod signalhandlers;
|
||||
mod table;
|
||||
mod trap_registry;
|
||||
mod traphandlers;
|
||||
mod vmcontext;
|
||||
|
||||
@@ -50,6 +51,7 @@ pub use crate::jit_int::GdbJitImageRegistration;
|
||||
pub use crate::mmap::Mmap;
|
||||
pub use crate::sig_registry::SignatureRegistry;
|
||||
pub use crate::signalhandlers::{wasmtime_init_eager, wasmtime_init_finish};
|
||||
pub use crate::trap_registry::{get_mut_trap_registry, get_trap_registry, TrapRegistrationGuard};
|
||||
pub use crate::traphandlers::{wasmtime_call, wasmtime_call_trampoline};
|
||||
pub use crate::vmcontext::{
|
||||
VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, VMFunctionImport, VMGlobalDefinition,
|
||||
|
||||
71
wasmtime-runtime/src/trap_registry.rs
Normal file
71
wasmtime-runtime/src/trap_registry.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use cranelift_codegen::ir;
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
lazy_static! {
|
||||
static ref REGISTRY: RwLock<TrapRegistry> = RwLock::new(TrapRegistry::default());
|
||||
}
|
||||
|
||||
/// The registry maintains descriptions of traps in currently allocated functions.
|
||||
#[derive(Default)]
|
||||
pub struct TrapRegistry {
|
||||
traps: HashMap<usize, TrapDescription>,
|
||||
}
|
||||
|
||||
/// Description of a trap.
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub struct TrapDescription {
|
||||
/// Location of the trap in source binary module.
|
||||
pub source_loc: ir::SourceLoc,
|
||||
/// Code of the trap.
|
||||
pub trap_code: ir::TrapCode,
|
||||
}
|
||||
|
||||
/// RAII guard for deregistering traps
|
||||
pub struct TrapRegistrationGuard(usize);
|
||||
|
||||
impl TrapRegistry {
|
||||
/// Registers a new trap.
|
||||
/// Returns a RAII guard that deregisters the trap when dropped.
|
||||
pub fn register_trap(
|
||||
&mut self,
|
||||
address: usize,
|
||||
source_loc: ir::SourceLoc,
|
||||
trap_code: ir::TrapCode,
|
||||
) -> TrapRegistrationGuard {
|
||||
let entry = TrapDescription {
|
||||
source_loc,
|
||||
trap_code,
|
||||
};
|
||||
let previous_trap = self.traps.insert(address, entry);
|
||||
assert!(previous_trap.is_none());
|
||||
TrapRegistrationGuard(address)
|
||||
}
|
||||
|
||||
fn deregister_trap(&mut self, address: usize) {
|
||||
assert!(self.traps.remove(&address).is_some());
|
||||
}
|
||||
|
||||
/// Gets a trap description at given address.
|
||||
pub fn get_trap(&self, address: usize) -> Option<TrapDescription> {
|
||||
self.traps.get(&address).copied()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TrapRegistrationGuard {
|
||||
fn drop(&mut self) {
|
||||
let mut registry = get_mut_trap_registry();
|
||||
registry.deregister_trap(self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets guarded writable reference to traps registry
|
||||
pub fn get_mut_trap_registry() -> RwLockWriteGuard<'static, TrapRegistry> {
|
||||
REGISTRY.write().expect("trap registry lock got poisoned")
|
||||
}
|
||||
|
||||
/// Gets guarded readable reference to traps registry
|
||||
pub fn get_trap_registry() -> RwLockReadGuard<'static, TrapRegistry> {
|
||||
REGISTRY.read().expect("trap registry lock got poisoned")
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
//! WebAssembly trap handling, which is built on top of the lower-level
|
||||
//! signalhandling mechanisms.
|
||||
|
||||
use crate::trap_registry::get_trap_registry;
|
||||
use crate::trap_registry::TrapDescription;
|
||||
use crate::vmcontext::{VMContext, VMFunctionBody};
|
||||
use core::cell::Cell;
|
||||
use core::ptr;
|
||||
use cranelift_codegen::ir;
|
||||
use std::string::String;
|
||||
|
||||
extern "C" {
|
||||
@@ -16,17 +19,44 @@ extern "C" {
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static TRAP_PC: Cell<*const u8> = Cell::new(ptr::null());
|
||||
static RECORDED_TRAP: Cell<Option<TrapDescription>> = Cell::new(None);
|
||||
static JMP_BUF: Cell<*const u8> = Cell::new(ptr::null());
|
||||
}
|
||||
|
||||
/// Check if there is a trap at given PC
|
||||
#[doc(hidden)]
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn CheckIfTrapAtAddress(_pc: *const u8) -> i8 {
|
||||
// TODO: stack overflow can happen at any random time (i.e. in malloc() in memory.grow)
|
||||
// and it's really hard to determine if the cause was stack overflow and if it happened
|
||||
// in WebAssembly module.
|
||||
// So, let's assume that any untrusted code called from WebAssembly doesn't trap.
|
||||
// Then, if we have called some WebAssembly code, it means the trap is stack overflow.
|
||||
JMP_BUF.with(|ptr| !ptr.get().is_null()) as i8
|
||||
}
|
||||
|
||||
/// Record the Trap code and wasm bytecode offset in TLS somewhere
|
||||
#[doc(hidden)]
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn RecordTrap(pc: *const u8) {
|
||||
// TODO: Look up the wasm bytecode offset and trap code and record them instead.
|
||||
TRAP_PC.with(|data| data.set(pc));
|
||||
// TODO: please see explanation in CheckIfTrapAtAddress.
|
||||
let registry = get_trap_registry();
|
||||
let trap_desc = registry
|
||||
.get_trap(pc as usize)
|
||||
.unwrap_or_else(|| TrapDescription {
|
||||
source_loc: ir::SourceLoc::default(),
|
||||
trap_code: ir::TrapCode::StackOverflow,
|
||||
});
|
||||
RECORDED_TRAP.with(|data| {
|
||||
assert_eq!(
|
||||
data.get(),
|
||||
None,
|
||||
"Only one trap per thread can be recorded at a moment!"
|
||||
);
|
||||
data.set(Some(trap_desc))
|
||||
});
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
@@ -50,13 +80,17 @@ pub extern "C" fn LeaveScope(ptr: *const u8) {
|
||||
JMP_BUF.with(|buf| buf.set(ptr))
|
||||
}
|
||||
|
||||
fn trap_message(_vmctx: *mut VMContext) -> String {
|
||||
let pc = TRAP_PC.with(|data| data.replace(ptr::null()));
|
||||
fn trap_message() -> String {
|
||||
let trap_desc = RECORDED_TRAP
|
||||
.with(|data| data.replace(None))
|
||||
.expect("trap_message must be called after trap occurred");
|
||||
|
||||
// TODO: Record trap metadata in the VMContext, and look up the
|
||||
// pc to obtain the TrapCode and SourceLoc.
|
||||
|
||||
format!("wasm trap at {:?}", pc)
|
||||
format!(
|
||||
"wasm trap: code {:?}, source location: {}",
|
||||
// todo print the error message from wast tests
|
||||
trap_desc.trap_code,
|
||||
trap_desc.source_loc,
|
||||
)
|
||||
}
|
||||
|
||||
/// Call the wasm function pointed to by `callee`. `values_vec` points to
|
||||
@@ -69,7 +103,7 @@ pub unsafe extern "C" fn wasmtime_call_trampoline(
|
||||
values_vec: *mut u8,
|
||||
) -> Result<(), String> {
|
||||
if WasmtimeCallTrampoline(vmctx as *mut u8, callee, values_vec) == 0 {
|
||||
Err(trap_message(vmctx))
|
||||
Err(trap_message())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -83,7 +117,7 @@ pub unsafe extern "C" fn wasmtime_call(
|
||||
callee: *const VMFunctionBody,
|
||||
) -> Result<(), String> {
|
||||
if WasmtimeCall(vmctx as *mut u8, callee) == 0 {
|
||||
Err(trap_message(vmctx))
|
||||
Err(trap_message())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user