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: 2) 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 /// Indicates whether an unmapped region of memory is placed before all 63 /// linear memories. 64 pub guard_before_linear_memory: Option<bool>, 65 66 /// Whether to initialize tables lazily, so that instantiation is 67 /// fast but indirect calls are a little slower. If no, tables are 68 /// initialized eagerly from any active element segments that apply to 69 /// them during instantiation. (default: yes) 70 pub table_lazy_init: Option<bool>, 71 72 /// Enable the pooling allocator, in place of the on-demand allocator. 73 pub pooling_allocator: Option<bool>, 74 75 /// The number of decommits to do per batch. A batch size of 1 76 /// effectively disables decommit batching. (default: 1) 77 pub pooling_decommit_batch_size: Option<usize>, 78 79 /// How many bytes to keep resident between instantiations for the 80 /// pooling allocator in linear memories. 81 pub pooling_memory_keep_resident: Option<usize>, 82 83 /// How many bytes to keep resident between instantiations for the 84 /// pooling allocator in tables. 85 pub pooling_table_keep_resident: Option<usize>, 86 87 /// Enable memory protection keys for the pooling allocator; this can 88 /// optimize the size of memory slots. 89 pub pooling_memory_protection_keys: Option<bool>, 90 91 /// Sets an upper limit on how many memory protection keys (MPK) Wasmtime 92 /// will use. (default: 16) 93 pub pooling_max_memory_protection_keys: Option<usize>, 94 95 /// Configure attempting to initialize linear memory via a 96 /// copy-on-write mapping (default: yes) 97 pub memory_init_cow: Option<bool>, 98 99 /// The maximum number of WebAssembly instances which can be created 100 /// with the pooling allocator. 101 pub pooling_total_core_instances: Option<u32>, 102 103 /// The maximum number of WebAssembly components which can be created 104 /// with the pooling allocator. 105 pub pooling_total_component_instances: Option<u32>, 106 107 /// The maximum number of WebAssembly memories which can be created with 108 /// the pooling allocator. 109 pub pooling_total_memories: Option<u32>, 110 111 /// The maximum number of WebAssembly tables which can be created with 112 /// the pooling allocator. 113 pub pooling_total_tables: Option<u32>, 114 115 /// The maximum number of WebAssembly stacks which can be created with 116 /// the pooling allocator. 117 pub pooling_total_stacks: Option<u32>, 118 119 /// The maximum runtime size of each linear memory in the pooling 120 /// allocator, in bytes. 121 pub pooling_max_memory_size: Option<usize>, 122 123 /// The maximum table elements for any table defined in a module when 124 /// using the pooling allocator. 125 pub pooling_table_elements: Option<usize>, 126 127 /// The maximum size, in bytes, allocated for a core instance's metadata 128 /// when using the pooling allocator. 129 pub pooling_max_core_instance_size: Option<usize>, 130 131 /// Configures the maximum number of "unused warm slots" to retain in the 132 /// pooling allocator. (default: 100) 133 pub pooling_max_unused_warm_slots: Option<u32>, 134 135 /// Configures whether or not stacks used for async futures are reset to 136 /// zero after usage. (default: false) 137 pub pooling_async_stack_zeroing: Option<bool>, 138 139 /// How much memory, in bytes, to keep resident for async stacks allocated 140 /// with the pooling allocator. (default: 0) 141 pub pooling_async_stack_keep_resident: Option<usize>, 142 143 /// The maximum size, in bytes, allocated for a component instance's 144 /// `VMComponentContext` metadata. (default: 1MiB) 145 pub pooling_max_component_instance_size: Option<usize>, 146 147 /// The maximum number of core instances a single component may contain 148 /// (default is unlimited). 149 pub pooling_max_core_instances_per_component: Option<u32>, 150 151 /// The maximum number of Wasm linear memories that a single component may 152 /// transitively contain (default is unlimited). 153 pub pooling_max_memories_per_component: Option<u32>, 154 155 /// The maximum number of tables that a single component may transitively 156 /// contain (default is unlimited). 157 pub pooling_max_tables_per_component: Option<u32>, 158 159 /// The maximum number of defined tables for a core module. (default: 1) 160 pub pooling_max_tables_per_module: Option<u32>, 161 162 /// The maximum number of defined linear memories for a module. (default: 1) 163 pub pooling_max_memories_per_module: Option<u32>, 164 165 /// The maximum number of concurrent GC heaps supported. (default: 1000) 166 pub pooling_total_gc_heaps: Option<u32>, 167 168 /// Enable or disable the use of host signal handlers for traps. 169 pub signals_based_traps: Option<bool>, 170 } 171 172 enum Optimize { 173 ... 174 } 175 } 176 177 wasmtime_option_group! { 178 #[derive(PartialEq, Clone)] 179 pub struct CodegenOptions { 180 /// Either `cranelift` or `winch`. 181 /// 182 /// Currently only `cranelift` and `winch` are supported, but not all 183 /// builds of Wasmtime have both built in. 184 pub compiler: Option<wasmtime::Strategy>, 185 /// Enable Cranelift's internal debug verifier (expensive) 186 pub cranelift_debug_verifier: Option<bool>, 187 /// Whether or not to enable caching of compiled modules. 188 pub cache: Option<bool>, 189 /// Configuration for compiled module caching. 190 pub cache_config: Option<String>, 191 /// Whether or not to enable parallel compilation of modules. 192 pub parallel_compilation: Option<bool>, 193 /// Whether to enable proof-carrying code (PCC)-based validation. 194 pub pcc: Option<bool>, 195 196 #[prefixed = "cranelift"] 197 /// Set a cranelift-specific option. Use `wasmtime settings` to see 198 /// all. 199 pub cranelift: Vec<(String, Option<String>)>, 200 } 201 202 enum Codegen { 203 ... 204 } 205 } 206 207 wasmtime_option_group! { 208 #[derive(PartialEq, Clone)] 209 pub struct DebugOptions { 210 /// Enable generation of DWARF debug information in compiled code. 211 pub debug_info: Option<bool>, 212 /// Configure whether compiled code can map native addresses to wasm. 213 pub address_map: Option<bool>, 214 /// Configure whether logging is enabled. 215 pub logging: Option<bool>, 216 /// Configure whether logs are emitted to files 217 pub log_to_files: Option<bool>, 218 /// Enable coredump generation to this file after a WebAssembly trap. 219 pub coredump: Option<String>, 220 } 221 222 enum Debug { 223 ... 224 } 225 } 226 227 wasmtime_option_group! { 228 #[derive(PartialEq, Clone)] 229 pub struct WasmOptions { 230 /// Enable canonicalization of all NaN values. 231 pub nan_canonicalization: Option<bool>, 232 /// Enable execution fuel with N units fuel, trapping after running out 233 /// of fuel. 234 /// 235 /// Most WebAssembly instructions consume 1 unit of fuel. Some 236 /// instructions, such as `nop`, `drop`, `block`, and `loop`, consume 0 237 /// units, as any execution cost associated with them involves other 238 /// instructions which do consume fuel. 239 pub fuel: Option<u64>, 240 /// Yield when a global epoch counter changes, allowing for async 241 /// operation without blocking the executor. 242 pub epoch_interruption: Option<bool>, 243 /// Maximum stack size, in bytes, that wasm is allowed to consume before a 244 /// stack overflow is reported. 245 pub max_wasm_stack: Option<usize>, 246 /// Stack size, in bytes, that will be allocated for async stacks. 247 /// 248 /// Note that this must be larger than `max-wasm-stack` and the 249 /// difference between the two is how much stack the host has to execute 250 /// on. 251 pub async_stack_size: Option<usize>, 252 /// Allow unknown exports when running commands. 253 pub unknown_exports_allow: Option<bool>, 254 /// Allow the main module to import unknown functions, using an 255 /// implementation that immediately traps, when running commands. 256 pub unknown_imports_trap: Option<bool>, 257 /// Allow the main module to import unknown functions, using an 258 /// implementation that returns default values, when running commands. 259 pub unknown_imports_default: Option<bool>, 260 /// Enables memory error checking. (see wmemcheck.md for more info) 261 pub wmemcheck: Option<bool>, 262 /// Maximum size, in bytes, that a linear memory is allowed to reach. 263 /// 264 /// Growth beyond this limit will cause `memory.grow` instructions in 265 /// WebAssembly modules to return -1 and fail. 266 pub max_memory_size: Option<usize>, 267 /// Maximum size, in table elements, that a table is allowed to reach. 268 pub max_table_elements: Option<usize>, 269 /// Maximum number of WebAssembly instances allowed to be created. 270 pub max_instances: Option<usize>, 271 /// Maximum number of WebAssembly tables allowed to be created. 272 pub max_tables: Option<usize>, 273 /// Maximum number of WebAssembly linear memories allowed to be created. 274 pub max_memories: Option<usize>, 275 /// Force a trap to be raised on `memory.grow` and `table.grow` failure 276 /// instead of returning -1 from these instructions. 277 /// 278 /// This is not necessarily a spec-compliant option to enable but can be 279 /// useful for tracking down a backtrace of what is requesting so much 280 /// memory, for example. 281 pub trap_on_grow_failure: Option<bool>, 282 /// Maximum execution time of wasm code before timing out (1, 2s, 100ms, etc) 283 pub timeout: Option<Duration>, 284 /// Configures support for all WebAssembly proposals implemented. 285 pub all_proposals: Option<bool>, 286 /// Configure support for the bulk memory proposal. 287 pub bulk_memory: Option<bool>, 288 /// Configure support for the multi-memory proposal. 289 pub multi_memory: Option<bool>, 290 /// Configure support for the multi-value proposal. 291 pub multi_value: Option<bool>, 292 /// Configure support for the reference-types proposal. 293 pub reference_types: Option<bool>, 294 /// Configure support for the simd proposal. 295 pub simd: Option<bool>, 296 /// Configure support for the relaxed-simd proposal. 297 pub relaxed_simd: Option<bool>, 298 /// Configure forcing deterministic and host-independent behavior of 299 /// the relaxed-simd instructions. 300 /// 301 /// By default these instructions may have architecture-specific behavior as 302 /// allowed by the specification, but this can be used to force the behavior 303 /// of these instructions to match the deterministic behavior classified in 304 /// the specification. Note that enabling this option may come at a 305 /// performance cost. 306 pub relaxed_simd_deterministic: Option<bool>, 307 /// Configure support for the tail-call proposal. 308 pub tail_call: Option<bool>, 309 /// Configure support for the threads proposal. 310 pub threads: Option<bool>, 311 /// Configure support for the memory64 proposal. 312 pub memory64: Option<bool>, 313 /// Configure support for the component-model proposal. 314 pub component_model: Option<bool>, 315 /// Configure support for 33+ flags in the component model. 316 pub component_model_more_flags: Option<bool>, 317 /// Component model support for more than one return value. 318 pub component_model_multiple_returns: Option<bool>, 319 /// Configure support for the function-references proposal. 320 pub function_references: Option<bool>, 321 /// Configure support for the GC proposal. 322 pub gc: Option<bool>, 323 /// Configure support for the custom-page-sizes proposal. 324 pub custom_page_sizes: Option<bool>, 325 /// Configure support for the wide-arithmetic proposal. 326 pub wide_arithmetic: Option<bool>, 327 } 328 329 enum Wasm { 330 ... 331 } 332 } 333 334 wasmtime_option_group! { 335 #[derive(PartialEq, Clone)] 336 pub struct WasiOptions { 337 /// Enable support for WASI CLI APIs, including filesystems, sockets, clocks, and random. 338 pub cli: Option<bool>, 339 /// Enable WASI APIs marked as: @unstable(feature = cli-exit-with-code) 340 pub cli_exit_with_code: Option<bool>, 341 /// Deprecated alias for `cli` 342 pub common: Option<bool>, 343 /// Enable support for WASI neural network API (experimental) 344 pub nn: Option<bool>, 345 /// Enable support for WASI threading API (experimental) 346 pub threads: Option<bool>, 347 /// Enable support for WASI HTTP API (experimental) 348 pub http: Option<bool>, 349 /// Enable support for WASI config API (experimental) 350 pub config: Option<bool>, 351 /// Enable support for WASI key-value API (experimental) 352 pub keyvalue: Option<bool>, 353 /// Inherit environment variables and file descriptors following the 354 /// systemd listen fd specification (UNIX only) 355 pub listenfd: Option<bool>, 356 /// Grant access to the given TCP listen socket 357 pub tcplisten: Vec<String>, 358 /// Implement WASI CLI APIs with preview2 primitives (experimental). 359 /// 360 /// Indicates that the implementation of WASI preview1 should be backed by 361 /// the preview2 implementation for components. 362 /// 363 /// This will become the default in the future and this option will be 364 /// removed. For now this is primarily here for testing. 365 pub preview2: Option<bool>, 366 /// Pre-load machine learning graphs (i.e., models) for use by wasi-nn. 367 /// 368 /// Each use of the flag will preload a ML model from the host directory 369 /// using the given model encoding. The model will be mapped to the 370 /// directory name: e.g., `--wasi-nn-graph openvino:/foo/bar` will preload 371 /// an OpenVINO model named `bar`. Note that which model encodings are 372 /// available is dependent on the backends implemented in the 373 /// `wasmtime_wasi_nn` crate. 374 pub nn_graph: Vec<WasiNnGraph>, 375 /// Flag for WASI preview2 to inherit the host's network within the 376 /// guest so it has full access to all addresses/ports/etc. 377 pub inherit_network: Option<bool>, 378 /// Indicates whether `wasi:sockets/ip-name-lookup` is enabled or not. 379 pub allow_ip_name_lookup: Option<bool>, 380 /// Indicates whether `wasi:sockets` TCP support is enabled or not. 381 pub tcp: Option<bool>, 382 /// Indicates whether `wasi:sockets` UDP support is enabled or not. 383 pub udp: Option<bool>, 384 /// Enable WASI APIs marked as: @unstable(feature = network-error-code) 385 pub network_error_code: Option<bool>, 386 /// Allows imports from the `wasi_unstable` core wasm module. 387 pub preview0: Option<bool>, 388 /// Inherit all environment variables from the parent process. 389 /// 390 /// This option can be further overwritten with `--env` flags. 391 pub inherit_env: Option<bool>, 392 /// Pass a wasi config variable to the program. 393 pub config_var: Vec<KeyValuePair>, 394 /// Preset data for the In-Memory provider of WASI key-value API. 395 pub keyvalue_in_memory_data: Vec<KeyValuePair>, 396 } 397 398 enum Wasi { 399 ... 400 } 401 } 402 403 #[derive(Debug, Clone, PartialEq)] 404 pub struct WasiNnGraph { 405 pub format: String, 406 pub dir: String, 407 } 408 409 #[derive(Debug, Clone, PartialEq)] 410 pub struct KeyValuePair { 411 pub key: String, 412 pub value: String, 413 } 414 415 /// Common options for commands that translate WebAssembly modules 416 #[derive(Parser, Clone)] 417 pub struct CommonOptions { 418 // These options groups are used to parse `-O` and such options but aren't 419 // the raw form consumed by the CLI. Instead they're pushed into the `pub` 420 // fields below as part of the `configure` method. 421 // 422 // Ideally clap would support `pub opts: OptimizeOptions` and parse directly 423 // into that but it does not appear to do so for multiple `-O` flags for 424 // now. 425 /// Optimization and tuning related options for wasm performance, `-O help` to 426 /// see all. 427 #[arg(short = 'O', long = "optimize", value_name = "KEY[=VAL[,..]]")] 428 opts_raw: Vec<opt::CommaSeparated<Optimize>>, 429 430 /// Codegen-related configuration options, `-C help` to see all. 431 #[arg(short = 'C', long = "codegen", value_name = "KEY[=VAL[,..]]")] 432 codegen_raw: Vec<opt::CommaSeparated<Codegen>>, 433 434 /// Debug-related configuration options, `-D help` to see all. 435 #[arg(short = 'D', long = "debug", value_name = "KEY[=VAL[,..]]")] 436 debug_raw: Vec<opt::CommaSeparated<Debug>>, 437 438 /// Options for configuring semantic execution of WebAssembly, `-W help` to see 439 /// all. 440 #[arg(short = 'W', long = "wasm", value_name = "KEY[=VAL[,..]]")] 441 wasm_raw: Vec<opt::CommaSeparated<Wasm>>, 442 443 /// Options for configuring WASI and its proposals, `-S help` to see all. 444 #[arg(short = 'S', long = "wasi", value_name = "KEY[=VAL[,..]]")] 445 wasi_raw: Vec<opt::CommaSeparated<Wasi>>, 446 447 // These fields are filled in by the `configure` method below via the 448 // options parsed from the CLI above. This is what the CLI should use. 449 #[arg(skip)] 450 configured: bool, 451 #[arg(skip)] 452 pub opts: OptimizeOptions, 453 #[arg(skip)] 454 pub codegen: CodegenOptions, 455 #[arg(skip)] 456 pub debug: DebugOptions, 457 #[arg(skip)] 458 pub wasm: WasmOptions, 459 #[arg(skip)] 460 pub wasi: WasiOptions, 461 } 462 463 macro_rules! match_feature { 464 ( 465 [$feat:tt : $config:expr] 466 $val:ident => $e:expr, 467 $p:pat => err, 468 ) => { 469 #[cfg(feature = $feat)] 470 { 471 if let Some($val) = $config { 472 $e; 473 } 474 } 475 #[cfg(not(feature = $feat))] 476 { 477 if let Some($p) = $config { 478 anyhow::bail!(concat!("support for ", $feat, " disabled at compile time")); 479 } 480 } 481 }; 482 } 483 484 impl CommonOptions { 485 fn configure(&mut self) { 486 if self.configured { 487 return; 488 } 489 self.configured = true; 490 self.opts.configure_with(&self.opts_raw); 491 self.codegen.configure_with(&self.codegen_raw); 492 self.debug.configure_with(&self.debug_raw); 493 self.wasm.configure_with(&self.wasm_raw); 494 self.wasi.configure_with(&self.wasi_raw); 495 } 496 497 pub fn init_logging(&mut self) -> Result<()> { 498 self.configure(); 499 if self.debug.logging == Some(false) { 500 return Ok(()); 501 } 502 #[cfg(feature = "logging")] 503 if self.debug.log_to_files == Some(true) { 504 let prefix = "wasmtime.dbg."; 505 init_file_per_thread_logger(prefix); 506 } else { 507 use std::io::IsTerminal; 508 use tracing_subscriber::{EnvFilter, FmtSubscriber}; 509 let b = FmtSubscriber::builder() 510 .with_writer(std::io::stderr) 511 .with_env_filter(EnvFilter::from_env("WASMTIME_LOG")) 512 .with_ansi(std::io::stderr().is_terminal()); 513 b.init(); 514 } 515 #[cfg(not(feature = "logging"))] 516 if self.debug.log_to_files == Some(true) || self.debug.logging == Some(true) { 517 anyhow::bail!("support for logging disabled at compile time"); 518 } 519 Ok(()) 520 } 521 522 pub fn config( 523 &mut self, 524 target: Option<&str>, 525 pooling_allocator_default: Option<bool>, 526 ) -> Result<Config> { 527 self.configure(); 528 let mut config = Config::new(); 529 530 match_feature! { 531 ["cranelift" : self.codegen.compiler] 532 strategy => config.strategy(strategy), 533 _ => err, 534 } 535 match_feature! { 536 ["cranelift" : target] 537 target => config.target(target)?, 538 _ => err, 539 } 540 match_feature! { 541 ["cranelift" : self.codegen.cranelift_debug_verifier] 542 enable => config.cranelift_debug_verifier(enable), 543 true => err, 544 } 545 if let Some(enable) = self.debug.debug_info { 546 config.debug_info(enable); 547 } 548 if self.debug.coredump.is_some() { 549 #[cfg(feature = "coredump")] 550 config.coredump_on_trap(true); 551 #[cfg(not(feature = "coredump"))] 552 anyhow::bail!("support for coredumps disabled at compile time"); 553 } 554 match_feature! { 555 ["cranelift" : self.opts.opt_level] 556 level => config.cranelift_opt_level(level), 557 _ => err, 558 } 559 match_feature! { 560 ["cranelift" : self.wasm.nan_canonicalization] 561 enable => config.cranelift_nan_canonicalization(enable), 562 true => err, 563 } 564 match_feature! { 565 ["cranelift" : self.codegen.pcc] 566 enable => config.cranelift_pcc(enable), 567 true => err, 568 } 569 570 self.enable_wasm_features(&mut config)?; 571 572 #[cfg(feature = "cranelift")] 573 for (name, value) in self.codegen.cranelift.iter() { 574 let name = name.replace('-', "_"); 575 unsafe { 576 match value { 577 Some(val) => { 578 config.cranelift_flag_set(&name, val); 579 } 580 None => { 581 config.cranelift_flag_enable(&name); 582 } 583 } 584 } 585 } 586 #[cfg(not(feature = "cranelift"))] 587 if !self.codegen.cranelift.is_empty() { 588 anyhow::bail!("support for cranelift disabled at compile time"); 589 } 590 591 #[cfg(feature = "cache")] 592 if self.codegen.cache != Some(false) { 593 match &self.codegen.cache_config { 594 Some(path) => { 595 config.cache_config_load(path)?; 596 } 597 None => { 598 config.cache_config_load_default()?; 599 } 600 } 601 } 602 #[cfg(not(feature = "cache"))] 603 if self.codegen.cache == Some(true) { 604 anyhow::bail!("support for caching disabled at compile time"); 605 } 606 607 match_feature! { 608 ["parallel-compilation" : self.codegen.parallel_compilation] 609 enable => config.parallel_compilation(enable), 610 true => err, 611 } 612 613 if let Some(max) = self.opts.static_memory_maximum_size { 614 config.static_memory_maximum_size(max); 615 } 616 617 if let Some(enable) = self.opts.static_memory_forced { 618 config.static_memory_forced(enable); 619 } 620 621 if let Some(size) = self.opts.static_memory_guard_size { 622 config.static_memory_guard_size(size); 623 } 624 625 if let Some(size) = self.opts.dynamic_memory_guard_size { 626 config.dynamic_memory_guard_size(size); 627 } 628 if let Some(size) = self.opts.dynamic_memory_reserved_for_growth { 629 config.dynamic_memory_reserved_for_growth(size); 630 } 631 if let Some(enable) = self.opts.guard_before_linear_memory { 632 config.guard_before_linear_memory(enable); 633 } 634 if let Some(enable) = self.opts.table_lazy_init { 635 config.table_lazy_init(enable); 636 } 637 638 // If fuel has been configured, set the `consume fuel` flag on the config. 639 if self.wasm.fuel.is_some() { 640 config.consume_fuel(true); 641 } 642 643 if let Some(enable) = self.wasm.epoch_interruption { 644 config.epoch_interruption(enable); 645 } 646 if let Some(enable) = self.debug.address_map { 647 config.generate_address_map(enable); 648 } 649 if let Some(enable) = self.opts.memory_init_cow { 650 config.memory_init_cow(enable); 651 } 652 if let Some(enable) = self.opts.signals_based_traps { 653 config.signals_based_traps(enable); 654 } 655 656 match_feature! { 657 ["pooling-allocator" : self.opts.pooling_allocator.or(pooling_allocator_default)] 658 enable => { 659 if enable { 660 let mut cfg = wasmtime::PoolingAllocationConfig::default(); 661 if let Some(size) = self.opts.pooling_memory_keep_resident { 662 cfg.linear_memory_keep_resident(size); 663 } 664 if let Some(size) = self.opts.pooling_table_keep_resident { 665 cfg.table_keep_resident(size); 666 } 667 if let Some(limit) = self.opts.pooling_total_core_instances { 668 cfg.total_core_instances(limit); 669 } 670 if let Some(limit) = self.opts.pooling_total_component_instances { 671 cfg.total_component_instances(limit); 672 } 673 if let Some(limit) = self.opts.pooling_total_memories { 674 cfg.total_memories(limit); 675 } 676 if let Some(limit) = self.opts.pooling_total_tables { 677 cfg.total_tables(limit); 678 } 679 if let Some(limit) = self.opts.pooling_table_elements { 680 cfg.table_elements(limit); 681 } 682 if let Some(limit) = self.opts.pooling_max_core_instance_size { 683 cfg.max_core_instance_size(limit); 684 } 685 match_feature! { 686 ["async" : self.opts.pooling_total_stacks] 687 limit => cfg.total_stacks(limit), 688 _ => err, 689 } 690 if let Some(max) = self.opts.pooling_max_memory_size { 691 cfg.max_memory_size(max); 692 } 693 if let Some(size) = self.opts.pooling_decommit_batch_size { 694 cfg.decommit_batch_size(size); 695 } 696 if let Some(max) = self.opts.pooling_max_unused_warm_slots { 697 cfg.max_unused_warm_slots(max); 698 } 699 match_feature! { 700 ["async" : self.opts.pooling_async_stack_zeroing] 701 enable => cfg.async_stack_zeroing(enable), 702 _ => err, 703 } 704 match_feature! { 705 ["async" : self.opts.pooling_async_stack_keep_resident] 706 size => cfg.async_stack_keep_resident(size), 707 _ => err, 708 } 709 if let Some(max) = self.opts.pooling_max_component_instance_size { 710 cfg.max_component_instance_size(max); 711 } 712 if let Some(max) = self.opts.pooling_max_core_instances_per_component { 713 cfg.max_core_instances_per_component(max); 714 } 715 if let Some(max) = self.opts.pooling_max_memories_per_component { 716 cfg.max_memories_per_component(max); 717 } 718 if let Some(max) = self.opts.pooling_max_tables_per_component { 719 cfg.max_tables_per_component(max); 720 } 721 if let Some(max) = self.opts.pooling_max_tables_per_module { 722 cfg.max_tables_per_module(max); 723 } 724 if let Some(max) = self.opts.pooling_max_memories_per_module { 725 cfg.max_memories_per_module(max); 726 } 727 match_feature! { 728 ["memory-protection-keys" : self.opts.pooling_memory_protection_keys] 729 enable => cfg.memory_protection_keys(if enable { 730 wasmtime::MpkEnabled::Enable 731 } else { 732 wasmtime::MpkEnabled::Disable 733 }), 734 _ => err, 735 } 736 match_feature! { 737 ["memory-protection-keys" : self.opts.pooling_max_memory_protection_keys] 738 max => cfg.max_memory_protection_keys(max), 739 _ => err, 740 } 741 match_feature! { 742 ["gc" : self.opts.pooling_total_gc_heaps] 743 max => cfg.total_gc_heaps(max), 744 _ => err, 745 } 746 config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling(cfg)); 747 } 748 }, 749 true => err, 750 } 751 752 if self.opts.pooling_memory_protection_keys.unwrap_or(false) 753 && !self.opts.pooling_allocator.unwrap_or(false) 754 { 755 anyhow::bail!("memory protection keys require the pooling allocator"); 756 } 757 758 if self.opts.pooling_max_memory_protection_keys.is_some() 759 && !self.opts.pooling_memory_protection_keys.unwrap_or(false) 760 { 761 anyhow::bail!( 762 "max memory protection keys requires memory protection keys to be enabled" 763 ); 764 } 765 766 match_feature! { 767 ["async" : self.wasm.async_stack_size] 768 size => config.async_stack_size(size), 769 _ => err, 770 } 771 772 if let Some(max) = self.wasm.max_wasm_stack { 773 config.max_wasm_stack(max); 774 775 // If `-Wasync-stack-size` isn't passed then automatically adjust it 776 // to the wasm stack size provided here too. That prevents the need 777 // to pass both when one can generally be inferred from the other. 778 #[cfg(feature = "async")] 779 if self.wasm.async_stack_size.is_none() { 780 const DEFAULT_HOST_STACK: usize = 512 << 10; 781 config.async_stack_size(max + DEFAULT_HOST_STACK); 782 } 783 } 784 785 if let Some(enable) = self.wasm.relaxed_simd_deterministic { 786 config.relaxed_simd_deterministic(enable); 787 } 788 match_feature! { 789 ["cranelift" : self.wasm.wmemcheck] 790 enable => config.wmemcheck(enable), 791 true => err, 792 } 793 794 Ok(config) 795 } 796 797 pub fn enable_wasm_features(&self, config: &mut Config) -> Result<()> { 798 let all = self.wasm.all_proposals; 799 800 if let Some(enable) = self.wasm.simd.or(all) { 801 config.wasm_simd(enable); 802 } 803 if let Some(enable) = self.wasm.relaxed_simd.or(all) { 804 config.wasm_relaxed_simd(enable); 805 } 806 if let Some(enable) = self.wasm.bulk_memory.or(all) { 807 config.wasm_bulk_memory(enable); 808 } 809 if let Some(enable) = self.wasm.multi_value.or(all) { 810 config.wasm_multi_value(enable); 811 } 812 if let Some(enable) = self.wasm.tail_call.or(all) { 813 config.wasm_tail_call(enable); 814 } 815 if let Some(enable) = self.wasm.multi_memory.or(all) { 816 config.wasm_multi_memory(enable); 817 } 818 if let Some(enable) = self.wasm.memory64.or(all) { 819 config.wasm_memory64(enable); 820 } 821 if let Some(enable) = self.wasm.custom_page_sizes.or(all) { 822 config.wasm_custom_page_sizes(enable); 823 } 824 if let Some(enable) = self.wasm.wide_arithmetic.or(all) { 825 config.wasm_wide_arithmetic(enable); 826 } 827 828 macro_rules! handle_conditionally_compiled { 829 ($(($feature:tt, $field:tt, $method:tt))*) => ($( 830 if let Some(enable) = self.wasm.$field.or(all) { 831 #[cfg(feature = $feature)] 832 config.$method(enable); 833 #[cfg(not(feature = $feature))] 834 if enable && all.is_none() { 835 anyhow::bail!("support for {} was disabled at compile-time", $feature); 836 } 837 } 838 )*) 839 } 840 841 handle_conditionally_compiled! { 842 ("component-model", component_model, wasm_component_model) 843 ("component-model", component_model_more_flags, wasm_component_model_more_flags) 844 ("component-model", component_model_multiple_returns, wasm_component_model_multiple_returns) 845 ("threads", threads, wasm_threads) 846 ("gc", gc, wasm_gc) 847 ("gc", reference_types, wasm_reference_types) 848 ("gc", function_references, wasm_function_references) 849 } 850 Ok(()) 851 } 852 } 853 854 impl PartialEq for CommonOptions { 855 fn eq(&self, other: &CommonOptions) -> bool { 856 let mut me = self.clone(); 857 me.configure(); 858 let mut other = other.clone(); 859 other.configure(); 860 let CommonOptions { 861 opts_raw: _, 862 codegen_raw: _, 863 debug_raw: _, 864 wasm_raw: _, 865 wasi_raw: _, 866 configured: _, 867 868 opts, 869 codegen, 870 debug, 871 wasm, 872 wasi, 873 } = me; 874 opts == other.opts 875 && codegen == other.codegen 876 && debug == other.debug 877 && wasm == other.wasm 878 && wasi == other.wasi 879 } 880 } 881