A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
cli: avoid overflow when deriving the async stack size (#14514)
Xia Chaod2fc272c61b3
diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rsindex 89b02b0db5..c551fc30b7 100644--- a/crates/cli-flags/src/lib.rs+++ b/crates/cli-flags/src/lib.rs@@ -1255,7 +1255,7 @@ impl CommonOptions {#[cfg(any(feature = "async", feature = "stack-switching"))]if self.wasm.async_stack_size.is_none() {const DEFAULT_HOST_STACK: usize = 512 << 10;- config.async_stack_size(max + DEFAULT_HOST_STACK);+ config.async_stack_size(max.saturating_add(DEFAULT_HOST_STACK));}}diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rsindex 63654779b3..7846db9c77 100644--- a/tests/all/cli_tests.rs+++ b/tests/all/cli_tests.rs@@ -4164,3 +4164,30 @@ fn wasi_snapshot0_poll_oneoff_hostcall_fuel() -> Result<()> {assert_eq!(stdout, "48\n"); // `errno::nomem`Ok(())}++// The CLI derives the async stack size from `-Wmax-wasm-stack` when the latter+// is set on its own, and that derivation used to overflow for very large+// values. A stack that large may legitimately fail to be allocated, but the+// CLI must not panic while computing the async stack size.+/// An enormous `-W max-wasm-stack` must not overflow the CLI's derivation of the+/// async stack size.+///+/// This asserts the absence of the *overflow*, not the absence of *any* failure:+/// on a 32-bit target `usize::MAX` asks for a ~4 GiB stack, so an allocation+/// failure is a legitimate outcome there (the merge queue's i686 job failed once+/// because this test demanded that the request succeed).+#[test]+fn max_wasm_stack_large_value_does_not_overflow() -> Result<()> {+ let output = run_wasmtime(&[+ "run",+ "-W",+ &format!("max-wasm-stack={}", usize::MAX),+ "tests/all/cli_tests/simple.wat",+ ]);+ let output = format!("{output:?}");+ assert!(+ !output.contains("attempt to add with overflow"),+ "the async stack size derivation overflowed: {output}"+ );+ Ok(())+}