Skip to content

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

Fix reading a stream on the host after a partial guest read (#14553)

Alex Crichton033d5c597b5e
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 4910103e4c..f3a8a82551 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -2912,14 +2912,29 @@ impl<T> StoreContextMut<'_, T> {
};
}
&WriteState::GuestReady { .. } => {
- let future = consume();
- transmit.read = ReadState::HostReady {
+ // If the guest writer has a pending event, then don't start
+ // another read event here even if there's some partial data
+ // available. Wait for that to be delivered and wait for the
+ // writer to do something else before a read is triggered.
+ let write_handle = transmit.write_handle;
+ let write_has_event = Waitable::Transmit(write_handle)
+ .common(state)?
+ .event
+ .is_some();
+ let future = if write_has_event {
+ None
+ } else {
+ Some(consume())
+ };
+ state.get_mut(id)?.read = ReadState::HostReady {
consume,
guest_offset: ItemCount::ZERO,
cancel: false,
cancel_waker: None,
};
- self.0.pipe_from_guest(kind, id, future);
+ if let Some(future) = future {
+ self.0.pipe_from_guest(kind, id, future);
+ }
}
WriteState::HostReady { .. } => {
let WriteState::HostReady { produce, .. } = mem::replace(
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 4bfb140aea..5dfe8b049b 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -4229,3 +4229,15 @@ fn max_wasm_stack_large_value_does_not_overflow() -> Result<()> {
);
Ok(())
}
+
+#[test]
+fn p3_partial_write_then_stdout() -> Result<()> {
+ let output = run_wasmtime(&[
+ "run",
+ "-Wcomponent-model-async",
+ "-Sp3",
+ "tests/all/cli_tests/p3-partial-write-then-stdout.wat",
+ ])?;
+ assert_eq!(output, "456789\n");
+ Ok(())
+}
diff --git a/tests/all/cli_tests/p3-partial-write-then-stdout.wat b/tests/all/cli_tests/p3-partial-write-then-stdout.wat
new file mode 100644
index 0000000000..0fbf81bc2e
--- /dev/null
+++ b/tests/all/cli_tests/p3-partial-write-then-stdout.wat
@@ -0,0 +1,105 @@
+;; Starts an 11-byte write to a `stream<u8>`, reads 4 of those bytes back
+;; from the readable end, and then hands the readable end to
+;; `wasi:cli/stdout#write-via-stream` while the write is still pending.
+;;
+;; The writer's event must account for all bytes consumed from its buffer:
+;; either just the 4 read by the guest (in which case the remaining 7 bytes are
+;; written again) or all 11 if the host consumed the rest. Either way
+;; "456789\n" ends up on stdout.
+(component
+ (import "wasi:cli/[email protected]" (instance $types
+ (type $e (enum "io" "illegal-byte-sequence" "pipe"))
+ (export "error-code" (type (eq $e)))))
+ (alias export $types "error-code" (type $ec))
+ (import "wasi:cli/[email protected]" (instance $stdout
+ (export "error-code" (type $ec2 (eq $ec)))
+ (export "write-via-stream" (func
+ (param "data" (stream u8)) (result (future (result (error $ec2))))))))
+
+ (core module $libc
+ (memory (export "mem") 1)
+ (data (i32.const 0) "0123456789\n"))
+ (core instance $libc (instantiate $libc))
+
+ (type $s (stream u8))
+ (core func $stream.new (canon stream.new $s))
+ (core func $stream.write (canon stream.write $s async (memory (core memory $libc "mem"))))
+ (core func $stream.read (canon stream.read $s async (memory (core memory $libc "mem"))))
+ (core func $stream.drop-writable (canon stream.drop-writable $s))
+ (core func $write-via-stream (canon lower (func $stdout "write-via-stream")))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable.join (canon waitable.join))
+ (core func $task.return (canon task.return (result (result))))
+
+ (core module $m
+ (import "" "stream.new" (func $stream.new (result i64)))
+ (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))
+ (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))
+ (import "" "stream.drop-writable" (func $stream.drop-writable (param i32)))
+ (import "" "write-via-stream" (func $write-via-stream (param i32) (result i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "join" (func $waitable.join (param i32 i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (global $w (mut i32) (i32.const 0))
+ (global $ws (mut i32) (i32.const 0))
+ (global $rewrote (mut i32) (i32.const 0))
+
+ (func (export "run") (result i32)
+ (local $pair i64) (local $r i32)
+ (local.set $pair (call $stream.new))
+ (local.set $r (i32.wrap_i64 (local.get $pair)))
+ (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $pair) (i64.const 32))))
+ (if (i32.ne (call $stream.write (global.get $w) (i32.const 0) (i32.const 11))
+ (i32.const -1 (; BLOCKED ;)))
+ (then unreachable))
+ (if (i32.ne (call $stream.read (local.get $r) (i32.const 100) (i32.const 4))
+ (i32.const 0x40 (; COMPLETED(4) ;)))
+ (then unreachable))
+ (drop (call $write-via-stream (local.get $r)))
+ (global.set $ws (call $waitable-set.new))
+ (call $waitable.join (global.get $w) (global.get $ws))
+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4))))
+
+ (func $finish (result i32)
+ (call $waitable.join (global.get $w) (i32.const 0))
+ (call $stream.drop-writable (global.get $w))
+ (call $task.return (i32.const 0))
+ (i32.const 0 (; EXIT ;)))
+
+ (func (export "cb") (param $ev i32) (param $idx i32) (param $payload i32) (result i32)
+ (local $ret i32)
+ (if (i32.ne (local.get $ev) (i32.const 3 (; STREAM_WRITE ;))) (then unreachable))
+
+ ;; if COMPLETED(7), then the re-write succeeded
+ (if (global.get $rewrote)
+ (then
+ (if (i32.ne (local.get $payload) (i32.const 0x70)) (then unreachable))
+ (return (call $finish))))
+
+ ;; if COMPLETED(11), then the host consumed the whole buffer in one go.
+ (if (i32.eq (local.get $payload) (i32.const 0xb0))
+ (then (return (call $finish))))
+
+ ;; if COMPLETED(4), then write the rest
+ (if (i32.ne (local.get $payload) (i32.const 0x40)) (then unreachable))
+ (global.set $rewrote (i32.const 1))
+ (local.set $ret (call $stream.write (global.get $w) (i32.const 4) (i32.const 7)))
+ (if (i32.eq (local.get $ret) (i32.const 0x70))
+ (then (return (call $finish))))
+ (if (i32.ne (local.get $ret) (i32.const -1)) (then unreachable))
+ (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4))))
+ )
+ (core instance $i (instantiate $m (with "" (instance
+ (export "stream.new" (func $stream.new))
+ (export "stream.write" (func $stream.write))
+ (export "stream.read" (func $stream.read))
+ (export "stream.drop-writable" (func $stream.drop-writable))
+ (export "write-via-stream" (func $write-via-stream))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "join" (func $waitable.join))
+ (export "task.return" (func $task.return))))))
+ (func $run async (result (result))
+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))
+ (instance $run (export "run" (func $run)))
+ (export "wasi:cli/[email protected]" (instance $run))
+)