Expose precise offset information in wasmtime::FrameInfo (#1495)

* Consolidate trap/frame information

This commit removes `TrapRegistry` in favor of consolidating this
information in the `FRAME_INFO` we already have in the `wasmtime` crate.
This allows us to keep information generally in one place and have one
canonical location for "map this PC to some original wasm stuff". The
intent for this is to next update with enough information to go from a
program counter to a position in the original wasm file.

* Expose module offset information in `FrameInfo`

This commit implements functionality for `FrameInfo`, the wasm stack
trace of a `Trap`, to return the module/function offset. This allows
knowing the precise wasm location of each stack frame, instead of only
the main trap itself. The intention here is to provide more visibility
into the wasm source when something traps, so you know precisely where
calls were and where traps were, in order to assist in debugging.
Eventually we might use this information for mapping back to native
source languages as well (given sufficient debug information).

This change makes a previously-optional artifact of compilation always
computed on the cranelift side of things. This `ModuleAddressMap` is
then propagated to the same store of information other frame information
is stored within. This also removes the need for passing a `SourceLoc`
with wasm traps or to wasm trap creation, since the backtrace's wasm
frames will be able to infer their own `SourceLoc` from the relevant
program counters.
This commit is contained in:
Alex Crichton
2020-04-15 08:00:15 -05:00
committed by GitHub
parent c5b6c57c34
commit be85242a3f
18 changed files with 338 additions and 482 deletions

View File

@@ -2,13 +2,11 @@
//! signalhandling mechanisms.
use crate::instance::{InstanceHandle, SignalHandler};
use crate::trap_registry::TrapDescription;
use crate::vmcontext::VMContext;
use backtrace::Backtrace;
use std::any::Any;
use std::cell::Cell;
use std::error::Error;
use std::fmt;
use std::io;
use std::ptr;
use std::sync::Once;
@@ -319,11 +317,19 @@ fn reset_guard_page() {}
pub enum Trap {
/// A user-raised trap through `raise_user_trap`.
User(Box<dyn Error + Send + Sync>),
/// A wasm-originating trap from wasm code itself.
/// A trap raised from jit code
Jit {
/// The program counter in JIT code where this trap happened.
pc: usize,
/// Native stack backtrace at the time the trap occurred
backtrace: Backtrace,
},
/// A trap raised from a wasm libcall
Wasm {
/// What sort of trap happened, as well as where in the original wasm module
/// it happened.
desc: TrapDescription,
/// Code of the trap.
trap_code: ir::TrapCode,
/// Native stack backtrace at the time the trap occurred
backtrace: Backtrace,
},
@@ -335,36 +341,23 @@ pub enum Trap {
},
}
impl fmt::Display for Trap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Trap::User(user) => user.fmt(f),
Trap::Wasm { desc, .. } => desc.fmt(f),
Trap::OOM { .. } => write!(f, "Out of memory"),
}
}
}
impl std::error::Error for Trap {}
impl Trap {
/// Construct a new Wasm trap with the given source location and trap code.
///
/// Internally saves a backtrace when constructed.
pub fn wasm(source_loc: ir::SourceLoc, trap_code: ir::TrapCode) -> Self {
let desc = TrapDescription {
source_loc,
pub fn wasm(trap_code: ir::TrapCode) -> Self {
let backtrace = Backtrace::new_unresolved();
Trap::Wasm {
trap_code,
};
let backtrace = Backtrace::new();
Trap::Wasm { desc, backtrace }
backtrace,
}
}
/// Construct a new OOM trap with the given source location and trap code.
///
/// Internally saves a backtrace when constructed.
pub fn oom() -> Self {
let backtrace = Backtrace::new();
let backtrace = Backtrace::new_unresolved();
Trap::OOM { backtrace }
}
}
@@ -413,7 +406,7 @@ enum UnwindReason {
Panic(Box<dyn Any + Send>),
UserTrap(Box<dyn Error + Send + Sync>),
LibTrap(Trap),
Trap { backtrace: Backtrace, pc: usize },
JitTrap { backtrace: Backtrace, pc: usize },
}
impl CallThreadState {
@@ -442,21 +435,9 @@ impl CallThreadState {
Err(Trap::User(data))
}
UnwindReason::LibTrap(trap) => Err(trap),
UnwindReason::Trap { backtrace, pc } => {
UnwindReason::JitTrap { backtrace, pc } => {
debug_assert_eq!(ret, 0);
let instance = unsafe { InstanceHandle::from_vmctx(self.vmctx) };
Err(Trap::Wasm {
desc: instance
.instance()
.trap_registration
.get_trap(pc)
.unwrap_or_else(|| TrapDescription {
source_loc: ir::SourceLoc::default(),
trap_code: ir::TrapCode::StackOverflow,
}),
backtrace,
})
Err(Trap::Jit { pc, backtrace })
}
UnwindReason::Panic(panic) => {
debug_assert_eq!(ret, 0);
@@ -546,7 +527,7 @@ impl CallThreadState {
}
let backtrace = Backtrace::new_unresolved();
self.reset_guard_page.set(reset_guard_page);
self.unwind.replace(UnwindReason::Trap {
self.unwind.replace(UnwindReason::JitTrap {
backtrace,
pc: pc as usize,
});