Add a limits and trap-on-OOM options to the CLI (#6149)
* Add a limits and trap-on-OOM options to the CLI This commit adds new options to the `wasmtime` CLI to control the `Store::limiter` behavior at runtime. This enables artificially restriction the memory usage of the wasm instance, for example. Additionally a new option is added to `StoreLimits` to force a trap on growth failure. This is intended to help quickly debug modules with backtraces if OOM is happening, or even diagnosing if OOM is happening in the first place. * Fix compile of fuzzing oracle
This commit is contained in:
@@ -9,7 +9,9 @@ use std::io::Write;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use wasmtime::{Engine, Func, Linker, Module, Store, Val, ValType};
|
||||
use wasmtime::{
|
||||
Engine, Func, Linker, Module, Store, StoreLimits, StoreLimitsBuilder, Val, ValType,
|
||||
};
|
||||
use wasmtime_cli_flags::{CommonOptions, WasiModules};
|
||||
use wasmtime_wasi::maybe_exit_on_error;
|
||||
use wasmtime_wasi::sync::{ambient_authority, Dir, TcpListener, WasiCtxBuilder};
|
||||
@@ -166,6 +168,38 @@ pub struct RunCommand {
|
||||
/// The arguments to pass to the module
|
||||
#[clap(value_name = "ARGS")]
|
||||
module_args: Vec<String>,
|
||||
|
||||
/// Maximum size, in bytes, that a linear memory is allowed to reach.
|
||||
///
|
||||
/// Growth beyond this limit will cause `memory.grow` instructions in
|
||||
/// WebAssembly modules to return -1 and fail.
|
||||
#[clap(long, value_name = "BYTES")]
|
||||
max_memory_size: Option<usize>,
|
||||
|
||||
/// Maximum size, in table elements, that a table is allowed to reach.
|
||||
#[clap(long)]
|
||||
max_table_elements: Option<u32>,
|
||||
|
||||
/// Maximum number of WebAssembly instances allowed to be created.
|
||||
#[clap(long)]
|
||||
max_instances: Option<usize>,
|
||||
|
||||
/// Maximum number of WebAssembly tables allowed to be created.
|
||||
#[clap(long)]
|
||||
max_tables: Option<usize>,
|
||||
|
||||
/// Maximum number of WebAssembly linear memories allowed to be created.
|
||||
#[clap(long)]
|
||||
max_memories: Option<usize>,
|
||||
|
||||
/// Force a trap to be raised on `memory.grow` and `table.grow` failure
|
||||
/// instead of returning -1 from these instructions.
|
||||
///
|
||||
/// This is not necessarily a spec-compliant option to enable but can be
|
||||
/// useful for tracking down a backtrace of what is requesting so much
|
||||
/// memory, for example.
|
||||
#[clap(long)]
|
||||
trap_on_grow_failure: bool,
|
||||
}
|
||||
|
||||
impl RunCommand {
|
||||
@@ -212,6 +246,27 @@ impl RunCommand {
|
||||
preopen_sockets,
|
||||
)?;
|
||||
|
||||
let mut limits = StoreLimitsBuilder::new();
|
||||
if let Some(max) = self.max_memory_size {
|
||||
limits = limits.memory_size(max);
|
||||
}
|
||||
if let Some(max) = self.max_table_elements {
|
||||
limits = limits.table_elements(max);
|
||||
}
|
||||
if let Some(max) = self.max_instances {
|
||||
limits = limits.instances(max);
|
||||
}
|
||||
if let Some(max) = self.max_tables {
|
||||
limits = limits.tables(max);
|
||||
}
|
||||
if let Some(max) = self.max_memories {
|
||||
limits = limits.memories(max);
|
||||
}
|
||||
store.data_mut().limits = limits
|
||||
.trap_on_grow_failure(self.trap_on_grow_failure)
|
||||
.build();
|
||||
store.limiter(|t| &mut t.limits);
|
||||
|
||||
// If fuel has been configured, we want to add the configured
|
||||
// fuel amount to this store.
|
||||
if let Some(fuel) = self.common.fuel {
|
||||
@@ -470,6 +525,7 @@ struct Host {
|
||||
wasi_nn: Option<Arc<WasiNnCtx>>,
|
||||
#[cfg(feature = "wasi-threads")]
|
||||
wasi_threads: Option<Arc<WasiThreadsCtx<Host>>>,
|
||||
limits: StoreLimits,
|
||||
}
|
||||
|
||||
/// Populates the given `Linker` with WASI APIs.
|
||||
|
||||
Reference in New Issue
Block a user