Skip to content

Update the ABI of async imports/exports #2198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions crates/wasmparser/src/validator/component_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use core::{
/// Functions that exceed this limit will instead pass parameters indirectly from
/// linear memory via a single pointer parameter.
const MAX_FLAT_FUNC_PARAMS: usize = 16;
/// The maximum number of parameters in the canonical ABI that can be passed by
/// value in async function imports/exports.
const MAX_FLAT_ASYNC_PARAMS: usize = 4;
/// The maximum number of results in the canonical ABI that can be returned by a function.
///
/// Functions that exceed this limit have their results written to linear memory via an
Expand Down Expand Up @@ -931,15 +934,7 @@ impl ComponentFuncType {
let mut sig = LoweredSignature::default();

if abi == Abi::Lower && options.concurrency.is_async() {
for _ in 0..2 {
sig.params.assert_push(ValType::I32);
}
sig.results.assert_push(ValType::I32);
options.require_memory(offset)?;
if self.result.is_some_and(|ty| ty.contains_ptr(types)) {
options.require_realloc(offset)?;
}
return Ok(sig.into_func_type());
sig.params.max = MAX_FLAT_ASYNC_PARAMS;
}

for (_, ty) in self.params.iter() {
Expand Down Expand Up @@ -975,9 +970,6 @@ impl ComponentFuncType {
}

match (abi, options.concurrency) {
(Abi::Lower, Concurrency::Async { .. }) => {
unreachable!("special-cased at the start of the function")
}
(Abi::Lower | Abi::Lift, Concurrency::Sync) => {
if let Some(ty) = &self.result {
// Results of lowered functions that contains pointers must be
Expand Down Expand Up @@ -1006,10 +998,19 @@ impl ComponentFuncType {
}
}
}
(Abi::Lift, Concurrency::Async { callback: Some(_) }) => {
(Abi::Lower, Concurrency::Async { callback: _ }) => {
if self.result.is_some() {
sig.params.max = MAX_LOWERED_TYPES;
sig.params.assert_push(ValType::I32);
options.require_memory(offset)?;
}
sig.results.assert_push(ValType::I32);
}
(Abi::Lift, Concurrency::Async { callback: None }) => {}
(Abi::Lift, Concurrency::Async { callback }) => {
if callback.is_some() {
sig.results.assert_push(ValType::I32);
}
}
}

Ok(sig.into_func_type())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(type (;1;) (func (param "s" string) (result string)))
(import "foo" (func (;0;) (type 1)))
(core module (;0;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;0;) (func (param i32 i32 i32) (result i32)))
(type (;1;) (func (param i32 i32 i32)))
(type (;2;) (func (param i32 i32 i32 i32) (result i32)))
(import "$root" "[async-lower]foo" (func (;0;) (type 0)))
Expand All @@ -28,17 +28,18 @@
)
)
(core module (;1;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;0;) (func (param i32 i32 i32) (result i32)))
(type (;1;) (func (param i32 i32 i32)))
(table (;0;) 4 4 funcref)
(export "0" (func $"indirect-$root-[async-lower]foo"))
(export "1" (func $indirect-$root-foo))
(export "2" (func $"indirect-foo:foo/bar-[async-lower]foo"))
(export "3" (func $indirect-foo:foo/bar-foo))
(export "$imports" (table 0))
(func $"indirect-$root-[async-lower]foo" (;0;) (type 0) (param i32 i32) (result i32)
(func $"indirect-$root-[async-lower]foo" (;0;) (type 0) (param i32 i32 i32) (result i32)
local.get 0
local.get 1
local.get 2
i32.const 0
call_indirect (type 0)
)
Expand All @@ -49,9 +50,10 @@
i32.const 1
call_indirect (type 1)
)
(func $"indirect-foo:foo/bar-[async-lower]foo" (;2;) (type 0) (param i32 i32) (result i32)
(func $"indirect-foo:foo/bar-[async-lower]foo" (;2;) (type 0) (param i32 i32 i32) (result i32)
local.get 0
local.get 1
local.get 2
i32.const 2
call_indirect (type 0)
)
Expand All @@ -67,7 +69,7 @@
)
)
(core module (;2;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;0;) (func (param i32 i32 i32) (result i32)))
(type (;1;) (func (param i32 i32 i32)))
(import "" "0" (func (;0;) (type 0)))
(import "" "1" (func (;1;) (type 1)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(func (import "$root" "[async-lower]foo") (param i32 i32) (result i32))
(func (import "foo:foo/bar" "[async-lower]foo") (param i32 i32) (result i32))
(func (import "$root" "[async-lower]foo") (param i32 i32 i32) (result i32))
(func (import "foo:foo/bar" "[async-lower]foo") (param i32 i32 i32) (result i32))
(func (import "$root" "foo") (param i32 i32 i32))
(func (import "foo:foo/bar" "foo") (param i32 i32 i32))
(memory (export "memory") 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,99 +15,99 @@
(type (;4;) (func (param "x" 2) (param "y" u32) (result 3)))
(import "foo" (func (;0;) (type 4)))
(core module (;0;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;0;) (func (param i32 i32 i32) (result i32)))
(type (;1;) (func (result i64)))
(type (;2;) (func (param i32 i32 i32) (result i32)))
(type (;3;) (func (param i32) (result i32)))
(type (;4;) (func (param i32)))
(type (;2;) (func (param i32) (result i32)))
(type (;3;) (func (param i32)))
(type (;4;) (func (param i32 i32) (result i32)))
(type (;5;) (func (param i32 i32)))
(type (;6;) (func (param i32 i32 i32 i32) (result i32)))
(import "$root" "[async-lower]foo" (func (;0;) (type 0)))
(import "foo:foo/bar" "[async-lower]foo" (func (;1;) (type 0)))
(import "$root" "[stream-new-2]foo" (func (;2;) (type 1)))
(import "$root" "[stream-read-2]foo" (func (;3;) (type 2)))
(import "$root" "[stream-write-2]foo" (func (;4;) (type 2)))
(import "$root" "[stream-cancel-read-2]foo" (func (;5;) (type 3)))
(import "$root" "[stream-cancel-write-2]foo" (func (;6;) (type 3)))
(import "$root" "[stream-close-readable-2]foo" (func (;7;) (type 4)))
(import "$root" "[stream-close-writable-2]foo" (func (;8;) (type 4)))
(import "$root" "[stream-read-2]foo" (func (;3;) (type 0)))
(import "$root" "[stream-write-2]foo" (func (;4;) (type 0)))
(import "$root" "[stream-cancel-read-2]foo" (func (;5;) (type 2)))
(import "$root" "[stream-cancel-write-2]foo" (func (;6;) (type 2)))
(import "$root" "[stream-close-readable-2]foo" (func (;7;) (type 3)))
(import "$root" "[stream-close-writable-2]foo" (func (;8;) (type 3)))
(import "foo:foo/bar" "[stream-new-2]foo" (func (;9;) (type 1)))
(import "foo:foo/bar" "[stream-read-2]foo" (func (;10;) (type 2)))
(import "foo:foo/bar" "[stream-write-2]foo" (func (;11;) (type 2)))
(import "foo:foo/bar" "[stream-cancel-read-2]foo" (func (;12;) (type 3)))
(import "foo:foo/bar" "[stream-cancel-write-2]foo" (func (;13;) (type 3)))
(import "foo:foo/bar" "[stream-close-readable-2]foo" (func (;14;) (type 4)))
(import "foo:foo/bar" "[stream-close-writable-2]foo" (func (;15;) (type 4)))
(import "foo:foo/bar" "[stream-read-2]foo" (func (;10;) (type 0)))
(import "foo:foo/bar" "[stream-write-2]foo" (func (;11;) (type 0)))
(import "foo:foo/bar" "[stream-cancel-read-2]foo" (func (;12;) (type 2)))
(import "foo:foo/bar" "[stream-cancel-write-2]foo" (func (;13;) (type 2)))
(import "foo:foo/bar" "[stream-close-readable-2]foo" (func (;14;) (type 3)))
(import "foo:foo/bar" "[stream-close-writable-2]foo" (func (;15;) (type 3)))
(import "$root" "[future-new-0]foo" (func (;16;) (type 1)))
(import "$root" "[future-read-0]foo" (func (;17;) (type 0)))
(import "$root" "[future-write-0]foo" (func (;18;) (type 0)))
(import "$root" "[future-cancel-read-0]foo" (func (;19;) (type 3)))
(import "$root" "[future-cancel-write-0]foo" (func (;20;) (type 3)))
(import "$root" "[future-close-readable-0]foo" (func (;21;) (type 4)))
(import "$root" "[future-close-writable-0]foo" (func (;22;) (type 4)))
(import "$root" "[future-read-0]foo" (func (;17;) (type 4)))
(import "$root" "[future-write-0]foo" (func (;18;) (type 4)))
(import "$root" "[future-cancel-read-0]foo" (func (;19;) (type 2)))
(import "$root" "[future-cancel-write-0]foo" (func (;20;) (type 2)))
(import "$root" "[future-close-readable-0]foo" (func (;21;) (type 3)))
(import "$root" "[future-close-writable-0]foo" (func (;22;) (type 3)))
(import "foo:foo/bar" "[future-new-0]foo" (func (;23;) (type 1)))
(import "foo:foo/bar" "[future-read-0]foo" (func (;24;) (type 0)))
(import "foo:foo/bar" "[future-write-0]foo" (func (;25;) (type 0)))
(import "foo:foo/bar" "[future-cancel-read-0]foo" (func (;26;) (type 3)))
(import "foo:foo/bar" "[future-cancel-write-0]foo" (func (;27;) (type 3)))
(import "foo:foo/bar" "[future-close-readable-0]foo" (func (;28;) (type 4)))
(import "foo:foo/bar" "[future-close-writable-0]foo" (func (;29;) (type 4)))
(import "foo:foo/bar" "[future-read-0]foo" (func (;24;) (type 4)))
(import "foo:foo/bar" "[future-write-0]foo" (func (;25;) (type 4)))
(import "foo:foo/bar" "[future-cancel-read-0]foo" (func (;26;) (type 2)))
(import "foo:foo/bar" "[future-cancel-write-0]foo" (func (;27;) (type 2)))
(import "foo:foo/bar" "[future-close-readable-0]foo" (func (;28;) (type 3)))
(import "foo:foo/bar" "[future-close-writable-0]foo" (func (;29;) (type 3)))
(import "$root" "[future-new-1]foo" (func (;30;) (type 1)))
(import "$root" "[future-read-1]foo" (func (;31;) (type 0)))
(import "$root" "[future-write-1]foo" (func (;32;) (type 0)))
(import "$root" "[future-cancel-read-1]foo" (func (;33;) (type 3)))
(import "$root" "[future-cancel-write-1]foo" (func (;34;) (type 3)))
(import "$root" "[future-close-readable-1]foo" (func (;35;) (type 4)))
(import "$root" "[future-close-writable-1]foo" (func (;36;) (type 4)))
(import "$root" "[future-read-1]foo" (func (;31;) (type 4)))
(import "$root" "[future-write-1]foo" (func (;32;) (type 4)))
(import "$root" "[future-cancel-read-1]foo" (func (;33;) (type 2)))
(import "$root" "[future-cancel-write-1]foo" (func (;34;) (type 2)))
(import "$root" "[future-close-readable-1]foo" (func (;35;) (type 3)))
(import "$root" "[future-close-writable-1]foo" (func (;36;) (type 3)))
(import "foo:foo/bar" "[future-new-1]foo" (func (;37;) (type 1)))
(import "foo:foo/bar" "[future-read-1]foo" (func (;38;) (type 0)))
(import "foo:foo/bar" "[future-write-1]foo" (func (;39;) (type 0)))
(import "foo:foo/bar" "[future-cancel-read-1]foo" (func (;40;) (type 3)))
(import "foo:foo/bar" "[future-cancel-write-1]foo" (func (;41;) (type 3)))
(import "foo:foo/bar" "[future-close-readable-1]foo" (func (;42;) (type 4)))
(import "foo:foo/bar" "[future-close-writable-1]foo" (func (;43;) (type 4)))
(import "foo:foo/bar" "[future-read-1]foo" (func (;38;) (type 4)))
(import "foo:foo/bar" "[future-write-1]foo" (func (;39;) (type 4)))
(import "foo:foo/bar" "[future-cancel-read-1]foo" (func (;40;) (type 2)))
(import "foo:foo/bar" "[future-cancel-write-1]foo" (func (;41;) (type 2)))
(import "foo:foo/bar" "[future-close-readable-1]foo" (func (;42;) (type 3)))
(import "foo:foo/bar" "[future-close-writable-1]foo" (func (;43;) (type 3)))
(import "[export]$root" "[stream-new-2]foo" (func (;44;) (type 1)))
(import "[export]$root" "[stream-read-2]foo" (func (;45;) (type 2)))
(import "[export]$root" "[stream-write-2]foo" (func (;46;) (type 2)))
(import "[export]$root" "[stream-cancel-read-2]foo" (func (;47;) (type 3)))
(import "[export]$root" "[stream-cancel-write-2]foo" (func (;48;) (type 3)))
(import "[export]$root" "[stream-close-readable-2]foo" (func (;49;) (type 4)))
(import "[export]$root" "[stream-close-writable-2]foo" (func (;50;) (type 4)))
(import "[export]$root" "[stream-read-2]foo" (func (;45;) (type 0)))
(import "[export]$root" "[stream-write-2]foo" (func (;46;) (type 0)))
(import "[export]$root" "[stream-cancel-read-2]foo" (func (;47;) (type 2)))
(import "[export]$root" "[stream-cancel-write-2]foo" (func (;48;) (type 2)))
(import "[export]$root" "[stream-close-readable-2]foo" (func (;49;) (type 3)))
(import "[export]$root" "[stream-close-writable-2]foo" (func (;50;) (type 3)))
(import "[export]foo:foo/bar" "[stream-new-2]foo" (func (;51;) (type 1)))
(import "[export]foo:foo/bar" "[stream-read-2]foo" (func (;52;) (type 2)))
(import "[export]foo:foo/bar" "[stream-write-2]foo" (func (;53;) (type 2)))
(import "[export]foo:foo/bar" "[stream-cancel-read-2]foo" (func (;54;) (type 3)))
(import "[export]foo:foo/bar" "[stream-cancel-write-2]foo" (func (;55;) (type 3)))
(import "[export]foo:foo/bar" "[stream-close-readable-2]foo" (func (;56;) (type 4)))
(import "[export]foo:foo/bar" "[stream-close-writable-2]foo" (func (;57;) (type 4)))
(import "[export]foo:foo/bar" "[stream-read-2]foo" (func (;52;) (type 0)))
(import "[export]foo:foo/bar" "[stream-write-2]foo" (func (;53;) (type 0)))
(import "[export]foo:foo/bar" "[stream-cancel-read-2]foo" (func (;54;) (type 2)))
(import "[export]foo:foo/bar" "[stream-cancel-write-2]foo" (func (;55;) (type 2)))
(import "[export]foo:foo/bar" "[stream-close-readable-2]foo" (func (;56;) (type 3)))
(import "[export]foo:foo/bar" "[stream-close-writable-2]foo" (func (;57;) (type 3)))
(import "[export]$root" "[future-new-0]foo" (func (;58;) (type 1)))
(import "[export]$root" "[future-read-0]foo" (func (;59;) (type 0)))
(import "[export]$root" "[future-write-0]foo" (func (;60;) (type 0)))
(import "[export]$root" "[future-cancel-read-0]foo" (func (;61;) (type 3)))
(import "[export]$root" "[future-cancel-write-0]foo" (func (;62;) (type 3)))
(import "[export]$root" "[future-close-readable-0]foo" (func (;63;) (type 4)))
(import "[export]$root" "[future-close-writable-0]foo" (func (;64;) (type 4)))
(import "[export]$root" "[future-read-0]foo" (func (;59;) (type 4)))
(import "[export]$root" "[future-write-0]foo" (func (;60;) (type 4)))
(import "[export]$root" "[future-cancel-read-0]foo" (func (;61;) (type 2)))
(import "[export]$root" "[future-cancel-write-0]foo" (func (;62;) (type 2)))
(import "[export]$root" "[future-close-readable-0]foo" (func (;63;) (type 3)))
(import "[export]$root" "[future-close-writable-0]foo" (func (;64;) (type 3)))
(import "[export]foo:foo/bar" "[future-new-0]foo" (func (;65;) (type 1)))
(import "[export]foo:foo/bar" "[future-read-0]foo" (func (;66;) (type 0)))
(import "[export]foo:foo/bar" "[future-write-0]foo" (func (;67;) (type 0)))
(import "[export]foo:foo/bar" "[future-cancel-read-0]foo" (func (;68;) (type 3)))
(import "[export]foo:foo/bar" "[future-cancel-write-0]foo" (func (;69;) (type 3)))
(import "[export]foo:foo/bar" "[future-close-readable-0]foo" (func (;70;) (type 4)))
(import "[export]foo:foo/bar" "[future-close-writable-0]foo" (func (;71;) (type 4)))
(import "[export]foo:foo/bar" "[future-read-0]foo" (func (;66;) (type 4)))
(import "[export]foo:foo/bar" "[future-write-0]foo" (func (;67;) (type 4)))
(import "[export]foo:foo/bar" "[future-cancel-read-0]foo" (func (;68;) (type 2)))
(import "[export]foo:foo/bar" "[future-cancel-write-0]foo" (func (;69;) (type 2)))
(import "[export]foo:foo/bar" "[future-close-readable-0]foo" (func (;70;) (type 3)))
(import "[export]foo:foo/bar" "[future-close-writable-0]foo" (func (;71;) (type 3)))
(import "[export]$root" "[future-new-1]foo" (func (;72;) (type 1)))
(import "[export]$root" "[future-read-1]foo" (func (;73;) (type 0)))
(import "[export]$root" "[future-write-1]foo" (func (;74;) (type 0)))
(import "[export]$root" "[future-cancel-read-1]foo" (func (;75;) (type 3)))
(import "[export]$root" "[future-cancel-write-1]foo" (func (;76;) (type 3)))
(import "[export]$root" "[future-close-readable-1]foo" (func (;77;) (type 4)))
(import "[export]$root" "[future-close-writable-1]foo" (func (;78;) (type 4)))
(import "[export]$root" "[future-read-1]foo" (func (;73;) (type 4)))
(import "[export]$root" "[future-write-1]foo" (func (;74;) (type 4)))
(import "[export]$root" "[future-cancel-read-1]foo" (func (;75;) (type 2)))
(import "[export]$root" "[future-cancel-write-1]foo" (func (;76;) (type 2)))
(import "[export]$root" "[future-close-readable-1]foo" (func (;77;) (type 3)))
(import "[export]$root" "[future-close-writable-1]foo" (func (;78;) (type 3)))
(import "[export]foo:foo/bar" "[future-new-1]foo" (func (;79;) (type 1)))
(import "[export]foo:foo/bar" "[future-read-1]foo" (func (;80;) (type 0)))
(import "[export]foo:foo/bar" "[future-write-1]foo" (func (;81;) (type 0)))
(import "[export]foo:foo/bar" "[future-cancel-read-1]foo" (func (;82;) (type 3)))
(import "[export]foo:foo/bar" "[future-cancel-write-1]foo" (func (;83;) (type 3)))
(import "[export]foo:foo/bar" "[future-close-readable-1]foo" (func (;84;) (type 4)))
(import "[export]foo:foo/bar" "[future-close-writable-1]foo" (func (;85;) (type 4)))
(import "[export]foo:foo/bar" "[future-read-1]foo" (func (;80;) (type 4)))
(import "[export]foo:foo/bar" "[future-write-1]foo" (func (;81;) (type 4)))
(import "[export]foo:foo/bar" "[future-cancel-read-1]foo" (func (;82;) (type 2)))
(import "[export]foo:foo/bar" "[future-cancel-write-1]foo" (func (;83;) (type 2)))
(import "[export]foo:foo/bar" "[future-close-readable-1]foo" (func (;84;) (type 3)))
(import "[export]foo:foo/bar" "[future-close-writable-1]foo" (func (;85;) (type 3)))
(memory (;0;) 1)
(export "[async-lift-stackful]foo" (func 86))
(export "[async-lift-stackful]foo:foo/bar#foo" (func 87))
Expand All @@ -128,7 +128,7 @@
)
)
(core module (;1;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;0;) (func (param i32 i32 i32) (result i32)))
(type (;1;) (func (param i32 i32 i32) (result i32)))
(type (;2;) (func (param i32 i32) (result i32)))
(table (;0;) 26 26 funcref)
Expand Down Expand Up @@ -159,9 +159,10 @@
(export "24" (func $"[export]foo:foo/bar-[future-read-1]foo"))
(export "25" (func $"[export]foo:foo/bar-[future-write-1]foo"))
(export "$imports" (table 0))
(func $"indirect-$root-[async-lower]foo" (;0;) (type 0) (param i32 i32) (result i32)
(func $"indirect-$root-[async-lower]foo" (;0;) (type 0) (param i32 i32 i32) (result i32)
local.get 0
local.get 1
local.get 2
i32.const 0
call_indirect (type 0)
)
Expand Down Expand Up @@ -203,9 +204,10 @@
i32.const 6
call_indirect (type 2)
)
(func $"indirect-foo:foo/bar-[async-lower]foo" (;7;) (type 0) (param i32 i32) (result i32)
(func $"indirect-foo:foo/bar-[async-lower]foo" (;7;) (type 0) (param i32 i32 i32) (result i32)
local.get 0
local.get 1
local.get 2
i32.const 7
call_indirect (type 0)
)
Expand Down Expand Up @@ -328,7 +330,7 @@
)
)
(core module (;2;)
(type (;0;) (func (param i32 i32) (result i32)))
(type (;0;) (func (param i32 i32 i32) (result i32)))
(type (;1;) (func (param i32 i32 i32) (result i32)))
(type (;2;) (func (param i32 i32) (result i32)))
(import "" "0" (func (;0;) (type 0)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(func (import "$root" "[async-lower]foo") (param i32 i32) (result i32))
(func (import "foo:foo/bar" "[async-lower]foo") (param i32 i32) (result i32))
(func (import "$root" "[async-lower]foo") (param i32 i32 i32) (result i32))
(func (import "foo:foo/bar" "[async-lower]foo") (param i32 i32 i32) (result i32))
(func (import "$root" "[stream-new-2]foo") (result i64))
(func (import "$root" "[stream-read-2]foo") (param i32 i32 i32) (result i32))
(func (import "$root" "[stream-write-2]foo") (param i32 i32 i32) (result i32))
Expand Down
Loading