Skip to content

Commit 611f79d

Browse files
authored
Add -Wasync-stack-size, automatically configure it too (#9302) (#9305)
* Add `-Wasync-stack-size`, automatically configure it too Recently the `wasmtime` CLI switched from sync to async to handle interrupting host APIs in WASI. Previously the CLI provided no means to configure the async stack size, however, which now means that when increasing the max wasm stack the CLI prints an error that cannot be resolved about the wasm stack being larger than the async stack. This commit fixes this issue by both adding a configuration option for the async stack size and additionally automatically increasing the async stack size when only the wasm stack size is provided. Closes #9298 * Fix feature-gated build
1 parent 0b195ef commit 611f79d

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

Cargo.toml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,25 @@ gc = ["wasmtime-cli-flags/gc"]
435435

436436
# CLI subcommands for the `wasmtime` executable. See `wasmtime $cmd --help`
437437
# for more information on each subcommand.
438-
serve = ["wasi-http", "component-model", "dep:http-body-util", "dep:http"]
438+
serve = [
439+
"wasi-http",
440+
"component-model",
441+
"dep:http-body-util",
442+
"dep:http",
443+
"wasmtime-cli-flags/async",
444+
]
439445
explore = ["dep:wasmtime-explorer", "dep:tempfile"]
440446
wast = ["dep:wasmtime-wast"]
441447
config = ["cache"]
442448
compile = ["cranelift"]
443-
run = ["dep:wasmtime-wasi", "wasmtime/runtime", "dep:listenfd", "dep:wasi-common", "dep:tokio"]
449+
run = [
450+
"dep:wasmtime-wasi",
451+
"wasmtime/runtime",
452+
"dep:listenfd",
453+
"dep:wasi-common",
454+
"dep:tokio",
455+
"wasmtime-cli-flags/async",
456+
]
444457

445458
[[test]]
446459
name = "host_segfault"

crates/cli-flags/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ wasmtime_option_group! {
199199
/// Maximum stack size, in bytes, that wasm is allowed to consume before a
200200
/// stack overflow is reported.
201201
pub max_wasm_stack: Option<usize>,
202+
/// Stack size, in bytes, that will be allocated for async stacks.
203+
///
204+
/// Note that this must be larger than `max-wasm-stack` and the
205+
/// difference between the two is how much stack the host has to execute
206+
/// on.
207+
pub async_stack_size: Option<usize>,
202208
/// Allow unknown exports when running commands.
203209
pub unknown_exports_allow: Option<bool>,
204210
/// Allow the main module to import unknown functions, using an
@@ -652,8 +658,23 @@ impl CommonOptions {
652658
anyhow::bail!("memory protection keys require the pooling allocator");
653659
}
654660

661+
match_feature! {
662+
["async" : self.wasm.async_stack_size]
663+
size => config.async_stack_size(size),
664+
_ => err,
665+
}
666+
655667
if let Some(max) = self.wasm.max_wasm_stack {
656668
config.max_wasm_stack(max);
669+
670+
// If `-Wasync-stack-size` isn't passed then automatically adjust it
671+
// to the wasm stack size provided here too. That prevents the need
672+
// to pass both when one can generally be inferred from the other.
673+
#[cfg(feature = "async")]
674+
if self.wasm.async_stack_size.is_none() {
675+
const DEFAULT_HOST_STACK: usize = 512 << 10;
676+
config.async_stack_size(max + DEFAULT_HOST_STACK);
677+
}
657678
}
658679

659680
if let Some(enable) = self.wasm.relaxed_simd_deterministic {

tests/all/cli_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,21 @@ fn mpk_without_pooling() -> Result<()> {
10931093
Ok(())
10941094
}
10951095

1096+
// Very basic use case: compile binary wasm file and run specific function with arguments.
1097+
#[test]
1098+
fn increase_stack_size() -> Result<()> {
1099+
run_wasmtime(&[
1100+
"run",
1101+
"--invoke",
1102+
"simple",
1103+
&format!("-Wmax-wasm-stack={}", 5 << 20),
1104+
"-Ccache=n",
1105+
"tests/all/cli_tests/simple.wat",
1106+
"4",
1107+
])?;
1108+
Ok(())
1109+
}
1110+
10961111
mod test_programs {
10971112
use super::{get_wasmtime_command, run_wasmtime};
10981113
use anyhow::{bail, Context, Result};

0 commit comments

Comments
 (0)