Skip to content

Commit d2406b9

Browse files
authored
Fix requiring realloc for async-lowered results-with-pointers (bytecodealliance#2572)
This commit fixes a mistake in the validation of `canon lower` options when the `async` ABI was used. Previously it accidentally skipped the requirement the `realloc` was present, but any lower'd functions with results that contain pointers (strings, lists, etc), requires `realloc`.
1 parent bb386f2 commit d2406b9

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

crates/wasmparser/src/validator/component_types.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,17 +1265,17 @@ impl ComponentFuncType {
12651265
}
12661266
}
12671267

1268+
// Results of lowered functions that contains pointers must be allocated
1269+
// by the callee meaning that realloc is required. Results of lifted
1270+
// function are allocated by the guest which means that no realloc
1271+
// option is necessary.
1272+
if let Some(ty) = &self.result {
1273+
options.require_realloc_if(offset, || abi == Abi::Lower && ty.contains_ptr(types))?;
1274+
}
1275+
12681276
match (abi, options.concurrency) {
12691277
(Abi::Lower | Abi::Lift, Concurrency::Sync) => {
12701278
if let Some(ty) = &self.result {
1271-
// Results of lowered functions that contains pointers must be
1272-
// allocated by the callee meaning that realloc is required.
1273-
// Results of lifted function are allocated by the guest which
1274-
// means that no realloc option is necessary.
1275-
options.require_realloc_if(offset, || {
1276-
abi == Abi::Lower && ty.contains_ptr(types)
1277-
})?;
1278-
12791279
if !ty.push_wasm_types(ptr_size, types, &mut sig.results) {
12801280
// Too many results to return directly, either a retptr
12811281
// parameter will be used (import) or a single pointer

tests/cli/component-model/async/lower.wast

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,21 @@
4848
)
4949
"the `async` canonical option requires an async function type"
5050
)
51+
52+
;; `async` option requires as `async` function type
53+
(assert_invalid
54+
(component
55+
(import "foo" (func $foo async (result string)))
56+
(core func $foo (canon lower (func $foo) async))
57+
)
58+
"canonical option `memory` is required"
59+
)
60+
(assert_invalid
61+
(component
62+
(import "foo" (func $foo async (result string)))
63+
(core module $libc (memory (export "memory") 1))
64+
(core instance $libc (instantiate $libc))
65+
(core func $foo (canon lower (func $foo) async (memory (core memory $libc "memory"))))
66+
)
67+
"canonical option `realloc` is required"
68+
)

tests/snapshots/cli/component-model/async/lower.wast.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
"filename": "lower.3.wasm",
2828
"module_type": "binary",
2929
"text": "the `async` canonical option requires an async function type"
30+
},
31+
{
32+
"type": "assert_invalid",
33+
"line": 54,
34+
"filename": "lower.4.wasm",
35+
"module_type": "binary",
36+
"text": "canonical option `memory` is required"
37+
},
38+
{
39+
"type": "assert_invalid",
40+
"line": 61,
41+
"filename": "lower.5.wasm",
42+
"module_type": "binary",
43+
"text": "canonical option `realloc` is required"
3044
}
3145
]
3246
}

0 commit comments

Comments
 (0)