Skip to content

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

Set current thread before lowering stream/future reads (#12736)

Sy Brande48949526dd9
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index fd73e5622e..822e3fceb4 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -4311,7 +4311,7 @@ struct LiftResult {
/// This exists to minimize table lookups and the necessity to pass stores around mutably
/// for the common case of identifying the task to which a thread belongs.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
-struct QualifiedThreadId {
+pub(crate) struct QualifiedThreadId {
task: TableId<GuestTask>,
thread: TableId<GuestThread>,
}
@@ -4806,7 +4806,7 @@ impl ConcurrentInstanceState {
}
#[derive(Debug, Copy, Clone)]
-enum CurrentThread {
+pub(crate) enum CurrentThread {
Guest(QualifiedThreadId),
Host(TableId<HostTask>),
None,
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
index 81e84275f9..fdc7f18ad3 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -1,6 +1,6 @@
use super::table::{TableDebug, TableId};
use super::{Event, GlobalErrorContextRefCount, Waitable, WaitableCommon};
-use crate::component::concurrent::{ConcurrentState, WorkItem, tls};
+use crate::component::concurrent::{ConcurrentState, QualifiedThreadId, WorkItem, tls};
use crate::component::func::{self, LiftContext, LowerContext};
use crate::component::matching::InstanceType;
use crate::component::types;
@@ -143,6 +143,7 @@ fn get_mut_by_index_from(
fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(
mut store: StoreContextMut<U>,
instance: Instance,
+ caller_thread: QualifiedThreadId,
options: OptionsIndex,
ty: TransmitIndex,
address: usize,
@@ -151,11 +152,21 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(
) -> Result<()> {
let count = buffer.remaining().len().min(count);
- let lower = &mut if T::MAY_REQUIRE_REALLOC {
- LowerContext::new
+ // If lowering may call realloc in the guest, then the guest may need
+ // to access its thread context, so we need to set the current thread before lowering
+ // and restore the old one afterward.
+ let (lower, old_thread) = if T::MAY_REQUIRE_REALLOC {
+ let old_thread = store.0.set_thread(caller_thread)?;
+ (
+ &mut LowerContext::new(store.as_context_mut(), options, instance),
+ Some(old_thread),
+ )
} else {
- LowerContext::new_without_realloc
- }(store.as_context_mut(), options, instance);
+ (
+ &mut LowerContext::new_without_realloc(store.as_context_mut(), options, instance),
+ None,
+ )
+ };
if address % usize::try_from(T::ALIGN32)? != 0 {
bail!("read pointer not aligned");
@@ -170,6 +181,10 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(
T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;
}
+ if let Some(old_thread) = old_thread {
+ store.0.set_thread(old_thread)?;
+ }
+
buffer.skip(count);
Ok(())
@@ -2196,7 +2211,8 @@ enum ReadState {
/// The read end is owned by a guest task and a read is pending.
GuestReady {
ty: TransmitIndex,
- caller: RuntimeComponentInstanceIndex,
+ caller_instance: RuntimeComponentInstanceIndex,
+ caller_thread: QualifiedThreadId,
flat_abi: Option<FlatAbi>,
instance: Instance,
options: OptionsIndex,
@@ -2930,7 +2946,8 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Write
count,
handle,
instance,
- caller,
+ caller_instance,
+ caller_thread,
} => {
let guest_offset = match guest_offset {
Some(i) => i,
@@ -2952,6 +2969,7 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Write
lower::<T, B, D>(
store.as_context_mut(),
instance,
+ caller_thread,
options,
ty,
address + (T::SIZE32 * guest_offset),
@@ -3008,7 +3026,8 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Write
count,
handle,
instance,
- caller,
+ caller_instance,
+ caller_thread,
};
crate::error::Ok(())
@@ -3178,11 +3197,12 @@ impl Instance {
self,
mut store: StoreContextMut<T>,
flat_abi: Option<FlatAbi>,
- write_caller: RuntimeComponentInstanceIndex,
+ write_caller_instance: RuntimeComponentInstanceIndex,
write_ty: TransmitIndex,
write_options: OptionsIndex,
write_address: usize,
- read_caller: RuntimeComponentInstanceIndex,
+ read_caller_instance: RuntimeComponentInstanceIndex,
+ read_caller_thread: QualifiedThreadId,
read_ty: TransmitIndex,
read_options: OptionsIndex,
read_address: usize,
@@ -3198,7 +3218,9 @@ impl Instance {
let payload = types[types[write_ty].ty].payload;
- if write_caller == read_caller && !allow_intra_component_read_write(payload) {
+ if write_caller_instance == read_caller_instance
+ && !allow_intra_component_read_write(payload)
+ {
bail!(
"cannot read from and write to intra-component future with non-numeric payload"
)
@@ -3228,6 +3250,10 @@ impl Instance {
.transpose()?;
if let Some(val) = val {
+ // Serializing the value may require calling the guest's realloc function, so we
+ // set the guest's thread context in case realloc requires it, and restore the original
+ // thread context after the copy is complete.
+ let old_thread = store.0.set_thread(read_caller_thread)?;
let lower = &mut LowerContext::new(store.as_context_mut(), read_options, self);
let types = lower.types;
let ty = match types[types[read_ty].ty].payload {
@@ -3240,10 +3266,11 @@ impl Instance {
&ValRaw::u32(read_address.try_into()?),
)?;
val.store(lower, ty, ptr)?;
+ store.0.set_thread(old_thread)?;
}
}
(TransmitIndex::Stream(write_ty), TransmitIndex::Stream(read_ty)) => {
- if write_caller == read_caller
+ if write_caller_instance == read_caller_instance
&& !allow_intra_component_read_write(types[types[write_ty].ty].payload)
{
bail!(
@@ -3284,7 +3311,7 @@ impl Instance {
// SAFETY: Both `src` and `dst` have been validated
// above.
unsafe {
- if write_caller == read_caller {
+ if write_caller_instance == read_caller_instance {
// If the same instance owns both ends of
// the stream, the source and destination
// buffers might overlap.
@@ -3326,6 +3353,10 @@ impl Instance {
let id = TableId::<TransmitHandle>::new(rep);
log::trace!("copy values {values:?} for {id:?}");
+ // Serializing the value may require calling the guest's realloc function, so we
+ // set the guest's thread context in case realloc requires it, and restore the original
+ // thread context after the copy is complete.
+ let old_thread = store.0.set_thread(read_caller_thread)?;
let lower = &mut LowerContext::new(store.as_context_mut(), read_options, self);
let ty = match lower.types[lower.types[read_ty].ty].payload {
Some(ty) => ty,
@@ -3348,6 +3379,7 @@ impl Instance {
value.store(lower, ty, ptr)?;
ptr += size
}
+ store.0.set_thread(old_thread)?;
}
}
_ => bail_bug!("mismatched transmit types in copy"),
@@ -3469,7 +3501,8 @@ impl Instance {
count: read_count,
handle: read_handle,
instance: read_instance,
- caller: read_caller,
+ caller_instance: read_caller_instance,
+ caller_thread: read_caller_thread,
} => {
if flat_abi != read_flat_abi {
bail_bug!("expected flat ABI calculations to be the same");
@@ -3515,7 +3548,8 @@ impl Instance {
ty,
options,
address,
- read_caller,
+ read_caller_instance,
+ read_caller_thread,
read_ty,
read_options,
read_address,
@@ -3557,7 +3591,8 @@ impl Instance {
count: read_count - count,
handle: read_handle,
instance: read_instance,
- caller: read_caller,
+ caller_instance: read_caller_instance,
+ caller_thread: read_caller_thread,
};
}
@@ -3634,7 +3669,7 @@ impl Instance {
pub(super) fn guest_read<T: 'static>(
self,
mut store: StoreContextMut<T>,
- caller: RuntimeComponentInstanceIndex,
+ caller_instance: RuntimeComponentInstanceIndex,
ty: TransmitIndex,
options: OptionsIndex,
flat_abi: Option<FlatAbi>,
@@ -3664,6 +3699,7 @@ impl Instance {
*state = TransmitLocalState::Busy;
let transmit_handle = TableId::<TransmitHandle>::new(rep);
let concurrent_state = store.0.concurrent_state_mut();
+ let caller_thread = concurrent_state.current_guest_thread()?;
let transmit_id = concurrent_state.get_mut(transmit_handle)?.state;
let transmit = concurrent_state.get_mut(transmit_id)?;
log::trace!(
@@ -3694,7 +3730,8 @@ impl Instance {
count,
handle,
instance: self,
- caller,
+ caller_instance,
+ caller_thread,
};
Ok::<_, crate::Error>(())
};
@@ -3737,7 +3774,8 @@ impl Instance {
write_ty,
write_options,
write_address,
- caller,
+ caller_instance,
+ caller_thread,
ty,
options,
address,
diff --git a/tests/misc_testsuite/component-model/async/task-builtins.wast b/tests/misc_testsuite/component-model/async/task-builtins.wast
index 5476fb8222..86155a312e 100644
--- a/tests/misc_testsuite/component-model/async/task-builtins.wast
+++ b/tests/misc_testsuite/component-model/async/task-builtins.wast
@@ -463,3 +463,258 @@
)
(assert_return (invoke "run"))
+
+;; Test when realloc is called to communicate stream/future values that various
+;; intrinsics work and have their expected values.
+(component
+ ;; Component that will drive the reader forward and write to the future/stream
+ (component $writer
+ (type $FT (future string))
+ (type $ST (stream string))
+ ;; The reader will provide runner functions for futures and streams separately
+ (import "reader" (instance $reader
+ (export "run-future" (func async (param "future" $FT) (result u32)))
+ (export "run-stream" (func async (param "stream" $ST) (result u32)))
+ ))
+ (core module $libc
+ (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $run-reader-future (canon lower (func $reader "run-future") (memory $libc "memory") async))
+ (core func $run-reader-stream (canon lower (func $reader "run-stream") (memory $libc "memory") async))
+ (core module $m
+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))
+ (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "stream.new" (func $stream.new (result i64)))
+ (import "" "future.new" (func $future.new (result i64)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "" "run-reader-future" (func $run-reader-future (param i32 i32) (result i32)))
+ (import "" "run-reader-stream" (func $run-reader-stream (param i32 i32) (result i32)))
+ (import "" "memory" (memory 1))
+
+ (global $ws (mut i32) (i32.const 0))
+ (global $fw (mut i32) (i32.const 0))
+ (global $sw (mut i32) (i32.const 0))
+ (global $state (mut i32) (i32.const 0))
+ (global $future-subtask (mut i32) (i32.const 0))
+ (global $stream-subtask (mut i32) (i32.const 0))
+ (global $future-retp i32 (i32.const 0x60))
+ (global $stream-retp i32 (i32.const 0x70))
+
+ (func (export "run") (result i32)
+ (local $ret i32) (local $ret64 i64)
+ (local $fr i32) (local $sr i32)
+
+ ;; store address and length of string
+ (i32.store offset=0 (i32.const 40) (i32.const 0x100))
+ (i32.store offset=4 (i32.const 40) (i32.const 2))
+
+ (local.set $ret64 (call $future.new))
+ (local.set $fr (i32.wrap_i64 (local.get $ret64)))
+ (global.set $fw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32))))
+ (local.set $ret64 (call $stream.new))
+ (local.set $sr (i32.wrap_i64 (local.get $ret64)))
+ (global.set $sw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32))))
+
+ (local.set $ret (call $run-reader-future (local.get $fr) (global.get $future-retp)))
+ (global.set $future-subtask (i32.shr_u (local.get $ret) (i32.const 4)))
+ (local.set $ret (call $future.write (global.get $fw) (i32.const 40)))
+ (if (i32.ne (i32.const 0 (; COMPLETED ;)) (local.get $ret))
+ (then unreachable))
+
+ (local.set $ret (call $run-reader-stream (local.get $sr) (global.get $stream-retp)))
+ (global.set $stream-subtask (i32.shr_u (local.get $ret) (i32.const 4)))
+ (local.set $ret (call $stream.write (global.get $sw) (i32.const 40) (i32.const 1)))
+ (if (i32.ne (i32.const 0x10 (; COMPLETED | 1<<4 ;)) (local.get $ret)) (then (unreachable)))
+
+ ;; Create a waitable set and join both subtasks to wait for both to complete
+ (global.set $ws (call $waitable-set.new))
+ (call $waitable.join (global.get $stream-subtask) (global.get $ws))
+ (call $waitable.join (global.get $future-subtask) (global.get $ws))
+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4)))
+ )
+
+ (global $future-completed (mut i32) (i32.const 0))
+ (global $stream-completed (mut i32) (i32.const 0))
+
+ ;; Callback invoked when a subtask completes. Since we joined both subtasks to the
+ ;; same waitable set, this will be called once for each completion. We track which
+ ;; subtasks have completed and only return when both are done.
+ (func (export "run-cb") (param $event_code i32) (param $index i32) (param $payload i32) (result i32)
+ (if (i32.ne (local.get $event_code) (i32.const 1 (; SUBTASK ;))) (then (unreachable)))
+ (if (i32.ne (local.get $payload) (i32.const 2 (; RETURNED ;))) (then (unreachable)))
+
+ ;; Track which subtask completed
+ (if (i32.eq (local.get $index) (global.get $future-subtask))
+ (then
+ (if (i32.ne (i32.load (global.get $future-retp)) (i32.const 42)) (then (unreachable)))
+ (global.set $future-completed (i32.const 1)))
+ (else
+ (if (i32.eq (local.get $index) (global.get $stream-subtask))
+ (then
+ (if (i32.ne (i32.load (global.get $stream-retp)) (i32.const 42)) (then (unreachable)))
+ (global.set $stream-completed (i32.const 1)))
+ (else unreachable))))
+
+ ;; If both completed, exit; otherwise keep waiting
+ (if (result i32)
+ (i32.and (i32.eq (global.get $future-completed) (i32.const 1))
+ (i32.eq (global.get $stream-completed) (i32.const 1)))
+ (then
+ (call $task.return (i32.const 42))
+ (i32.const 0 (; EXIT ;)))
+ (else
+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4)))))
+ )
+
+ (data (i32.const 0x100) "hi")
+ )
+ (canon future.new $FT (core func $future.new))
+ (canon future.write $FT async
+ (memory $libc "memory") (core func $future.write))
+ (canon stream.new $ST (core func $stream.new))
+ (canon stream.write $ST async
+ (memory $libc "memory") (core func $stream.write))
+ (canon waitable.join (core func $waitable.join))
+ (canon waitable-set.new (core func $waitable-set.new))
+ (canon task.return (result u32) (memory $libc "memory") (core func $task.return))
+ (canon context.set i32 0 (core func $context.set))
+
+ (core instance $M (instantiate $m (with "" (instance
+ (export "memory" (memory $libc "memory"))
+ (export "future.new" (func $future.new))
+ (export "future.write" (func $future.write))
+ (export "stream.new" (func $stream.new))
+ (export "stream.write" (func $stream.write))
+ (export "waitable.join" (func $waitable.join))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "task.return" (func $task.return))
+ (export "run-reader-future" (func $run-reader-future))
+ (export "run-reader-stream" (func $run-reader-stream))
+ ))))
+
+ (func (export "run") async (result u32)
+ (canon lift (core func $M "run") (memory $libc "memory")
+ async (callback (func $M "run-cb")))
+ )
+ )
+
+ (component $reader
+ (core module $libc
+ (import "" "backpressure.inc" (func $backpressure.inc))
+ (import "" "backpressure.dec" (func $backpressure.dec))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+
+ (memory (export "memory") 1)
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ (if (i32.ne (local.get 0) (i32.const 0)) (then (unreachable)))
+ (if (i32.ne (local.get 1) (i32.const 0)) (then (unreachable)))
+ (if (i32.ne (local.get 2) (i32.const 1)) (then (unreachable)))
+ (if (i32.ne (local.get 3) (i32.const 2)) (then (unreachable)))
+
+ call $context.get
+ i32.const 400
+ i32.ne
+ if unreachable end
+
+ i32.const 500
+ call $context.set
+
+ call $backpressure.inc
+ call $backpressure.dec
+
+ i32.const 200
+ )
+ )
+
+ (core func $backpressure.inc (canon backpressure.inc))
+ (core func $backpressure.dec (canon backpressure.dec))
+ (core func $context.get (canon context.get i32 0))
+ (core func $context.set (canon context.set i32 0))
+
+ (core instance $libc (instantiate $libc (with "" (instance
+ (export "backpressure.inc" (func $backpressure.inc))
+ (export "backpressure.dec" (func $backpressure.dec))
+ (export "context.get" (func $context.get))
+ (export "context.set" (func $context.set))
+ ))))
+
+ (type $FT (future string))
+ (type $ST (stream string))
+ (canon future.new $FT (core func $future.new))
+ (canon future.read $FT
+ (memory $libc "memory") (realloc (func $libc "realloc")) (core func $future.read))
+ (canon stream.new $ST (core func $stream.new))
+ (canon stream.read $ST
+ (memory $libc "memory") (realloc (func $libc "realloc")) (core func $stream.read))
+ (canon waitable.join (core func $waitable.join))
+ (canon waitable-set.new (core func $waitable-set.new))
+ (canon waitable-set.wait (memory $libc "memory") (core func $waitable-set.wait))
+ (canon task.return (result u32) (memory $libc "memory") (core func $task.return))
+
+ (core module $m
+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))
+ (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))
+ (import "" "context.set" (func $context.set (param i32)))
+ (import "" "context.get" (func $context.get (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "" "memory" (memory 1))
+
+ ;; Set context[0] to 400, then read the future, which should call realloc and set
+ ;; context[0] to 500, then check that we see that value.
+ (func (export "run-future") (param $fr i32) (result i32)
+ (local $ret i32)
+
+ (call $context.set (i32.const 400))
+ (local.set $ret (call $future.read (local.get $fr) (i32.const 40)))
+ (if (i32.ne (i32.const 0 (; COMPLETED ;)) (local.get $ret)) (then (unreachable)))
+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))
+
+ (call $task.return (i32.const 42))
+ (i32.const 0 (; EXIT ;))
+ )
+
+ ;; Same as above, but for streams.
+ (func (export "run-stream") (param $sr i32) (result i32)
+ (local $ret i32)
+
+ (call $context.set (i32.const 400))
+ (local.set $ret (call $stream.read (local.get $sr) (i32.const 40) (i32.const 1)))
+ (if (i32.ne (i32.const 0x10 (; COMPLETED | 1<<4 ;)) (local.get $ret)) (then (unreachable)))
+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))
+
+ (call $task.return (i32.const 42))
+ (i32.const 0 (; EXIT ;))
+ )
+
+ (func (export "run-cb") (param i32 i32 i32) (result i32) unreachable))
+
+ (core instance $M (instantiate $m (with "" (instance
+ (export "future.read" (func $future.read))
+ (export "stream.read" (func $stream.read))
+ (export "context.set" (func $context.set))
+ (export "context.get" (func $context.get))
+ (export "task.return" (func $task.return))
+ (export "memory" (memory 1))))))
+ (func (export "run-future") async (param "future" $FT) (result u32)
+ (canon lift (core func $M "run-future") (memory $libc "memory") (realloc (func $libc "realloc"))
+ async (callback (func $M "run-cb"))
+ )
+ )
+ (func (export "run-stream") async (param "stream" $ST) (result u32)
+ (canon lift (core func $M "run-stream") (memory $libc "memory") (realloc (func $libc "realloc"))
+ async (callback (func $M "run-cb"))
+ )
+ )
+ )
+
+ (instance $Reader (instantiate $reader))
+ (instance $Writer (instantiate $writer
+ (with "reader" (instance $Reader)
+ )))
+
+ (func (export "run") (alias export $Writer "run"))
+)
+(assert_return (invoke "run") (u32.const 42))