diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml index 90364d48aee8..278c58366112 100644 --- a/crates/runtime/Cargo.toml +++ b/crates/runtime/Cargo.toml @@ -29,7 +29,7 @@ anyhow = { workspace = true } paste = "1.0.3" encoding_rs = { version = "0.8.31", optional = true } sptr = "0.3.2" -wasm-encoder = { workspace = true } +wasm-encoder = { workspace = true, optional = true } [target.'cfg(target_os = "linux")'.dependencies] memfd = "0.6.2" @@ -65,9 +65,10 @@ cc = "1.0" wasmtime-versioned-export-macros = { workspace = true } [features] -async = ["wasmtime-fiber"] +async = ["dep:wasmtime-fiber"] pooling-allocator = [] component-model = ["wasmtime-environ/component-model", "dep:encoding_rs"] wmemcheck = ['dep:wasmtime-wmemcheck'] -debug-builtins = ['wasmtime-jit-debug'] +debug-builtins = ['dep:wasmtime-jit-debug'] gc = ["wasmtime-environ/gc"] +coredump = ["dep:wasm-encoder"] diff --git a/crates/runtime/src/traphandlers.rs b/crates/runtime/src/traphandlers.rs index 92003e8b3270..7f8ac5156d83 100644 --- a/crates/runtime/src/traphandlers.rs +++ b/crates/runtime/src/traphandlers.rs @@ -2,6 +2,12 @@ //! signalhandling mechanisms. mod backtrace; + +#[cfg(feature = "coredump")] +#[path = "traphandlers/coredump_enabled.rs"] +mod coredump; +#[cfg(not(feature = "coredump"))] +#[path = "traphandlers/coredump_disabled.rs"] mod coredump; use crate::sys::traphandlers; @@ -277,6 +283,7 @@ mod call_thread_state { pub(super) jmp_buf: Cell<*const u8>, pub(super) signal_handler: Option<*const SignalHandler<'static>>, pub(super) capture_backtrace: bool, + #[cfg(feature = "coredump")] pub(super) capture_coredump: bool, pub(crate) limits: *const VMRuntimeLimits, @@ -314,11 +321,14 @@ mod call_thread_state { capture_coredump: bool, limits: *const VMRuntimeLimits, ) -> CallThreadState { + let _ = capture_coredump; + CallThreadState { unwind: UnsafeCell::new(MaybeUninit::uninit()), jmp_buf: Cell::new(ptr::null()), signal_handler, capture_backtrace, + #[cfg(feature = "coredump")] capture_coredump, limits, prev: Cell::new(ptr::null()), @@ -500,17 +510,6 @@ impl CallThreadState { Some(unsafe { Backtrace::new_with_trap_state(limits, self, trap_pc_and_fp) }) } - fn capture_coredump( - &self, - limits: *const VMRuntimeLimits, - trap_pc_and_fp: Option<(usize, usize)>, - ) -> Option { - if !self.capture_coredump { - return None; - } - Some(CoreDumpStack::new(&self, limits, trap_pc_and_fp)) - } - pub(crate) fn iter<'a>(&'a self) -> impl Iterator + 'a { let mut state = Some(self); std::iter::from_fn(move || { diff --git a/crates/runtime/src/traphandlers/coredump_disabled.rs b/crates/runtime/src/traphandlers/coredump_disabled.rs new file mode 100644 index 000000000000..3a203ce6199b --- /dev/null +++ b/crates/runtime/src/traphandlers/coredump_disabled.rs @@ -0,0 +1,16 @@ +use crate::traphandlers::CallThreadState; +use crate::VMRuntimeLimits; + +/// A WebAssembly Coredump +#[derive(Debug)] +pub enum CoreDumpStack {} + +impl CallThreadState { + pub(super) fn capture_coredump( + &self, + _limits: *const VMRuntimeLimits, + _trap_pc_and_fp: Option<(usize, usize)>, + ) -> Option { + None + } +} diff --git a/crates/runtime/src/traphandlers/coredump.rs b/crates/runtime/src/traphandlers/coredump_enabled.rs similarity index 78% rename from crates/runtime/src/traphandlers/coredump.rs rename to crates/runtime/src/traphandlers/coredump_enabled.rs index 2216d5f75ea3..d60fd6458934 100644 --- a/crates/runtime/src/traphandlers/coredump.rs +++ b/crates/runtime/src/traphandlers/coredump_enabled.rs @@ -20,19 +20,21 @@ pub struct CoreDumpStack { pub operand_stack: Vec>, } -impl CoreDumpStack { - /// Capture a core dump of the current wasm state - pub fn new( - cts: &CallThreadState, +impl CallThreadState { + pub(super) fn capture_coredump( + &self, limits: *const VMRuntimeLimits, trap_pc_and_fp: Option<(usize, usize)>, - ) -> Self { - let bt = unsafe { Backtrace::new_with_trap_state(limits, cts, trap_pc_and_fp) }; + ) -> Option { + if !self.capture_coredump { + return None; + } + let bt = unsafe { Backtrace::new_with_trap_state(limits, self, trap_pc_and_fp) }; - Self { + Some(CoreDumpStack { bt, locals: vec![], operand_stack: vec![], - } + }) } } diff --git a/crates/wasmtime/Cargo.toml b/crates/wasmtime/Cargo.toml index 08d7efda9ed7..b64bc952091c 100644 --- a/crates/wasmtime/Cargo.toml +++ b/crates/wasmtime/Cargo.toml @@ -167,7 +167,7 @@ wmemcheck = ["wasmtime-runtime?/wmemcheck", "wasmtime-cranelift?/wmemcheck"] demangle = ["wasmtime-environ/demangle"] # Enable support for generating core dumps on traps. -coredump = ["dep:wasm-encoder", "runtime"] +coredump = ["dep:wasm-encoder", "runtime", "wasmtime-runtime/coredump"] # Export some symbols from the final binary to assist in debugging # Cranelift-generated code with native debuggers like GDB and LLDB.