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 /// Allows imports from the `wasi_unstable` core wasm module. 263 pub preview0: Option<bool>, 264 } 265 266 enum Wasi { 267 ... 268 } 269 } 270 271 #[derive(Debug, Clone, PartialEq)] 272 pub struct WasiNnGraph { 273 pub format: String, 274 pub dir: String, 275 } 276 277 /// Common options for commands that translate WebAssembly modules 278 #[derive(Parser, Clone)] 279 pub struct CommonOptions { 280 // These options groups are used to parse `-O` and such options but aren't 281 // the raw form consumed by the CLI. Instead they're pushed into the `pub` 282 // fields below as part of the `configure` method. 283 // 284 // Ideally clap would support `pub opts: OptimizeOptions` and parse directly 285 // into that but it does not appear to do so for multiple `-O` flags for 286 // now. 287 /// Optimization and tuning related options for wasm performance, `-O help` to 288 /// see all. 289 #[clap(short = 'O', long = "optimize", value_name = "KEY[=VAL[,..]]")] 290 opts_raw: Vec<opt::CommaSeparated<Optimize>>, 291 292 /// Codegen-related configuration options, `-C help` to see all. 293 #[clap(short = 'C', long = "codegen", value_name = "KEY[=VAL[,..]]")] 294 codegen_raw: Vec<opt::CommaSeparated<Codegen>>, 295 296 /// Debug-related configuration options, `-D help` to see all. 297 #[clap(short = 'D', long = "debug", value_name = "KEY[=VAL[,..]]")] 298 debug_raw: Vec<opt::CommaSeparated<Debug>>, 299 300 /// Options for configuring semantic execution of WebAssembly, `-W help` to see 301 /// all. 302 #[clap(short = 'W', long = "wasm", value_name = "KEY[=VAL[,..]]")] 303 wasm_raw: Vec<opt::CommaSeparated<Wasm>>, 304 305 /// Options for configuring WASI and its proposals, `-S help` to see all. 306 #[clap(short = 'S', long = "wasi", value_name = "KEY[=VAL[,..]]")] 307 wasi_raw: Vec<opt::CommaSeparated<Wasi>>, 308 309 // These fields are filled in by the `configure` method below via the 310 // options parsed from the CLI above. This is what the CLI should use. 311 #[clap(skip)] 312 configured: bool, 313 #[clap(skip)] 314 pub opts: OptimizeOptions, 315 #[clap(skip)] 316 pub codegen: CodegenOptions, 317 #[clap(skip)] 318 pub debug: DebugOptions, 319 #[clap(skip)] 320 pub wasm: WasmOptions, 321 #[clap(skip)] 322 pub wasi: WasiOptions, 323 } 324 325 macro_rules! match_feature { 326 ( 327 [$feat:tt : $config:expr] 328 $val:ident => $e:expr, 329 $p:pat => err, 330 ) => { 331 #[cfg(feature = $feat)] 332 { 333 if let Some($val) = $config { 334 $e; 335 } 336 } 337 #[cfg(not(feature = $feat))] 338 { 339 if let Some($p) = $config { 340 anyhow::bail!(concat!("support for ", $feat, " disabled at compile time")); 341 } 342 } 343 }; 344 } 345 346 impl CommonOptions { 347 fn configure(&mut self) { 348 if self.configured { 349 return; 350 } 351 self.configured = true; 352 self.opts.configure_with(&self.opts_raw); 353 self.codegen.configure_with(&self.codegen_raw); 354 self.debug.configure_with(&self.debug_raw); 355 self.wasm.configure_with(&self.wasm_raw); 356 self.wasi.configure_with(&self.wasi_raw); 357 } 358 359 pub fn init_logging(&mut self) -> Result<()> { 360 self.configure(); 361 if self.debug.logging == Some(false) { 362 return Ok(()); 363 } 364 #[cfg(feature = "logging")] 365 if self.debug.log_to_files == Some(true) { 366 let prefix = "wasmtime.dbg."; 367 init_file_per_thread_logger(prefix); 368 } else { 369 use std::io::IsTerminal; 370 use tracing_subscriber::{EnvFilter, FmtSubscriber}; 371 let b = FmtSubscriber::builder() 372 .with_writer(std::io::stderr) 373 .with_env_filter(EnvFilter::from_env("WASMTIME_LOG")) 374 .with_ansi(std::io::stderr().is_terminal()); 375 b.init(); 376 } 377 #[cfg(not(feature = "logging"))] 378 if self.debug.log_to_files == Some(true) || self.debug.logging == Some(true) { 379 anyhow::bail!("support for logging disabled at compile time"); 380 } 381 Ok(()) 382 } 383 384 pub fn config(&mut self, target: Option<&str>) -> Result<Config> { 385 self.configure(); 386 let mut config = Config::new(); 387 388 match_feature! { 389 ["cranelift" : self.codegen.compiler] 390 strategy => config.strategy(strategy), 391 _ => err, 392 } 393 match_feature! { 394 ["cranelift" : target] 395 target => config.target(target)?, 396 _ => err, 397 } 398 match_feature! { 399 ["cranelift" : self.codegen.cranelift_debug_verifier] 400 enable => config.cranelift_debug_verifier(enable), 401 true => err, 402 } 403 if let Some(enable) = self.debug.debug_info { 404 config.debug_info(enable); 405 } 406 if self.debug.coredump.is_some() { 407 #[cfg(feature = "coredump")] 408 config.coredump_on_trap(true); 409 #[cfg(not(feature = "coredump"))] 410 anyhow::bail!("support for coredumps disabled at compile time"); 411 } 412 match_feature! { 413 ["cranelift" : self.opts.opt_level] 414 level => config.cranelift_opt_level(level), 415 _ => err, 416 } 417 match_feature! { 418 ["cranelift" : self.wasm.nan_canonicalization] 419 enable => config.cranelift_nan_canonicalization(enable), 420 true => err, 421 } 422 match_feature! { 423 ["cranelift" : self.codegen.pcc] 424 enable => config.cranelift_pcc(enable), 425 true => err, 426 } 427 428 self.enable_wasm_features(&mut config)?; 429 430 #[cfg(feature = "cranelift")] 431 for (name, value) in self.codegen.cranelift.iter() { 432 let name = name.replace('-', "_"); 433 unsafe { 434 match value { 435 Some(val) => { 436 config.cranelift_flag_set(&name, val); 437 } 438 None => { 439 config.cranelift_flag_enable(&name); 440 } 441 } 442 } 443 } 444 #[cfg(not(feature = "cranelift"))] 445 if !self.codegen.cranelift.is_empty() { 446 anyhow::bail!("support for cranelift disabled at compile time"); 447 } 448 449 #[cfg(feature = "cache")] 450 if self.codegen.cache != Some(false) { 451 match &self.codegen.cache_config { 452 Some(path) => { 453 config.cache_config_load(path)?; 454 } 455 None => { 456 config.cache_config_load_default()?; 457 } 458 } 459 } 460 #[cfg(not(feature = "cache"))] 461 if self.codegen.cache == Some(true) { 462 anyhow::bail!("support for caching disabled at compile time"); 463 } 464 465 match_feature! { 466 ["parallel-compilation" : self.codegen.parallel_compilation] 467 enable => config.parallel_compilation(enable), 468 true => err, 469 } 470 471 if let Some(max) = self.opts.static_memory_maximum_size { 472 config.static_memory_maximum_size(max); 473 } 474 475 if let Some(enable) = self.opts.static_memory_forced { 476 config.static_memory_forced(enable); 477 } 478 479 if let Some(size) = self.opts.static_memory_guard_size { 480 config.static_memory_guard_size(size); 481 } 482 483 if let Some(size) = self.opts.dynamic_memory_guard_size { 484 config.dynamic_memory_guard_size(size); 485 } 486 if let Some(size) = self.opts.dynamic_memory_reserved_for_growth { 487 config.dynamic_memory_reserved_for_growth(size); 488 } 489 490 // If fuel has been configured, set the `consume fuel` flag on the config. 491 if self.wasm.fuel.is_some() { 492 config.consume_fuel(true); 493 } 494 495 if let Some(enable) = self.wasm.epoch_interruption { 496 config.epoch_interruption(enable); 497 } 498 if let Some(enable) = self.debug.address_map { 499 config.generate_address_map(enable); 500 } 501 if let Some(enable) = self.opts.memory_init_cow { 502 config.memory_init_cow(enable); 503 } 504 505 match_feature! { 506 ["pooling-allocator" : self.opts.pooling_allocator] 507 enable => { 508 if enable { 509 let mut cfg = wasmtime::PoolingAllocationConfig::default(); 510 if let Some(size) = self.opts.pooling_memory_keep_resident { 511 cfg.linear_memory_keep_resident(size); 512 } 513 if let Some(size) = self.opts.pooling_table_keep_resident { 514 cfg.table_keep_resident(size); 515 } 516 config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling(cfg)); 517 } 518 }, 519 true => err, 520 } 521 522 if let Some(max) = self.wasm.max_wasm_stack { 523 config.max_wasm_stack(max); 524 } 525 526 if let Some(enable) = self.wasm.relaxed_simd_deterministic { 527 config.relaxed_simd_deterministic(enable); 528 } 529 match_feature! { 530 ["cranelift" : self.wasm.wmemcheck] 531 enable => config.wmemcheck(enable), 532 true => err, 533 } 534 535 Ok(config) 536 } 537 538 pub fn enable_wasm_features(&self, config: &mut Config) -> Result<()> { 539 let all = self.wasm.all_proposals; 540 541 if let Some(enable) = self.wasm.simd.or(all) { 542 config.wasm_simd(enable); 543 } 544 if let Some(enable) = self.wasm.relaxed_simd.or(all) { 545 config.wasm_relaxed_simd(enable); 546 } 547 if let Some(enable) = self.wasm.bulk_memory.or(all) { 548 config.wasm_bulk_memory(enable); 549 } 550 if let Some(enable) = self.wasm.reference_types.or(all) { 551 config.wasm_reference_types(enable); 552 } 553 if let Some(enable) = self.wasm.function_references.or(all) { 554 config.wasm_function_references(enable); 555 } 556 if let Some(enable) = self.wasm.multi_value.or(all) { 557 config.wasm_multi_value(enable); 558 } 559 if let Some(enable) = self.wasm.tail_call.or(all) { 560 config.wasm_tail_call(enable); 561 } 562 if let Some(enable) = self.wasm.threads.or(all) { 563 config.wasm_threads(enable); 564 } 565 if let Some(enable) = self.wasm.multi_memory.or(all) { 566 config.wasm_multi_memory(enable); 567 } 568 if let Some(enable) = self.wasm.memory64.or(all) { 569 config.wasm_memory64(enable); 570 } 571 if let Some(enable) = self.wasm.component_model.or(all) { 572 #[cfg(feature = "component-model")] 573 config.wasm_component_model(enable); 574 #[cfg(not(feature = "component-model"))] 575 if enable && all.is_none() { 576 anyhow::bail!("support for the component model was disabled at compile-time"); 577 } 578 } 579 Ok(()) 580 } 581 } 582 583 impl PartialEq for CommonOptions { 584 fn eq(&self, other: &CommonOptions) -> bool { 585 let mut me = self.clone(); 586 me.configure(); 587 let mut other = other.clone(); 588 other.configure(); 589 let CommonOptions { 590 opts_raw: _, 591 codegen_raw: _, 592 debug_raw: _, 593 wasm_raw: _, 594 wasi_raw: _, 595 configured: _, 596 597 opts, 598 codegen, 599 debug, 600 wasm, 601 wasi, 602 } = me; 603 opts == other.opts 604 && codegen == other.codegen 605 && debug == other.debug 606 && wasm == other.wasm 607 && wasi == other.wasi 608 } 609 } 610