* Remove the `jit_function_registry` global state This commit removes on the final pieces of global state in wasmtime today, the `jit_function_registry` module. The purpose of this module is to help translate a native backtrace with native program counters into a wasm backtrace with module names, function names, and wasm module indices. To that end this module retained a global map of function ranges to this metadata information for each compiled function. It turns out that we already had a `NAMES` global in the `wasmtime` crate for symbolicating backtrace addresses, so this commit moves that global into its own file and restructures the internals to account for program counter ranges as well. The general set of changes here are: * Remove `jit_function_registry` * Remove `NAMES` * Create a new `frame_info` module which has a singleton global registering compiled module's frame information. * Update traps to use the `frame_info` module to symbolicate pcs, directly extracting a `FrameInfo` from the module. * Register and unregister information on a module level instead of on a per-function level (at least in terms of locking granluarity). This commit leaves the new `FRAME_INFO` global variable as the only remaining "critical" global variable in `wasmtime`, which only exists due to the API of `Trap` where it doesn't take in any extra context when capturing a stack trace through which we could hang off frame information. I'm thinking though that this is ok, and we can always tweak the API of `Trap` in the future if necessary if we truly need to accomodate this. * Remove a lazy_static dep * Add some comments and restructure
120 lines
3.7 KiB
Rust
120 lines
3.7 KiB
Rust
use crate::frame_info::FRAME_INFO;
|
|
use crate::FrameInfo;
|
|
use backtrace::Backtrace;
|
|
use std::fmt;
|
|
use std::sync::Arc;
|
|
|
|
/// A struct representing an aborted instruction execution, with a message
|
|
/// indicating the cause.
|
|
#[derive(Clone)]
|
|
pub struct Trap {
|
|
inner: Arc<TrapInner>,
|
|
}
|
|
|
|
struct TrapInner {
|
|
message: String,
|
|
wasm_trace: Vec<FrameInfo>,
|
|
native_trace: Backtrace,
|
|
}
|
|
|
|
fn _assert_trap_is_sync_and_send(t: &Trap) -> (&dyn Sync, &dyn Send) {
|
|
(t, t)
|
|
}
|
|
|
|
impl Trap {
|
|
/// Creates a new `Trap` with `message`.
|
|
/// # Example
|
|
/// ```
|
|
/// let trap = wasmtime::Trap::new("unexpected error");
|
|
/// assert_eq!("unexpected error", trap.message());
|
|
/// ```
|
|
pub fn new<I: Into<String>>(message: I) -> Self {
|
|
Trap::new_with_trace(message.into(), Backtrace::new_unresolved())
|
|
}
|
|
|
|
pub(crate) fn from_jit(jit: wasmtime_runtime::Trap) -> Self {
|
|
match jit {
|
|
wasmtime_runtime::Trap::User(error) => {
|
|
// Since we're the only one using the wasmtime internals (in
|
|
// theory) we should only see user errors which were originally
|
|
// created from our own `Trap` type (see the trampoline module
|
|
// with functions).
|
|
//
|
|
// If this unwrap trips for someone we'll need to tweak the
|
|
// return type of this function to probably be `anyhow::Error`
|
|
// or something like that.
|
|
*error
|
|
.downcast()
|
|
.expect("only `Trap` user errors are supported")
|
|
}
|
|
wasmtime_runtime::Trap::Wasm { desc, backtrace } => {
|
|
Trap::new_with_trace(desc.to_string(), backtrace)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn new_with_trace(message: String, native_trace: Backtrace) -> Self {
|
|
let mut wasm_trace = Vec::new();
|
|
for frame in native_trace.frames() {
|
|
let pc = frame.ip() as usize;
|
|
if let Some(info) = FRAME_INFO.lookup(pc) {
|
|
wasm_trace.push(info);
|
|
}
|
|
}
|
|
Trap {
|
|
inner: Arc::new(TrapInner {
|
|
message,
|
|
wasm_trace,
|
|
native_trace,
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Returns a reference the `message` stored in `Trap`.
|
|
pub fn message(&self) -> &str {
|
|
&self.inner.message
|
|
}
|
|
|
|
/// Returns a list of function frames in WebAssembly code that led to this
|
|
/// trap happening.
|
|
pub fn trace(&self) -> &[FrameInfo] {
|
|
&self.inner.wasm_trace
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Trap {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.debug_struct("Trap")
|
|
.field("message", &self.inner.message)
|
|
.field("wasm_trace", &self.inner.wasm_trace)
|
|
.field("native_trace", &self.inner.native_trace)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Trap {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}", self.inner.message)?;
|
|
let trace = self.trace();
|
|
if trace.is_empty() {
|
|
return Ok(());
|
|
}
|
|
writeln!(f, "\nwasm backtrace:")?;
|
|
for (i, frame) in self.trace().iter().enumerate() {
|
|
let name = frame.module_name().unwrap_or("<unknown>");
|
|
write!(f, " {}: {}!", i, name)?;
|
|
match frame.func_name() {
|
|
Some(name) => match rustc_demangle::try_demangle(name) {
|
|
Ok(name) => write!(f, "{}", name)?,
|
|
Err(_) => write!(f, "{}", name)?,
|
|
},
|
|
None => write!(f, "<wasm function {}>", frame.func_index())?,
|
|
}
|
|
writeln!(f, "")?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for Trap {}
|