Skip to content

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

Backtraces: handle stack-walking on stack overflow properly on s390x/Pulley, accounting for youngest frame. (#14687)

Chris Fallinc76569e2bd6d
diff --git a/crates/wasmtime/src/runtime/vm/interpreter.rs b/crates/wasmtime/src/runtime/vm/interpreter.rs
index 1376faf43e..97ed6bc5b8 100644
--- a/crates/wasmtime/src/runtime/vm/interpreter.rs
+++ b/crates/wasmtime/src/runtime/vm/interpreter.rs
@@ -7,7 +7,7 @@ use crate::{Engine, ValRaw};
use core::marker;
use core::ptr::NonNull;
use pulley_interpreter::interp::{DoneReason, RegType, TrapKind, Val, Vm, XRegVal};
-use pulley_interpreter::{Reg, XReg};
+use pulley_interpreter::{Opcode, Reg, XReg};
use wasmtime_environ::{BuiltinFunctionIndex, HostCall, Trap};
use wasmtime_unwinder::Handler;
use wasmtime_unwinder::Unwind;
@@ -475,9 +475,29 @@ impl InterpreterRef<'_> {
kind: Option<TrapKind>,
caller: NonNull<VMContext>,
) -> NonNull<u8> {
+ // Pulley checks for stack overflow whenever the stack pointer is
+ // decremented, including in the `push_frame` and `push_frame_save`
+ // instructions at the start of a function. A stack overflow there
+ // happens before the trapping function has pushed its frame, so `fp` is
+ // still its caller's and its return address is still in `lr`.
+ let lr = match kind {
+ Some(TrapKind::StackOverflow) => {
+ // SAFETY: `pc` points to the start of the trapping
+ // instruction, so its first byte is the opcode.
+ let opcode = Opcode::new(unsafe { *pc.as_ptr() });
+ match opcode {
+ Some(Opcode::PushFrame | Opcode::PushFrameSave) => {
+ Some(self.vm().lr() as usize)
+ }
+ _ => None,
+ }
+ }
+ _ => None,
+ };
let regs = TrapRegisters {
pc: pc.as_ptr() as usize,
fp: self.vm().fp() as usize,
+ lr,
};
let trap = match kind {
Some(kind) => match kind {
diff --git a/crates/wasmtime/src/runtime/vm/sys/custom/traphandlers.rs b/crates/wasmtime/src/runtime/vm/sys/custom/traphandlers.rs
index 186e6063ff..0346bedf4b 100644
--- a/crates/wasmtime/src/runtime/vm/sys/custom/traphandlers.rs
+++ b/crates/wasmtime/src/runtime/vm/sys/custom/traphandlers.rs
@@ -31,7 +31,7 @@ extern "C" fn handle_trap(pc: usize, fp: usize, has_faulting_addr: bool, faultin
} else {
None
};
- let regs = TrapRegisters { pc, fp };
+ let regs = TrapRegisters { pc, fp, lr: None };
let test = info.test_if_trap(regs, faulting_addr, |_handler| {
panic!("custom signal handlers are not supported on this platform");
});
diff --git a/crates/wasmtime/src/runtime/vm/sys/unix/machports.rs b/crates/wasmtime/src/runtime/vm/sys/unix/machports.rs
index b385717d91..ccb2a083c4 100644
--- a/crates/wasmtime/src/runtime/vm/sys/unix/machports.rs
+++ b/crates/wasmtime/src/runtime/vm/sys/unix/machports.rs
@@ -461,7 +461,7 @@ unsafe fn handle_exception(request: &mut ExceptionRequest) -> bool {
unsafe extern "C" fn unwind(pc: usize, fp: usize, fault1: usize, fault2: usize, trap: u8) -> ! {
let handler = tls::with(|state| {
let state = state.unwrap();
- let regs = TrapRegisters { pc, fp };
+ let regs = TrapRegisters { pc, fp, lr: None };
let faulting_addr = match fault1 {
0 => None,
_ => Some(fault2),
diff --git a/crates/wasmtime/src/runtime/vm/sys/unix/signals.rs b/crates/wasmtime/src/runtime/vm/sys/unix/signals.rs
index 72f34df639..5df0256db7 100644
--- a/crates/wasmtime/src/runtime/vm/sys/unix/signals.rs
+++ b/crates/wasmtime/src/runtime/vm/sys/unix/signals.rs
@@ -244,6 +244,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.gregs[libc::REG_RIP as usize] as usize,
fp: cx.uc_mcontext.gregs[libc::REG_RBP as usize] as usize,
+ lr: None,
}
}
all(target_os = "linux", target_arch = "x86") => {
@@ -251,6 +252,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.gregs[libc::REG_EIP as usize] as usize,
fp: cx.uc_mcontext.gregs[libc::REG_EBP as usize] as usize,
+ lr: None,
}
}
all(any(target_os = "linux", target_os = "android"), target_arch = "aarch64") => {
@@ -258,6 +260,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.pc as usize,
fp: cx.uc_mcontext.regs[29] as usize,
+ lr: None,
}
}
all(target_os = "linux", target_arch = "s390x") => {
@@ -280,6 +283,9 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: (cx.uc_mcontext.psw.addr - trap_offset) as usize,
fp: *(cx.uc_mcontext.gregs[15] as *const usize),
+ // Stack-limit checks in the prologue happen before the frame is
+ // set up, while the return address is still in %r14.
+ lr: Some(cx.uc_mcontext.gregs[14] as usize),
}
}
}
@@ -289,6 +295,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: (*cx.uc_mcontext).__ss.__rip as usize,
fp: (*cx.uc_mcontext).__ss.__rbp as usize,
+ lr: None,
}
}
}
@@ -298,6 +305,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: (*cx.uc_mcontext).__ss.__pc as usize,
fp: (*cx.uc_mcontext).__ss.__fp as usize,
+ lr: None,
}
}
}
@@ -306,6 +314,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.mc_rip as usize,
fp: cx.uc_mcontext.mc_rbp as usize,
+ lr: None,
}
}
all(target_os = "linux", target_arch = "riscv64") => {
@@ -313,6 +322,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.__gregs[libc::REG_PC] as usize,
fp: cx.uc_mcontext.__gregs[libc::REG_S0] as usize,
+ lr: None,
}
}
all(target_os = "freebsd", target_arch = "aarch64") => {
@@ -320,6 +330,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.mc_gpregs.gp_elr as usize,
fp: cx.uc_mcontext.mc_gpregs.gp_x[29] as usize,
+ lr: None,
}
}
all(target_os = "openbsd", target_arch = "x86_64") => {
@@ -327,6 +338,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.sc_rip as usize,
fp: cx.sc_rbp as usize,
+ lr: None,
}
}
all(target_os = "linux", target_arch = "arm") => {
@@ -334,6 +346,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.arm_pc as usize,
fp: cx.uc_mcontext.arm_fp as usize,
+ lr: None,
}
}
all(target_os = "redox", target_arch = "x86_64") => {
@@ -341,6 +354,7 @@ unsafe fn get_trap_registers(cx: *mut libc::c_void, _signum: libc::c_int) -> Tra
TrapRegisters {
pc: cx.uc_mcontext.rip as usize,
fp: cx.uc_mcontext.rbp as usize,
+ lr: None,
}
}
_ => {
diff --git a/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs b/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs
index 73556a93dc..e2f6315982 100644
--- a/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs
+++ b/crates/wasmtime/src/runtime/vm/sys/windows/vectored_exceptions.rs
@@ -211,12 +211,14 @@ unsafe extern "system" fn exception_handler(exception_info: *mut EXCEPTION_POINT
let regs = TrapRegisters {
pc: context.Rip as usize,
fp: context.Rbp as usize,
+ lr: None,
};
}
target_arch = "aarch64" => {
let regs = TrapRegisters {
pc: context.Pc as usize,
fp: unsafe { context.Anonymous.Anonymous.Fp as usize },
+ lr: None,
};
}
_ => {
diff --git a/crates/wasmtime/src/runtime/vm/traphandlers.rs b/crates/wasmtime/src/runtime/vm/traphandlers.rs
index 95a3520d24..f828a00846 100644
--- a/crates/wasmtime/src/runtime/vm/traphandlers.rs
+++ b/crates/wasmtime/src/runtime/vm/traphandlers.rs
@@ -50,6 +50,15 @@ pub use traphandlers::SignalHandler;
pub(crate) struct TrapRegisters {
pub pc: usize,
pub fp: usize,
+ /// The return address of the trapping function, on platforms where it is
+ /// held in a register (rather than on the stack) until the function sets
+ /// up its frame.
+ ///
+ /// This is only consulted for stack-overflow traps, which on such
+ /// platforms (s390x and Pulley) occur before the trapping function has
+ /// set up its frame, while `fp` is still its caller's frame pointer. See
+ /// `set_jit_trap` for how this is used.
+ pub lr: Option<usize>,
}
/// Return value from `test_if_trap`.
@@ -1085,20 +1094,40 @@ impl CallThreadState {
pub(crate) fn set_jit_trap(
&self,
- TrapRegisters { pc, fp, .. }: TrapRegisters,
+ TrapRegisters { pc, fp, lr }: TrapRegisters,
faulting_addr: Option<usize>,
trap: wasmtime_environ::CompiledTrap,
) {
+ // The backtrace starts at the trapping PC and FP, except for stack
+ // overflows that are detected before the trapping function has set up
+ // its frame (indicated by the presence of `lr`). In that case we
+ // attribute the stack overflow to the call site in the caller, as if the
+ // caller were responsible for allocating the callee's frame: we start
+ // the backtrace at the return address in the caller, with the caller's
+ // FP, which is still the current one. The PC recorded in the trap
+ // reason remains the actual trapping PC, so the backtrace's youngest
+ // frame is treated like any other return address and resolves to the
+ // call instruction.
+ let backtrace_pc = match trap {
+ wasmtime_environ::CompiledTrap::Normal(wasmtime_environ::Trap::StackOverflow) => {
+ lr.unwrap_or(pc)
+ }
+ _ => pc,
+ };
let mut unwind = UnwindReason::from(TrapReason::Jit {
pc,
faulting_addr,
trap,
});
if let UnwindReason::Trap(Ok(trap)) = &mut unwind {
- trap.backtrace =
- self.capture_backtrace(self.vm_store_context.get().as_ptr(), Some((pc, fp)));
- trap.coredumpstack =
- self.capture_coredump(self.vm_store_context.get().as_ptr(), Some((pc, fp)));
+ trap.backtrace = self.capture_backtrace(
+ self.vm_store_context.get().as_ptr(),
+ Some((backtrace_pc, fp)),
+ );
+ trap.coredumpstack = self.capture_coredump(
+ self.vm_store_context.get().as_ptr(),
+ Some((backtrace_pc, fp)),
+ );
}
self.record_unwind(unwind);
}
diff --git a/tests/all/traps.rs b/tests/all/traps.rs
index 0a0de3e65a..d52d4cfc31 100644
--- a/tests/all/traps.rs
+++ b/tests/all/traps.rs
@@ -293,6 +293,37 @@ fn test_trap_stack_overflow() -> Result<()> {
Ok(())
}
+#[wasmtime_test]
+fn test_trap_stack_overflow_mutual_recursion(config: &mut Config) -> Result<()> {
+ let max_frames = NonZeroUsize::new(32).unwrap();
+ let wat = r#"
+ (module $rec_mod
+ (func $even (export "even") (call $odd))
+ (func $odd (call $even))
+ )
+ "#;
+
+ config.wasm_backtrace_max_frames(Some(max_frames));
+ let engine = Engine::new(&config)?;
+ let module = Module::new(&engine, wat)?;
+ let mut store = Store::new(&engine, ());
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let even = instance.get_typed_func::<(), ()>(&mut store, "even")?;
+
+ let e = even.call(&mut store, ()).unwrap_err();
+
+ // No frame may be skipped, so the frames must alternate between the two
+ // functions.
+ let trace = e.downcast_ref::<WasmBacktrace>().unwrap().frames();
+ assert_eq!(trace.len(), max_frames.get());
+ for pair in trace.windows(2) {
+ assert_ne!(pair[0].func_index(), pair[1].func_index(), "{trace:?}");
+ }
+ assert_eq!(e.downcast::<Trap>()?, Trap::StackOverflow);
+
+ Ok(())
+}
+
#[test]
fn trap_display_pretty() -> Result<()> {
let mut store = Store::<()>::default();