Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Clean up the rendering of a core dump error (#15268)

Alex Crichton9c65b1f509f3
diff --git a/crates/core/src/error/error.rs b/crates/core/src/error/error.rs
index 0a9ece9c5a..73e3bc2403 100644
--- a/crates/core/src/error/error.rs
+++ b/crates/core/src/error/error.rs
@@ -5,7 +5,7 @@ use super::vtable::Vtable;
use crate::error::{OutOfMemory, Result};
use core::{
any::TypeId,
- fmt::{self, Debug},
+ fmt::{self, Debug, Write},
iter::FusedIterator,
mem,
ptr::NonNull,
@@ -446,12 +446,20 @@ impl fmt::Debug for Error {
f.write_str("\n\nCaused by:\n")?;
let multiple_causes = source.source().is_some();
for (i, e) in source.chain().enumerate() {
- if multiple_causes {
+ let indent = if multiple_causes {
write!(f, "{i: >5}: ")?;
+ " "
} else {
write!(f, " ")?;
- }
- writeln!(f, "{e}")?;
+ " "
+ };
+
+ let mut indented = Indented {
+ inner: f,
+ indent,
+ at_line_start: false,
+ };
+ writeln!(indented, "{e}")?;
}
}
@@ -468,6 +476,34 @@ impl fmt::Debug for Error {
}
}
+/// A writer which indents each line after the first with `indent`.
+///
+/// Empty lines aren't indented, to avoid trailing whitespace.
+struct Indented<'a, 'b> {
+ inner: &'a mut fmt::Formatter<'b>,
+ indent: &'static str,
+ at_line_start: bool,
+}
+
+impl fmt::Write for Indented<'_, '_> {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ for (i, line) in s.split('\n').enumerate() {
+ if i > 0 {
+ self.inner.write_str("\n")?;
+ self.at_line_start = true;
+ }
+ if !line.is_empty() {
+ if self.at_line_start {
+ self.inner.write_str(self.indent)?;
+ self.at_line_start = false;
+ }
+ self.inner.write_str(line)?;
+ }
+ }
+ Ok(())
+ }
+}
+
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let inner = &self.inner;
diff --git a/crates/core/tests/tests.rs b/crates/core/tests/tests.rs
index 79fd575ece..d36676d0ce 100644
--- a/crates/core/tests/tests.rs
+++ b/crates/core/tests/tests.rs
@@ -673,6 +673,59 @@ Caused by:
assert_eq!(actual, expected);
}
}
+#[test]
+fn fmt_debug_with_multi_line_causes() {
+ let error = Error::msg("whoops")
+ .context("line one\nline two\n\nline four")
+ .context("yikes");
+ let actual = format!("{error:?}");
+
+ let expected = "\
+yikes
+
+Caused by:
+ 0: line one
+ line two
+
+ line four
+ 1: whoops
+";
+
+ #[cfg(feature = "backtrace")]
+ {
+ assert!(actual.starts_with(expected));
+ }
+
+ #[cfg(not(feature = "backtrace"))]
+ {
+ assert_eq!(actual, expected);
+ }
+}
+
+#[test]
+fn fmt_debug_with_single_multi_line_cause() {
+ let error = Error::msg("line one\nline two").context("uh oh");
+ let actual = format!("{error:?}");
+
+ let expected = "\
+uh oh
+
+Caused by:
+ line one
+ line two
+";
+
+ #[cfg(feature = "backtrace")]
+ {
+ assert!(actual.starts_with(expected));
+ }
+
+ #[cfg(not(feature = "backtrace"))]
+ {
+ assert_eq!(actual, expected);
+ }
+}
+
#[test]
fn fmt_debug_with_single_cause() {
let error = Error::msg("whoops").context("uh oh");
diff --git a/crates/wasmtime/src/runtime/coredump.rs b/crates/wasmtime/src/runtime/coredump.rs
index cd8ff046d3..2d5f8bf6c1 100644
--- a/crates/wasmtime/src/runtime/coredump.rs
+++ b/crates/wasmtime/src/runtime/coredump.rs
@@ -29,7 +29,6 @@ use std::fmt;
/// [`Func::call`]: crate::Func::call
/// [`Instance::new`]: crate::Instance::new
pub struct WasmCoreDump {
- name: String,
modules: Vec<Module>,
instances: Vec<Instance>,
memories: Vec<Memory>,
@@ -52,7 +51,6 @@ impl WasmCoreDump {
store.for_each_global(|_store, global| store_globals.push(global));
WasmCoreDump {
- name: String::from("store_name"),
modules,
instances,
memories: store_memories,
@@ -348,31 +346,7 @@ impl WasmCoreDump {
impl fmt::Display for WasmCoreDump {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- writeln!(f, "wasm coredump generated while executing {}:", self.name)?;
- writeln!(f, "modules:")?;
- for module in self.modules.iter() {
- writeln!(f, " {}", module.name().unwrap_or("<module>"))?;
- }
-
- writeln!(f, "instances:")?;
- for instance in self.instances.iter() {
- writeln!(f, " {instance:?}")?;
- }
-
- writeln!(f, "memories:")?;
- for memory in self.memories.iter() {
- writeln!(f, " {memory:?}")?;
- }
-
- writeln!(f, "globals:")?;
- for global in self.globals.iter() {
- writeln!(f, " {global:?}")?;
- }
-
- writeln!(f, "backtrace:")?;
- write!(f, "{}", self.backtrace)?;
-
- Ok(())
+ write!(f, "wasm core dump captured")
}
}
diff --git a/docs/examples-debugging-core-dumps.md b/docs/examples-debugging-core-dumps.md
index c7bfbc669c..4b0badc9b7 100644
--- a/docs/examples-debugging-core-dumps.md
+++ b/docs/examples-debugging-core-dumps.md
@@ -50,17 +50,8 @@ Error: failed to run main module `/home/nick/scratch/trap.wasm`
Caused by:
0: core dumped at /home/nick/scratch/trap.coredump
1: failed to invoke command default
- 2: wasm coredump generated while executing store_name:
- modules:
- <module>
- instances:
- Instance(store=1, index=1)
- memories:
- Memory(store=1, index=1)
- globals:
- Global(store=1, index=0)
- backtrace:
- error while executing at wasm backtrace:
+ 2: wasm core dump captured
+ 3: error while executing at wasm backtrace:
0: 0x5961 - <unknown>!__rust_start_panic
1: 0x562a - <unknown>!rust_panic
2: 0x555d - <unknown>!std::panicking::rust_panic_with_hook::h58e7d0b3d70e95b6