dusk_wasmtime/runtime/
trap.rs

1#[cfg(feature = "coredump")]
2use super::coredump::WasmCoreDump;
3use crate::store::StoreOpaque;
4use crate::{AsContext, Module};
5use anyhow::Error;
6use std::fmt;
7use wasmtime_environ::{
8    demangle_function_name, demangle_function_name_or_index, EntityRef, FilePos,
9};
10
11/// Representation of a WebAssembly trap and what caused it to occur.
12///
13/// WebAssembly traps happen explicitly for instructions such as `unreachable`
14/// but can also happen as side effects of other instructions such as `i32.load`
15/// loading an out-of-bounds address. Traps halt the execution of WebAssembly
16/// and cause an error to be returned to the host. This enumeration is a list of
17/// all possible traps that can happen in wasm, in addition to some
18/// Wasmtime-specific trap codes listed here as well.
19///
20/// # Errors in Wasmtime
21///
22/// Error-handling in Wasmtime is primarily done through the [`anyhow`] crate
23/// where most results are a [`Result<T>`](anyhow::Result) which is an alias for
24/// [`Result<T, anyhow::Error>`](std::result::Result). Errors in Wasmtime are
25/// represented with [`anyhow::Error`] which acts as a container for any type of
26/// error in addition to optional context for this error. The "base" error or
27/// [`anyhow::Error::root_cause`] is a [`Trap`] whenever WebAssembly hits a
28/// trap, or otherwise it's whatever the host created the error with when
29/// returning an error for a host call.
30///
31/// Any error which happens while WebAssembly is executing will also, by
32/// default, capture a backtrace of the wasm frames while executing. This
33/// backtrace is represented with a [`WasmBacktrace`] instance and is attached
34/// to the [`anyhow::Error`] return value as a
35/// [`context`](anyhow::Error::context). Inspecting a [`WasmBacktrace`] can be
36/// done with the [`downcast_ref`](anyhow::Error::downcast_ref) function. For
37/// information on this see the [`WasmBacktrace`] documentation.
38///
39/// # Examples
40///
41/// ```
42/// # use wasmtime::*;
43/// # fn main() -> Result<()> {
44/// let engine = Engine::default();
45/// let module = Module::new(
46///     &engine,
47///     r#"
48///         (module
49///             (func (export "trap")
50///                 unreachable)
51///             (func $overflow (export "overflow")
52///                 call $overflow)
53///         )
54///     "#,
55/// )?;
56/// let mut store = Store::new(&engine, ());
57/// let instance = Instance::new(&mut store, &module, &[])?;
58///
59/// let trap = instance.get_typed_func::<(), ()>(&mut store, "trap")?;
60/// let error = trap.call(&mut store, ()).unwrap_err();
61/// assert_eq!(*error.downcast_ref::<Trap>().unwrap(), Trap::UnreachableCodeReached);
62/// assert!(error.root_cause().is::<Trap>());
63///
64/// let overflow = instance.get_typed_func::<(), ()>(&mut store, "overflow")?;
65/// let error = overflow.call(&mut store, ()).unwrap_err();
66/// assert_eq!(*error.downcast_ref::<Trap>().unwrap(), Trap::StackOverflow);
67/// # Ok(())
68/// # }
69/// ```
70pub use wasmtime_environ::Trap;
71
72// Same safety requirements and caveats as
73// `wasmtime_runtime::raise_user_trap`.
74pub(crate) unsafe fn raise(error: anyhow::Error) -> ! {
75    let needs_backtrace = error.downcast_ref::<WasmBacktrace>().is_none();
76    wasmtime_runtime::raise_user_trap(error, needs_backtrace)
77}
78
79#[cold] // traps are exceptional, this helps move handling off the main path
80pub(crate) fn from_runtime_box(
81    store: &mut StoreOpaque,
82    runtime_trap: Box<wasmtime_runtime::Trap>,
83) -> Error {
84    let wasmtime_runtime::Trap {
85        reason,
86        backtrace,
87        coredumpstack,
88    } = *runtime_trap;
89    let (mut error, pc) = match reason {
90        // For user-defined errors they're already an `anyhow::Error` so no
91        // conversion is really necessary here, but a `backtrace` may have
92        // been captured so it's attempted to get inserted here.
93        //
94        // If the error is actually a `Trap` then the backtrace is inserted
95        // directly into the `Trap` since there's storage there for it.
96        // Otherwise though this represents a host-defined error which isn't
97        // using a `Trap` but instead some other condition that was fatal to
98        // wasm itself. In that situation the backtrace is inserted as
99        // contextual information on error using `error.context(...)` to
100        // provide useful information to debug with for the embedder/caller,
101        // otherwise the information about what the wasm was doing when the
102        // error was generated would be lost.
103        wasmtime_runtime::TrapReason::User {
104            error,
105            needs_backtrace,
106        } => {
107            debug_assert!(
108                needs_backtrace == backtrace.is_some() || !store.engine().config().wasm_backtrace
109            );
110            (error, None)
111        }
112        wasmtime_runtime::TrapReason::Jit {
113            pc,
114            faulting_addr,
115            trap,
116        } => {
117            let mut err: Error = trap.into();
118
119            // If a fault address was present, for example with segfaults,
120            // then simultaneously assert that it's within a known linear memory
121            // and additionally translate it to a wasm-local address to be added
122            // as context to the error.
123            if let Some(fault) = faulting_addr.and_then(|addr| store.wasm_fault(pc, addr)) {
124                err = err.context(fault);
125            }
126            (err, Some(pc))
127        }
128        wasmtime_runtime::TrapReason::Wasm(trap_code) => (trap_code.into(), None),
129    };
130
131    if let Some(bt) = backtrace {
132        let bt = WasmBacktrace::from_captured(store, bt, pc);
133        if !bt.wasm_trace.is_empty() {
134            error = error.context(bt);
135        }
136    }
137
138    let _ = &coredumpstack;
139    #[cfg(feature = "coredump")]
140    if let Some(coredump) = coredumpstack {
141        let bt = WasmBacktrace::from_captured(store, coredump.bt, pc);
142        let cd = WasmCoreDump::new(store, bt);
143        error = error.context(cd);
144    }
145
146    error
147}
148
149/// Representation of a backtrace of function frames in a WebAssembly module for
150/// where an error happened.
151///
152/// This structure is attached to the [`anyhow::Error`] returned from many
153/// Wasmtime functions that execute WebAssembly such as [`Instance::new`] or
154/// [`Func::call`]. This can be acquired with the [`anyhow::Error::downcast`]
155/// family of methods to programmatically inspect the backtrace. Otherwise since
156/// it's part of the error returned this will get printed along with the rest of
157/// the error when the error is logged.
158///
159/// Capturing of wasm backtraces can be configured through the
160/// [`Config::wasm_backtrace`](crate::Config::wasm_backtrace) method.
161///
162/// For more information about errors in wasmtime see the documentation of the
163/// [`Trap`] type.
164///
165/// [`Func::call`]: crate::Func::call
166/// [`Instance::new`]: crate::Instance::new
167///
168/// # Examples
169///
170/// ```
171/// # use wasmtime::*;
172/// # fn main() -> Result<()> {
173/// let engine = Engine::default();
174/// let module = Module::new(
175///     &engine,
176///     r#"
177///         (module
178///             (func $start (export "run")
179///                 call $trap)
180///             (func $trap
181///                 unreachable)
182///         )
183///     "#,
184/// )?;
185/// let mut store = Store::new(&engine, ());
186/// let instance = Instance::new(&mut store, &module, &[])?;
187/// let func = instance.get_typed_func::<(), ()>(&mut store, "run")?;
188/// let error = func.call(&mut store, ()).unwrap_err();
189/// let bt = error.downcast_ref::<WasmBacktrace>().unwrap();
190/// let frames = bt.frames();
191/// assert_eq!(frames.len(), 2);
192/// assert_eq!(frames[0].func_name(), Some("trap"));
193/// assert_eq!(frames[1].func_name(), Some("start"));
194/// # Ok(())
195/// # }
196/// ```
197#[derive(Debug)]
198pub struct WasmBacktrace {
199    wasm_trace: Vec<FrameInfo>,
200    hint_wasm_backtrace_details_env: bool,
201    // This is currently only present for the `Debug` implementation for extra
202    // context.
203    #[allow(dead_code)]
204    runtime_trace: wasmtime_runtime::Backtrace,
205}
206
207impl WasmBacktrace {
208    /// Captures a trace of the WebAssembly frames on the stack for the
209    /// provided store.
210    ///
211    /// This will return a [`WasmBacktrace`] which holds captured
212    /// [`FrameInfo`]s for each frame of WebAssembly on the call stack of the
213    /// current thread. If no WebAssembly is on the stack then the returned
214    /// backtrace will have no frames in it.
215    ///
216    /// Note that this function will respect the [`Config::wasm_backtrace`]
217    /// configuration option and will return an empty backtrace if that is
218    /// disabled. To always capture a backtrace use the
219    /// [`WasmBacktrace::force_capture`] method.
220    ///
221    /// Also note that this function will only capture frames from the
222    /// specified `store` on the stack, ignoring frames from other stores if
223    /// present.
224    ///
225    /// [`Config::wasm_backtrace`]: crate::Config::wasm_backtrace
226    ///
227    /// # Example
228    ///
229    /// ```
230    /// # use wasmtime::*;
231    /// # fn main() -> Result<()> {
232    /// let engine = Engine::default();
233    /// let module = Module::new(
234    ///     &engine,
235    ///     r#"
236    ///         (module
237    ///             (import "" "" (func $host))
238    ///             (func $foo (export "f") call $bar)
239    ///             (func $bar call $host)
240    ///         )
241    ///     "#,
242    /// )?;
243    ///
244    /// let mut store = Store::new(&engine, ());
245    /// let func = Func::wrap(&mut store, |cx: Caller<'_, ()>| {
246    ///     let trace = WasmBacktrace::capture(&cx);
247    ///     println!("{trace:?}");
248    /// });
249    /// let instance = Instance::new(&mut store, &module, &[func.into()])?;
250    /// let func = instance.get_typed_func::<(), ()>(&mut store, "f")?;
251    /// func.call(&mut store, ())?;
252    /// # Ok(())
253    /// # }
254    /// ```
255    pub fn capture(store: impl AsContext) -> WasmBacktrace {
256        let store = store.as_context();
257        if store.engine().config().wasm_backtrace {
258            Self::force_capture(store)
259        } else {
260            WasmBacktrace {
261                wasm_trace: Vec::new(),
262                hint_wasm_backtrace_details_env: false,
263                runtime_trace: wasmtime_runtime::Backtrace::empty(),
264            }
265        }
266    }
267
268    /// Unconditionally captures a trace of the WebAssembly frames on the stack
269    /// for the provided store.
270    ///
271    /// Same as [`WasmBacktrace::capture`] except that it disregards the
272    /// [`Config::wasm_backtrace`](crate::Config::wasm_backtrace) setting and
273    /// always captures a backtrace.
274    pub fn force_capture(store: impl AsContext) -> WasmBacktrace {
275        let store = store.as_context();
276        Self::from_captured(
277            store.0,
278            wasmtime_runtime::Backtrace::new(store.0.runtime_limits()),
279            None,
280        )
281    }
282
283    fn from_captured(
284        store: &StoreOpaque,
285        runtime_trace: wasmtime_runtime::Backtrace,
286        trap_pc: Option<usize>,
287    ) -> Self {
288        let mut wasm_trace = Vec::<FrameInfo>::with_capacity(runtime_trace.frames().len());
289        let mut hint_wasm_backtrace_details_env = false;
290        let wasm_backtrace_details_env_used =
291            store.engine().config().wasm_backtrace_details_env_used;
292
293        for frame in runtime_trace.frames() {
294            debug_assert!(frame.pc() != 0);
295
296            // Note that we need to be careful about the pc we pass in
297            // here to lookup frame information. This program counter is
298            // used to translate back to an original source location in
299            // the origin wasm module. If this pc is the exact pc that
300            // the trap happened at, then we look up that pc precisely.
301            // Otherwise backtrace information typically points at the
302            // pc *after* the call instruction (because otherwise it's
303            // likely a call instruction on the stack). In that case we
304            // want to lookup information for the previous instruction
305            // (the call instruction) so we subtract one as the lookup.
306            let pc_to_lookup = if Some(frame.pc()) == trap_pc {
307                frame.pc()
308            } else {
309                frame.pc() - 1
310            };
311
312            // NB: The PC we are looking up _must_ be a Wasm PC since
313            // `wasmtime_runtime::Backtrace` only contains Wasm frames.
314            //
315            // However, consider the case where we have multiple, nested calls
316            // across stores (with host code in between, by necessity, since
317            // only things in the same store can be linked directly together):
318            //
319            //     | ...             |
320            //     | Host            |  |
321            //     +-----------------+  | stack
322            //     | Wasm in store A |  | grows
323            //     +-----------------+  | down
324            //     | Host            |  |
325            //     +-----------------+  |
326            //     | Wasm in store B |  V
327            //     +-----------------+
328            //
329            // In this scenario, the `wasmtime_runtime::Backtrace` will contain
330            // two frames: Wasm in store B followed by Wasm in store A. But
331            // `store.modules()` will only have the module information for
332            // modules instantiated within this store. Therefore, we use `if let
333            // Some(..)` instead of the `unwrap` you might otherwise expect and
334            // we ignore frames from modules that were not registered in this
335            // store's module registry.
336            if let Some((info, module)) = store.modules().lookup_frame_info(pc_to_lookup) {
337                wasm_trace.push(info);
338
339                // If this frame has unparsed debug information and the
340                // store's configuration indicates that we were
341                // respecting the environment variable of whether to
342                // do this then we will print out a helpful note in
343                // `Display` to indicate that more detailed information
344                // in a trap may be available.
345                let has_unparsed_debuginfo = module.compiled_module().has_unparsed_debuginfo();
346                if has_unparsed_debuginfo && wasm_backtrace_details_env_used {
347                    hint_wasm_backtrace_details_env = true;
348                }
349            }
350        }
351
352        Self {
353            wasm_trace,
354            runtime_trace,
355            hint_wasm_backtrace_details_env,
356        }
357    }
358
359    /// Returns a list of function frames in WebAssembly this backtrace
360    /// represents.
361    pub fn frames(&self) -> &[FrameInfo] {
362        self.wasm_trace.as_slice()
363    }
364}
365
366impl fmt::Display for WasmBacktrace {
367    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
368        writeln!(f, "error while executing at wasm backtrace:")?;
369
370        let mut needs_newline = false;
371        for (i, frame) in self.wasm_trace.iter().enumerate() {
372            // Avoid putting a trailing newline on the output
373            if needs_newline {
374                writeln!(f, "")?;
375            } else {
376                needs_newline = true;
377            }
378            let name = frame.module().name().unwrap_or("<unknown>");
379            write!(f, "  {:>3}: ", i)?;
380
381            if let Some(offset) = frame.module_offset() {
382                write!(f, "{:#6x} - ", offset)?;
383            }
384
385            let write_raw_func_name = |f: &mut fmt::Formatter<'_>| {
386                demangle_function_name_or_index(f, frame.func_name(), frame.func_index() as usize)
387            };
388            if frame.symbols().is_empty() {
389                write!(f, "{}!", name)?;
390                write_raw_func_name(f)?;
391            } else {
392                for (i, symbol) in frame.symbols().iter().enumerate() {
393                    if i > 0 {
394                        write!(f, "              - ")?;
395                    } else {
396                        // ...
397                    }
398                    match symbol.name() {
399                        Some(name) => demangle_function_name(f, name)?,
400                        None if i == 0 => write_raw_func_name(f)?,
401                        None => write!(f, "<inlined function>")?,
402                    }
403                    if let Some(file) = symbol.file() {
404                        writeln!(f, "")?;
405                        write!(f, "                    at {}", file)?;
406                        if let Some(line) = symbol.line() {
407                            write!(f, ":{}", line)?;
408                            if let Some(col) = symbol.column() {
409                                write!(f, ":{}", col)?;
410                            }
411                        }
412                    }
413                }
414            }
415        }
416        if self.hint_wasm_backtrace_details_env {
417            write!(f, "\nnote: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information")?;
418        }
419        Ok(())
420    }
421}
422
423/// Description of a frame in a backtrace for a [`WasmBacktrace`].
424///
425/// Whenever an error happens while WebAssembly is executing a
426/// [`WasmBacktrace`] will be attached to the error returned which can be used
427/// to acquire this `FrameInfo`. For more information see [`WasmBacktrace`].
428#[derive(Debug)]
429pub struct FrameInfo {
430    module: Module,
431    func_index: u32,
432    func_name: Option<String>,
433    func_start: FilePos,
434    instr: Option<FilePos>,
435    symbols: Vec<FrameSymbol>,
436}
437
438impl FrameInfo {
439    /// Fetches frame information about a program counter in a backtrace.
440    ///
441    /// Returns an object if this `pc` is known to this module, or returns `None`
442    /// if no information can be found.
443    pub(crate) fn new(module: Module, text_offset: usize) -> Option<FrameInfo> {
444        let compiled_module = module.compiled_module();
445        let (index, _func_offset) = compiled_module.func_by_text_offset(text_offset)?;
446        let info = compiled_module.wasm_func_info(index);
447        let func_start = info.start_srcloc;
448        let instr = wasmtime_environ::lookup_file_pos(
449            compiled_module.code_memory().address_map_data(),
450            text_offset,
451        );
452        let index = compiled_module.module().func_index(index);
453        let func_index = index.index() as u32;
454        let func_name = compiled_module.func_name(index).map(|s| s.to_string());
455
456        // In debug mode for now assert that we found a mapping for `pc` within
457        // the function, because otherwise something is buggy along the way and
458        // not accounting for all the instructions. This isn't super critical
459        // though so we can omit this check in release mode.
460        //
461        // Note that if the module doesn't even have an address map due to
462        // compilation settings then it's expected that `instr` is `None`.
463        debug_assert!(
464            instr.is_some() || !compiled_module.has_address_map(),
465            "failed to find instruction for {:#x}",
466            text_offset
467        );
468
469        // Use our wasm-relative pc to symbolize this frame. If there's a
470        // symbolication context (dwarf debug info) available then we can try to
471        // look this up there.
472        //
473        // Note that dwarf pcs are code-section-relative, hence the subtraction
474        // from the location of `instr`. Also note that all errors are ignored
475        // here for now since technically wasm modules can always have any
476        // custom section contents.
477        let mut symbols = Vec::new();
478
479        let _ = &mut symbols;
480        #[cfg(feature = "addr2line")]
481        if let Some(s) = &compiled_module.symbolize_context().ok().and_then(|c| c) {
482            if let Some(offset) = instr.and_then(|i| i.file_offset()) {
483                let to_lookup = u64::from(offset) - s.code_section_offset();
484                if let Ok(mut frames) = s.addr2line().find_frames(to_lookup).skip_all_loads() {
485                    while let Ok(Some(frame)) = frames.next() {
486                        symbols.push(FrameSymbol {
487                            name: frame
488                                .function
489                                .as_ref()
490                                .and_then(|l| l.raw_name().ok())
491                                .map(|s| s.to_string()),
492                            file: frame
493                                .location
494                                .as_ref()
495                                .and_then(|l| l.file)
496                                .map(|s| s.to_string()),
497                            line: frame.location.as_ref().and_then(|l| l.line),
498                            column: frame.location.as_ref().and_then(|l| l.column),
499                        });
500                    }
501                }
502            }
503        }
504
505        Some(FrameInfo {
506            module,
507            func_index,
508            func_name,
509            instr,
510            func_start,
511            symbols,
512        })
513    }
514
515    /// Returns the WebAssembly function index for this frame.
516    ///
517    /// This function index is the index in the function index space of the
518    /// WebAssembly module that this frame comes from.
519    pub fn func_index(&self) -> u32 {
520        self.func_index
521    }
522
523    /// Returns the module for this frame.
524    ///
525    /// This is the module who's code was being run in this frame.
526    pub fn module(&self) -> &Module {
527        &self.module
528    }
529
530    /// Returns a descriptive name of the function for this frame, if one is
531    /// available.
532    ///
533    /// The name of this function may come from the `name` section of the
534    /// WebAssembly binary, or wasmtime may try to infer a better name for it if
535    /// not available, for example the name of the export if it's exported.
536    ///
537    /// This return value is primarily used for debugging and human-readable
538    /// purposes for things like traps. Note that the exact return value may be
539    /// tweaked over time here and isn't guaranteed to be something in
540    /// particular about a wasm module due to its primary purpose of assisting
541    /// in debugging.
542    ///
543    /// This function returns `None` when no name could be inferred.
544    pub fn func_name(&self) -> Option<&str> {
545        self.func_name.as_deref()
546    }
547
548    /// Returns the offset within the original wasm module this frame's program
549    /// counter was at.
550    ///
551    /// The offset here is the offset from the beginning of the original wasm
552    /// module to the instruction that this frame points to.
553    ///
554    /// Note that `None` may be returned if the original module was not
555    /// compiled with mapping information to yield this information. This is
556    /// controlled by the
557    /// [`Config::generate_address_map`](crate::Config::generate_address_map)
558    /// configuration option.
559    pub fn module_offset(&self) -> Option<usize> {
560        Some(self.instr?.file_offset()? as usize)
561    }
562
563    /// Returns the offset from the original wasm module's function to this
564    /// frame's program counter.
565    ///
566    /// The offset here is the offset from the beginning of the defining
567    /// function of this frame (within the wasm module) to the instruction this
568    /// frame points to.
569    ///
570    /// Note that `None` may be returned if the original module was not
571    /// compiled with mapping information to yield this information. This is
572    /// controlled by the
573    /// [`Config::generate_address_map`](crate::Config::generate_address_map)
574    /// configuration option.
575    pub fn func_offset(&self) -> Option<usize> {
576        let instr_offset = self.instr?.file_offset()?;
577        Some((instr_offset - self.func_start.file_offset()?) as usize)
578    }
579
580    /// Returns the debug symbols found, if any, for this function frame.
581    ///
582    /// When a wasm program is compiled with DWARF debug information then this
583    /// function may be populated to return symbols which contain extra debug
584    /// information about a frame including the filename and line number. If no
585    /// debug information was found or if it was malformed then this will return
586    /// an empty array.
587    pub fn symbols(&self) -> &[FrameSymbol] {
588        &self.symbols
589    }
590}
591
592/// Debug information for a symbol that is attached to a [`FrameInfo`].
593///
594/// When DWARF debug information is present in a wasm file then this structure
595/// can be found on a [`FrameInfo`] and can be used to learn about filenames,
596/// line numbers, etc, which are the origin of a function in a stack trace.
597#[derive(Debug)]
598pub struct FrameSymbol {
599    name: Option<String>,
600    file: Option<String>,
601    line: Option<u32>,
602    column: Option<u32>,
603}
604
605impl FrameSymbol {
606    /// Returns the function name associated with this symbol.
607    ///
608    /// Note that this may not be present with malformed debug information, or
609    /// the debug information may not include it. Also note that the symbol is
610    /// frequently mangled, so you might need to run some form of demangling
611    /// over it.
612    pub fn name(&self) -> Option<&str> {
613        self.name.as_deref()
614    }
615
616    /// Returns the source code filename this symbol was defined in.
617    ///
618    /// Note that this may not be present with malformed debug information, or
619    /// the debug information may not include it.
620    pub fn file(&self) -> Option<&str> {
621        self.file.as_deref()
622    }
623
624    /// Returns the 1-indexed source code line number this symbol was defined
625    /// on.
626    ///
627    /// Note that this may not be present with malformed debug information, or
628    /// the debug information may not include it.
629    pub fn line(&self) -> Option<u32> {
630        self.line
631    }
632
633    /// Returns the 1-indexed source code column number this symbol was defined
634    /// on.
635    ///
636    /// Note that this may not be present with malformed debug information, or
637    /// the debug information may not include it.
638    pub fn column(&self) -> Option<u32> {
639        self.column
640    }
641}