[wasmtime-api] Record original Trap from API callback. (#657)

* Record original Trap from API callback.

Fixes #645

* use TrapRegistry

* comment about magic number
This commit is contained in:
Yury Delendik
2019-12-04 07:57:24 -06:00
committed by GitHub
parent fd8a5f62ed
commit 991592c4ba
6 changed files with 170 additions and 29 deletions

View File

@@ -0,0 +1,54 @@
use std::cell::Cell;
use super::binemit;
use super::ir::{SourceLoc, TrapCode};
use crate::r#ref::HostRef;
use crate::Trap;
use wasmtime_environ::TrapInformation;
// Randomly selected user TrapCode magic number 13.
pub const API_TRAP_CODE: TrapCode = TrapCode::User(13);
thread_local! {
static RECORDED_API_TRAP: Cell<Option<HostRef<Trap>>> = Cell::new(None);
}
pub fn record_api_trap(trap: HostRef<Trap>) {
RECORDED_API_TRAP.with(|data| {
let trap = Cell::new(Some(trap));
data.swap(&trap);
assert!(
trap.take().is_none(),
"Only one API trap per thread can be recorded at a moment!"
);
});
}
pub fn take_api_trap() -> Option<HostRef<Trap>> {
RECORDED_API_TRAP.with(|data| data.take())
}
pub(crate) struct TrapSink {
pub traps: Vec<TrapInformation>,
}
impl TrapSink {
pub fn new() -> Self {
Self { traps: Vec::new() }
}
}
impl binemit::TrapSink for TrapSink {
fn trap(
&mut self,
code_offset: binemit::CodeOffset,
source_loc: SourceLoc,
trap_code: TrapCode,
) {
self.traps.push(TrapInformation {
code_offset,
source_loc,
trap_code,
});
}
}