Skip to content

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

Debug support: handle panics in hostcall-error debug hook. (#14654)

Chris Fallin31dbff68da6b
diff --git a/crates/wasmtime/src/runtime/vm/traphandlers.rs b/crates/wasmtime/src/runtime/vm/traphandlers.rs
index 78d244de3f..25ccabd63e 100644
--- a/crates/wasmtime/src/runtime/vm/traphandlers.rs
+++ b/crates/wasmtime/src/runtime/vm/traphandlers.rs
@@ -836,7 +836,30 @@ impl CallThreadState {
event = crate::DebugEvent::Exception(exn.clone());
}
- store.block_on_debug_handler(event)
+ // The debug handler is embedder code and may panic. This
+ // function is called from the `raise` libcall, which
+ // doesn't catch panics, so catch them here and replace
+ // the unwind reason with the panic so that it's
+ // resumed on the other side of wasm, like a panic in a
+ // host function.
+ let f = || {
+ store
+ .block_on_debug_handler(event)
+ .map_err(UnwindReason::from)
+ };
+
+ #[cfg(all(feature = "std", panic = "unwind"))]
+ {
+ match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
+ Ok(result) => result,
+ Err(panic) => Err(UnwindReason::Panic(panic)),
+ }
+ }
+
+ #[cfg(not(all(feature = "std", panic = "unwind")))]
+ {
+ f()
+ }
}
TrapReason::Jit { .. } => {
@@ -849,10 +872,10 @@ impl CallThreadState {
// If the debugger invocation itself resulted in an `Err`
// (which can only come from the `block_on` hitting a
- // failure mode), we need to override our unwind as-if
- // were handling a host error.
- if let Err(err) = result {
- unwind = Some(UnwindReason::from(err));
+ // failure mode, or from the handler panicking), we need to
+ // override our unwind with that reason.
+ if let Err(reason) = result {
+ unwind = Some(reason);
}
}
diff --git a/tests/all/debug.rs b/tests/all/debug.rs
index 6ca80676eb..d31131cd38 100644
--- a/tests/all/debug.rs
+++ b/tests/all/debug.rs
@@ -815,6 +815,69 @@ async fn hostcall_error_events() -> wasmtime::Result<()> {
Ok(())
}
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn hostcall_error_handler_panic_propagates() -> wasmtime::Result<()> {
+ let _ = env_logger::try_init();
+
+ let (module, mut store) = get_module_and_store(
+ |_config| {},
+ r#"
+ (module
+ (import "" "fail" (func))
+ (func (export "main")
+ call 0))
+ "#,
+ )?;
+
+ store.set_debug_handler(PanicOnHostcallError);
+
+ let fail = Func::wrap(
+ &mut store,
+ |_caller: Caller<'_, ()>| -> wasmtime::Result<()> {
+ Err(wasmtime::format_err!("host error"))
+ },
+ );
+ let instance = Instance::new_async(&mut store, &module, &[Extern::Func(fail)]).await?;
+ let func = instance.get_func(&mut store, "main").unwrap();
+
+ // The handler's panic should propagate out of `call_async` like a panic
+ // in a host function, rather than aborting the process.
+ let err = tokio::spawn(async move {
+ let mut results = [];
+ let _ = func.call_async(&mut store, &[], &mut results).await;
+ })
+ .await
+ .unwrap_err();
+ assert!(err.is_panic());
+ let panic = err.into_panic();
+ assert_eq!(
+ panic.downcast_ref::<&str>(),
+ Some(&"debug handler panic on hostcall error")
+ );
+ return Ok(());
+
+ #[derive(Clone)]
+ struct PanicOnHostcallError;
+
+ impl DebugHandler for PanicOnHostcallError {
+ type Data = ();
+
+ fn handle(
+ &self,
+ _store: StoreContextMut<'_, ()>,
+ event: DebugEvent<'_>,
+ ) -> impl Future<Output = ()> + Send {
+ let is_hostcall_error = matches!(event, DebugEvent::HostcallError(_));
+ async move {
+ if is_hostcall_error {
+ panic!("debug handler panic on hostcall error");
+ }
+ }
+ }
+ }
+}
+
#[tokio::test]
#[cfg_attr(miri, ignore)]
async fn breakpoint_events() -> wasmtime::Result<()> {