From 4d9e10dae109ccac4af5690685126e16ee850605 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 31 May 2022 10:39:11 -0500 Subject: [PATCH] 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`. --- crates/c-api/src/trap.rs | 14 ++------------ crates/wasmtime/src/config.rs | 1 - crates/wasmtime/src/trap.rs | 8 ++++++++ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/crates/c-api/src/trap.rs b/crates/c-api/src/trap.rs index 213bb9740f..6f60709fe4 100644 --- a/crates/c-api/src/trap.rs +++ b/crates/c-api/src/trap.rs @@ -64,13 +64,7 @@ pub extern "C" fn wasm_trap_message(trap: &wasm_trap_t, out: &mut wasm_message_t #[no_mangle] pub extern "C" fn wasm_trap_origin(raw: &wasm_trap_t) -> Option> { - if raw - .trap - .trace() - .expect("backtraces are always enabled") - .len() - > 0 - { + if raw.trap.trace().unwrap_or(&[]).len() > 0 { Some(Box::new(wasm_frame_t { trap: raw.trap.clone(), idx: 0, @@ -84,11 +78,7 @@ pub extern "C" fn wasm_trap_origin(raw: &wasm_trap_t) -> Option &mut Self { self.wasm_backtrace = enable; #[cfg(compiler)] diff --git a/crates/wasmtime/src/trap.rs b/crates/wasmtime/src/trap.rs index 977e8bd050..85d61ee6f9 100644 --- a/crates/wasmtime/src/trap.rs +++ b/crates/wasmtime/src/trap.rs @@ -295,6 +295,14 @@ impl Trap { /// Returns a list of function frames in WebAssembly code that led to this /// 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]> { self.inner .backtrace