Skip to content

Commit

Permalink
Add -Wasync-stack-size, automatically configure it too (#9302)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexcrichton authored Sep 24, 2024
1 parent 356a3bf commit 4d03b85
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,25 @@ gc = ["wasmtime-cli-flags/gc"]

# CLI subcommands for the `wasmtime` executable. See `wasmtime $cmd --help`
# for more information on each subcommand.
serve = ["wasi-http", "component-model", "dep:http-body-util", "dep:http"]
serve = [
"wasi-http",
"component-model",
"dep:http-body-util",
"dep:http",
"wasmtime-cli-flags/async",
]
explore = ["dep:wasmtime-explorer", "dep:tempfile"]
wast = ["dep:wasmtime-wast"]
config = ["cache"]
compile = ["cranelift"]
run = ["dep:wasmtime-wasi", "wasmtime/runtime", "dep:listenfd", "dep:wasi-common", "dep:tokio"]
run = [
"dep:wasmtime-wasi",
"wasmtime/runtime",
"dep:listenfd",
"dep:wasi-common",
"dep:tokio",
"wasmtime-cli-flags/async",
]

[[test]]
name = "host_segfault"
Expand Down
21 changes: 21 additions & 0 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ wasmtime_option_group! {
/// Maximum stack size, in bytes, that wasm is allowed to consume before a
/// stack overflow is reported.
pub max_wasm_stack: Option<usize>,
/// Stack size, in bytes, that will be allocated for async stacks.
///
/// Note that this must be larger than `max-wasm-stack` and the
/// difference between the two is how much stack the host has to execute
/// on.
pub async_stack_size: Option<usize>,
/// Allow unknown exports when running commands.
pub unknown_exports_allow: Option<bool>,
/// Allow the main module to import unknown functions, using an
Expand Down Expand Up @@ -658,8 +664,23 @@ impl CommonOptions {
anyhow::bail!("memory protection keys require the pooling allocator");
}

match_feature! {
["async" : self.wasm.async_stack_size]
size => config.async_stack_size(size),
_ => err,
}

if let Some(max) = self.wasm.max_wasm_stack {
config.max_wasm_stack(max);

// If `-Wasync-stack-size` isn't passed then automatically adjust it
// to the wasm stack size provided here too. That prevents the need
// to pass both when one can generally be inferred from the other.
#[cfg(feature = "async")]
if self.wasm.async_stack_size.is_none() {
const DEFAULT_HOST_STACK: usize = 512 << 10;
config.async_stack_size(max + DEFAULT_HOST_STACK);
}
}

if let Some(enable) = self.wasm.relaxed_simd_deterministic {
Expand Down
15 changes: 15 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,21 @@ fn mpk_without_pooling() -> Result<()> {
Ok(())
}

// Very basic use case: compile binary wasm file and run specific function with arguments.
#[test]
fn increase_stack_size() -> Result<()> {
run_wasmtime(&[
"run",
"--invoke",
"simple",
&format!("-Wmax-wasm-stack={}", 5 << 20),
"-Ccache=n",
"tests/all/cli_tests/simple.wat",
"4",
])?;
Ok(())
}

mod test_programs {
use super::{get_wasmtime_command, run_wasmtime};
use anyhow::{bail, Context, Result};
Expand Down

0 comments on commit 4d03b85

Please sign in to comment.