Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cap host allocations in `write_zeros` for some streams (#14482)

Alex Crichton5efe8ce0836f
diff --git a/crates/c-api/src/wasi.rs b/crates/c-api/src/wasi.rs
index abada403d2..0f9722b87f 100644
--- a/crates/c-api/src/wasi.rs
+++ b/crates/c-api/src/wasi.rs
@@ -228,7 +228,7 @@ impl wasmtime_wasi::p2::OutputStream for CustomOutputStream {
Ok(())
}
fn check_write(&mut self) -> Result<usize, StreamError> {
- Ok(usize::MAX)
+ Ok(64 * 1024)
}
}
diff --git a/crates/test-programs/src/bin/p2_cli_stdout_write_zeros_to_sink.rs b/crates/test-programs/src/bin/p2_cli_stdout_write_zeros_to_sink.rs
new file mode 100644
index 0000000000..b741af9fd5
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_cli_stdout_write_zeros_to_sink.rs
@@ -0,0 +1,6 @@
+fn main() {
+ let mut args = std::env::args().skip(1);
+ let amt_to_write = args.next().unwrap().parse().unwrap();
+ let stdout = wasip2::cli::stdout::get_stdout();
+ stdout.write_zeroes(amt_to_write).unwrap();
+}
diff --git a/crates/wasi-io/src/streams.rs b/crates/wasi-io/src/streams.rs
index 474eeb796b..8c276138a5 100644
--- a/crates/wasi-io/src/streams.rs
+++ b/crates/wasi-io/src/streams.rs
@@ -170,6 +170,13 @@ pub trait OutputStream: Pollable {
///
/// Note that this method does not block.
///
+ /// Additionally note that this method controls the allowed allocation size
+ /// of other methods. For example `write_zeroes` will allow up to the
+ /// allowance of `check_write` and will allocate that many zeroes currently.
+ /// This should not be an overly-large value as otherwise guests will be
+ /// able to force the host to perform such an allocation. This number is
+ /// typicall ~64k for WASI streams at this time.
+ ///
/// # Errors
///
/// Returns an [`StreamError`] if:
diff --git a/crates/wasi-tls/src/p2/io.rs b/crates/wasi-tls/src/p2/io.rs
index 7edbc315cd..4542e15741 100644
--- a/crates/wasi-tls/src/p2/io.rs
+++ b/crates/wasi-tls/src/p2/io.rs
@@ -272,7 +272,7 @@ enum WriteState<IO> {
Closed,
Error(io::Error),
}
-const READY_SIZE: usize = 1024 * 1024 * 1024;
+const READY_SIZE: usize = 64 * 1024;
impl<IO> WriteState<IO>
where
diff --git a/crates/wasi/src/cli/file.rs b/crates/wasi/src/cli/file.rs
index ca154ae915..ddf0705f9e 100644
--- a/crates/wasi/src/cli/file.rs
+++ b/crates/wasi/src/cli/file.rs
@@ -61,7 +61,7 @@ impl OutputStream for OutputFile {
}
fn check_write(&mut self) -> StreamResult<usize> {
- Ok(1024 * 1024)
+ Ok(crate::MAX_READ_SIZE_ALLOC)
}
}
diff --git a/crates/wasi/src/cli/stdout.rs b/crates/wasi/src/cli/stdout.rs
index 6995c76882..0bbed8e59e 100644
--- a/crates/wasi/src/cli/stdout.rs
+++ b/crates/wasi/src/cli/stdout.rs
@@ -74,7 +74,7 @@ enum StdioOutputStream {
/// The number of bytes a single `write` is permitted to carry, as reported by
/// `check_write`.
-const WRITE_BUDGET: usize = 1024 * 1024;
+const WRITE_BUDGET: usize = crate::MAX_READ_SIZE_ALLOC;
impl OutputStream for StdioOutputStream {
fn write(&mut self, bytes: Bytes) -> p2::StreamResult<()> {
diff --git a/crates/wasi/src/p2/filesystem.rs b/crates/wasi/src/p2/filesystem.rs
index f0a1d0589d..d6d9b43111 100644
--- a/crates/wasi/src/p2/filesystem.rs
+++ b/crates/wasi/src/p2/filesystem.rs
@@ -274,7 +274,7 @@ impl FileOutputStream {
}
// FIXME: configurable? determine from how much space left in file?
-const FILE_WRITE_CAPACITY: usize = 1024 * 1024;
+const FILE_WRITE_CAPACITY: usize = crate::MAX_READ_SIZE_ALLOC;
#[async_trait::async_trait]
impl OutputStream for FileOutputStream {
diff --git a/crates/wasi/src/p2/pipe.rs b/crates/wasi/src/p2/pipe.rs
index 05dbff1a56..7b5bce9ac2 100644
--- a/crates/wasi/src/p2/pipe.rs
+++ b/crates/wasi/src/p2/pipe.rs
@@ -285,7 +285,7 @@ impl OutputStream for SinkOutputStream {
fn check_write(&mut self) -> Result<usize, StreamError> {
// This stream is always ready for writing.
- Ok(usize::MAX)
+ Ok(crate::MAX_READ_SIZE_ALLOC)
}
}
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index b67197b818..63be4984e8 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -1501,7 +1501,7 @@ impl wasmtime_wasi::p2::OutputStream for LogStream {
}
fn check_write(&mut self) -> StreamResult<usize> {
- Ok(1024 * 1024)
+ Ok(64 * 1024)
}
}
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 4a0add760a..ac4953237f 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -3219,6 +3219,32 @@ start a print 1234
run_wasmtime(&["run", "-Stcp", P3_CLI_DENY_LISTEN_COMPONENT])?;
Ok(())
}
+
+ #[test]
+ fn p2_cli_stdout_write_zeros_to_sink() -> Result<()> {
+ for n in ["0", "100", "10000"] {
+ run_wasmtime(&[
+ "run",
+ "-Sinherit-stdout=n",
+ P2_CLI_STDOUT_WRITE_ZEROS_TO_SINK_COMPONENT,
+ n,
+ ])?;
+ }
+ let output = super::wasmtime(&[
+ "run",
+ "-Sinherit-stdout=n",
+ P2_CLI_STDOUT_WRITE_ZEROS_TO_SINK_COMPONENT,
+ "10000000000",
+ ])?
+ .output()?;
+ assert_eq!(output.stdout, b"");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains("cannot write more zeroes than `check_write` allows"),
+ "bad stderr: {stderr}"
+ );
+ Ok(())
+ }
}
#[test]