A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Deliver some more spec-mandated async traps (#14668)
Alex Crichton3aa015666a2d
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex f88e8f4015..c71a14bd8f 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -4016,16 +4016,11 @@ impl Instance {.handle_table().waitable_set_rep(set_handle)?;- let state = store.concurrent_state_mut()?;- if let Some(old) = waitable.common(state)?.set- && state.get_mut(old)?.is_sync_call_set- {- bail!(Trap::WaitableSyncAndAsync);- }-Some(TableId::<WaitableSet>::new(set))};+ waitable.trap_if_has_sync_waiter(store.concurrent_state_mut()?)?;+log::trace!("waitable {waitable:?} (handle {waitable_handle}) join set {set:?} (handle {set_handle})",);@@ -5740,6 +5735,20 @@ impl Waitable {Ok(())}+ /// Trap if a thread is currently synchronously waiting on this waitable.+ ///+ /// This corresponds to the spec's `trap_if(w.has_sync_waiter)` and is+ /// used to prevent other threads from stealing or otherwise interfering+ /// with the event the sync waiter is expecting.+ fn trap_if_has_sync_waiter(&self, state: &mut ConcurrentState) -> Result<()> {+ if let Some(set) = self.common(state)?.set+ && state.get_mut(set)?.is_sync_call_set+ {+ bail!(Trap::WaitableSyncAndAsync);+ }+ Ok(())+ }+/// Set or clear the pending event for this waitable and either deliver it/// to the first waiter, if any, or mark it as ready to be delivered to the/// next waiter that arrives.diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex 7ced6850e0..f992d44fce 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -3556,6 +3556,7 @@ impl Instance {let address = usize::try_from(address)?;self.check_bounds(store.0, options, ty, address, count.as_usize())?;+ let async_ = self.options(store.0, options).async_;let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;let TransmitLocalState::Write { done } = *state else {bail!(Trap::ConcurrentFutureStreamOp);@@ -3578,6 +3579,11 @@ impl Instance {});}+ if !async_ {+ Waitable::Transmit(transmit_handle).trap_if_in_waitable_set(concurrent_state)?;+ }+ let transmit = concurrent_state.get_mut(transmit_id)?;+let new_state = if let ReadState::Dropped = &transmit.read {ReadState::Dropped} else {@@ -3781,7 +3787,7 @@ impl Instance {}};- if result == ReturnCode::Blocked && !self.options(store.0, options).async_ {+ if result == ReturnCode::Blocked && !async_ {result = self.wait_for_write(store.0, caller, transmit_handle)?;}@@ -3815,6 +3821,7 @@ impl Instance {let address = usize::try_from(address)?;self.check_bounds(store.0, options, ty, address, count.as_usize())?;+ let async_ = self.options(store.0, options).async_;let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;let TransmitLocalState::Read { done } = *state else {bail!(Trap::ConcurrentFutureStreamOp);@@ -3840,6 +3847,11 @@ impl Instance {}}+ if !async_ {+ Waitable::Transmit(transmit_handle).trap_if_in_waitable_set(concurrent_state)?;+ }+ let transmit = concurrent_state.get_mut(transmit_id)?;+let new_state = if let WriteState::Dropped = &transmit.write {WriteState::Dropped} else {@@ -4023,7 +4035,7 @@ impl Instance {}};- if result == ReturnCode::Blocked && !self.options(store.0, options).async_ {+ if result == ReturnCode::Blocked && !async_ {result = self.wait_for_read(store.0, caller_instance, transmit_handle)?;}@@ -4080,6 +4092,7 @@ impl Instance {);let waitable = Waitable::Transmit(transmit.write_handle);+ waitable.trap_if_has_sync_waiter(state)?;if !async_ {waitable.trap_if_in_waitable_set(state)?;}@@ -4174,6 +4187,7 @@ impl Instance {let waitable = Waitable::Transmit(transmit.read_handle);+ waitable.trap_if_has_sync_waiter(state)?;if !async_ {waitable.trap_if_in_waitable_set(state)?;}diff --git a/tests/misc_testsuite/component-model/async/join-during-sync-read.wast b/tests/misc_testsuite/component-model/async/join-during-sync-read.wastindex 4a1dac834c..b658694776 100644--- a/tests/misc_testsuite/component-model/async/join-during-sync-read.wast+++ b/tests/misc_testsuite/component-model/async/join-during-sync-read.wast@@ -2,8 +2,8 @@;;! component_model_more_async_builtins = true;;! component_model_threading = true-;; Tests that adding a waitable to a waitable-set traps if that waitable is-;; being used concurrently in a synchronous operation.+;; Tests that touching a waitable which another thread is synchronously+;; waiting on traps.(component(core module $libc@@ -64,3 +64,128 @@)(assert_trap (invoke "run") "waitable cannot be used synchronously while added to a waitable set")++(component definition $C+ (core module $libc+ (table (export "table") 4 funcref)+ (memory (export "memory") 1)+ )+ (core module $m+ (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))+ (import "" "thread.yield-then-resume" (func $thread.yield-then-resume (param i32) (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "stream.new" (func $stream.new (result i64)))+ (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))+ (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))+ (import "" "stream.cancel-read" (func $stream.cancel-read (param i32) (result i32)))+ (import "" "stream.cancel-write" (func $stream.cancel-write (param i32) (result i32)))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "future.cancel-read" (func $future.cancel-read (param i32) (result i32)))+ (import "" "future.cancel-write" (func $future.cancel-write (param i32) (result i32)))+ (import "libc" "table" (table $table 4 funcref))++ ;; Thread entry points which block forever in a sync copy, since the other+ ;; end is never used.+ (func $stream-read (param i32)+ (drop (call $stream.read (local.get 0) (i32.const 0) (i32.const 4))))+ (func $stream-write (param i32)+ (drop (call $stream.write (local.get 0) (i32.const 0) (i32.const 4))))+ (func $future-read (param i32)+ (drop (call $future.read (local.get 0) (i32.const 0))))+ (func $future-write (param i32)+ (drop (call $future.write (local.get 0) (i32.const 0))))+ (elem (table $table) (i32.const 0) func $stream-read $stream-write $future-read $future-write)++ ;; Starts a thread running the entry point at table index `$f` on `$end`,+ ;; lets it block, and returns `$end`.+ (func $blocked-in (param $f i32) (param $end i32) (result i32)+ (drop (call $thread.yield-then-resume+ (call $thread.new-indirect (local.get $f) (local.get $end))))+ (local.get $end))++ (func $readable (param i64) (result i32)+ (i32.wrap_i64 (local.get 0)))+ (func $writable (param i64) (result i32)+ (i32.wrap_i64 (i64.shr_u (local.get 0) (i64.const 32))))++ (func (export "waitable.join")+ (call $waitable.join+ (call $blocked-in (i32.const 0) (call $readable (call $stream.new)))+ (i32.const 0))+ unreachable)+ (func (export "stream.cancel-read")+ (drop (call $stream.cancel-read+ (call $blocked-in (i32.const 0) (call $readable (call $stream.new)))))+ unreachable)+ (func (export "stream.cancel-write")+ (drop (call $stream.cancel-write+ (call $blocked-in (i32.const 1) (call $writable (call $stream.new)))))+ unreachable)+ (func (export "future.cancel-read")+ (drop (call $future.cancel-read+ (call $blocked-in (i32.const 2) (call $readable (call $future.new)))))+ unreachable)+ (func (export "future.cancel-write")+ (drop (call $future.cancel-write+ (call $blocked-in (i32.const 3) (call $writable (call $future.new)))))+ unreachable)+ )++ (core instance $libc (instantiate $libc))+ (core type $start-func-ty (func (param i32)))+ (alias core export $libc "table" (core table $table))++ (type $s (stream u8))+ (type $f (future u8))+ (core func $thread.new-indirect (canon thread.new-indirect $start-func-ty (core table $table)))+ (core func $thread.yield-then-resume (canon thread.yield-then-resume))+ (core func $waitable.join (canon waitable.join))+ (core func $stream.new (canon stream.new $s))+ (core func $stream.read (canon stream.read $s (memory (core memory $libc "memory"))))+ (core func $stream.write (canon stream.write $s (memory (core memory $libc "memory"))))+ (core func $stream.cancel-read (canon stream.cancel-read $s async))+ (core func $stream.cancel-write (canon stream.cancel-write $s async))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f (memory (core memory $libc "memory"))))+ (core func $future.write (canon future.write $f (memory (core memory $libc "memory"))))+ (core func $future.cancel-read (canon future.cancel-read $f async))+ (core func $future.cancel-write (canon future.cancel-write $f async))++ (core instance $m (instantiate $m+ (with "" (instance+ (export "thread.new-indirect" (func $thread.new-indirect))+ (export "thread.yield-then-resume" (func $thread.yield-then-resume))+ (export "waitable.join" (func $waitable.join))+ (export "stream.new" (func $stream.new))+ (export "stream.read" (func $stream.read))+ (export "stream.write" (func $stream.write))+ (export "stream.cancel-read" (func $stream.cancel-read))+ (export "stream.cancel-write" (func $stream.cancel-write))+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "future.cancel-read" (func $future.cancel-read))+ (export "future.cancel-write" (func $future.cancel-write))+ ))+ (with "libc" (instance $libc))+ ))++ (func (export "waitable-join") async (canon lift (core func $m "waitable.join")))+ (func (export "stream-cancel-read") async (canon lift (core func $m "stream.cancel-read")))+ (func (export "stream-cancel-write") async (canon lift (core func $m "stream.cancel-write")))+ (func (export "future-cancel-read") async (canon lift (core func $m "future.cancel-read")))+ (func (export "future-cancel-write") async (canon lift (core func $m "future.cancel-write")))+)++(component instance $i $C)+(assert_trap (invoke "waitable-join") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "stream-cancel-read") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "stream-cancel-write") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "future-cancel-read") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "future-cancel-write") "waitable cannot be used synchronously while added to a waitable set")diff --git a/tests/misc_testsuite/component-model/async/sync-copy-in-waitable-set.wast b/tests/misc_testsuite/component-model/async/sync-copy-in-waitable-set.wastnew file mode 100644index 0000000000..07a692031d--- /dev/null+++ b/tests/misc_testsuite/component-model/async/sync-copy-in-waitable-set.wast@@ -0,0 +1,111 @@+;;! component_model_async = true+;;! component_model_more_async_builtins = true++;; A synchronous `{stream,future}.{read,write}` on an end which is in a waitable+;; set traps on entry, even though here the copy could complete without blocking+;; since the other end already has a pending async copy.+(component definition $C+ (core module $libc (memory (export "memory") 1))+ (core instance $libc (instantiate $libc))+ (type $s (stream u8))+ (type $f (future u8))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $stream.new (canon stream.new $s))+ (core func $stream.read (canon stream.read $s (memory (core memory $libc "memory"))))+ (core func $stream.write (canon stream.write $s (memory (core memory $libc "memory"))))+ (core func $stream.read-async (canon stream.read $s async (memory (core memory $libc "memory"))))+ (core func $stream.write-async (canon stream.write $s async (memory (core memory $libc "memory"))))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f (memory (core memory $libc "memory"))))+ (core func $future.write (canon future.write $f (memory (core memory $libc "memory"))))+ (core func $future.read-async (canon future.read $f async (memory (core memory $libc "memory"))))+ (core func $future.write-async (canon future.write $f async (memory (core memory $libc "memory"))))+ (core module $m+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "stream.new" (func $stream.new (result i64)))+ (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))+ (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))+ (import "" "stream.read-async" (func $stream.read-async (param i32 i32 i32) (result i32)))+ (import "" "stream.write-async" (func $stream.write-async (param i32 i32 i32) (result i32)))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "future.read-async" (func $future.read-async (param i32 i32) (result i32)))+ (import "" "future.write-async" (func $future.write-async (param i32 i32) (result i32)))++ (func $readable (param i64) (result i32)+ (i32.wrap_i64 (local.get 0)))+ (func $writable (param i64) (result i32)+ (i32.wrap_i64 (i64.shr_u (local.get 0) (i64.const 32))))+ (func $expect-blocked (param i32)+ (if (i32.ne (local.get 0) (i32.const -1)) (then unreachable)))+ ;; Adds `$end` to a new waitable set and returns it.+ (func $joined (param $end i32) (result i32)+ (call $waitable.join (local.get $end) (call $waitable-set.new))+ (local.get $end))++ (func (export "stream.read")+ (local $pair i64)+ (local.set $pair (call $stream.new))+ (call $expect-blocked+ (call $stream.write-async (call $writable (local.get $pair)) (i32.const 0) (i32.const 4)))+ (drop (call $stream.read+ (call $joined (call $readable (local.get $pair))) (i32.const 0) (i32.const 4)))+ unreachable)+ (func (export "stream.write")+ (local $pair i64)+ (local.set $pair (call $stream.new))+ (call $expect-blocked+ (call $stream.read-async (call $readable (local.get $pair)) (i32.const 0) (i32.const 4)))+ (drop (call $stream.write+ (call $joined (call $writable (local.get $pair))) (i32.const 0) (i32.const 4)))+ unreachable)+ (func (export "future.read")+ (local $pair i64)+ (local.set $pair (call $future.new))+ (call $expect-blocked+ (call $future.write-async (call $writable (local.get $pair)) (i32.const 0)))+ (drop (call $future.read+ (call $joined (call $readable (local.get $pair))) (i32.const 0)))+ unreachable)+ (func (export "future.write")+ (local $pair i64)+ (local.set $pair (call $future.new))+ (call $expect-blocked+ (call $future.read-async (call $readable (local.get $pair)) (i32.const 0)))+ (drop (call $future.write+ (call $joined (call $writable (local.get $pair))) (i32.const 0)))+ unreachable)+ )+ (core instance $m (instantiate $m+ (with "" (instance+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "stream.new" (func $stream.new))+ (export "stream.read" (func $stream.read))+ (export "stream.write" (func $stream.write))+ (export "stream.read-async" (func $stream.read-async))+ (export "stream.write-async" (func $stream.write-async))+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "future.read-async" (func $future.read-async))+ (export "future.write-async" (func $future.write-async))+ ))+ ))+ (func (export "stream-read") async (canon lift (core func $m "stream.read")))+ (func (export "stream-write") async (canon lift (core func $m "stream.write")))+ (func (export "future-read") async (canon lift (core func $m "future.read")))+ (func (export "future-write") async (canon lift (core func $m "future.write")))+)++(component instance $i $C)+(assert_trap (invoke "stream-read") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "stream-write") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "future-read") "waitable cannot be used synchronously while added to a waitable set")+(component instance $i $C)+(assert_trap (invoke "future-write") "waitable cannot be used synchronously while added to a waitable set")