Skip to content

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

require write-before-drop for futures unconditionally (#13691)

Joel Dicefb42a1112f9a
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 ed5687b8b2..7b017f724b 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -2488,10 +2488,7 @@ impl StoreOpaque {
}
WriteState::HostReady { .. } => {}
v @ WriteState::Open => {
- if let (Some(on_drop_open), false) = (
- on_drop_open,
- transmit.done || matches!(transmit.read, ReadState::Dropped),
- ) {
+ if let (Some(on_drop_open), false) = (on_drop_open, transmit.done) {
on_drop_open()?;
} else {
*v = WriteState::Dropped;
diff --git a/tests/misc_testsuite/component-model/async/futures-must-write2.wast b/tests/misc_testsuite/component-model/async/futures-must-write2.wast
new file mode 100644
index 0000000000..46bd52d2ab
--- /dev/null
+++ b/tests/misc_testsuite/component-model/async/futures-must-write2.wast
@@ -0,0 +1,39 @@
+;;! component_model_async = true
+
+;; The writable end of a future must be written to before it is dropped
+;; regardless of whether the readable end is dropped first.
+(component
+ (core module $libc (memory (export "m") 1))
+ (core instance $libc (instantiate $libc))
+
+ (type $f (future u8))
+ (core func $future.new (canon future.new $f))
+ (core func $future.drop-readable (canon future.drop-readable $f))
+ (core func $future.drop-writable (canon future.drop-writable $f))
+
+ (core module $m
+ (import "" "m" (memory 1))
+ (import "" "future.new" (func $future.new (result i64)))
+ (import "" "future.drop-readable" (func $future.drop-readable (param i32)))
+ (import "" "future.drop-writable" (func $future.drop-writable (param i32)))
+
+ (func (export "run")
+ (local $tmp i64) (local $r i32) (local $w i32)
+ (local.set $tmp (call $future.new))
+ (local.set $r (i32.wrap_i64 (local.get $tmp)))
+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))
+ (call $future.drop-readable (local.get $r))
+ (call $future.drop-writable (local.get $w))
+ )
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "m" (memory $libc "m"))
+ (export "future.new" (func $future.new))
+ (export "future.drop-readable" (func $future.drop-readable))
+ (export "future.drop-writable" (func $future.drop-writable))
+ ))
+ ))
+ (func (export "run") async (canon lift (core func $i "run")))
+)
+(assert_trap (invoke "run") "cannot drop future write end without first writing a value")