1 //! Contains the common Wasmtime command line interface (CLI) flags. 2 3 use anyhow::Result; 4 use clap::Parser; 5 use std::time::Duration; 6 use wasmtime::Config; 7 8 pub mod opt; 9 10 #[cfg(feature = "logging")] 11 fn init_file_per_thread_logger(prefix: &'static str) { 12 file_per_thread_logger::initialize(prefix); 13 file_per_thread_logger::allow_uninitialized(); 14 15 // Extending behavior of default spawner: 16 // https://docs.rs/rayon/1.1.0/rayon/struct.ThreadPoolBuilder.html#method.spawn_handler 17 // Source code says DefaultSpawner is implementation detail and 18 // shouldn't be used directly. 19 #[cfg(feature = "parallel-compilation")] 20 rayon::ThreadPoolBuilder::new() 21 .spawn_handler(move |thread| { 22 let mut b = std::thread::Builder::new(); 23 if let Some(name) = thread.name() { 24 b = b.name(name.to_owned()); 25 } 26 if let Some(stack_size) = thread.stack_size() { 27 b = b.stack_size(stack_size); 28 } 29 b.spawn(move || { 30 file_per_thread_logger::initialize(prefix); 31 thread.run() 32 })?; 33 Ok(()) 34 }) 35 .build_global() 36 .unwrap(); 37 } 38 39 wasmtime_option_group! { 40 #[derive(PartialEq, Clone)] 41 pub struct OptimizeOptions { 42 /// Optimization level of generated code (0-2, s; default: 0) 43 pub opt_level: Option<wasmtime::OptLevel>, 44 45 /// Byte size of the guard region after dynamic memories are allocated 46 pub dynamic_memory_guard_size: Option<u64>, 47 48 /// Force using a "static" style for all wasm memories 49 pub static_memory_forced: Option<bool>, 50 51 /// Maximum size in bytes of wasm memory before it becomes dynamically 52 /// relocatable instead of up-front-reserved. 53 pub static_memory_maximum_size: Option<u64>, 54 55 /// Byte size of the guard region after static memories are allocated 56 pub static_memory_guard_size: Option<u64>, 57 58 /// Bytes to reserve at the end of linear memory for growth for dynamic 59 /// memories. 60 pub dynamic_memory_reserved_for_growth: Option<u64>, 61 62 /// Enable the pooling allocator, in place of the on-demand allocator. 63 pub pooling_allocator: Option<bool>, 64 65 /// How many bytes to keep resident between instantiations for the 66 /// pooling allocator in linear memories. 67 pub pooling_memory_keep_resident: Option<usize>, 68 69 /// How many bytes to keep resident between instantiations for the 70 /// pooling allocator in tables. 71 pub pooling_table_keep_resident: Option<usize>, 72 73 /// Configure attempting to initialize linear memory via a 74 /// copy-on-write mapping (default: yes) 75 pub memory_init_cow: Option<bool>, 76 } 77 78 enum Optimize { 79 ... 80 } 81 } 82 83 wasmtime_option_group! { 84 #[derive(PartialEq, Clone)] 85 pub struct CodegenOptions { 86 /// Either `cranelift` or `winch`. 87 /// 88 /// Currently only `cranelift` and `winch` are supported, but not all 89 /// builds of Wasmtime have both built in. 90 pub compiler: Option<wasmtime::Strategy>, 91 /// Enable Cranelift's internal debug verifier (expensive) 92 pub cranelift_debug_verifier: Option<bool>, 93 /// Whether or not to enable caching of compiled modules. 94 pub cache: Option<bool>, 95 /// Configuration for compiled module caching. 96 pub cache_config: Option<String>, 97 /// Whether or not to enable parallel compilation of modules. 98 pub parallel_compilation: Option<bool>, 99 /// Whether to enable proof-carrying code (PCC)-based validation. 100 pub pcc: Option<bool>, 101 102 #[prefixed = "cranelift"] 103 /// Set a cranelift-specific option. Use `wasmtime settings` to see 104 /// all. 105 pub cranelift: Vec<(String, Option<String>)>, 106 } 107 108 enum Codegen { 109 ... 110 } 111 } 112 113 wasmtime_option_group! { 114 #[derive(PartialEq, Clone)] 115 pub struct DebugOptions { 116 /// Enable generation of DWARF debug information in compiled code. 117 pub debug_info: Option<bool>, 118 /// Configure whether compiled code can map native addresses to wasm. 119 pub address_map: Option<bool>, 120 /// Configure whether logging is enabled. 121 pub logging: Option<bool>, 122 /// Configure whether logs are emitted to files 123 pub log_to_files: Option<bool>, 124 /// Enable coredump generation to this file after a WebAssembly trap. 125 pub coredump: Option<String>, 126 } 127 128 enum Debug { 129 ... 130 } 131 } 132 133 wasmtime_option_group! { 134 #[derive(PartialEq, Clone)] 135 pub struct WasmOptions { 136 /// Enable canonicalization of all NaN values. 137 pub nan_canonicalization: Option<bool>, 138 /// Enable execution fuel with N units fuel, trapping after running out 139 /// of fuel. 140 /// 141 /// Most WebAssembly instructions consume 1 unit of fuel. Some 142 /// instructions, such as `nop`, `drop`, `block`, and `loop`, consume 0 143 /// units, as any execution cost associated with them involves other 144 /// instructions which do consume fuel. 145 pub fuel: Option<u64>, 146 /// Yield when a global epoch counter changes, allowing for async 147 /// operation without blocking the executor. 148 pub epoch_interruption: Option<bool>, 149 /// Maximum stack size, in bytes, that wasm is allowed to consume before a 150 /// stack overflow is reported. 151 pub max_wasm_stack: Option<usize>, 152 /// Allow unknown exports when running commands. 153 pub unknown_exports_allow: Option<bool>, 154 /// Allow the main module to import unknown functions, using an 155 /// implementation that immediately traps, when running commands. 156 pub unknown_imports_trap: Option<bool>, 157 /// Allow the main module to import unknown functions, using an 158 /// implementation that returns default values, when running commands. 159 pub unknown_imports_default: Option<bool>, 160 /// Enables memory error checking. (see wmemcheck.md for more info) 161 pub wmemcheck: Option<bool>, 162 /// Maximum size, in bytes, that a linear memory is allowed to reach. 163 /// 164 /// Growth beyond this limit will cause `memory.grow` instructions in 165 /// WebAssembly modules to return -1 and fail. 166 pub max_memory_size: Option<usize>, 167 /// Maximum size, in table elements, that a table is allowed to reach. 168 pub max_table_elements: Option<u32>, 169 /// Maximum number of WebAssembly instances allowed to be created. 170 pub max_instances: Option<usize>, 171 /// Maximum number of WebAssembly tables allowed to be created. 172 pub max_tables: Option<usize>, 173 /// Maximum number of WebAssembly linear memories allowed to be created. 174 pub max_memories: Option<usize>, 175 /// Force a trap to be raised on `memory.grow` and `table.grow` failure 176 /// instead of returning -1 from these instructions. 177 /// 178 /// This is not necessarily a spec-compliant option to enable but can be 179 /// useful for tracking down a backtrace of what is requesting so much 180 /// memory, for example. 181 pub trap_on_grow_failure: Option<bool>, 182 /// Maximum execution time of wasm code before timing out (1, 2s, 100ms, etc) 183 pub timeout: Option<Duration>, 184 /// Configures support for all WebAssembly proposals implemented. 185 pub all_proposals: Option<bool>, 186 /// Configure support for the bulk memory proposal. 187 pub bulk_memory: Option<bool>, 188 /// Configure support for the multi-memory proposal. 189 pub multi_memory: Option<bool>, 190 /// Configure support for the multi-value proposal. 191 pub multi_value: Option<bool>, 192 /// Configure support for the reference-types proposal. 193 pub reference_types: Option<bool>, 194 /// Configure support for the simd proposal. 195 pub simd: Option<bool>, 196 /// Configure support for the relaxed-simd proposal. 197 pub relaxed_simd: Option<bool>, 198 /// Configure forcing deterministic and host-independent behavior of 199 /// the relaxed-simd instructions. 200 /// 201 /// By default these instructions may have architecture-specific behavior as 202 /// allowed by the specification, but this can be used to force the behavior 203 /// of these instructions to match the deterministic behavior classified in 204 /// the specification. Note that enabling this option may come at a 205 /// performance cost. 206 pub relaxed_simd_deterministic: Option<bool>, 207 /// Configure support for the tail-call proposal. 208 pub tail_call: Option<bool>, 209 /// Configure support for the threads proposal. 210 pub threads: Option<bool>, 211 /// Configure support for the memory64 proposal. 212 pub memory64: Option<bool>, 213 /// Configure support for the component-model proposal. 214 pub component_model: Option<bool>, 215 /// Configure support for the function-references proposal. 216 pub function_references: Option<bool>, 217 } 218 219 enum Wasm { 220 ... 221 } 222 } 223 224 wasmtime_option_group! { 225 #[derive(PartialEq, Clone)] 226 pub struct WasiOptions { 227 /// Enable support for WASI common APIs 228 pub common: Option<bool>, 229 /// Enable suport for WASI neural network API (experimental) 230 pub nn: Option<bool>, 231 /// Enable suport for WASI threading API (experimental) 232 pub threads: Option<bool>, 233 /// Enable suport for WASI HTTP API (experimental) 234 pub http: Option<bool>, 235 /// Inherit environment variables and file descriptors following the 236 /// systemd listen fd specification (UNIX only) 237 pub listenfd: Option<bool>, 238 /// Grant access to the given TCP listen socket 239 pub tcplisten: Vec<String>, 240 /// Implement WASI with preview2 primitives (experimental). 241 /// 242 /// Indicates that the implementation of WASI preview1 should be backed by 243 /// the preview2 implementation for components. 244 /// 245 /// This will become the default in the future and this option will be 246 /// removed. For now this is primarily here for testing. 247 pub preview2: Option<bool>, 248 /// Pre-load machine learning graphs (i.e., models) for use by wasi-nn. 249 /// 250 /// Each use of the flag will preload a ML model from the host directory 251 /// using the given model encoding. The model will be mapped to the 252 /// directory name: e.g., `--wasi-nn-graph openvino:/foo/bar` will preload 253 /// an OpenVINO model named `bar`. Note that which model encodings are 254 /// available is dependent on the backends implemented in the 255 /// `wasmtime_wasi_nn` crate. 256 pub nn_graph: Vec<WasiNnGraph>, 257 /// Flag for WASI preview2 to inherit the host's network within the 258 /// guest so it has full access to all addresses/ports/etc. 259 pub inherit_network: Option<bool>, 260 /// Indicates whether `wasi:sockets/ip-name-lookup` is enabled or not. 261 pub allow_ip_name_lookup: Option<bool>, 262 /// Indicates whether `wasi:sockets` TCP support is enabled or not. 263 pub tcp: Option<bool>, 264 /// Indicates whether `wasi:sockets` UDP support is enabled or not. 265 pub udp: Option<bool>, 266 /// Allows imports from the `wasi_unstable` core wasm module. 267 pub preview0: Option<bool>, 268 } 269 270 enum Wasi { 271 ... 272 } 273 } 274 275 #[derive(Debug, Clone, PartialEq)] 276 pub struct WasiNnGraph { 277 pub format: String, 278 pub dir: String, 279 } 280 281 /// Common options for commands that translate WebAssembly modules 282 #[derive(Parser, Clone)] 283 pub struct CommonOptions { 284 // These options groups are used to parse `-O` and such options but aren't 285 // the raw form consumed by the CLI. Instead they're pushed into the `pub` 286 // fields below as part of the `configure` method. 287 // 288 // Ideally clap would support `pub opts: OptimizeOptions` and parse directly 289 // into that but it does not appear to do so for multiple `-O` flags for 290 // now. 291 /// Optimization and tuning related options for wasm performance, `-O help` to 292 /// see all. 293 #[arg(short = 'O', long = "optimize", value_name = "KEY[=VAL[,..]]")] 294 opts_raw: Vec<opt::CommaSeparated<Optimize>>, 295 296 /// Codegen-related configuration options, `-C help` to see all. 297 #[arg(short = 'C', long = "codegen", value_name = "KEY[=VAL[,..]]")] 298 codegen_raw: Vec<opt::CommaSeparated<Codegen>>, 299 300 /// Debug-related configuration options, `-D help` to see all. 301 #[arg(short = 'D', long = "debug", value_name = "KEY[=VAL[,..]]")] 302 debug_raw: Vec<opt::CommaSeparated<Debug>>, 303 304 /// Options for configuring semantic execution of WebAssembly, `-W help` to see 305 /// all. 306 #[arg(short = 'W', long = "wasm", value_name = "KEY[=VAL[,..]]")] 307 wasm_raw: Vec<opt::CommaSeparated<Wasm>>, 308 309 /// Options for configuring WASI and its proposals, `-S help` to see all. 310 #[arg(short = 'S', long = "wasi", value_name = "KEY[=VAL[,..]]")] 311 wasi_raw: Vec<opt::CommaSeparated<Wasi>>, 312 313 // These fields are filled in by the `configure` method below via the 314 // options parsed from the CLI above. This is what the CLI should use. 315 #[arg(skip)] 316 configured: bool, 317 #[arg(skip)] 318 pub opts: OptimizeOptions, 319 #[arg(skip)] 320 pub codegen: CodegenOptions, 321 #[arg(skip)] 322 pub debug: DebugOptions, 323 #[arg(skip)] 324 pub wasm: WasmOptions, 325 #[arg(skip)] 326 pub wasi: WasiOptions, 327 } 328 329 macro_rules! match_feature { 330 ( 331 [$feat:tt : $config:expr] 332 $val:ident => $e:expr, 333 $p:pat => err, 334 ) => { 335 #[cfg(feature = $feat)] 336 { 337 if let Some($val) = $config { 338 $e; 339 } 340 } 341 #[cfg(not(feature = $feat))] 342 { 343 if let Some($p) = $config { 344 anyhow::bail!(concat!("support for ", $feat, " disabled at compile time")); 345 } 346 } 347 }; 348 } 349 350 impl CommonOptions { 351 fn configure(&mut self) { 352 if self.configured { 353 return; 354 } 355 self.configured = true; 356 self.opts.configure_with(&self.opts_raw); 357 self.codegen.configure_with(&self.codegen_raw); 358 self.debug.configure_with(&self.debug_raw); 359 self.wasm.configure_with(&self.wasm_raw); 360 self.wasi.configure_with(&self.wasi_raw); 361 } 362 363 pub fn init_logging(&mut self) -> Result<()> { 364 self.configure(); 365 if self.debug.logging == Some(false) { 366 return Ok(()); 367 } 368 #[cfg(feature = "logging")] 369 if self.debug.log_to_files == Some(true) { 370 let prefix = "wasmtime.dbg."; 371 init_file_per_thread_logger(prefix); 372 } else { 373 use std::io::IsTerminal; 374 use tracing_subscriber::{EnvFilter, FmtSubscriber}; 375 let b = FmtSubscriber::builder() 376 .with_writer(std::io::stderr) 377 .with_env_filter(EnvFilter::from_env("WASMTIME_LOG")) 378 .with_ansi(std::io::stderr().is_terminal()); 379 b.init(); 380 } 381 #[cfg(not(feature = "logging"))] 382 if self.debug.log_to_files == Some(true) || self.debug.logging == Some(true) { 383 anyhow::bail!("support for logging disabled at compile time"); 384 } 385 Ok(()) 386 } 387 388 pub fn config(&mut self, target: Option<&str>) -> Result<Config> { 389 self.configure(); 390 let mut config = Config::new(); 391 392 match_feature! { 393 ["cranelift" : self.codegen.compiler] 394 strategy => config.strategy(strategy), 395 _ => err, 396 } 397 match_feature! { 398 ["cranelift" : target] 399 target => config.target(target)?, 400 _ => err, 401 } 402 match_feature! { 403 ["cranelift" : self.codegen.cranelift_debug_verifier] 404 enable => config.cranelift_debug_verifier(enable), 405 true => err, 406 } 407 if let Some(enable) = self.debug.debug_info { 408 config.debug_info(enable); 409 } 410 if self.debug.coredump.is_some() { 411 #[cfg(feature = "coredump")] 412 config.coredump_on_trap(true); 413 #[cfg(not(feature = "coredump"))] 414 anyhow::bail!("support for coredumps disabled at compile time"); 415 } 416 match_feature! { 417 ["cranelift" : self.opts.opt_level] 418 level => config.cranelift_opt_level(level), 419 _ => err, 420 } 421 match_feature! { 422 ["cranelift" : self.wasm.nan_canonicalization] 423 enable => config.cranelift_nan_canonicalization(enable), 424 true => err, 425 } 426 match_feature! { 427 ["cranelift" : self.codegen.pcc] 428 enable => config.cranelift_pcc(enable), 429 true => err, 430 } 431 432 self.enable_wasm_features(&mut config)?; 433 434 #[cfg(feature = "cranelift")] 435 for (name, value) in self.codegen.cranelift.iter() { 436 let name = name.replace('-', "_"); 437 unsafe { 438 match value { 439 Some(val) => { 440 config.cranelift_flag_set(&name, val); 441 } 442 None => { 443 config.cranelift_flag_enable(&name); 444 } 445 } 446 } 447 } 448 #[cfg(not(feature = "cranelift"))] 449 if !self.codegen.cranelift.is_empty() { 450 anyhow::bail!("support for cranelift disabled at compile time"); 451 } 452 453 #[cfg(feature = "cache")] 454 if self.codegen.cache != Some(false) { 455 match &self.codegen.cache_config { 456 Some(path) => { 457 config.cache_config_load(path)?; 458 } 459 None => { 460 config.cache_config_load_default()?; 461 } 462 } 463 } 464 #[cfg(not(feature = "cache"))] 465 if self.codegen.cache == Some(true) { 466 anyhow::bail!("support for caching disabled at compile time"); 467 } 468 469 match_feature! { 470 ["parallel-compilation" : self.codegen.parallel_compilation] 471 enable => config.parallel_compilation(enable), 472 true => err, 473 } 474 475 if let Some(max) = self.opts.static_memory_maximum_size { 476 config.static_memory_maximum_size(max); 477 } 478 479 if let Some(enable) = self.opts.static_memory_forced { 480 config.static_memory_forced(enable); 481 } 482 483 if let Some(size) = self.opts.static_memory_guard_size { 484 config.static_memory_guard_size(size); 485 } 486 487 if let Some(size) = self.opts.dynamic_memory_guard_size { 488 config.dynamic_memory_guard_size(size); 489 } 490 if let Some(size) = self.opts.dynamic_memory_reserved_for_growth { 491 config.dynamic_memory_reserved_for_growth(size); 492 } 493 494 // If fuel has been configured, set the `consume fuel` flag on the config. 495 if self.wasm.fuel.is_some() { 496 config.consume_fuel(true); 497 } 498 499 if let Some(enable) = self.wasm.epoch_interruption { 500 config.epoch_interruption(enable); 501 } 502 if let Some(enable) = self.debug.address_map { 503 config.generate_address_map(enable); 504 } 505 if let Some(enable) = self.opts.memory_init_cow { 506 config.memory_init_cow(enable); 507 } 508 509 match_feature! { 510 ["pooling-allocator" : self.opts.pooling_allocator] 511 enable => { 512 if enable { 513 let mut cfg = wasmtime::PoolingAllocationConfig::default(); 514 if let Some(size) = self.opts.pooling_memory_keep_resident { 515 cfg.linear_memory_keep_resident(size); 516 } 517 if let Some(size) = self.opts.pooling_table_keep_resident { 518 cfg.table_keep_resident(size); 519 } 520 config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling(cfg)); 521 } 522 }, 523 true => err, 524 } 525 526 if let Some(max) = self.wasm.max_wasm_stack { 527 config.max_wasm_stack(max); 528 } 529 530 if let Some(enable) = self.wasm.relaxed_simd_deterministic { 531 config.relaxed_simd_deterministic(enable); 532 } 533 match_feature! { 534 ["cranelift" : self.wasm.wmemcheck] 535 enable => config.wmemcheck(enable), 536 true => err, 537 } 538 539 Ok(config) 540 } 541 542 pub fn enable_wasm_features(&self, config: &mut Config) -> Result<()> { 543 let all = self.wasm.all_proposals; 544 545 if let Some(enable) = self.wasm.simd.or(all) { 546 config.wasm_simd(enable); 547 } 548 if let Some(enable) = self.wasm.relaxed_simd.or(all) { 549 config.wasm_relaxed_simd(enable); 550 } 551 if let Some(enable) = self.wasm.bulk_memory.or(all) { 552 config.wasm_bulk_memory(enable); 553 } 554 if let Some(enable) = self.wasm.reference_types.or(all) { 555 config.wasm_reference_types(enable); 556 } 557 if let Some(enable) = self.wasm.function_references.or(all) { 558 config.wasm_function_references(enable); 559 } 560 if let Some(enable) = self.wasm.multi_value.or(all) { 561 config.wasm_multi_value(enable); 562 } 563 if let Some(enable) = self.wasm.tail_call.or(all) { 564 config.wasm_tail_call(enable); 565 } 566 if let Some(enable) = self.wasm.threads.or(all) { 567 config.wasm_threads(enable); 568 } 569 if let Some(enable) = self.wasm.multi_memory.or(all) { 570 config.wasm_multi_memory(enable); 571 } 572 if let Some(enable) = self.wasm.memory64.or(all) { 573 config.wasm_memory64(enable); 574 } 575 if let Some(enable) = self.wasm.component_model.or(all) { 576 #[cfg(feature = "component-model")] 577 config.wasm_component_model(enable); 578 #[cfg(not(feature = "component-model"))] 579 if enable && all.is_none() { 580 anyhow::bail!("support for the component model was disabled at compile-time"); 581 } 582 } 583 Ok(()) 584 } 585 } 586 587 impl PartialEq for CommonOptions { 588 fn eq(&self, other: &CommonOptions) -> bool { 589 let mut me = self.clone(); 590 me.configure(); 591 let mut other = other.clone(); 592 other.configure(); 593 let CommonOptions { 594 opts_raw: _, 595 codegen_raw: _, 596 debug_raw: _, 597 wasm_raw: _, 598 wasi_raw: _, 599 configured: _, 600 601 opts, 602 codegen, 603 debug, 604 wasm, 605 wasi, 606 } = me; 607 opts == other.opts 608 && codegen == other.codegen 609 && debug == other.debug 610 && wasm == other.wasm 611 && wasi == other.wasi 612 } 613 } 614