Fix panics in the C API related to trap frames (#4196)

The `wasmtime-cpp` test suite uncovered an issue where asking for the
frames of a trap would fail immediately after the trap was created. In
addition to fixing this issue I've also updated the documentation of
`Trap::frames` to indicate when it returns `None`.
This commit is contained in:
Alex Crichton
2022-05-31 10:39:11 -05:00
committed by GitHub
parent 7d3639522e
commit 4d9e10dae1
3 changed files with 10 additions and 13 deletions

View File

@@ -64,13 +64,7 @@ pub extern "C" fn wasm_trap_message(trap: &wasm_trap_t, out: &mut wasm_message_t
#[no_mangle] #[no_mangle]
pub extern "C" fn wasm_trap_origin(raw: &wasm_trap_t) -> Option<Box<wasm_frame_t>> { pub extern "C" fn wasm_trap_origin(raw: &wasm_trap_t) -> Option<Box<wasm_frame_t>> {
if raw if raw.trap.trace().unwrap_or(&[]).len() > 0 {
.trap
.trace()
.expect("backtraces are always enabled")
.len()
> 0
{
Some(Box::new(wasm_frame_t { Some(Box::new(wasm_frame_t {
trap: raw.trap.clone(), trap: raw.trap.clone(),
idx: 0, idx: 0,
@@ -84,11 +78,7 @@ pub extern "C" fn wasm_trap_origin(raw: &wasm_trap_t) -> Option<Box<wasm_frame_t
#[no_mangle] #[no_mangle]
pub extern "C" fn wasm_trap_trace(raw: &wasm_trap_t, out: &mut wasm_frame_vec_t) { pub extern "C" fn wasm_trap_trace(raw: &wasm_trap_t, out: &mut wasm_frame_vec_t) {
let vec = (0..raw let vec = (0..raw.trap.trace().unwrap_or(&[]).len())
.trap
.trace()
.expect("backtraces are always enabled")
.len())
.map(|idx| { .map(|idx| {
Some(Box::new(wasm_frame_t { Some(Box::new(wasm_frame_t {
trap: raw.trap.clone(), trap: raw.trap.clone(),

View File

@@ -288,7 +288,6 @@ impl Config {
/// ///
/// When disabled, wasm backtrace details are ignored, and [`crate::Trap::trace()`] /// When disabled, wasm backtrace details are ignored, and [`crate::Trap::trace()`]
/// will always return `None`. /// will always return `None`.
pub fn wasm_backtrace(&mut self, enable: bool) -> &mut Self { pub fn wasm_backtrace(&mut self, enable: bool) -> &mut Self {
self.wasm_backtrace = enable; self.wasm_backtrace = enable;
#[cfg(compiler)] #[cfg(compiler)]

View File

@@ -295,6 +295,14 @@ impl Trap {
/// Returns a list of function frames in WebAssembly code that led to this /// Returns a list of function frames in WebAssembly code that led to this
/// trap happening. /// trap happening.
///
/// This function return an `Option` of a list of frames to indicate that
/// wasm frames are not always available. Frames will never be available if
/// backtraces are disabled via
/// [`Config::wasm_backtrace`](crate::Config::wasm_backtrace). Frames will
/// also not be available for freshly-created traps. WebAssembly frames are
/// currently only captured when the trap reaches wasm itself to get raised
/// across a wasm boundary.
pub fn trace(&self) -> Option<&[FrameInfo]> { pub fn trace(&self) -> Option<&[FrameInfo]> {
self.inner self.inner
.backtrace .backtrace