Skip to content

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

Fix a `bail_bug!` on recursive host calls (#14910)

Alex Crichton602fc9587c74
diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rs
index c893701b3b..d639eb23bb 100644
--- a/crates/wasmtime/src/runtime/component/func/host.rs
+++ b/crates/wasmtime/src/runtime/component/func/host.rs
@@ -453,6 +453,13 @@ where
.run(store.as_context_mut(), params)
.finish_sync(store.0, entered_host_task)?;
+ // If the store was poisoned while the host function ran, e.g. because
+ // it made a nested call back into the guest which failed but then
+ // returned normally, then don't let the guest continue running.
+ if store.0.trapped() {
+ bail!(crate::Trap::CannotEnterComponent);
+ }
+
let mut lower = LowerContext::new(store, options, instance);
let fty = &lower.types[ty];
let result_tys = &lower.types[fty.results];
diff --git a/tests/all/component_model/import.rs b/tests/all/component_model/import.rs
index e9bf62ee80..285e155ef9 100644
--- a/tests/all/component_model/import.rs
+++ b/tests/all/component_model/import.rs
@@ -4,7 +4,7 @@ use super::REALLOC_AND_FREE;
use std::ops::Deref;
use wasmtime::Result;
use wasmtime::component::*;
-use wasmtime::{Config, Engine, Store, StoreContextMut, WasmBacktrace};
+use wasmtime::{Config, Engine, Store, StoreContextMut, Trap, WasmBacktrace};
#[test]
fn can_compile() -> Result<()> {
@@ -481,6 +481,63 @@ fn reenter_during_host() -> Result<()> {
Ok(())
}
+#[test]
+fn reenter_during_host_and_trap() -> Result<()> {
+ let component = r#"
+(component
+ (import "thunk" (func $thunk))
+ (core func $thunk_lower (canon lower (func $thunk)))
+
+ (core module $m
+ (import "host" "thunk" (func $thunk))
+
+ (func (export "run")
+ call $thunk
+ unreachable)
+ (func (export "trap")
+ unreachable)
+ )
+ (core instance $m (instantiate $m
+ (with "host" (instance (export "thunk" (func $thunk_lower))))
+ ))
+
+ (func (export "run")
+ (canon lift (core func $m "run"))
+ )
+ (func (export "trap")
+ (canon lift (core func $m "trap"))
+ )
+)
+ "#;
+
+ let engine = super::engine();
+ let component = Component::new(&engine, component)?;
+
+ // The host handles the reentrant call's trap and returns normally, but
+ // the store is now poisoned so the guest must not continue running.
+ let mut store = Store::new(&engine, None);
+ let mut linker = Linker::new(&engine);
+ linker.root().func_wrap(
+ "thunk",
+ |mut store: StoreContextMut<'_, Option<TypedFunc<(), ()>>>, _: ()| -> Result<()> {
+ let func = store.data().unwrap();
+ assert!(func.call(&mut store, ()).is_err());
+ Ok(())
+ },
+ )?;
+ let instance = linker.instantiate(&mut store, &component)?;
+ let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
+ *store.data_mut() = Some(instance.get_typed_func::<(), ()>(&mut store, "trap")?);
+ let err = run.call(&mut store, ()).unwrap_err();
+ assert_eq!(
+ err.downcast_ref::<Trap>(),
+ Some(&Trap::CannotEnterComponent),
+ "{err:?}"
+ );
+
+ Ok(())
+}
+
#[tokio::test]
async fn stack_and_heap_args_and_rets() -> Result<()> {
test_stack_and_heap_args_and_rets(false).await