1use anyhow::Result;
4use clap::Parser;
5use std::time::Duration;
6use wasmtime::Config;
7
8pub mod opt;
9
10#[cfg(feature = "logging")]
11fn init_file_per_thread_logger(prefix: &'static str) {
12 file_per_thread_logger::initialize(prefix);
13 file_per_thread_logger::allow_uninitialized();
14
15 #[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
39wasmtime_option_group! {
40 #[derive(PartialEq, Clone)]
41 pub struct OptimizeOptions {
42 pub opt_level: Option<wasmtime::OptLevel>,
44
45 pub dynamic_memory_guard_size: Option<u64>,
47
48 pub static_memory_forced: Option<bool>,
50
51 pub static_memory_maximum_size: Option<u64>,
54
55 pub static_memory_guard_size: Option<u64>,
57
58 pub dynamic_memory_reserved_for_growth: Option<u64>,
61
62 pub guard_before_linear_memory: Option<bool>,
65
66 pub table_lazy_init: Option<bool>,
71
72 pub pooling_allocator: Option<bool>,
74
75 pub pooling_decommit_batch_size: Option<u32>,
78
79 pub pooling_memory_keep_resident: Option<usize>,
82
83 pub pooling_table_keep_resident: Option<usize>,
86
87 pub memory_protection_keys: Option<bool>,
90
91 pub memory_init_cow: Option<bool>,
94
95 pub pooling_total_core_instances: Option<u32>,
98
99 pub pooling_total_component_instances: Option<u32>,
102
103 pub pooling_total_memories: Option<u32>,
106
107 pub pooling_total_tables: Option<u32>,
110
111 pub pooling_total_stacks: Option<u32>,
114
115 pub pooling_max_memory_size: Option<usize>,
118 }
119
120 enum Optimize {
121 ...
122 }
123}
124
125wasmtime_option_group! {
126 #[derive(PartialEq, Clone)]
127 pub struct CodegenOptions {
128 pub compiler: Option<wasmtime::Strategy>,
133 pub cranelift_debug_verifier: Option<bool>,
135 pub cache: Option<bool>,
137 pub cache_config: Option<String>,
139 pub parallel_compilation: Option<bool>,
141 pub pcc: Option<bool>,
143
144 #[prefixed = "cranelift"]
145 pub cranelift: Vec<(String, Option<String>)>,
148 }
149
150 enum Codegen {
151 ...
152 }
153}
154
155wasmtime_option_group! {
156 #[derive(PartialEq, Clone)]
157 pub struct DebugOptions {
158 pub debug_info: Option<bool>,
160 pub address_map: Option<bool>,
162 pub logging: Option<bool>,
164 pub log_to_files: Option<bool>,
166 pub coredump: Option<String>,
168 }
169
170 enum Debug {
171 ...
172 }
173}
174
175wasmtime_option_group! {
176 #[derive(PartialEq, Clone)]
177 pub struct WasmOptions {
178 pub nan_canonicalization: Option<bool>,
180 pub fuel: Option<u64>,
188 pub epoch_interruption: Option<bool>,
191 pub max_wasm_stack: Option<usize>,
194 pub unknown_exports_allow: Option<bool>,
196 pub unknown_imports_trap: Option<bool>,
199 pub unknown_imports_default: Option<bool>,
202 pub wmemcheck: Option<bool>,
204 pub max_memory_size: Option<usize>,
209 pub max_table_elements: Option<u32>,
211 pub max_instances: Option<usize>,
213 pub max_tables: Option<usize>,
215 pub max_memories: Option<usize>,
217 pub trap_on_grow_failure: Option<bool>,
224 pub timeout: Option<Duration>,
226 pub all_proposals: Option<bool>,
228 pub bulk_memory: Option<bool>,
230 pub multi_memory: Option<bool>,
232 pub multi_value: Option<bool>,
234 pub reference_types: Option<bool>,
236 pub simd: Option<bool>,
238 pub relaxed_simd: Option<bool>,
240 pub relaxed_simd_deterministic: Option<bool>,
249 pub tail_call: Option<bool>,
251 pub threads: Option<bool>,
253 pub memory64: Option<bool>,
255 pub component_model: Option<bool>,
257 pub component_model_more_flags: Option<bool>,
259 pub component_model_multiple_returns: Option<bool>,
261 pub function_references: Option<bool>,
263 pub gc: Option<bool>,
265 pub custom_page_sizes: Option<bool>,
267 }
268
269 enum Wasm {
270 ...
271 }
272}
273
274wasmtime_option_group! {
275 #[derive(PartialEq, Clone)]
276 pub struct WasiOptions {
277 pub cli: Option<bool>,
279 pub common: Option<bool>,
281 pub nn: Option<bool>,
283 pub threads: Option<bool>,
285 pub http: Option<bool>,
287 pub runtime_config: Option<bool>,
289 pub keyvalue: Option<bool>,
291 pub listenfd: Option<bool>,
294 pub tcplisten: Vec<String>,
296 pub preview2: Option<bool>,
304 pub nn_graph: Vec<WasiNnGraph>,
313 pub inherit_network: Option<bool>,
316 pub allow_ip_name_lookup: Option<bool>,
318 pub tcp: Option<bool>,
320 pub udp: Option<bool>,
322 pub preview0: Option<bool>,
324 pub inherit_env: Option<bool>,
328 pub runtime_config_var: Vec<KeyValuePair>,
330 pub keyvalue_in_memory_data: Vec<KeyValuePair>,
332 }
333
334 enum Wasi {
335 ...
336 }
337}
338
339#[derive(Debug, Clone, PartialEq)]
340pub struct WasiNnGraph {
341 pub format: String,
342 pub dir: String,
343}
344
345#[derive(Debug, Clone, PartialEq)]
346pub struct KeyValuePair {
347 pub key: String,
348 pub value: String,
349}
350
351#[derive(Parser, Clone)]
353pub struct CommonOptions {
354 #[arg(short = 'O', long = "optimize", value_name = "KEY[=VAL[,..]]")]
364 opts_raw: Vec<opt::CommaSeparated<Optimize>>,
365
366 #[arg(short = 'C', long = "codegen", value_name = "KEY[=VAL[,..]]")]
368 codegen_raw: Vec<opt::CommaSeparated<Codegen>>,
369
370 #[arg(short = 'D', long = "debug", value_name = "KEY[=VAL[,..]]")]
372 debug_raw: Vec<opt::CommaSeparated<Debug>>,
373
374 #[arg(short = 'W', long = "wasm", value_name = "KEY[=VAL[,..]]")]
377 wasm_raw: Vec<opt::CommaSeparated<Wasm>>,
378
379 #[arg(short = 'S', long = "wasi", value_name = "KEY[=VAL[,..]]")]
381 wasi_raw: Vec<opt::CommaSeparated<Wasi>>,
382
383 #[arg(skip)]
386 configured: bool,
387 #[arg(skip)]
388 pub opts: OptimizeOptions,
389 #[arg(skip)]
390 pub codegen: CodegenOptions,
391 #[arg(skip)]
392 pub debug: DebugOptions,
393 #[arg(skip)]
394 pub wasm: WasmOptions,
395 #[arg(skip)]
396 pub wasi: WasiOptions,
397}
398
399macro_rules! match_feature {
400 (
401 [$feat:tt : $config:expr]
402 $val:ident => $e:expr,
403 $p:pat => err,
404 ) => {
405 #[cfg(feature = $feat)]
406 {
407 if let Some($val) = $config {
408 $e;
409 }
410 }
411 #[cfg(not(feature = $feat))]
412 {
413 if let Some($p) = $config {
414 anyhow::bail!(concat!("support for ", $feat, " disabled at compile time"));
415 }
416 }
417 };
418}
419
420impl CommonOptions {
421 fn configure(&mut self) {
422 if self.configured {
423 return;
424 }
425 self.configured = true;
426 self.opts.configure_with(&self.opts_raw);
427 self.codegen.configure_with(&self.codegen_raw);
428 self.debug.configure_with(&self.debug_raw);
429 self.wasm.configure_with(&self.wasm_raw);
430 self.wasi.configure_with(&self.wasi_raw);
431 }
432
433 pub fn init_logging(&mut self) -> Result<()> {
434 self.configure();
435 if self.debug.logging == Some(false) {
436 return Ok(());
437 }
438 #[cfg(feature = "logging")]
439 if self.debug.log_to_files == Some(true) {
440 let prefix = "wasmtime.dbg.";
441 init_file_per_thread_logger(prefix);
442 } else {
443 use std::io::IsTerminal;
444 use tracing_subscriber::{EnvFilter, FmtSubscriber};
445 let b = FmtSubscriber::builder()
446 .with_writer(std::io::stderr)
447 .with_env_filter(EnvFilter::from_env("WASMTIME_LOG"))
448 .with_ansi(std::io::stderr().is_terminal());
449 b.init();
450 }
451 #[cfg(not(feature = "logging"))]
452 if self.debug.log_to_files == Some(true) || self.debug.logging == Some(true) {
453 anyhow::bail!("support for logging disabled at compile time");
454 }
455 Ok(())
456 }
457
458 pub fn config(
459 &mut self,
460 target: Option<&str>,
461 pooling_allocator_default: Option<bool>,
462 ) -> Result<Config> {
463 self.configure();
464 let mut config = Config::new();
465
466 match_feature! {
467 ["cranelift" : self.codegen.compiler]
468 strategy => config.strategy(strategy),
469 _ => err,
470 }
471 match_feature! {
472 ["cranelift" : target]
473 target => config.target(target)?,
474 _ => err,
475 }
476 match_feature! {
477 ["cranelift" : self.codegen.cranelift_debug_verifier]
478 enable => config.cranelift_debug_verifier(enable),
479 true => err,
480 }
481 if let Some(enable) = self.debug.debug_info {
482 config.debug_info(enable);
483 }
484 if self.debug.coredump.is_some() {
485 #[cfg(feature = "coredump")]
486 config.coredump_on_trap(true);
487 #[cfg(not(feature = "coredump"))]
488 anyhow::bail!("support for coredumps disabled at compile time");
489 }
490 match_feature! {
491 ["cranelift" : self.opts.opt_level]
492 level => config.cranelift_opt_level(level),
493 _ => err,
494 }
495 match_feature! {
496 ["cranelift" : self.wasm.nan_canonicalization]
497 enable => config.cranelift_nan_canonicalization(enable),
498 true => err,
499 }
500 match_feature! {
501 ["cranelift" : self.codegen.pcc]
502 enable => config.cranelift_pcc(enable),
503 true => err,
504 }
505
506 self.enable_wasm_features(&mut config)?;
507
508 #[cfg(feature = "cranelift")]
509 for (name, value) in self.codegen.cranelift.iter() {
510 let name = name.replace('-', "_");
511 unsafe {
512 match value {
513 Some(val) => {
514 config.cranelift_flag_set(&name, val);
515 }
516 None => {
517 config.cranelift_flag_enable(&name);
518 }
519 }
520 }
521 }
522 #[cfg(not(feature = "cranelift"))]
523 if !self.codegen.cranelift.is_empty() {
524 anyhow::bail!("support for cranelift disabled at compile time");
525 }
526
527 #[cfg(feature = "cache")]
528 if self.codegen.cache != Some(false) {
529 match &self.codegen.cache_config {
530 Some(path) => {
531 config.cache_config_load(path)?;
532 }
533 None => {
534 config.cache_config_load_default()?;
535 }
536 }
537 }
538 #[cfg(not(feature = "cache"))]
539 if self.codegen.cache == Some(true) {
540 anyhow::bail!("support for caching disabled at compile time");
541 }
542
543 match_feature! {
544 ["parallel-compilation" : self.codegen.parallel_compilation]
545 enable => config.parallel_compilation(enable),
546 true => err,
547 }
548
549 if let Some(max) = self.opts.static_memory_maximum_size {
550 config.static_memory_maximum_size(max);
551 }
552
553 if let Some(enable) = self.opts.static_memory_forced {
554 config.static_memory_forced(enable);
555 }
556
557 if let Some(size) = self.opts.static_memory_guard_size {
558 config.static_memory_guard_size(size);
559 }
560
561 if let Some(size) = self.opts.dynamic_memory_guard_size {
562 config.dynamic_memory_guard_size(size);
563 }
564 if let Some(size) = self.opts.dynamic_memory_reserved_for_growth {
565 config.dynamic_memory_reserved_for_growth(size);
566 }
567 if let Some(enable) = self.opts.guard_before_linear_memory {
568 config.guard_before_linear_memory(enable);
569 }
570 if let Some(enable) = self.opts.table_lazy_init {
571 config.table_lazy_init(enable);
572 }
573
574 if self.wasm.fuel.is_some() {
576 config.consume_fuel(true);
577 }
578
579 if let Some(enable) = self.wasm.epoch_interruption {
580 config.epoch_interruption(enable);
581 }
582 if let Some(enable) = self.debug.address_map {
583 config.generate_address_map(enable);
584 }
585 if let Some(enable) = self.opts.memory_init_cow {
586 config.memory_init_cow(enable);
587 }
588
589 match_feature! {
590 ["pooling-allocator" : self.opts.pooling_allocator.or(pooling_allocator_default)]
591 enable => {
592 if enable {
593 let mut cfg = wasmtime::PoolingAllocationConfig::default();
594 if let Some(size) = self.opts.pooling_memory_keep_resident {
595 cfg.linear_memory_keep_resident(size);
596 }
597 if let Some(size) = self.opts.pooling_table_keep_resident {
598 cfg.table_keep_resident(size);
599 }
600 if let Some(limit) = self.opts.pooling_total_core_instances {
601 cfg.total_core_instances(limit);
602 }
603 if let Some(limit) = self.opts.pooling_total_component_instances {
604 cfg.total_component_instances(limit);
605 }
606 if let Some(limit) = self.opts.pooling_total_memories {
607 cfg.total_memories(limit);
608 }
609 if let Some(limit) = self.opts.pooling_total_tables {
610 cfg.total_tables(limit);
611 }
612 match_feature! {
613 ["async" : self.opts.pooling_total_stacks]
614 limit => cfg.total_stacks(limit),
615 _ => err,
616 }
617 if let Some(limit) = self.opts.pooling_max_memory_size {
618 cfg.max_memory_size(limit);
619 }
620 match_feature! {
621 ["memory-protection-keys" : self.opts.memory_protection_keys]
622 enable => cfg.memory_protection_keys(if enable {
623 wasmtime::MpkEnabled::Enable
624 } else {
625 wasmtime::MpkEnabled::Disable
626 }),
627 _ => err,
628 }
629 config.allocation_strategy(wasmtime::InstanceAllocationStrategy::Pooling(cfg));
630 }
631 },
632 true => err,
633 }
634
635 if self.opts.memory_protection_keys.unwrap_or(false)
636 && !self.opts.pooling_allocator.unwrap_or(false)
637 {
638 anyhow::bail!("memory protection keys require the pooling allocator");
639 }
640
641 if let Some(max) = self.wasm.max_wasm_stack {
642 config.max_wasm_stack(max);
643 }
644
645 if let Some(enable) = self.wasm.relaxed_simd_deterministic {
646 config.relaxed_simd_deterministic(enable);
647 }
648 match_feature! {
649 ["cranelift" : self.wasm.wmemcheck]
650 enable => config.wmemcheck(enable),
651 true => err,
652 }
653
654 Ok(config)
655 }
656
657 pub fn enable_wasm_features(&self, config: &mut Config) -> Result<()> {
658 let all = self.wasm.all_proposals;
659
660 if let Some(enable) = self.wasm.simd.or(all) {
661 config.wasm_simd(enable);
662 }
663 if let Some(enable) = self.wasm.relaxed_simd.or(all) {
664 config.wasm_relaxed_simd(enable);
665 }
666 if let Some(enable) = self.wasm.bulk_memory.or(all) {
667 config.wasm_bulk_memory(enable);
668 }
669 if let Some(enable) = self.wasm.multi_value.or(all) {
670 config.wasm_multi_value(enable);
671 }
672 if let Some(enable) = self.wasm.tail_call.or(all) {
673 config.wasm_tail_call(enable);
674 }
675 if let Some(enable) = self.wasm.multi_memory.or(all) {
676 config.wasm_multi_memory(enable);
677 }
678 if let Some(enable) = self.wasm.memory64.or(all) {
679 config.wasm_memory64(enable);
680 }
681 if let Some(enable) = self.wasm.custom_page_sizes.or(all) {
682 config.wasm_custom_page_sizes(enable);
683 }
684
685 macro_rules! handle_conditionally_compiled {
686 ($(($feature:tt, $field:tt, $method:tt))*) => ($(
687 if let Some(enable) = self.wasm.$field.or(all) {
688 #[cfg(feature = $feature)]
689 config.$method(enable);
690 #[cfg(not(feature = $feature))]
691 if enable && all.is_none() {
692 anyhow::bail!("support for {} was disabled at compile-time", $feature);
693 }
694 }
695 )*)
696 }
697
698 handle_conditionally_compiled! {
699 ("component-model", component_model, wasm_component_model)
700 ("component-model", component_model_more_flags, wasm_component_model_more_flags)
701 ("component-model", component_model_multiple_returns, wasm_component_model_multiple_returns)
702 ("threads", threads, wasm_threads)
703 ("gc", gc, wasm_gc)
704 ("gc", reference_types, wasm_reference_types)
705 ("gc", function_references, wasm_function_references)
706 }
707 Ok(())
708 }
709}
710
711impl PartialEq for CommonOptions {
712 fn eq(&self, other: &CommonOptions) -> bool {
713 let mut me = self.clone();
714 me.configure();
715 let mut other = other.clone();
716 other.configure();
717 let CommonOptions {
718 opts_raw: _,
719 codegen_raw: _,
720 debug_raw: _,
721 wasm_raw: _,
722 wasi_raw: _,
723 configured: _,
724
725 opts,
726 codegen,
727 debug,
728 wasm,
729 wasi,
730 } = me;
731 opts == other.opts
732 && codegen == other.codegen
733 && debug == other.debug
734 && wasm == other.wasm
735 && wasi == other.wasi
736 }
737}