Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 7a264bf

Browse files
committed
trap on synchronous future/stream read/writes
We don't support these yet. They shouldn't be difficult to support, but in the meantime let's not silently assume they're async. Fixes #71 Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 2bee925 commit 7a264bf

6 files changed

Lines changed: 281 additions & 7 deletions

File tree

crates/cranelift/src/compiler/component.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,13 @@ impl<'a> TrampolineCompiler<'a> {
12851285
// realloc: *mut VMFuncRef
12861286
callee_args.push(self.load_realloc(vmctx, options.realloc));
12871287
// string_encoding: StringEncoding
1288-
callee_args.push(self.string_encoding(options.string_encoding))
1288+
callee_args.push(self.string_encoding(options.string_encoding));
1289+
// async: bool
1290+
callee_args.push(
1291+
self.builder
1292+
.ins()
1293+
.iconst(ir::types::I8, if options.async_ { 1 } else { 0 }),
1294+
);
12891295
}
12901296

12911297
for ty in tys {
@@ -1310,6 +1316,9 @@ impl<'a> TrampolineCompiler<'a> {
13101316
vmctx,
13111317
self.load_memory(vmctx, options.memory.unwrap()),
13121318
self.load_realloc(vmctx, options.realloc),
1319+
self.builder
1320+
.ins()
1321+
.iconst(ir::types::I8, if options.async_ { 1 } else { 0 }),
13131322
];
13141323
for ty in tys {
13151324
callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(*ty)));

crates/environ/src/component.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ macro_rules! foreach_builtin_component_function {
112112
#[cfg(feature = "component-model-async")]
113113
future_new(vmctx: vmctx, ty: u32) -> u64;
114114
#[cfg(feature = "component-model-async")]
115-
future_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, future: u32, address: u32) -> u64;
115+
future_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, err_ctx_ty: u32, future: u32, address: u32) -> u64;
116116
#[cfg(feature = "component-model-async")]
117-
future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, future: u32, address: u32) -> u64;
117+
future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, err_ctx_ty: u32, future: u32, address: u32) -> u64;
118118
#[cfg(feature = "component-model-async")]
119119
future_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
120120
#[cfg(feature = "component-model-async")]
@@ -126,9 +126,9 @@ macro_rules! foreach_builtin_component_function {
126126
#[cfg(feature = "component-model-async")]
127127
stream_new(vmctx: vmctx, ty: u32) -> u64;
128128
#[cfg(feature = "component-model-async")]
129-
stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, stream: u32, address: u32, count: u32) -> u64;
129+
stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, err_ctx_ty: u32, stream: u32, address: u32, count: u32) -> u64;
130130
#[cfg(feature = "component-model-async")]
131-
stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, stream: u32, address: u32, count: u32) -> u64;
131+
stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, async_: u8, ty: u32, err_ctx_ty: u32, stream: u32, address: u32, count: u32) -> u64;
132132
#[cfg(feature = "component-model-async")]
133133
stream_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
134134
#[cfg(feature = "component-model-async")]
@@ -138,9 +138,9 @@ macro_rules! foreach_builtin_component_function {
138138
#[cfg(feature = "component-model-async")]
139139
stream_close_readable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, reader: u32, error: u32) -> bool;
140140
#[cfg(feature = "component-model-async")]
141-
flat_stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, ty: u32, err_ctx_ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
141+
flat_stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, async_: u8, ty: u32, err_ctx_ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
142142
#[cfg(feature = "component-model-async")]
143-
flat_stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, ty: u32, err_ctx_ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
143+
flat_stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, async_: u8, ty: u32, err_ctx_ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
144144
#[cfg(feature = "component-model-async")]
145145
error_context_new(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, debug_msg_address: u32, debug_msg_len: u32) -> u64;
146146
#[cfg(feature = "component-model-async")]

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,7 @@ pub unsafe trait VMComponentAsyncStore {
24272427
memory: *mut VMMemoryDefinition,
24282428
realloc: *mut VMFuncRef,
24292429
string_encoding: u8,
2430+
async_: bool,
24302431
ty: TypeFutureTableIndex,
24312432
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
24322433
future: u32,
@@ -2440,6 +2441,7 @@ pub unsafe trait VMComponentAsyncStore {
24402441
memory: *mut VMMemoryDefinition,
24412442
realloc: *mut VMFuncRef,
24422443
string_encoding: u8,
2444+
async_: bool,
24432445
ty: TypeFutureTableIndex,
24442446
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
24452447
future: u32,
@@ -2453,6 +2455,7 @@ pub unsafe trait VMComponentAsyncStore {
24532455
memory: *mut VMMemoryDefinition,
24542456
realloc: *mut VMFuncRef,
24552457
string_encoding: u8,
2458+
async_: bool,
24562459
ty: TypeStreamTableIndex,
24572460
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
24582461
stream: u32,
@@ -2467,6 +2470,7 @@ pub unsafe trait VMComponentAsyncStore {
24672470
memory: *mut VMMemoryDefinition,
24682471
realloc: *mut VMFuncRef,
24692472
string_encoding: u8,
2473+
async_: bool,
24702474
ty: TypeStreamTableIndex,
24712475
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
24722476
stream: u32,
@@ -2481,6 +2485,7 @@ pub unsafe trait VMComponentAsyncStore {
24812485
instance: &mut ComponentInstance,
24822486
memory: *mut VMMemoryDefinition,
24832487
realloc: *mut VMFuncRef,
2488+
async_: bool,
24842489
ty: TypeStreamTableIndex,
24852490
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
24862491
payload_size: u32,
@@ -2497,6 +2502,7 @@ pub unsafe trait VMComponentAsyncStore {
24972502
instance: &mut ComponentInstance,
24982503
memory: *mut VMMemoryDefinition,
24992504
realloc: *mut VMFuncRef,
2505+
async_: bool,
25002506
ty: TypeStreamTableIndex,
25012507
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
25022508
payload_size: u32,
@@ -2629,6 +2635,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
26292635
memory: *mut VMMemoryDefinition,
26302636
realloc: *mut VMFuncRef,
26312637
string_encoding: u8,
2638+
async_: bool,
26322639
ty: TypeFutureTableIndex,
26332640
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
26342641
future: u32,
@@ -2639,6 +2646,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
26392646
memory,
26402647
realloc,
26412648
string_encoding,
2649+
async_,
26422650
TableIndex::Future(ty),
26432651
err_ctx_ty,
26442652
None,
@@ -2654,6 +2662,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
26542662
memory: *mut VMMemoryDefinition,
26552663
realloc: *mut VMFuncRef,
26562664
string_encoding: u8,
2665+
async_: bool,
26572666
ty: TypeFutureTableIndex,
26582667
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
26592668
future: u32,
@@ -2664,6 +2673,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
26642673
memory,
26652674
realloc,
26662675
string_encoding,
2676+
async_,
26672677
TableIndex::Future(ty),
26682678
err_ctx_ty,
26692679
None,
@@ -2679,6 +2689,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
26792689
memory: *mut VMMemoryDefinition,
26802690
realloc: *mut VMFuncRef,
26812691
string_encoding: u8,
2692+
async_: bool,
26822693
ty: TypeStreamTableIndex,
26832694
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
26842695
stream: u32,
@@ -2690,6 +2701,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
26902701
memory,
26912702
realloc,
26922703
string_encoding,
2704+
async_,
26932705
TableIndex::Stream(ty),
26942706
err_ctx_ty,
26952707
None,
@@ -2705,6 +2717,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
27052717
memory: *mut VMMemoryDefinition,
27062718
realloc: *mut VMFuncRef,
27072719
string_encoding: u8,
2720+
async_: bool,
27082721
ty: TypeStreamTableIndex,
27092722
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
27102723
stream: u32,
@@ -2716,6 +2729,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
27162729
memory,
27172730
realloc,
27182731
string_encoding,
2732+
async_,
27192733
TableIndex::Stream(ty),
27202734
err_ctx_ty,
27212735
None,
@@ -2730,6 +2744,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
27302744
instance: &mut ComponentInstance,
27312745
memory: *mut VMMemoryDefinition,
27322746
realloc: *mut VMFuncRef,
2747+
async_: bool,
27332748
ty: TypeStreamTableIndex,
27342749
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
27352750
payload_size: u32,
@@ -2743,6 +2758,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
27432758
memory,
27442759
realloc,
27452760
StringEncoding::Utf8 as u8,
2761+
async_,
27462762
TableIndex::Stream(ty),
27472763
err_ctx_ty,
27482764
Some(FlatAbi {
@@ -2760,6 +2776,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
27602776
instance: &mut ComponentInstance,
27612777
memory: *mut VMMemoryDefinition,
27622778
realloc: *mut VMFuncRef,
2779+
async_: bool,
27632780
ty: TypeStreamTableIndex,
27642781
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
27652782
payload_size: u32,
@@ -2773,6 +2790,7 @@ unsafe impl<T> VMComponentAsyncStore for StoreInner<T> {
27732790
memory,
27742791
realloc,
27752792
StringEncoding::Utf8 as u8,
2793+
async_,
27762794
TableIndex::Stream(ty),
27772795
err_ctx_ty,
27782796
Some(FlatAbi {

crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,13 +2185,18 @@ impl ComponentInstance {
21852185
memory: *mut VMMemoryDefinition,
21862186
realloc: *mut VMFuncRef,
21872187
string_encoding: u8,
2188+
async_: bool,
21882189
ty: TableIndex,
21892190
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
21902191
flat_abi: Option<FlatAbi>,
21912192
handle: u32,
21922193
address: u32,
21932194
count: u32,
21942195
) -> Result<u32> {
2196+
if !async_ {
2197+
bail!("synchronous stream and future writes not yet supported");
2198+
}
2199+
21952200
// TODO: handle errors sent via `{stream|future}.close-readable`:
21962201
_ = err_ctx_ty;
21972202

@@ -2324,13 +2329,18 @@ impl ComponentInstance {
23242329
memory: *mut VMMemoryDefinition,
23252330
realloc: *mut VMFuncRef,
23262331
string_encoding: u8,
2332+
async_: bool,
23272333
ty: TableIndex,
23282334
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
23292335
flat_abi: Option<FlatAbi>,
23302336
handle: u32,
23312337
address: u32,
23322338
count: u32,
23332339
) -> Result<u32> {
2340+
if !async_ {
2341+
bail!("synchronous stream and future reads not yet supported");
2342+
}
2343+
23342344
let address = usize::try_from(address).unwrap();
23352345
let count = usize::try_from(count).unwrap();
23362346
let options = unsafe {

crates/wasmtime/src/runtime/vm/component/libcalls.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ unsafe fn future_write(
885885
memory: *mut u8,
886886
realloc: *mut u8,
887887
string_encoding: u8,
888+
async_: u8,
888889
ty: u32,
889890
err_ctx_ty: u32,
890891
future: u32,
@@ -896,6 +897,7 @@ unsafe fn future_write(
896897
memory.cast::<crate::vm::VMMemoryDefinition>(),
897898
realloc.cast::<crate::vm::VMFuncRef>(),
898899
string_encoding,
900+
async_ != 0,
899901
wasmtime_environ::component::TypeFutureTableIndex::from_u32(ty),
900902
wasmtime_environ::component::TypeComponentLocalErrorContextTableIndex::from_u32(
901903
err_ctx_ty,
@@ -912,6 +914,7 @@ unsafe fn future_read(
912914
memory: *mut u8,
913915
realloc: *mut u8,
914916
string_encoding: u8,
917+
async_: u8,
915918
ty: u32,
916919
err_ctx_ty: u32,
917920
future: u32,
@@ -923,6 +926,7 @@ unsafe fn future_read(
923926
memory.cast::<crate::vm::VMMemoryDefinition>(),
924927
realloc.cast::<crate::vm::VMFuncRef>(),
925928
string_encoding,
929+
async_ != 0,
926930
wasmtime_environ::component::TypeFutureTableIndex::from_u32(ty),
927931
wasmtime_environ::component::TypeComponentLocalErrorContextTableIndex::from_u32(
928932
err_ctx_ty,
@@ -1020,6 +1024,7 @@ unsafe fn stream_write(
10201024
memory: *mut u8,
10211025
realloc: *mut u8,
10221026
string_encoding: u8,
1027+
async_: u8,
10231028
ty: u32,
10241029
err_ctx_ty: u32,
10251030
stream: u32,
@@ -1032,6 +1037,7 @@ unsafe fn stream_write(
10321037
memory.cast::<crate::vm::VMMemoryDefinition>(),
10331038
realloc.cast::<crate::vm::VMFuncRef>(),
10341039
string_encoding,
1040+
async_ != 0,
10351041
wasmtime_environ::component::TypeStreamTableIndex::from_u32(ty),
10361042
wasmtime_environ::component::TypeComponentLocalErrorContextTableIndex::from_u32(
10371043
err_ctx_ty,
@@ -1049,6 +1055,7 @@ unsafe fn stream_read(
10491055
memory: *mut u8,
10501056
realloc: *mut u8,
10511057
string_encoding: u8,
1058+
async_: u8,
10521059
ty: u32,
10531060
err_ctx_ty: u32,
10541061
stream: u32,
@@ -1061,6 +1068,7 @@ unsafe fn stream_read(
10611068
memory.cast::<crate::vm::VMMemoryDefinition>(),
10621069
realloc.cast::<crate::vm::VMFuncRef>(),
10631070
string_encoding,
1071+
async_ != 0,
10641072
wasmtime_environ::component::TypeStreamTableIndex::from_u32(ty),
10651073
wasmtime_environ::component::TypeComponentLocalErrorContextTableIndex::from_u32(
10661074
err_ctx_ty,
@@ -1149,6 +1157,7 @@ unsafe fn flat_stream_write(
11491157
vmctx: NonNull<VMComponentContext>,
11501158
memory: *mut u8,
11511159
realloc: *mut u8,
1160+
async_: u8,
11521161
ty: u32,
11531162
err_ctx_ty: u32,
11541163
payload_size: u32,
@@ -1164,6 +1173,7 @@ unsafe fn flat_stream_write(
11641173
instance,
11651174
memory.cast::<crate::vm::VMMemoryDefinition>(),
11661175
realloc.cast::<crate::vm::VMFuncRef>(),
1176+
async_ != 0,
11671177
wasmtime_environ::component::TypeStreamTableIndex::from_u32(ty),
11681178
wasmtime_environ::component::TypeComponentLocalErrorContextTableIndex::from_u32(
11691179
err_ctx_ty,
@@ -1182,6 +1192,7 @@ unsafe fn flat_stream_read(
11821192
vmctx: NonNull<VMComponentContext>,
11831193
memory: *mut u8,
11841194
realloc: *mut u8,
1195+
async_: u8,
11851196
ty: u32,
11861197
err_ctx_ty: u32,
11871198
payload_size: u32,
@@ -1197,6 +1208,7 @@ unsafe fn flat_stream_read(
11971208
instance,
11981209
memory.cast::<crate::vm::VMMemoryDefinition>(),
11991210
realloc.cast::<crate::vm::VMFuncRef>(),
1211+
async_ != 0,
12001212
wasmtime_environ::component::TypeStreamTableIndex::from_u32(ty),
12011213
wasmtime_environ::component::TypeComponentLocalErrorContextTableIndex::from_u32(
12021214
err_ctx_ty,

0 commit comments

Comments
 (0)