Skip to content

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

Handle exceptions in component `start` functions (#14689)

Alex Crichton05e7deebf1dd
diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rs
index c6e81058a1..3cef979cf1 100644
--- a/crates/wasmtime/src/runtime/component/instance.rs
+++ b/crates/wasmtime/src/runtime/component/instance.rs
@@ -10,7 +10,7 @@ use crate::instance::OwnedImports;
use crate::linker::DefinitionType;
use crate::prelude::*;
use crate::runtime::vm::component::{ComponentInstance, TypedResource, TypedResourceIndex};
-use crate::runtime::vm::{self, VMFuncRef, VMStore};
+use crate::runtime::vm::{self, UncaughtException, VMFuncRef, VMStore};
use crate::store::{AsStoreOpaque, Asyncness, StoreOpaque};
use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
use alloc::sync::Arc;
@@ -1089,7 +1089,7 @@ pub(crate) fn start_raw<T>(
if let Some(callee) = callee {
store.0.enter_guest_sync_call(false, callee)?;
}
- instance.start_raw(store)?;
+ instance.start_raw(store, UncaughtException::Trap)?;
if callee.is_some() {
store.0.exit_guest_sync_call()?;
}
diff --git a/crates/wasmtime/src/runtime/instance.rs b/crates/wasmtime/src/runtime/instance.rs
index 45f1b67f5e..36b9fa66f1 100644
--- a/crates/wasmtime/src/runtime/instance.rs
+++ b/crates/wasmtime/src/runtime/instance.rs
@@ -269,11 +269,13 @@ impl Instance {
// expressions.
if needs_startup {
if asyncness == Asyncness::No {
- instance.start_raw(store)?;
+ instance.start_raw(store, UncaughtException::Propagate)?;
} else {
#[cfg(feature = "async")]
{
- store.on_fiber(|store| instance.start_raw(store)).await??;
+ store
+ .on_fiber(|store| instance.start_raw(store, UncaughtException::Propagate))
+ .await??;
}
#[cfg(not(feature = "async"))]
unreachable!();
@@ -346,7 +348,11 @@ impl Instance {
}
}
- pub(crate) fn start_raw<T>(&self, store: &mut StoreContextMut<'_, T>) -> Result<()> {
+ pub(crate) fn start_raw<T>(
+ &self,
+ store: &mut StoreContextMut<'_, T>,
+ uncaught_exception: UncaughtException,
+ ) -> Result<()> {
// If a start function is present, invoke it. Make sure we use all the
// trap-handling configuration in `store` as well.
let store_id = store.0.id();
@@ -364,7 +370,7 @@ impl Instance {
let funcref = f.vm_func_ref(store.0);
super::func::invoke_wasm_and_catch_traps(
store,
- UncaughtException::Propagate,
+ uncaught_exception,
|_default_caller, vm| {
VMFuncRef::array_call(funcref, vm, caller_vmctx, NonNull::from(&mut []))
},
diff --git a/tests/misc_testsuite/component-model/exceptions.wast b/tests/misc_testsuite/component-model/exceptions.wast
index ab866eded1..5a29214762 100644
--- a/tests/misc_testsuite/component-model/exceptions.wast
+++ b/tests/misc_testsuite/component-model/exceptions.wast
@@ -247,3 +247,14 @@
(func (export "run") (canon lift (core func $i "run"))))
(assert_trap (invoke "run") "uncaught exception propagated out of component")
+
+;; An exception thrown by a core module's start function during component
+;; instantiation traps instead of propagating as an exception.
+(assert_trap
+ (component
+ (core module $m
+ (tag $t)
+ (func $start (throw $t))
+ (start $start))
+ (core instance $i (instantiate $m)))
+ "uncaught exception propagated out of component")