A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
fix panic when trapping while handling multiple work items (#11678)
Joel Dicecefa3bf7795b
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 851bafdced..23598a48f9 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -1295,8 +1295,33 @@ impl Instance {// The future we were passed has not yet completed, so handle// any work items and then loop again.Either::Right(ready) => {- for item in ready {- self.handle_work_item(store.as_context_mut(), item).await?;+ struct Dispose<'a, T: 'static, I: Iterator<Item = WorkItem>> {+ store: StoreContextMut<'a, T>,+ ready: I,+ }++ impl<'a, T, I: Iterator<Item = WorkItem>> Drop for Dispose<'a, T, I> {+ fn drop(&mut self) {+ while let Some(item) = self.ready.next() {+ match item {+ WorkItem::ResumeFiber(mut fiber) => fiber.dispose(self.store.0),+ WorkItem::PushFuture(future) => {+ tls::set(self.store.0, move || drop(future))+ }+ _ => {}+ }+ }+ }+ }++ let mut dispose = Dispose {+ store: store.as_context_mut(),+ ready: ready.into_iter(),+ };++ while let Some(item) = dispose.ready.next() {+ self.handle_work_item(dispose.store.as_context_mut(), item)+ .await?;}}}diff --git a/tests/misc_testsuite/component-model-async/subtask-wait.wast b/tests/misc_testsuite/component-model-async/subtask-wait.wastnew file mode 100644index 0000000000..d2c64da15d--- /dev/null+++ b/tests/misc_testsuite/component-model-async/subtask-wait.wast@@ -0,0 +1,83 @@+;;! component_model_async = true+;;! reference_types = true+;;! gc_types = true++;; This test previously caused Wasmtime to panic while handling a trap due to an+;; improperly disposed fiber. That bug is fixed now, and this test helps ensure+;; it stays fixed.+;;+;; (Copied from https://github.com/bytecodealliance/wasmtime/issues/11668#issue-3402875697)+(component+ (component $A+ (core module $a+ (func (export "run") (result i32)+ i32.const 1)+ (func (export "run-cb") (param i32 i32 i32) (result i32)+ unreachable)+ )++ (core instance $a (instantiate $a))+ (func (export "run")+ (canon lift (core func $a "run") async (callback (func $a "run-cb"))))+ )+ (component $B+ (import "a" (instance $a (export "run" (func))))++ (core module $libc (memory (export "memory") 1))+ (core instance $libc (instantiate $libc))++ (core func $run (canon lower (func $a "run") async))+ (core func $new (canon waitable-set.new))+ (core func $join (canon waitable.join))+ (core func $drop (canon waitable-set.drop))+ (core func $wait (canon waitable-set.wait (memory $libc "memory")))++ (core module $b+ (import "" "run" (func $run_a (result i32)))+ (import "" "new" (func $new (result i32)))+ (import "" "join" (func $join (param i32 i32)))+ (import "" "drop" (func $drop (param i32)))+ (import "" "wait" (func $wait (param i32 i32) (result i32)))++ (func (export "run")+ (local $ret i32)+ (local $set i32)++ (local.set $ret (call $run_a))++ ;; make sure it's in the "started" state+ (if (i32.ne (i32.and (local.get $ret) (i32.const 0xf)) (i32.const 1))+ (then (unreachable)))++ ;; extract the waitable handle+ (local.set $ret (i32.shr_u (local.get $ret) (i32.const 4)))++ ;; Make a waitable set and insert our handle into it+ (local.set $set (call $new))+ (call $join (local.get $ret) (local.get $set))++ ;; wait for something to happen filling in memory address 4, but don't+ ;; actually see what happened since this traps right now.+ (call $wait (local.get $set) (i32.const 4))+ drop+ )+ )+ (core instance $b (instantiate $b+ (with "" (instance+ (export "run" (func $run))+ (export "new" (func $new))+ (export "join" (func $join))+ (export "drop" (func $drop))+ (export "wait" (func $wait))+ ))+ ))+ (func (export "run")+ (canon lift (core func $b "run")))+ )++ (instance $a (instantiate $A))+ (instance $b (instantiate $B (with "a" (instance $a))))+ (export "run" (func $b "run"))+)++(assert_trap (invoke "run") "wasm `unreachable` instruction executed")