Skip to main content

wasmtime/
config.rs

1use crate::Engine;
2use crate::prelude::*;
3use alloc::sync::Arc;
4use bitflags::Flags;
5use core::fmt;
6use core::num::{NonZeroU32, NonZeroUsize};
7use core::str::FromStr;
8#[cfg(any(feature = "cranelift", feature = "winch"))]
9use std::path::Path;
10pub use wasmparser::WasmFeatures;
11#[cfg(any(feature = "cranelift", feature = "winch"))]
12use wasmtime_environ::FlagValue;
13use wasmtime_environ::{ConfigTunables, OperatorCost, OperatorCostStrategy, TripleExt, Tunables};
14
15#[cfg(feature = "runtime")]
16use crate::memory::MemoryCreator;
17#[cfg(feature = "runtime")]
18use crate::profiling_agent::{self, ProfilingAgent};
19#[cfg(feature = "runtime")]
20use crate::runtime::vm::{
21    GcRuntime, InstanceAllocator, OnDemandInstanceAllocator, RuntimeMemoryCreator,
22};
23#[cfg(feature = "runtime")]
24use crate::trampoline::MemoryCreatorProxy;
25
26#[cfg(feature = "async")]
27use crate::stack::{StackCreator, StackCreatorProxy};
28#[cfg(feature = "async")]
29use wasmtime_fiber::RuntimeFiberStackCreator;
30
31#[cfg(feature = "runtime")]
32pub use crate::runtime::code_memory::CustomCodeMemory;
33#[cfg(feature = "cache")]
34pub use wasmtime_cache::{Cache, CacheConfig};
35#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
36pub use wasmtime_environ::CacheStore;
37pub use wasmtime_environ::Inlining;
38
39pub(crate) const DEFAULT_WASM_BACKTRACE_MAX_FRAMES: NonZeroUsize = NonZeroUsize::new(20).unwrap();
40
41/// Represents the module instance allocation strategy to use.
42#[derive(Clone)]
43#[non_exhaustive]
44pub enum InstanceAllocationStrategy {
45    /// The on-demand instance allocation strategy.
46    ///
47    /// Resources related to a module instance are allocated at instantiation time and
48    /// immediately deallocated when the `Store` referencing the instance is dropped.
49    ///
50    /// This is the default allocation strategy for Wasmtime.
51    OnDemand,
52    /// The pooling instance allocation strategy.
53    ///
54    /// A pool of resources is created in advance and module instantiation reuses resources
55    /// from the pool. Resources are returned to the pool when the `Store` referencing the instance
56    /// is dropped.
57    ///
58    /// When GC is enabled, the pooling allocator requires that the GC heap
59    /// configuration matches the linear memory configuration (i.e.,
60    /// `gc_heap_reservation` must equal `memory_reservation`, etc.). By
61    /// default, if no `gc_heap_*` tunables are explicitly configured, they
62    /// automatically inherit the `memory_*` values.
63    #[cfg(feature = "pooling-allocator")]
64    Pooling(PoolingAllocationConfig),
65}
66
67impl InstanceAllocationStrategy {
68    /// The default pooling instance allocation strategy.
69    #[cfg(feature = "pooling-allocator")]
70    pub fn pooling() -> Self {
71        Self::Pooling(Default::default())
72    }
73}
74
75impl Default for InstanceAllocationStrategy {
76    fn default() -> Self {
77        Self::OnDemand
78    }
79}
80
81#[cfg(feature = "pooling-allocator")]
82impl From<PoolingAllocationConfig> for InstanceAllocationStrategy {
83    fn from(cfg: PoolingAllocationConfig) -> InstanceAllocationStrategy {
84        InstanceAllocationStrategy::Pooling(cfg)
85    }
86}
87
88#[derive(Clone)]
89/// Configure the strategy used for versioning in serializing and deserializing [`crate::Module`].
90pub enum ModuleVersionStrategy {
91    /// Use the wasmtime crate's Cargo package version.
92    WasmtimeVersion,
93    /// Use a custom version string. Must be at most 255 bytes.
94    Custom(String),
95    /// Emit no version string in serialization, and accept all version strings in deserialization.
96    None,
97}
98
99impl Default for ModuleVersionStrategy {
100    fn default() -> Self {
101        ModuleVersionStrategy::WasmtimeVersion
102    }
103}
104
105impl core::hash::Hash for ModuleVersionStrategy {
106    fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
107        match self {
108            Self::WasmtimeVersion => env!("CARGO_PKG_VERSION").hash(hasher),
109            Self::Custom(s) => s.hash(hasher),
110            Self::None => {}
111        };
112    }
113}
114
115impl ModuleVersionStrategy {
116    /// Get the string-encoding version of the module.
117    pub fn as_str(&self) -> &str {
118        match &self {
119            Self::WasmtimeVersion => env!("CARGO_PKG_VERSION_MAJOR"),
120            Self::Custom(c) => c,
121            Self::None => "",
122        }
123    }
124}
125
126/// Configuration for record/replay
127#[derive(Clone)]
128#[non_exhaustive]
129pub enum RRConfig {
130    #[cfg(feature = "rr")]
131    /// Recording on store is enabled
132    Recording,
133    #[cfg(feature = "rr")]
134    /// Replaying on store is enabled
135    Replaying,
136    /// No record/replay is enabled
137    None,
138}
139
140/// Global configuration options used to create an [`Engine`]
141/// and customize its behavior.
142///
143/// This structure exposed a builder-like interface and is primarily consumed by
144/// [`Engine::new()`].
145///
146/// The validation of `Config` is deferred until the engine is being built, thus
147/// a problematic config may cause [`Engine::new`] to fail.
148///
149/// # Defaults
150///
151/// The `Default` trait implementation and the return value from
152/// [`Config::new()`] are the same and represent the default set of
153/// configuration for an engine. The exact set of defaults will differ based on
154/// properties such as enabled Cargo features at compile time and the configured
155/// target (see [`Config::target`]). Configuration options document their
156/// default values and what the conditional value of the default is where
157/// applicable.
158#[derive(Clone)]
159pub struct Config {
160    #[cfg(any(feature = "cranelift", feature = "winch"))]
161    compiler_config: Option<CompilerConfig>,
162    target: Option<target_lexicon::Triple>,
163    #[cfg(feature = "gc")]
164    collector: Collector,
165    profiling_strategy: ProfilingStrategy,
166    tunables: ConfigTunables,
167
168    #[cfg(feature = "cache")]
169    pub(crate) cache: Option<Cache>,
170    #[cfg(feature = "runtime")]
171    pub(crate) mem_creator: Option<Arc<dyn RuntimeMemoryCreator>>,
172    #[cfg(feature = "runtime")]
173    pub(crate) custom_code_memory: Option<Arc<dyn CustomCodeMemory>>,
174    pub(crate) allocation_strategy: InstanceAllocationStrategy,
175    pub(crate) max_wasm_stack: usize,
176    /// Explicitly enabled features via `Config::wasm_*` methods. This is a
177    /// signal that the embedder specifically wants something turned on
178    /// regardless of the defaults that Wasmtime might otherwise have enabled.
179    ///
180    /// Note that this, and `disabled_features` below, start as the empty set of
181    /// features to only track explicit user requests.
182    pub(crate) enabled_features: WasmFeatures,
183    /// Same as `enabled_features`, but for those that are explicitly disabled.
184    pub(crate) disabled_features: WasmFeatures,
185    pub(crate) wasm_backtrace_details_env_used: bool,
186    pub(crate) wasm_backtrace_max_frames: Option<NonZeroUsize>,
187    pub(crate) native_unwind_info: Option<bool>,
188    pub(crate) async_stack_size: usize,
189    pub(crate) async_stack_zeroing: bool,
190    #[cfg(feature = "async")]
191    pub(crate) stack_creator: Option<Arc<dyn RuntimeFiberStackCreator>>,
192    pub(crate) module_version: ModuleVersionStrategy,
193    pub(crate) parallel_compilation: bool,
194    pub(crate) memory_guaranteed_dense_image_size: u64,
195    pub(crate) force_memory_init_memfd: bool,
196    pub(crate) wmemcheck: bool,
197    #[cfg(feature = "coredump")]
198    pub(crate) coredump_on_trap: bool,
199    pub(crate) macos_use_mach_ports: bool,
200    pub(crate) detect_host_feature: Option<fn(&str) -> Option<bool>>,
201    pub(crate) x86_float_abi_ok: Option<bool>,
202    pub(crate) shared_memory: bool,
203    pub(crate) rr_config: RRConfig,
204}
205
206/// User-provided configuration for the compiler.
207#[cfg(any(feature = "cranelift", feature = "winch"))]
208#[derive(Debug, Clone)]
209struct CompilerConfig {
210    strategy: Option<Strategy>,
211    settings: crate::hash_map::HashMap<String, (String, UserSpecified)>,
212    flags: crate::hash_map::HashMap<String, UserSpecified>,
213    #[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
214    cache_store: Option<Arc<dyn CacheStore>>,
215    clif_dir: Option<std::path::PathBuf>,
216    wmemcheck: bool,
217}
218
219#[cfg(any(feature = "cranelift", feature = "winch"))]
220#[derive(Debug, Clone)]
221enum UserSpecified {
222    Yes,
223    No,
224}
225
226#[cfg(any(feature = "cranelift", feature = "winch"))]
227impl CompilerConfig {
228    fn new() -> Self {
229        Self {
230            strategy: Strategy::Auto.not_auto(),
231            settings: Default::default(),
232            flags: Default::default(),
233            #[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
234            cache_store: None,
235            clif_dir: None,
236            wmemcheck: false,
237        }
238    }
239
240    /// Ensures that the key is not set or equals to the given value.
241    /// If the key is not set, it will be set to the given value.
242    ///
243    /// # Returns
244    ///
245    /// Returns true if successfully set or already had the given setting
246    /// value, or false if the setting was explicitly set to something
247    /// else previously.
248    fn ensure_setting_unset_or_given(&mut self, k: &str, v: &str) -> bool {
249        if let Some((value, _)) = self.settings.get(k) {
250            if value != v {
251                return false;
252            }
253        } else {
254            self.settings
255                .insert(k.to_string(), (v.to_string(), UserSpecified::No));
256        }
257        true
258    }
259}
260
261#[cfg(any(feature = "cranelift", feature = "winch"))]
262impl Default for CompilerConfig {
263    fn default() -> Self {
264        Self::new()
265    }
266}
267
268impl Config {
269    /// Creates a new configuration object with the default configuration
270    /// specified.
271    pub fn new() -> Self {
272        let mut ret = Self {
273            tunables: ConfigTunables::default(),
274            #[cfg(any(feature = "cranelift", feature = "winch"))]
275            compiler_config: Some(CompilerConfig::default()),
276            target: None,
277            #[cfg(feature = "gc")]
278            collector: Collector::default(),
279            #[cfg(feature = "cache")]
280            cache: None,
281            profiling_strategy: ProfilingStrategy::None,
282            #[cfg(feature = "runtime")]
283            mem_creator: None,
284            #[cfg(feature = "runtime")]
285            custom_code_memory: None,
286            allocation_strategy: InstanceAllocationStrategy::OnDemand,
287            // 512k of stack -- note that this is chosen currently to not be too
288            // big, not be too small, and be a good default for most platforms.
289            // One platform of particular note is Windows where the stack size
290            // of the main thread seems to, by default, be smaller than that of
291            // Linux and macOS. This 512k value at least lets our current test
292            // suite pass on the main thread of Windows (using `--test-threads
293            // 1` forces this), or at least it passed when this change was
294            // committed.
295            max_wasm_stack: 512 * 1024,
296            wasm_backtrace_details_env_used: false,
297            wasm_backtrace_max_frames: Some(DEFAULT_WASM_BACKTRACE_MAX_FRAMES),
298            native_unwind_info: None,
299            enabled_features: WasmFeatures::empty(),
300            disabled_features: WasmFeatures::empty(),
301            async_stack_size: 2 << 20,
302            async_stack_zeroing: false,
303            #[cfg(feature = "async")]
304            stack_creator: None,
305            module_version: ModuleVersionStrategy::default(),
306            parallel_compilation: !cfg!(miri),
307            memory_guaranteed_dense_image_size: 16 << 20,
308            force_memory_init_memfd: false,
309            wmemcheck: false,
310            #[cfg(feature = "coredump")]
311            coredump_on_trap: false,
312            macos_use_mach_ports: !cfg!(miri),
313            #[cfg(feature = "std")]
314            detect_host_feature: Some(detect_host_feature),
315            #[cfg(not(feature = "std"))]
316            detect_host_feature: None,
317            x86_float_abi_ok: None,
318            shared_memory: false,
319            rr_config: RRConfig::None,
320        };
321        ret.wasm_backtrace_details(WasmBacktraceDetails::Environment);
322        ret
323    }
324
325    #[cfg(any(feature = "cranelift", feature = "winch"))]
326    pub(crate) fn has_compiler(&self) -> bool {
327        self.compiler_config.is_some()
328    }
329
330    #[track_caller]
331    #[cfg(any(feature = "cranelift", feature = "winch"))]
332    fn compiler_config_mut(&mut self) -> &mut CompilerConfig {
333        self.compiler_config.as_mut().expect(
334            "cannot configure compiler settings for `Config`s \
335             created by `Config::without_compiler`",
336        )
337    }
338
339    /// Configure whether Wasm compilation is enabled.
340    ///
341    /// Disabling Wasm compilation will allow you to load and run
342    /// [pre-compiled][Engine::precompile_module] Wasm programs, but not
343    /// to compile and run new Wasm programs that have not already been
344    /// pre-compiled.
345    ///
346    /// Many compilation-related configuration methods will panic if compilation
347    /// has been disabled.
348    ///
349    /// Note that there are two ways to disable Wasm compilation:
350    ///
351    /// 1. Statically, by disabling the `"cranelift"` and `"winch"` cargo
352    ///    features when building Wasmtime. These builds of Wasmtime will have
353    ///    smaller code size, since they do not include any of the code to
354    ///    compile Wasm.
355    ///
356    /// 2. Dynamically, by passing `false` to this method at run-time when
357    ///    configuring Wasmtime. The Wasmtime binary will still include the code
358    ///    for compiling Wasm, it just won't be executed, so code size is larger
359    ///    than with the first approach.
360    ///
361    /// The static approach is better in most cases, however dynamically calling
362    /// `enable_compiler(false)` is useful whenever you create multiple
363    /// [`Engine`]s in the same process, some of which must be able to compile
364    /// Wasm and some of which should never do so. Tests are a common example of
365    /// such a situation, especially when there are multiple Rust binaries in
366    /// the same cargo workspace, and cargo's feature resolution enables the
367    /// `"cranelift"` or `"winch"` features across the whole workspace.
368    #[cfg(any(feature = "cranelift", feature = "winch"))]
369    pub fn enable_compiler(&mut self, enable: bool) -> &mut Self {
370        match (enable, &self.compiler_config) {
371            (true, Some(_)) | (false, None) => {}
372            (true, None) => {
373                self.compiler_config = Some(CompilerConfig::default());
374            }
375            (false, Some(_)) => {
376                self.compiler_config = None;
377            }
378        }
379        self
380    }
381
382    /// Configures the target platform of this [`Config`].
383    ///
384    /// This method is used to configure the output of compilation in an
385    /// [`Engine`]. This can be used, for example, to
386    /// cross-compile from one platform to another. By default, the host target
387    /// triple is used meaning compiled code is suitable to run on the host.
388    ///
389    /// Note that the [`Module`](crate::Module) type can only be created if the
390    /// target configured here matches the host. Otherwise if a cross-compile is
391    /// being performed where the host doesn't match the target then
392    /// [`Engine::precompile_module`] must be used instead.
393    ///
394    /// Target-specific flags (such as CPU features) will not be inferred by
395    /// default for the target when one is provided here. This means that this
396    /// can also be used, for example, with the host architecture to disable all
397    /// host-inferred feature flags. Configuring target-specific flags can be
398    /// done with [`Config::cranelift_flag_set`] and
399    /// [`Config::cranelift_flag_enable`].
400    ///
401    /// # Errors
402    ///
403    /// This method will error if the given target triple is not supported.
404    pub fn target(&mut self, target: &str) -> Result<&mut Self> {
405        self.target =
406            Some(target_lexicon::Triple::from_str(target).map_err(|e| crate::format_err!(e))?);
407
408        Ok(self)
409    }
410
411    /// Enables the incremental compilation cache in Cranelift, using the provided `CacheStore`
412    /// backend for storage.
413    ///
414    /// # Panics
415    ///
416    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
417    #[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
418    pub fn enable_incremental_compilation(
419        &mut self,
420        cache_store: Arc<dyn CacheStore>,
421    ) -> Result<&mut Self> {
422        self.compiler_config_mut().cache_store = Some(cache_store);
423        Ok(self)
424    }
425
426    #[doc(hidden)]
427    #[deprecated(note = "no longer has any effect")]
428    #[cfg(feature = "async")]
429    pub fn async_support(&mut self, _enable: bool) -> &mut Self {
430        self
431    }
432
433    /// Configures whether DWARF debug information will be emitted
434    /// during compilation for a native debugger on the Wasmtime
435    /// process to consume.
436    ///
437    /// Note that the `debug-builtins` compile-time Cargo feature must also be
438    /// enabled for native debuggers such as GDB or LLDB to be able to debug
439    /// guest WebAssembly programs.
440    ///
441    /// By default this option is `false`.
442    /// **Note** Enabling this option is not compatible with the Winch compiler.
443    pub fn debug_info(&mut self, enable: bool) -> &mut Self {
444        self.tunables.debug_native = Some(enable);
445        self
446    }
447
448    /// Whether or not symbols are located in generated compiled module
449    /// artifacts.
450    ///
451    /// Wasmtime's currently representation of compiled artifacts is an ELF
452    /// file. ELF files have symbol tables and such and this option enables
453    /// whether symbols are emitted for wasm functions. This utility can be
454    /// useful when profiling wasm modules (many profilers work with
455    /// ELF-in-memory by default without futher configuration), introspection of
456    /// a `*.cwasm` (e.g. the symbol table is what `wasmtime objdump` reads), or
457    /// just general binary analysis of the result ELF file. Large wasm modules
458    /// can have large symbol tables, however, and the symbols serve no purpose
459    /// at runtime meaning that they are pure overhead for minimal module as
460    /// well. This option can be used to disable these symbols which will reduce
461    /// the debuggability of modules but will also reduce their size.
462    ///
463    /// Note that the ELF file representation is considered an implementation
464    /// detail of Wasmtime and embedders should not rely on this format.
465    /// Wasmtime may change the format of artifacts in the future.
466    ///
467    /// This option is `true` by default.
468    ///
469    /// This option is required if [`Config::debug_info`] is enabled.
470    pub fn debug_symbols(&mut self, enable: bool) -> &mut Self {
471        self.tunables.debug_symbols = Some(enable);
472        self
473    }
474
475    /// Configures whether compiled guest code will be instrumented to
476    /// provide debugging at the Wasm VM level.
477    ///
478    /// This is required in order to enable a guest-level debugging
479    /// API that can precisely examine Wasm VM state and (eventually,
480    /// once it is complete) set breakpoints and watchpoints and step
481    /// through code.
482    ///
483    /// Without this enabled, debugging can only be done via a native
484    /// debugger operating on the compiled guest code (see
485    /// [`Config::debug_info`] and is "best-effort": we may be able to
486    /// recover some Wasm locals or operand stack values, but it is
487    /// not guaranteed, even when optimizations are disabled.
488    ///
489    /// When this is enabled, additional instrumentation is inserted
490    /// that directly tracks the Wasm VM state at every step. This has
491    /// some performance impact, but allows perfect debugging
492    /// fidelity.
493    ///
494    /// Breakpoints, watchpoints, and stepping are not yet supported,
495    /// but will be added in a future version of Wasmtime.
496    ///
497    /// This enables use of the [`crate::FrameHandle`] API which is
498    /// provided by [`crate::Caller::debug_exit_frames`] or
499    /// [`crate::Store::debug_exit_frames`].
500    ///
501    /// ***Note*** Enabling this option is not compatible with the
502    /// Winch compiler.
503    #[cfg(feature = "debug")]
504    pub fn guest_debug(&mut self, enable: bool) -> &mut Self {
505        self.tunables.debug_guest = Some(enable);
506        self
507    }
508
509    /// Configures whether [`WasmBacktrace`] will be present in the context of
510    /// errors returned from Wasmtime.
511    ///
512    /// This method is deprecated in favor of
513    /// [`Config::wasm_backtrace_max_frames`]. Calling `wasm_backtrace(false)`
514    /// is equivalent to `wasm_backtrace_max_frames(None)`, and
515    /// `wasm_backtrace(true)` will leave `wasm_backtrace_max_frames` unchanged
516    /// if the value is `Some` and will otherwise restore the default `Some`
517    /// value.
518    ///
519    /// [`WasmBacktrace`]: crate::WasmBacktrace
520    #[deprecated = "use `wasm_backtrace_max_frames` instead"]
521    pub fn wasm_backtrace(&mut self, enable: bool) -> &mut Self {
522        match (enable, self.wasm_backtrace_max_frames) {
523            (false, _) => self.wasm_backtrace_max_frames = None,
524            // Wasm backtraces were disabled; enable them with the
525            // default maximum number of frames to capture.
526            (true, None) => {
527                self.wasm_backtrace_max_frames = Some(DEFAULT_WASM_BACKTRACE_MAX_FRAMES)
528            }
529            // Wasm backtraces are already enabled; keep the existing
530            // max-frames configuration.
531            (true, Some(_)) => {}
532        }
533        self
534    }
535
536    /// Configures whether backtraces in `Trap` will parse debug info in the wasm file to
537    /// have filename/line number information.
538    ///
539    /// When enabled this will causes modules to retain debugging information
540    /// found in wasm binaries. This debug information will be used when a trap
541    /// happens to symbolicate each stack frame and attempt to print a
542    /// filename/line number for each wasm frame in the stack trace.
543    ///
544    /// By default this option is `WasmBacktraceDetails::Environment`, meaning
545    /// that wasm will read `WASMTIME_BACKTRACE_DETAILS` to indicate whether
546    /// details should be parsed. Note that the `std` feature of this crate must
547    /// be active to read environment variables, otherwise this is disabled by
548    /// default.
549    pub fn wasm_backtrace_details(&mut self, enable: WasmBacktraceDetails) -> &mut Self {
550        self.wasm_backtrace_details_env_used = false;
551        self.tunables.parse_wasm_debuginfo = match enable {
552            WasmBacktraceDetails::Enable => Some(true),
553            WasmBacktraceDetails::Disable => Some(false),
554            WasmBacktraceDetails::Environment => {
555                #[cfg(feature = "std")]
556                {
557                    self.wasm_backtrace_details_env_used = true;
558                    std::env::var("WASMTIME_BACKTRACE_DETAILS")
559                        .map(|s| Some(s == "1"))
560                        .unwrap_or(Some(false))
561                }
562                #[cfg(not(feature = "std"))]
563                {
564                    Some(false)
565                }
566            }
567        };
568        self
569    }
570
571    /// Configures the maximum number of WebAssembly frames to collect in
572    /// backtraces.
573    ///
574    /// A backtrace may be collected whenever an error is returned from a host
575    /// function call through to WebAssembly or when WebAssembly itself hits a
576    /// trap condition, such as an out-of-bounds memory access. This flag
577    /// indicates, in these conditions, whether the backtrace is collected or
578    /// not and how many frames should be collected.
579    ///
580    /// Currently wasm backtraces are implemented through frame pointer walking.
581    /// This means that collecting a backtrace is expected to be a fast and
582    /// relatively cheap operation. Additionally backtrace collection is
583    /// suitable in concurrent environments since one thread capturing a
584    /// backtrace won't block other threads.
585    ///
586    /// Collected backtraces are attached via
587    /// [`Error::context`](crate::Error::context) to errors returned from host
588    /// functions. The [`WasmBacktrace`] type can be acquired via
589    /// [`Error::downcast_ref`](crate::Error::downcast_ref) to inspect the
590    /// backtrace. When this option is set to `None` then this context is never
591    /// applied to errors coming out of wasm.
592    ///
593    /// The default value is 20.
594    ///
595    /// [`WasmBacktrace`]: crate::WasmBacktrace
596    pub fn wasm_backtrace_max_frames(&mut self, limit: Option<NonZeroUsize>) -> &mut Self {
597        self.wasm_backtrace_max_frames = limit;
598        self
599    }
600
601    /// Configures whether to generate native unwind information
602    /// (e.g. `.eh_frame` on Linux).
603    ///
604    /// This configuration option only exists to help third-party stack
605    /// capturing mechanisms, such as the system's unwinder or the `backtrace`
606    /// crate, determine how to unwind through Wasm frames. It does not affect
607    /// whether Wasmtime can capture Wasm backtraces or not. The presence of
608    /// [`WasmBacktrace`] is controlled by the
609    /// [`Config::wasm_backtrace_max_frames`] option.
610    ///
611    /// Native unwind information is included:
612    /// - When targeting Windows, since the Windows ABI requires it.
613    /// - By default.
614    ///
615    /// Note that systems loading many modules may wish to disable this
616    /// configuration option instead of leaving it on-by-default. Some platforms
617    /// exhibit quadratic behavior when registering/unregistering unwinding
618    /// information which can greatly slow down the module loading/unloading
619    /// process.
620    ///
621    /// [`WasmBacktrace`]: crate::WasmBacktrace
622    pub fn native_unwind_info(&mut self, enable: bool) -> &mut Self {
623        self.native_unwind_info = Some(enable);
624        self
625    }
626
627    /// Configures whether execution of WebAssembly will "consume fuel" to
628    /// either halt or yield execution as desired.
629    ///
630    /// This can be used to deterministically prevent infinitely-executing
631    /// WebAssembly code by instrumenting generated code to consume fuel as it
632    /// executes. When fuel runs out a trap is raised, however [`Store`] can be
633    /// configured to yield execution periodically via
634    /// [`crate::Store::fuel_async_yield_interval`].
635    ///
636    /// Note that a [`Store`] starts with no fuel, so if you enable this option
637    /// you'll have to be sure to pour some fuel into [`Store`] before
638    /// executing some code.
639    ///
640    /// By default this option is `false`.
641    ///
642    /// **Note** Enabling this option is not compatible with the Winch compiler.
643    ///
644    /// [`Store`]: crate::Store
645    pub fn consume_fuel(&mut self, enable: bool) -> &mut Self {
646        self.tunables.consume_fuel = Some(enable);
647        self
648    }
649
650    /// Configures the fuel cost of each WebAssembly operator.
651    ///
652    /// This is only relevant when [`Config::consume_fuel`] is enabled.
653    pub fn operator_cost(&mut self, cost: OperatorCost) -> &mut Self {
654        self.tunables.operator_cost = Some(OperatorCostStrategy::table(cost));
655        self
656    }
657
658    /// Enables epoch-based interruption.
659    ///
660    /// When executing code in async mode, we sometimes want to
661    /// implement a form of cooperative timeslicing: long-running Wasm
662    /// guest code should periodically yield to the executor
663    /// loop. This yielding could be implemented by using "fuel" (see
664    /// [`consume_fuel`](Config::consume_fuel)). However, fuel
665    /// instrumentation is somewhat expensive: it modifies the
666    /// compiled form of the Wasm code so that it maintains a precise
667    /// instruction count, frequently checking this count against the
668    /// remaining fuel. If one does not need this precise count or
669    /// deterministic interruptions, and only needs a periodic
670    /// interrupt of some form, then It would be better to have a more
671    /// lightweight mechanism.
672    ///
673    /// Epoch-based interruption is that mechanism. There is a global
674    /// "epoch", which is a counter that divides time into arbitrary
675    /// periods (or epochs). This counter lives on the
676    /// [`Engine`] and can be incremented by calling
677    /// [`Engine::increment_epoch`].
678    /// Epoch-based instrumentation works by setting a "deadline
679    /// epoch". The compiled code knows the deadline, and at certain
680    /// points, checks the current epoch against that deadline. It
681    /// will yield if the deadline has been reached.
682    ///
683    /// The idea is that checking an infrequently-changing counter is
684    /// cheaper than counting and frequently storing a precise metric
685    /// (instructions executed) locally. The interruptions are not
686    /// deterministic, but if the embedder increments the epoch in a
687    /// periodic way (say, every regular timer tick by a thread or
688    /// signal handler), then we can ensure that all async code will
689    /// yield to the executor within a bounded time.
690    ///
691    /// The deadline check cannot be avoided by malicious wasm code. It is safe
692    /// to use epoch deadlines to limit the execution time of untrusted
693    /// code.
694    ///
695    /// The [`Store`](crate::Store) tracks the deadline, and controls
696    /// what happens when the deadline is reached during
697    /// execution. Several behaviors are possible:
698    ///
699    /// - Trap if code is executing when the epoch deadline is
700    ///   met. See
701    ///   [`Store::epoch_deadline_trap`](crate::Store::epoch_deadline_trap).
702    ///
703    /// - Call an arbitrary function. This function may chose to trap or
704    ///   increment the epoch. See
705    ///   [`Store::epoch_deadline_callback`](crate::Store::epoch_deadline_callback).
706    ///
707    /// - Yield to the executor loop, then resume when the future is
708    ///   next polled. See
709    ///   [`Store::epoch_deadline_async_yield_and_update`](crate::Store::epoch_deadline_async_yield_and_update).
710    ///
711    /// Trapping is the default. The yielding behaviour may be used for
712    /// the timeslicing behavior described above.
713    ///
714    /// This feature is available with or without async support.
715    /// However, without async support, the timeslicing behaviour is
716    /// not available. This means epoch-based interruption can only
717    /// serve as a simple external-interruption mechanism.
718    ///
719    /// An initial deadline must be set before executing code by calling
720    /// [`Store::set_epoch_deadline`](crate::Store::set_epoch_deadline). If this
721    /// deadline is not configured then wasm will immediately trap.
722    ///
723    /// ## Interaction with blocking host calls
724    ///
725    /// Epochs (and fuel) do not assist in handling WebAssembly code blocked in
726    /// a call to the host. For example if the WebAssembly function calls
727    /// `wasi:io/poll.poll` to sleep epochs will not assist in waking this up or
728    /// timing it out. Epochs intentionally only affect running WebAssembly code
729    /// itself and it's left to the embedder to determine how best to wake up
730    /// indefinitely blocking code in the host.
731    ///
732    /// The typical solution for this, however, is to use the `async` variant of
733    /// WASI host functions. This models computation as a Rust `Future` which
734    /// means that when blocking happens the future is only suspended and
735    /// control yields back to the main event loop. This gives the embedder the
736    /// opportunity to use `tokio::time::timeout` for example on a wasm
737    /// computation and have the desired effect of cancelling a blocking
738    /// operation when a timeout expires.
739    ///
740    /// ## When to use fuel vs. epochs
741    ///
742    /// In general, epoch-based interruption results in faster
743    /// execution. This difference is sometimes significant: in some
744    /// measurements, up to 2-3x. This is because epoch-based
745    /// interruption does less work: it only watches for a global
746    /// rarely-changing counter to increment, rather than keeping a
747    /// local frequently-changing counter and comparing it to a
748    /// deadline.
749    ///
750    /// Fuel, in contrast, should be used when *deterministic*
751    /// yielding or trapping is needed. For example, if it is required
752    /// that the same function call with the same starting state will
753    /// always either complete or trap with an out-of-fuel error,
754    /// deterministically, then fuel with a fixed bound should be
755    /// used.
756    ///
757    /// **Note** Enabling this option is not compatible with the Winch compiler.
758    ///
759    /// # See Also
760    ///
761    /// - [`Store::set_epoch_deadline`](crate::Store::set_epoch_deadline)
762    /// - [`Store::epoch_deadline_trap`](crate::Store::epoch_deadline_trap)
763    /// - [`Store::epoch_deadline_callback`](crate::Store::epoch_deadline_callback)
764    /// - [`Store::epoch_deadline_async_yield_and_update`](crate::Store::epoch_deadline_async_yield_and_update)
765    pub fn epoch_interruption(&mut self, enable: bool) -> &mut Self {
766        self.tunables.epoch_interruption = Some(enable);
767        self
768    }
769
770    /// XXX: For internal fuzzing and debugging use only!
771    #[doc(hidden)]
772    pub fn gc_zeal_alloc_counter(&mut self, counter: Option<NonZeroU32>) -> Result<&mut Self> {
773        #[cfg(not(gc_zeal))]
774        {
775            let _ = counter;
776            bail!(
777                "cannot set `gc_zeal_alloc_counter` because Wasmtime was not built with `cfg(gc_zeal)`"
778            );
779        }
780
781        #[cfg(gc_zeal)]
782        {
783            self.tunables.gc_zeal_alloc_counter = Some(counter);
784            Ok(self)
785        }
786    }
787
788    /// Configures the maximum amount of stack space available for
789    /// executing WebAssembly code.
790    ///
791    /// WebAssembly has well-defined semantics on stack overflow. This is
792    /// intended to be a knob which can help configure how much stack space
793    /// wasm execution is allowed to consume. Note that the number here is not
794    /// super-precise, but rather wasm will take at most "pretty close to this
795    /// much" stack space.
796    ///
797    /// If a wasm call (or series of nested wasm calls) take more stack space
798    /// than the `size` specified then a stack overflow trap will be raised.
799    ///
800    /// Caveat: this knob only limits the stack space consumed by wasm code.
801    /// More importantly, it does not ensure that this much stack space is
802    /// available on the calling thread stack. Exhausting the thread stack
803    /// typically leads to an **abort** of the process.
804    ///
805    /// Here are some examples of how that could happen:
806    ///
807    /// - Let's assume this option is set to 2 MiB and then a thread that has
808    ///   a stack with 512 KiB left.
809    ///
810    ///   If wasm code consumes more than 512 KiB then the process will be aborted.
811    ///
812    /// - Assuming the same conditions, but this time wasm code does not consume
813    ///   any stack but calls into a host function. The host function consumes
814    ///   more than 512 KiB of stack space. The process will be aborted.
815    ///
816    /// There's another gotcha related to recursive calling into wasm: the stack
817    /// space consumed by a host function is counted towards this limit. The
818    /// host functions are not prevented from consuming more than this limit.
819    /// However, if the host function that used more than this limit and called
820    /// back into wasm, then the execution will trap immediately because of
821    /// stack overflow.
822    ///
823    /// When the `async` feature is enabled, this value cannot exceed the
824    /// `async_stack_size` option. Be careful not to set this value too close
825    /// to `async_stack_size` as doing so may limit how much stack space
826    /// is available for host functions.
827    ///
828    /// By default this option is 512 KiB.
829    ///
830    /// # Errors
831    ///
832    /// The [`Engine::new`] method will fail if the `size` specified here is
833    /// either 0 or larger than the [`Config::async_stack_size`] configuration.
834    pub fn max_wasm_stack(&mut self, size: usize) -> &mut Self {
835        self.max_wasm_stack = size;
836        self
837    }
838
839    /// Configures the size of the stacks used for asynchronous execution.
840    ///
841    /// This setting configures the size of the stacks that are allocated for
842    /// asynchronous execution. The value cannot be less than `max_wasm_stack`.
843    ///
844    /// The amount of stack space guaranteed for host functions is
845    /// `async_stack_size - max_wasm_stack`, so take care not to set these two values
846    /// close to one another; doing so may cause host functions to overflow the
847    /// stack and abort the process.
848    ///
849    /// By default this option is 2 MiB.
850    ///
851    /// # Errors
852    ///
853    /// The [`Engine::new`] method will fail if the value for this option is
854    /// smaller than the [`Config::max_wasm_stack`] option.
855    pub fn async_stack_size(&mut self, size: usize) -> &mut Self {
856        self.async_stack_size = size;
857        self
858    }
859
860    /// Configures whether or not stacks used for async futures are zeroed
861    /// before (re)use.
862    ///
863    /// When the [`call_async`] variant of calling WebAssembly is used
864    /// then Wasmtime will create a separate runtime execution stack for each
865    /// future produced by [`call_async`]. By default upon allocation, depending
866    /// on the platform, these stacks might be filled with uninitialized
867    /// memory. This is safe and correct because, modulo bugs in Wasmtime,
868    /// compiled Wasm code will never read from a stack slot before it
869    /// initializes the stack slot.
870    ///
871    /// However, as a defense-in-depth mechanism, you may configure Wasmtime to
872    /// ensure that these stacks are zeroed before they are used. Notably, if
873    /// you are using the pooling allocator, stacks can be pooled and reused
874    /// across different Wasm guests; ensuring that stacks are zeroed can
875    /// prevent data leakage between Wasm guests even in the face of potential
876    /// read-of-stack-slot-before-initialization bugs in Wasmtime's compiler.
877    ///
878    /// Stack zeroing can be a costly operation in highly concurrent
879    /// environments due to modifications of the virtual address space requiring
880    /// process-wide synchronization. It can also be costly in `no-std`
881    /// environments that must manually zero memory, and cannot rely on an OS
882    /// and virtual memory to provide zeroed pages.
883    ///
884    /// This option defaults to `false`.
885    ///
886    /// [`call_async`]: crate::TypedFunc::call_async
887    pub fn async_stack_zeroing(&mut self, enable: bool) -> &mut Self {
888        self.async_stack_zeroing = enable;
889        self
890    }
891
892    /// Explicitly enables (and un-disables) a given set of [`WasmFeatures`].
893    ///
894    /// Note: this is a low-level method that does not necessarily imply that
895    /// wasmtime _supports_ a feature. It should only be used to _disable_
896    /// features that callers want to be rejected by the parser or _enable_
897    /// features callers are certain that the current configuration of wasmtime
898    /// supports.
899    ///
900    /// Feature validation is deferred until an engine is being built, thus by
901    /// enabling features here a caller may cause
902    /// [`Engine::new`] to fail later, if the feature
903    /// configuration isn't supported.
904    pub fn wasm_features(&mut self, flag: WasmFeatures, enable: bool) -> &mut Self {
905        self.enabled_features.set(flag, enable);
906        self.disabled_features.set(flag, !enable);
907        self
908    }
909
910    /// Configures whether the WebAssembly tail calls proposal will be enabled
911    /// for compilation or not.
912    ///
913    /// The [WebAssembly tail calls proposal] introduces the `return_call` and
914    /// `return_call_indirect` instructions. These instructions allow for Wasm
915    /// programs to implement some recursive algorithms with *O(1)* stack space
916    /// usage.
917    ///
918    /// This is `true` by default except when the Winch compiler is enabled.
919    ///
920    /// [WebAssembly tail calls proposal]: https://github.com/WebAssembly/tail-call
921    pub fn wasm_tail_call(&mut self, enable: bool) -> &mut Self {
922        self.wasm_features(WasmFeatures::TAIL_CALL, enable);
923        self
924    }
925
926    /// Configures whether the WebAssembly [branch-hinting] proposal is enabled.
927    ///
928    /// When enabled, the `metadata.code.branch_hint` custom section is parsed
929    /// and used to lay out cold code paths during compilation. The hints are
930    /// advisory and never affect execution semantics.
931    ///
932    /// This is `false` by default until the proposal has been fuzzed.
933    ///
934    /// [branch-hinting]: https://github.com/WebAssembly/branch-hinting
935    pub fn wasm_branch_hinting(&mut self, enable: bool) -> &mut Self {
936        self.tunables.branch_hinting = Some(enable);
937        self
938    }
939
940    /// Configures whether the WebAssembly custom-page-sizes proposal will be
941    /// enabled for compilation or not.
942    ///
943    /// The [WebAssembly custom-page-sizes proposal] allows a memory to
944    /// customize its page sizes. By default, Wasm page sizes are 64KiB
945    /// large. This proposal allows the memory to opt into smaller page sizes
946    /// instead, allowing Wasm to run in environments with less than 64KiB RAM
947    /// available, for example.
948    ///
949    /// Note that the page size is part of the memory's type, and because
950    /// different memories may have different types, they may also have
951    /// different page sizes.
952    ///
953    /// Currently the only valid page sizes are 64KiB (the default) and 1
954    /// byte. Future extensions may relax this constraint and allow all powers
955    /// of two.
956    ///
957    /// Support for this proposal is disabled by default.
958    ///
959    /// [WebAssembly custom-page-sizes proposal]: https://github.com/WebAssembly/custom-page-sizes
960    pub fn wasm_custom_page_sizes(&mut self, enable: bool) -> &mut Self {
961        self.wasm_features(WasmFeatures::CUSTOM_PAGE_SIZES, enable);
962        self
963    }
964
965    /// Configures whether the WebAssembly [threads] proposal will be enabled
966    /// for compilation.
967    ///
968    /// This feature gates items such as shared memories and atomic
969    /// instructions. Note that the threads feature depends on the bulk memory
970    /// feature, which is enabled by default. Additionally note that while the
971    /// wasm feature is called "threads" it does not actually include the
972    /// ability to spawn threads. Spawning threads is part of the [wasi-threads]
973    /// proposal which is a separately gated feature in Wasmtime.
974    ///
975    /// Embeddings of Wasmtime are able to build their own custom threading
976    /// scheme on top of the core wasm threads proposal, however.
977    ///
978    /// The default value for this option is whether the `threads`
979    /// crate feature of Wasmtime is enabled or not. By default this crate
980    /// feature is enabled.
981    ///
982    /// [threads]: https://github.com/webassembly/threads
983    /// [wasi-threads]: https://github.com/webassembly/wasi-threads
984    #[cfg(feature = "threads")]
985    pub fn wasm_threads(&mut self, enable: bool) -> &mut Self {
986        self.wasm_features(WasmFeatures::THREADS, enable);
987        self
988    }
989
990    /// Configures whether the WebAssembly [shared-everything-threads] proposal
991    /// will be enabled for compilation.
992    ///
993    /// This feature gates extended use of the `shared` attribute on items other
994    /// than memories, extra atomic instructions, and new component model
995    /// intrinsics for spawning threads. It depends on the
996    /// [`wasm_threads`][Self::wasm_threads] being enabled.
997    ///
998    /// [shared-everything-threads]:
999    ///     https://github.com/webassembly/shared-everything-threads
1000    pub fn wasm_shared_everything_threads(&mut self, enable: bool) -> &mut Self {
1001        self.wasm_features(WasmFeatures::SHARED_EVERYTHING_THREADS, enable);
1002        self
1003    }
1004
1005    /// Configures whether the [WebAssembly reference types proposal][proposal]
1006    /// will be enabled for compilation.
1007    ///
1008    /// This feature gates items such as the `externref` and `funcref` types as
1009    /// well as allowing a module to define multiple tables.
1010    ///
1011    /// Note that the reference types proposal depends on the bulk memory proposal.
1012    ///
1013    /// This feature is `true` by default.
1014    ///
1015    /// # Errors
1016    ///
1017    /// The validation of this feature are deferred until the engine is being built,
1018    /// and thus may cause [`Engine::new`] fail if the `bulk_memory` feature is disabled.
1019    ///
1020    /// [proposal]: https://github.com/webassembly/reference-types
1021    #[cfg(feature = "gc")]
1022    pub fn wasm_reference_types(&mut self, enable: bool) -> &mut Self {
1023        self.wasm_features(WasmFeatures::REFERENCE_TYPES, enable);
1024        self
1025    }
1026
1027    /// Configures whether the [WebAssembly function references
1028    /// proposal][proposal] will be enabled for compilation.
1029    ///
1030    /// This feature gates non-nullable reference types, function reference
1031    /// types, `call_ref`, `ref.func`, and non-nullable reference related
1032    /// instructions.
1033    ///
1034    /// Note that the function references proposal depends on the reference
1035    /// types proposal.
1036    ///
1037    /// This feature is `true` by default.
1038    ///
1039    /// [proposal]: https://github.com/WebAssembly/function-references
1040    #[cfg(feature = "gc")]
1041    pub fn wasm_function_references(&mut self, enable: bool) -> &mut Self {
1042        self.wasm_features(WasmFeatures::FUNCTION_REFERENCES, enable);
1043        self
1044    }
1045
1046    /// Configures whether the [WebAssembly wide-arithmetic][proposal] will be
1047    /// enabled for compilation.
1048    ///
1049    /// This feature is `false` by default.
1050    ///
1051    /// [proposal]: https://github.com/WebAssembly/wide-arithmetic
1052    pub fn wasm_wide_arithmetic(&mut self, enable: bool) -> &mut Self {
1053        self.wasm_features(WasmFeatures::WIDE_ARITHMETIC, enable);
1054        self
1055    }
1056
1057    /// Configures whether the [WebAssembly Garbage Collection
1058    /// proposal][proposal] will be enabled for compilation.
1059    ///
1060    /// This feature gates `struct` and `array` type definitions and references,
1061    /// the `i31ref` type, and all related instructions.
1062    ///
1063    /// Note that the function references proposal depends on the typed function
1064    /// references proposal.
1065    ///
1066    /// This feature is `true` by default.
1067    ///
1068    /// [proposal]: https://github.com/WebAssembly/gc
1069    pub fn wasm_gc(&mut self, enable: bool) -> &mut Self {
1070        self.wasm_features(WasmFeatures::GC, enable);
1071        self
1072    }
1073
1074    /// Configures whether the WebAssembly SIMD proposal will be
1075    /// enabled for compilation.
1076    ///
1077    /// The [WebAssembly SIMD proposal][proposal]. This feature gates items such
1078    /// as the `v128` type and all of its operators being in a module. Note that
1079    /// this does not enable the [relaxed simd proposal].
1080    ///
1081    /// **Note**
1082    ///
1083    /// On x86_64 platforms the base CPU feature requirement for SIMD
1084    /// is SSE2 for the Cranelift compiler and AVX for the Winch compiler.
1085    ///
1086    /// This is `true` by default.
1087    ///
1088    /// [proposal]: https://github.com/webassembly/simd
1089    /// [relaxed simd proposal]: https://github.com/WebAssembly/relaxed-simd
1090    pub fn wasm_simd(&mut self, enable: bool) -> &mut Self {
1091        self.wasm_features(WasmFeatures::SIMD, enable);
1092        self
1093    }
1094
1095    /// Configures whether the WebAssembly Relaxed SIMD proposal will be
1096    /// enabled for compilation.
1097    ///
1098    /// The relaxed SIMD proposal adds new instructions to WebAssembly which,
1099    /// for some specific inputs, are allowed to produce different results on
1100    /// different hosts. More-or-less this proposal enables exposing
1101    /// platform-specific semantics of SIMD instructions in a controlled
1102    /// fashion to a WebAssembly program. From an embedder's perspective this
1103    /// means that WebAssembly programs may execute differently depending on
1104    /// whether the host is x86_64 or AArch64, for example.
1105    ///
1106    /// By default Wasmtime lowers relaxed SIMD instructions to the fastest
1107    /// lowering for the platform it's running on. This means that, by default,
1108    /// some relaxed SIMD instructions may have different results for the same
1109    /// inputs across x86_64 and AArch64. This behavior can be disabled through
1110    /// the [`Config::relaxed_simd_deterministic`] option which will force
1111    /// deterministic behavior across all platforms, as classified by the
1112    /// specification, at the cost of performance.
1113    ///
1114    /// This is `true` by default.
1115    ///
1116    /// [proposal]: https://github.com/webassembly/relaxed-simd
1117    pub fn wasm_relaxed_simd(&mut self, enable: bool) -> &mut Self {
1118        self.wasm_features(WasmFeatures::RELAXED_SIMD, enable);
1119        self
1120    }
1121
1122    /// This option can be used to control the behavior of the [relaxed SIMD
1123    /// proposal's][proposal] instructions.
1124    ///
1125    /// The relaxed SIMD proposal introduces instructions that are allowed to
1126    /// have different behavior on different architectures, primarily to afford
1127    /// an efficient implementation on all architectures. This means, however,
1128    /// that the same module may execute differently on one host than another,
1129    /// which typically is not otherwise the case. This option is provided to
1130    /// force Wasmtime to generate deterministic code for all relaxed simd
1131    /// instructions, at the cost of performance, for all architectures. When
1132    /// this option is enabled then the deterministic behavior of all
1133    /// instructions in the relaxed SIMD proposal is selected.
1134    ///
1135    /// This is `false` by default.
1136    ///
1137    /// [proposal]: https://github.com/webassembly/relaxed-simd
1138    pub fn relaxed_simd_deterministic(&mut self, enable: bool) -> &mut Self {
1139        self.tunables.relaxed_simd_deterministic = Some(enable);
1140        self
1141    }
1142
1143    /// Configures whether the [WebAssembly bulk memory operations
1144    /// proposal][proposal] will be enabled for compilation.
1145    ///
1146    /// This feature gates items such as the `memory.copy` instruction, passive
1147    /// data/table segments, etc, being in a module.
1148    ///
1149    /// This is `true` by default.
1150    ///
1151    /// Feature `reference_types`, which is also `true` by default, requires
1152    /// this feature to be enabled. Thus disabling this feature must also disable
1153    /// `reference_types` as well using [`wasm_reference_types`](crate::Config::wasm_reference_types).
1154    ///
1155    /// # Errors
1156    ///
1157    /// Disabling this feature without disabling `reference_types` will cause
1158    /// [`Engine::new`] to fail.
1159    ///
1160    /// [proposal]: https://github.com/webassembly/bulk-memory-operations
1161    pub fn wasm_bulk_memory(&mut self, enable: bool) -> &mut Self {
1162        self.wasm_features(WasmFeatures::BULK_MEMORY, enable);
1163        self
1164    }
1165
1166    /// Configures whether the WebAssembly multi-value [proposal] will
1167    /// be enabled for compilation.
1168    ///
1169    /// This feature gates functions and blocks returning multiple values in a
1170    /// module, for example.
1171    ///
1172    /// This is `true` by default.
1173    ///
1174    /// [proposal]: https://github.com/webassembly/multi-value
1175    pub fn wasm_multi_value(&mut self, enable: bool) -> &mut Self {
1176        self.wasm_features(WasmFeatures::MULTI_VALUE, enable);
1177        self
1178    }
1179
1180    /// Configures whether the WebAssembly multi-memory [proposal] will
1181    /// be enabled for compilation.
1182    ///
1183    /// This feature gates modules having more than one linear memory
1184    /// declaration or import.
1185    ///
1186    /// This is `true` by default.
1187    ///
1188    /// [proposal]: https://github.com/webassembly/multi-memory
1189    pub fn wasm_multi_memory(&mut self, enable: bool) -> &mut Self {
1190        self.wasm_features(WasmFeatures::MULTI_MEMORY, enable);
1191        self
1192    }
1193
1194    /// Configures whether the WebAssembly memory64 [proposal] will
1195    /// be enabled for compilation.
1196    ///
1197    /// Note that this the upstream specification is not finalized and Wasmtime
1198    /// may also have bugs for this feature since it hasn't been exercised
1199    /// much.
1200    ///
1201    /// This is `false` by default.
1202    ///
1203    /// [proposal]: https://github.com/webassembly/memory64
1204    pub fn wasm_memory64(&mut self, enable: bool) -> &mut Self {
1205        self.wasm_features(WasmFeatures::MEMORY64, enable);
1206        self
1207    }
1208
1209    /// Configures whether the WebAssembly extended-const [proposal] will
1210    /// be enabled for compilation.
1211    ///
1212    /// This is `true` by default.
1213    ///
1214    /// [proposal]: https://github.com/webassembly/extended-const
1215    pub fn wasm_extended_const(&mut self, enable: bool) -> &mut Self {
1216        self.wasm_features(WasmFeatures::EXTENDED_CONST, enable);
1217        self
1218    }
1219
1220    /// Configures whether the [WebAssembly stack switching
1221    /// proposal][proposal] will be enabled for compilation.
1222    ///
1223    /// This feature gates the use of control tags.
1224    ///
1225    /// This feature depends on the `function_reference_types` and
1226    /// `exceptions` features.
1227    ///
1228    /// This feature is `false` by default.
1229    ///
1230    /// # Errors
1231    ///
1232    /// [proposal]: https://github.com/webassembly/stack-switching
1233    pub fn wasm_stack_switching(&mut self, enable: bool) -> &mut Self {
1234        self.wasm_features(WasmFeatures::STACK_SWITCHING, enable);
1235        self
1236    }
1237
1238    /// Configures whether the WebAssembly component-model [proposal] will
1239    /// be enabled for compilation.
1240    ///
1241    /// This flag can be used to blanket disable all components within Wasmtime.
1242    /// Otherwise usage of components requires statically using
1243    /// [`Component`](crate::component::Component) instead of
1244    /// [`Module`](crate::Module) for example anyway.
1245    ///
1246    /// The default value for this option is whether the `component-model`
1247    /// crate feature of Wasmtime is enabled or not. By default this crate
1248    /// feature is enabled.
1249    ///
1250    /// [proposal]: https://github.com/webassembly/component-model
1251    #[cfg(feature = "component-model")]
1252    pub fn wasm_component_model(&mut self, enable: bool) -> &mut Self {
1253        self.wasm_features(WasmFeatures::COMPONENT_MODEL, enable);
1254        self
1255    }
1256
1257    /// Configures whether components support the async ABI [proposal] for
1258    /// lifting and lowering functions, as well as `stream`, `future`, and
1259    /// `error-context` types.
1260    ///
1261    /// Please note that Wasmtime's support for this feature is _very_
1262    /// incomplete.
1263    ///
1264    /// [proposal]:
1265    ///     https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md
1266    #[cfg(feature = "component-model-async")]
1267    pub fn wasm_component_model_async(&mut self, enable: bool) -> &mut Self {
1268        self.wasm_features(WasmFeatures::CM_ASYNC, enable);
1269        self
1270    }
1271
1272    /// This corresponds to the 🚝 emoji in the component model specification.
1273    ///
1274    /// Please note that Wasmtime's support for this feature is _very_
1275    /// incomplete.
1276    ///
1277    /// [proposal]:
1278    ///     https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md
1279    #[cfg(feature = "component-model-async")]
1280    pub fn wasm_component_model_more_async_builtins(&mut self, enable: bool) -> &mut Self {
1281        self.wasm_features(WasmFeatures::CM_MORE_ASYNC_BUILTINS, enable);
1282        self
1283    }
1284
1285    /// This corresponds to the 🚟 emoji in the component model specification.
1286    ///
1287    /// Please note that Wasmtime's support for this feature is _very_
1288    /// incomplete.
1289    ///
1290    /// [proposal]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md
1291    #[cfg(feature = "component-model-async")]
1292    pub fn wasm_component_model_async_stackful(&mut self, enable: bool) -> &mut Self {
1293        self.wasm_features(WasmFeatures::CM_ASYNC_STACKFUL, enable);
1294        self
1295    }
1296
1297    /// This corresponds to the 🧵 emoji in the component model specification.
1298    ///
1299    /// Please note that Wasmtime's support for this feature is _very_
1300    /// incomplete.
1301    ///
1302    /// [proposal]:
1303    ///     https://github.com/WebAssembly/component-model/pull/557
1304    #[cfg(feature = "component-model-async")]
1305    pub fn wasm_component_model_threading(&mut self, enable: bool) -> &mut Self {
1306        self.wasm_features(WasmFeatures::CM_THREADING, enable);
1307        self
1308    }
1309
1310    /// This corresponds to the 📝 emoji in the component model specification.
1311    ///
1312    /// Please note that Wasmtime's support for this feature is _very_
1313    /// incomplete.
1314    ///
1315    /// [proposal]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md
1316    #[cfg(feature = "component-model")]
1317    pub fn wasm_component_model_error_context(&mut self, enable: bool) -> &mut Self {
1318        self.wasm_features(WasmFeatures::CM_ERROR_CONTEXT, enable);
1319        self
1320    }
1321
1322    /// Configures whether the [GC extension to the component-model
1323    /// proposal][proposal] is enabled or not.
1324    ///
1325    /// This corresponds to the 🛸 emoji in the component model specification.
1326    ///
1327    /// Please note that Wasmtime's support for this feature is _very_
1328    /// incomplete.
1329    ///
1330    /// [proposal]: https://github.com/WebAssembly/component-model/issues/525
1331    #[cfg(feature = "component-model")]
1332    pub fn wasm_component_model_gc(&mut self, enable: bool) -> &mut Self {
1333        self.wasm_features(WasmFeatures::CM_GC, enable);
1334        self
1335    }
1336
1337    /// Configures whether the component model map type is enabled or not.
1338    ///
1339    /// This is part of the component model specification and enables the
1340    /// `map<k, v>` type in WIT and the component binary format.
1341    #[cfg(feature = "component-model")]
1342    pub fn wasm_component_model_map(&mut self, enable: bool) -> &mut Self {
1343        self.wasm_features(WasmFeatures::CM_MAP, enable);
1344        self
1345    }
1346
1347    /// This corresponds to the 🔧 emoji in the component model specification.
1348    ///
1349    /// Please note that Wasmtime's support for this feature is _very_
1350    /// incomplete.
1351    #[cfg(feature = "component-model")]
1352    pub fn wasm_component_model_fixed_length_lists(&mut self, enable: bool) -> &mut Self {
1353        self.wasm_features(WasmFeatures::CM_FIXED_LENGTH_LISTS, enable);
1354        self
1355    }
1356
1357    /// This corresponds to the 🏷️ emoji in the component model specification.
1358    ///
1359    /// Please note that Wasmtime's support for this feature is a work in
1360    /// progress.
1361    #[cfg(feature = "component-model")]
1362    pub fn wasm_component_model_implements(&mut self, enable: bool) -> &mut Self {
1363        self.wasm_features(WasmFeatures::CM_IMPLEMENTS, enable);
1364        self
1365    }
1366
1367    /// Configures whether the [Exception-handling proposal][proposal] is enabled or not.
1368    ///
1369    /// This is `true` by default.
1370    ///
1371    /// [proposal]: https://github.com/WebAssembly/exception-handling
1372    #[cfg(feature = "gc")]
1373    pub fn wasm_exceptions(&mut self, enable: bool) -> &mut Self {
1374        self.wasm_features(WasmFeatures::EXCEPTIONS, enable);
1375        self
1376    }
1377
1378    #[doc(hidden)] // FIXME(#3427) - if/when implemented then un-hide this
1379    #[deprecated = "This configuration option only exists for internal \
1380                    usage with the spec testsuite. It may be removed at \
1381                    any time and without warning. Do not rely on it!"]
1382    pub fn wasm_legacy_exceptions(&mut self, enable: bool) -> &mut Self {
1383        self.wasm_features(WasmFeatures::LEGACY_EXCEPTIONS, enable);
1384        self
1385    }
1386
1387    /// Configures which compilation strategy will be used for wasm modules.
1388    ///
1389    /// This method can be used to configure which compiler is used for wasm
1390    /// modules, and for more documentation consult the [`Strategy`] enumeration
1391    /// and its documentation.
1392    ///
1393    /// The default value for this is `Strategy::Auto`.
1394    ///
1395    /// # Panics
1396    ///
1397    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1398    #[cfg(any(feature = "cranelift", feature = "winch"))]
1399    pub fn strategy(&mut self, strategy: Strategy) -> &mut Self {
1400        self.compiler_config_mut().strategy = strategy.not_auto();
1401        self
1402    }
1403
1404    /// Configures which garbage collector will be used for Wasm modules.
1405    ///
1406    /// This method can be used to configure which garbage collector
1407    /// implementation is used for Wasm modules. For more documentation, consult
1408    /// the [`Collector`] enumeration and its documentation.
1409    ///
1410    /// The default value for this is `Collector::Auto`.
1411    #[cfg(feature = "gc")]
1412    pub fn collector(&mut self, collector: Collector) -> &mut Self {
1413        self.collector = collector;
1414        self
1415    }
1416
1417    /// Creates a default profiler based on the profiling strategy chosen.
1418    ///
1419    /// Profiler creation calls the type's default initializer where the purpose is
1420    /// really just to put in place the type used for profiling.
1421    ///
1422    /// Some [`ProfilingStrategy`] require specific platforms or particular feature
1423    /// to be enabled, such as `ProfilingStrategy::JitDump` requires the `jitdump`
1424    /// feature.
1425    ///
1426    /// # Errors
1427    ///
1428    /// The validation of this field is deferred until the engine is being built, and thus may
1429    /// cause [`Engine::new`] fail if the required feature is disabled, or the platform is not
1430    /// supported.
1431    pub fn profiler(&mut self, profile: ProfilingStrategy) -> &mut Self {
1432        self.profiling_strategy = profile;
1433        self
1434    }
1435
1436    /// Configures whether the debug verifier of Cranelift is enabled or not.
1437    ///
1438    /// When Cranelift is used as a code generation backend this will configure
1439    /// it to have the `enable_verifier` flag which will enable a number of debug
1440    /// checks inside of Cranelift. This is largely only useful for the
1441    /// developers of wasmtime itself.
1442    ///
1443    /// The default value for this is `false`
1444    ///
1445    /// # Panics
1446    ///
1447    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1448    #[cfg(any(feature = "cranelift", feature = "winch"))]
1449    pub fn cranelift_debug_verifier(&mut self, enable: bool) -> &mut Self {
1450        let val = if enable { "true" } else { "false" };
1451        self.compiler_config_mut().settings.insert(
1452            "enable_verifier".to_string(),
1453            (val.to_string(), UserSpecified::No),
1454        );
1455        self
1456    }
1457
1458    /// Configures whether extra debug checks are inserted into
1459    /// Wasmtime-generated code by Cranelift.
1460    ///
1461    /// The default value for this is `false`
1462    ///
1463    /// # Panics
1464    ///
1465    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1466    #[cfg(any(feature = "cranelift", feature = "winch"))]
1467    pub fn cranelift_wasmtime_debug_checks(&mut self, enable: bool) -> &mut Self {
1468        unsafe { self.cranelift_flag_set("wasmtime_debug_checks", &enable.to_string()) }
1469    }
1470
1471    /// Configures the Cranelift code generator optimization level.
1472    ///
1473    /// When the Cranelift code generator is used you can configure the
1474    /// optimization level used for generated code in a few various ways. For
1475    /// more information see the documentation of [`OptLevel`].
1476    ///
1477    /// The default value for this is `OptLevel::Speed`.
1478    ///
1479    /// # Panics
1480    ///
1481    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1482    #[cfg(any(feature = "cranelift", feature = "winch"))]
1483    pub fn cranelift_opt_level(&mut self, level: OptLevel) -> &mut Self {
1484        let val = match level {
1485            OptLevel::None => "none",
1486            OptLevel::Speed => "speed",
1487            OptLevel::SpeedAndSize => "speed_and_size",
1488        };
1489        self.compiler_config_mut().settings.insert(
1490            "opt_level".to_string(),
1491            (val.to_string(), UserSpecified::No),
1492        );
1493        self
1494    }
1495
1496    /// Configures the regalloc algorithm used by the Cranelift code generator.
1497    ///
1498    /// Cranelift can select any of several register allocator algorithms. Each
1499    /// of these algorithms generates correct code, but they represent different
1500    /// tradeoffs between compile speed (how expensive the compilation process
1501    /// is) and run-time speed (how fast the generated code runs).
1502    /// For more information see the documentation of [`RegallocAlgorithm`].
1503    ///
1504    /// The default value for this is `RegallocAlgorithm::Backtracking`.
1505    ///
1506    /// # Panics
1507    ///
1508    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1509    #[cfg(any(feature = "cranelift", feature = "winch"))]
1510    pub fn cranelift_regalloc_algorithm(&mut self, algo: RegallocAlgorithm) -> &mut Self {
1511        let val = match algo {
1512            RegallocAlgorithm::Backtracking => "backtracking",
1513            RegallocAlgorithm::SinglePass => "single_pass",
1514        };
1515        self.compiler_config_mut().settings.insert(
1516            "regalloc_algorithm".to_string(),
1517            (val.to_string(), UserSpecified::No),
1518        );
1519        self
1520    }
1521
1522    /// Configures whether Cranelift should perform a NaN-canonicalization pass.
1523    ///
1524    /// When Cranelift is used as a code generation backend this will configure
1525    /// it to replace NaNs with a single canonical value. This is useful for
1526    /// users requiring entirely deterministic WebAssembly computation.  This is
1527    /// not required by the WebAssembly spec, so it is not enabled by default.
1528    ///
1529    /// Note that this option affects not only WebAssembly's `f32` and `f64`
1530    /// types but additionally the `v128` type. This option will cause
1531    /// operations using any of these types to have extra checks placed after
1532    /// them to normalize NaN values as needed.
1533    ///
1534    /// The default value for this is `false`
1535    ///
1536    /// # Panics
1537    ///
1538    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1539    #[cfg(any(feature = "cranelift", feature = "winch"))]
1540    pub fn cranelift_nan_canonicalization(&mut self, enable: bool) -> &mut Self {
1541        let val = if enable { "true" } else { "false" };
1542        self.compiler_config_mut().settings.insert(
1543            "enable_nan_canonicalization".to_string(),
1544            (val.to_string(), UserSpecified::No),
1545        );
1546        self
1547    }
1548
1549    /// Allows setting a Cranelift boolean flag or preset. This allows
1550    /// fine-tuning of Cranelift settings.
1551    ///
1552    /// Since Cranelift flags may be unstable, this method should not be considered to be stable
1553    /// either; other `Config` functions should be preferred for stability.
1554    ///
1555    /// # Safety
1556    ///
1557    /// This is marked as unsafe, because setting the wrong flag might break invariants,
1558    /// resulting in execution hazards.
1559    ///
1560    /// # Errors
1561    ///
1562    /// The validation of the flags are deferred until the engine is being built, and thus may
1563    /// cause [`Engine::new`] fail if the flag's name does not exist, or the value is not appropriate
1564    /// for the flag type.
1565    ///
1566    /// # Panics
1567    ///
1568    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1569    #[cfg(any(feature = "cranelift", feature = "winch"))]
1570    pub unsafe fn cranelift_flag_enable(&mut self, flag: &str) -> &mut Self {
1571        self.compiler_config_mut()
1572            .flags
1573            .insert(flag.to_string(), UserSpecified::Yes);
1574        self
1575    }
1576
1577    /// Allows settings another Cranelift flag defined by a flag name and value. This allows
1578    /// fine-tuning of Cranelift settings.
1579    ///
1580    /// Since Cranelift flags may be unstable, this method should not be considered to be stable
1581    /// either; other `Config` functions should be preferred for stability.
1582    ///
1583    /// # Safety
1584    ///
1585    /// This is marked as unsafe, because setting the wrong flag might break invariants,
1586    /// resulting in execution hazards.
1587    ///
1588    /// # Errors
1589    ///
1590    /// The validation of the flags are deferred until the engine is being built, and thus may
1591    /// cause [`Engine::new`] fail if the flag's name does not exist, or incompatible with other
1592    /// settings.
1593    ///
1594    /// For example, feature `wasm_backtrace` will set `unwind_info` to `true`, but if it's
1595    /// manually set to false then it will fail.
1596    ///
1597    /// # Panics
1598    ///
1599    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
1600    #[cfg(any(feature = "cranelift", feature = "winch"))]
1601    pub unsafe fn cranelift_flag_set(&mut self, name: &str, value: &str) -> &mut Self {
1602        self.compiler_config_mut()
1603            .settings
1604            .insert(name.to_string(), (value.to_string(), UserSpecified::Yes));
1605        self
1606    }
1607
1608    /// Set a custom [`Cache`].
1609    ///
1610    /// To load a cache configuration from a file, use [`Cache::from_file`]. Otherwise, you can
1611    /// create a new cache config using [`CacheConfig::new`] and passing that to [`Cache::new`].
1612    ///
1613    /// If you want to disable the cache, you can call this method with `None`.
1614    ///
1615    /// By default, new configs do not have caching enabled.
1616    /// Every call to [`Module::new(my_wasm)`][crate::Module::new] will recompile `my_wasm`,
1617    /// even when it is unchanged, unless an enabled `CacheConfig` is provided.
1618    ///
1619    /// This method is only available when the `cache` feature of this crate is
1620    /// enabled.
1621    ///
1622    /// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
1623    #[cfg(feature = "cache")]
1624    pub fn cache(&mut self, cache: Option<Cache>) -> &mut Self {
1625        self.cache = cache;
1626        self
1627    }
1628
1629    /// Sets a custom memory creator.
1630    ///
1631    /// Custom memory creators are used when creating host `Memory` objects or when
1632    /// creating instance linear memories for the on-demand instance allocation strategy.
1633    #[cfg(feature = "runtime")]
1634    pub fn with_host_memory(&mut self, mem_creator: Arc<dyn MemoryCreator>) -> &mut Self {
1635        self.mem_creator = Some(Arc::new(MemoryCreatorProxy(mem_creator)));
1636        self
1637    }
1638
1639    /// Sets a custom stack creator.
1640    ///
1641    /// Custom memory creators are used when creating creating async instance stacks for
1642    /// the on-demand instance allocation strategy.
1643    #[cfg(feature = "async")]
1644    pub fn with_host_stack(&mut self, stack_creator: Arc<dyn StackCreator>) -> &mut Self {
1645        self.stack_creator = Some(Arc::new(StackCreatorProxy(stack_creator)));
1646        self
1647    }
1648
1649    /// Sets a custom executable-memory publisher.
1650    ///
1651    /// Custom executable-memory publishers are hooks that allow
1652    /// Wasmtime to make certain regions of memory executable when
1653    /// loading precompiled modules or compiling new modules
1654    /// in-process. In most modern operating systems, memory allocated
1655    /// for heap usage is readable and writable by default but not
1656    /// executable. To jump to machine code stored in that memory, we
1657    /// need to make it executable. For security reasons, we usually
1658    /// also make it read-only at the same time, so the executing code
1659    /// can't be modified later.
1660    ///
1661    /// By default, Wasmtime will use the appropriate system calls on
1662    /// the host platform for this work. However, it also allows
1663    /// plugging in a custom implementation via this configuration
1664    /// option. This may be useful on custom or `no_std` platforms,
1665    /// for example, especially where virtual memory is not otherwise
1666    /// used by Wasmtime (no `signals-and-traps` feature).
1667    #[cfg(feature = "runtime")]
1668    pub fn with_custom_code_memory(
1669        &mut self,
1670        custom_code_memory: Option<Arc<dyn CustomCodeMemory>>,
1671    ) -> &mut Self {
1672        self.custom_code_memory = custom_code_memory;
1673        self
1674    }
1675
1676    /// Sets the instance allocation strategy to use.
1677    ///
1678    /// This is notably used in conjunction with
1679    /// [`InstanceAllocationStrategy::Pooling`] and [`PoolingAllocationConfig`].
1680    pub fn allocation_strategy(
1681        &mut self,
1682        strategy: impl Into<InstanceAllocationStrategy>,
1683    ) -> &mut Self {
1684        self.allocation_strategy = strategy.into();
1685        self
1686    }
1687
1688    /// Specifies the capacity of linear memories, in bytes, in their initial
1689    /// allocation.
1690    ///
1691    /// > Note: this value has important performance ramifications, be sure to
1692    /// > benchmark when setting this to a non-default value and read over this
1693    /// > documentation.
1694    ///
1695    /// This function will change the size of the initial memory allocation made
1696    /// for linear memories. This setting is only applicable when the initial
1697    /// size of a linear memory is below this threshold. Linear memories are
1698    /// allocated in the virtual address space of the host process with OS APIs
1699    /// such as `mmap` and this setting affects how large the allocation will
1700    /// be.
1701    ///
1702    /// ## Background: WebAssembly Linear Memories
1703    ///
1704    /// WebAssembly linear memories always start with a minimum size and can
1705    /// possibly grow up to a maximum size. The minimum size is always specified
1706    /// in a WebAssembly module itself and the maximum size can either be
1707    /// optionally specified in the module or inherently limited by the index
1708    /// type. For example for this module:
1709    ///
1710    /// ```wasm
1711    /// (module
1712    ///     (memory $a 4)
1713    ///     (memory $b 4096 4096 (pagesize 1))
1714    ///     (memory $c i64 10)
1715    /// )
1716    /// ```
1717    ///
1718    /// * Memory `$a` initially allocates 4 WebAssembly pages (256KiB) and can
1719    ///   grow up to 4GiB, the limit of the 32-bit index space.
1720    /// * Memory `$b` initially allocates 4096 WebAssembly pages, but in this
1721    ///   case its page size is 1, so it's 4096 bytes. Memory can also grow no
1722    ///   further meaning that it will always be 4096 bytes.
1723    /// * Memory `$c` is a 64-bit linear memory which starts with 640KiB of
1724    ///   memory and can theoretically grow up to 2^64 bytes, although most
1725    ///   hosts will run out of memory long before that.
1726    ///
1727    /// All operations on linear memories done by wasm are required to be
1728    /// in-bounds. Any access beyond the end of a linear memory is considered a
1729    /// trap.
1730    ///
1731    /// ## What this setting affects: Virtual Memory
1732    ///
1733    /// This setting is used to configure the behavior of the size of the linear
1734    /// memory allocation performed for each of these memories. For example the
1735    /// initial linear memory allocation looks like this:
1736    ///
1737    /// ```text
1738    ///              memory_reservation
1739    ///                    |
1740    ///          ◄─────────┴────────────────►
1741    /// ┌───────┬─────────┬──────────────────┬───────┐
1742    /// │ guard │ initial │ ... capacity ... │ guard │
1743    /// └───────┴─────────┴──────────────────┴───────┘
1744    ///  ◄──┬──►                              ◄──┬──►
1745    ///     │                                    │
1746    ///     │                             memory_guard_size
1747    ///     │
1748    ///     │
1749    ///  memory_guard_size (if guard_before_linear_memory)
1750    /// ```
1751    ///
1752    /// Memory in the `initial` range is accessible to the instance and can be
1753    /// read/written by wasm code. Memory in the `guard` regions is never
1754    /// accessible to wasm code and memory in `capacity` is initially
1755    /// inaccessible but may become accessible through `memory.grow` instructions
1756    /// for example.
1757    ///
1758    /// This means that this setting is the size of the initial chunk of virtual
1759    /// memory that a linear memory may grow into.
1760    ///
1761    /// ## What this setting affects: Runtime Speed
1762    ///
1763    /// This is a performance-sensitive setting which is taken into account
1764    /// during the compilation process of a WebAssembly module. For example if a
1765    /// 32-bit WebAssembly linear memory has a `memory_reservation` size of 4GiB
1766    /// then bounds checks can be elided because `capacity` will be guaranteed
1767    /// to be unmapped for all addressable bytes that wasm can access (modulo a
1768    /// few details).
1769    ///
1770    /// If `memory_reservation` was something smaller like 256KiB then that
1771    /// would have a much smaller impact on virtual memory but the compile code
1772    /// would then need to have explicit bounds checks to ensure that
1773    /// loads/stores are in-bounds.
1774    ///
1775    /// The goal of this setting is to enable skipping bounds checks in most
1776    /// modules by default. Some situations which require explicit bounds checks
1777    /// though are:
1778    ///
1779    /// * When `memory_reservation` is smaller than the addressable size of the
1780    ///   linear memory. For example if 64-bit linear memories always need
1781    ///   bounds checks as they can address the entire virtual address spacce.
1782    ///   For 32-bit linear memories a `memory_reservation` minimum size of 4GiB
1783    ///   is required to elide bounds checks.
1784    ///
1785    /// * When linear memories have a page size of 1 then bounds checks are
1786    ///   required. In this situation virtual memory can't be relied upon
1787    ///   because that operates at the host page size granularity where wasm
1788    ///   requires a per-byte level granularity.
1789    ///
1790    /// * Configuration settings such as [`Config::signals_based_traps`] can be
1791    ///   used to disable the use of signal handlers and virtual memory so
1792    ///   explicit bounds checks are required.
1793    ///
1794    /// * When [`Config::memory_guard_size`] is too small a bounds check may be
1795    ///   required. For 32-bit wasm addresses are actually 33-bit effective
1796    ///   addresses because loads/stores have a 32-bit static offset to add to
1797    ///   the dynamic 32-bit address. If the static offset is larger than the
1798    ///   size of the guard region then an explicit bounds check is required.
1799    ///
1800    /// ## What this setting affects: Memory Growth Behavior
1801    ///
1802    /// In addition to affecting bounds checks emitted in compiled code this
1803    /// setting also affects how WebAssembly linear memories are grown. The
1804    /// `memory.grow` instruction can be used to make a linear memory larger and
1805    /// this is also affected by APIs such as
1806    /// [`Memory::grow`](crate::Memory::grow).
1807    ///
1808    /// In these situations when the amount being grown is small enough to fit
1809    /// within the remaining capacity then the linear memory doesn't have to be
1810    /// moved at runtime. If the capacity runs out though then a new linear
1811    /// memory allocation must be made and the contents of linear memory is
1812    /// copied over.
1813    ///
1814    /// For example here's a situation where a copy happens:
1815    ///
1816    /// * The `memory_reservation` setting is configured to 128KiB.
1817    /// * A WebAssembly linear memory starts with a single 64KiB page.
1818    /// * This memory can be grown by one page to contain the full 128KiB of
1819    ///   memory.
1820    /// * If grown by one more page, though, then a 192KiB allocation must be
1821    ///   made and the previous 128KiB of contents are copied into the new
1822    ///   allocation.
1823    ///
1824    /// This growth behavior can have a significant performance impact if lots
1825    /// of data needs to be copied on growth. Conversely if memory growth never
1826    /// needs to happen because the capacity will always be large enough then
1827    /// optimizations can be applied to cache the base pointer of linear memory.
1828    ///
1829    /// When memory is grown then the
1830    /// [`Config::memory_reservation_for_growth`] is used for the new
1831    /// memory allocation to have memory to grow into.
1832    ///
1833    /// When using the pooling allocator via [`PoolingAllocationConfig`] then
1834    /// memories are never allowed to move so requests for growth are instead
1835    /// rejected with an error.
1836    ///
1837    /// ## When this setting is not used
1838    ///
1839    /// This setting is ignored and unused when the initial size of linear
1840    /// memory is larger than this threshold. For example if this setting is set
1841    /// to 1MiB but a wasm module requires a 2MiB minimum allocation then this
1842    /// setting is ignored. In this situation the minimum size of memory will be
1843    /// allocated along with [`Config::memory_reservation_for_growth`]
1844    /// after it to grow into.
1845    ///
1846    /// That means that this value can be set to zero. That can be useful in
1847    /// benchmarking to see the overhead of bounds checks for example.
1848    /// Additionally it can be used to minimize the virtual memory allocated by
1849    /// Wasmtime.
1850    ///
1851    /// ## Default Value
1852    ///
1853    /// The default value for this property depends on the host platform. For
1854    /// 64-bit platforms there's lots of address space available, so the default
1855    /// configured here is 4GiB. When coupled with the default size of
1856    /// [`Config::memory_guard_size`] this means that 32-bit WebAssembly linear
1857    /// memories with 64KiB page sizes will skip almost all bounds checks by
1858    /// default.
1859    ///
1860    /// For 32-bit platforms this value defaults to 10MiB. This means that
1861    /// bounds checks will be required on 32-bit platforms.
1862    pub fn memory_reservation(&mut self, bytes: u64) -> &mut Self {
1863        self.tunables.memory_reservation = Some(bytes);
1864        self
1865    }
1866
1867    /// Indicates whether linear memories may relocate their base pointer at
1868    /// runtime.
1869    ///
1870    /// WebAssembly linear memories either have a maximum size that's explicitly
1871    /// listed in the type of a memory or inherently limited by the index type
1872    /// of the memory (e.g. 4GiB for 32-bit linear memories). Depending on how
1873    /// the linear memory is allocated (see [`Config::memory_reservation`]) it
1874    /// may be necessary to move the memory in the host's virtual address space
1875    /// during growth. This option controls whether this movement is allowed or
1876    /// not.
1877    ///
1878    /// An example of a linear memory needing to move is when
1879    /// [`Config::memory_reservation`] is 0 then a linear memory will be
1880    /// allocated as the minimum size of the memory plus
1881    /// [`Config::memory_reservation_for_growth`]. When memory grows beyond the
1882    /// reservation for growth then the memory needs to be relocated.
1883    ///
1884    /// When this option is set to `false` then it can have a number of impacts
1885    /// on how memories work at runtime:
1886    ///
1887    /// * Modules can be compiled with static knowledge the base pointer of
1888    ///   linear memory never changes to enable optimizations such as
1889    ///   loop invariant code motion (hoisting the base pointer out of a loop).
1890    ///
1891    /// * Memories cannot grow in excess of their original allocation. This
1892    ///   means that [`Config::memory_reservation`] and
1893    ///   [`Config::memory_reservation_for_growth`] may need tuning to ensure
1894    ///   the memory configuration works at runtime.
1895    ///
1896    /// The default value for this option is `true`.
1897    pub fn memory_may_move(&mut self, enable: bool) -> &mut Self {
1898        self.tunables.memory_may_move = Some(enable);
1899        self
1900    }
1901
1902    /// Configures the size, in bytes, of the guard region used at the end of a
1903    /// linear memory's address space reservation.
1904    ///
1905    /// > Note: this value has important performance ramifications, be sure to
1906    /// > understand what this value does before tweaking it and benchmarking.
1907    ///
1908    /// This setting controls how many bytes are guaranteed to be unmapped after
1909    /// the virtual memory allocation of a linear memory. When
1910    /// combined with sufficiently large values of
1911    /// [`Config::memory_reservation`] (e.g. 4GiB for 32-bit linear memories)
1912    /// then a guard region can be used to eliminate bounds checks in generated
1913    /// code.
1914    ///
1915    /// This setting additionally can be used to help deduplicate bounds checks
1916    /// in code that otherwise requires bounds checks. For example with a 4KiB
1917    /// guard region then a 64-bit linear memory which accesses addresses `x+8`
1918    /// and `x+16` only needs to perform a single bounds check on `x`. If that
1919    /// bounds check passes then the offset is guaranteed to either reside in
1920    /// linear memory or the guard region, resulting in deterministic behavior
1921    /// either way.
1922    ///
1923    /// ## How big should the guard be?
1924    ///
1925    /// In general, like with configuring [`Config::memory_reservation`], you
1926    /// probably don't want to change this value from the defaults. Removing
1927    /// bounds checks is dependent on a number of factors where the size of the
1928    /// guard region is only one piece of the equation. Other factors include:
1929    ///
1930    /// * [`Config::memory_reservation`]
1931    /// * The index type of the linear memory (e.g. 32-bit or 64-bit)
1932    /// * The page size of the linear memory
1933    /// * Other settings such as [`Config::signals_based_traps`]
1934    ///
1935    /// Embeddings using virtual memory almost always want at least some guard
1936    /// region, but otherwise changes from the default should be profiled
1937    /// locally to see the performance impact.
1938    ///
1939    /// ## Default
1940    ///
1941    /// The default value for this property is 32MiB on 64-bit platforms. This
1942    /// allows eliminating almost all bounds checks on loads/stores with an
1943    /// immediate offset of less than 32MiB. On 32-bit platforms this defaults
1944    /// to 64KiB.
1945    pub fn memory_guard_size(&mut self, bytes: u64) -> &mut Self {
1946        self.tunables.memory_guard_size = Some(bytes);
1947        self
1948    }
1949
1950    /// Configures the size, in bytes, of the extra virtual memory space
1951    /// reserved after a linear memory is relocated.
1952    ///
1953    /// This setting is used in conjunction with [`Config::memory_reservation`]
1954    /// to configure what happens after a linear memory is relocated in the host
1955    /// address space. If the initial size of a linear memory exceeds
1956    /// [`Config::memory_reservation`] or if it grows beyond that size
1957    /// throughout its lifetime then this setting will be used.
1958    ///
1959    /// When a linear memory is relocated it will initially look like this:
1960    ///
1961    /// ```text
1962    ///            memory.size
1963    ///                 │
1964    ///          ◄──────┴─────►
1965    /// ┌───────┬──────────────┬───────┐
1966    /// │ guard │  accessible  │ guard │
1967    /// └───────┴──────────────┴───────┘
1968    ///                         ◄──┬──►
1969    ///                            │
1970    ///                     memory_guard_size
1971    /// ```
1972    ///
1973    /// where `accessible` needs to be grown but there's no more memory to grow
1974    /// into. A new region of the virtual address space will be allocated that
1975    /// looks like this:
1976    ///
1977    /// ```text
1978    ///                           memory_reservation_for_growth
1979    ///                                       │
1980    ///            memory.size                │
1981    ///                 │                     │
1982    ///          ◄──────┴─────► ◄─────────────┴───────────►
1983    /// ┌───────┬──────────────┬───────────────────────────┬───────┐
1984    /// │ guard │  accessible  │ .. reserved for growth .. │ guard │
1985    /// └───────┴──────────────┴───────────────────────────┴───────┘
1986    ///                                                     ◄──┬──►
1987    ///                                                        │
1988    ///                                               memory_guard_size
1989    /// ```
1990    ///
1991    /// This means that up to `memory_reservation_for_growth` bytes can be
1992    /// allocated again before the entire linear memory needs to be moved again
1993    /// when another `memory_reservation_for_growth` bytes will be appended to
1994    /// the size of the allocation.
1995    ///
1996    /// Note that this is a currently simple heuristic for optimizing the growth
1997    /// of dynamic memories, primarily implemented for the memory64 proposal
1998    /// where the maximum size of memory is larger than 4GiB. This setting is
1999    /// unlikely to be a one-size-fits-all style approach and if you're an
2000    /// embedder running into issues with growth and are interested in having
2001    /// other growth strategies available here please feel free to [open an
2002    /// issue on the Wasmtime repository][issue]!
2003    ///
2004    /// [issue]: https://github.com/bytecodealliance/wasmtime/issues/new
2005    ///
2006    /// ## Default
2007    ///
2008    /// For 64-bit platforms this defaults to 2GiB, and for 32-bit platforms
2009    /// this defaults to 1MiB.
2010    pub fn memory_reservation_for_growth(&mut self, bytes: u64) -> &mut Self {
2011        self.tunables.memory_reservation_for_growth = Some(bytes);
2012        self
2013    }
2014
2015    /// Configures the initial size, in bytes, to be allocated for GC heaps.
2016    ///
2017    /// This is similar to [`Config::memory_reservation`] but applies to the GC
2018    /// heap rather than to linear memories. See that method for more details
2019    /// on what "reservation" means and the implications of this setting.
2020    ///
2021    /// ## Default
2022    ///
2023    /// If none of the `gc_heap_*` tunables are explicitly configured, they
2024    /// default to the same values as their `memory_*` counterparts. Otherwise,
2025    /// the default value for this property depends on the host platform: for
2026    /// 64-bit platforms this defaults to 4GiB, and for 32-bit platforms this
2027    /// defaults to 10MiB.
2028    pub fn gc_heap_reservation(&mut self, bytes: u64) -> &mut Self {
2029        self.tunables.gc_heap_reservation = Some(bytes);
2030        self
2031    }
2032
2033    /// Configures the size, in bytes, of the guard page region for GC heaps.
2034    ///
2035    /// This is similar to [`Config::memory_guard_size`] but applies to the GC
2036    /// heap rather than to linear memories. See that method for more details on
2037    /// what guard pages are and the implications of this setting.
2038    ///
2039    /// ## Default
2040    ///
2041    /// If none of the `gc_heap_*` tunables are explicitly configured, they
2042    /// default to the same values as their `memory_*` counterparts. Otherwise,
2043    /// the default value for this property is 32MiB on 64-bit platforms and
2044    /// 64KiB on 32-bit platforms.
2045    pub fn gc_heap_guard_size(&mut self, bytes: u64) -> &mut Self {
2046        self.tunables.gc_heap_guard_size = Some(bytes);
2047        self
2048    }
2049
2050    /// Configures the size, in bytes, of the extra virtual memory space
2051    /// reserved after a GC heap is relocated.
2052    ///
2053    /// This is similar to [`Config::memory_reservation_for_growth`] but applies
2054    /// to the GC heap rather than to linear memories. See that method for more
2055    /// details.
2056    ///
2057    /// ## Default
2058    ///
2059    /// If none of the `gc_heap_*` tunables are explicitly configured, they
2060    /// default to the same values as their `memory_*` counterparts. Otherwise,
2061    /// for 64-bit platforms this defaults to 2GiB, and for 32-bit platforms
2062    /// this defaults to 1MiB.
2063    pub fn gc_heap_reservation_for_growth(&mut self, bytes: u64) -> &mut Self {
2064        self.tunables.gc_heap_reservation_for_growth = Some(bytes);
2065        self
2066    }
2067
2068    /// Indicates whether GC heaps are allowed to be reallocated after initial
2069    /// allocation at runtime.
2070    ///
2071    /// This is similar to [`Config::memory_may_move`] but applies to the GC
2072    /// heap rather than to linear memories. See that method for more details.
2073    ///
2074    /// ## Default
2075    ///
2076    /// If none of the `gc_heap_*` tunables are explicitly configured, they
2077    /// default to the same values as their `memory_*` counterparts. Otherwise,
2078    /// the default value for this option is `true`.
2079    pub fn gc_heap_may_move(&mut self, enable: bool) -> &mut Self {
2080        self.tunables.gc_heap_may_move = Some(enable);
2081        self
2082    }
2083
2084    /// Indicates whether a guard region is present before allocations of
2085    /// linear memory.
2086    ///
2087    /// Guard regions before linear memories are never used during normal
2088    /// operation of WebAssembly modules, even if they have out-of-bounds
2089    /// loads. The only purpose for a preceding guard region in linear memory
2090    /// is extra protection against possible bugs in code generators like
2091    /// Cranelift. This setting does not affect performance in any way, but will
2092    /// result in larger virtual memory reservations for linear memories (it
2093    /// won't actually ever use more memory, just use more of the address
2094    /// space).
2095    ///
2096    /// The size of the guard region before linear memory is the same as the
2097    /// guard size that comes after linear memory, which is configured by
2098    /// [`Config::memory_guard_size`].
2099    ///
2100    /// ## Default
2101    ///
2102    /// This value defaults to `true`.
2103    pub fn guard_before_linear_memory(&mut self, enable: bool) -> &mut Self {
2104        self.tunables.guard_before_linear_memory = Some(enable);
2105        self
2106    }
2107
2108    /// Indicates whether to initialize tables lazily, so that instantiation
2109    /// is fast but indirect calls are a little slower. If false, tables
2110    /// are initialized eagerly during instantiation from any active element
2111    /// segments that apply to them.
2112    ///
2113    /// **Note** Disabling this option is not compatible with the Winch compiler.
2114    ///
2115    /// ## Default
2116    ///
2117    /// This value defaults to `true`.
2118    pub fn table_lazy_init(&mut self, table_lazy_init: bool) -> &mut Self {
2119        self.tunables.table_lazy_init = Some(table_lazy_init);
2120        self
2121    }
2122
2123    /// Configure the version information used in serialized and deserialized [`crate::Module`]s.
2124    /// This effects the behavior of [`crate::Module::serialize()`], as well as
2125    /// [`crate::Module::deserialize()`] and related functions.
2126    ///
2127    /// The default strategy is to use the wasmtime crate's Cargo package version.
2128    pub fn module_version(&mut self, strategy: ModuleVersionStrategy) -> Result<&mut Self> {
2129        match strategy {
2130            // This case requires special precondition for assertion in SerializedModule::to_bytes
2131            ModuleVersionStrategy::Custom(ref v) => {
2132                if v.as_bytes().len() > 255 {
2133                    bail!("custom module version cannot be more than 255 bytes: {v}");
2134                }
2135            }
2136            _ => {}
2137        }
2138        self.module_version = strategy;
2139        Ok(self)
2140    }
2141
2142    /// Configure whether wasmtime should compile a module using multiple
2143    /// threads.
2144    ///
2145    /// Disabling this will result in a single thread being used to compile
2146    /// the wasm bytecode.
2147    ///
2148    /// By default parallel compilation is enabled.
2149    #[cfg(feature = "parallel-compilation")]
2150    pub fn parallel_compilation(&mut self, parallel: bool) -> &mut Self {
2151        self.parallel_compilation = parallel;
2152        self
2153    }
2154
2155    /// Configures whether compiled artifacts will contain information to map
2156    /// native program addresses back to the original wasm module.
2157    ///
2158    /// This configuration option is `true` by default and, if enabled,
2159    /// generates the appropriate tables in compiled modules to map from native
2160    /// address back to wasm source addresses. This is used for displaying wasm
2161    /// program counters in backtraces as well as generating filenames/line
2162    /// numbers if so configured as well (and the original wasm module has DWARF
2163    /// debugging information present).
2164    pub fn generate_address_map(&mut self, generate: bool) -> &mut Self {
2165        self.tunables.generate_address_map = Some(generate);
2166        self
2167    }
2168
2169    /// Configures whether copy-on-write memory-mapped data is used to
2170    /// initialize a linear memory.
2171    ///
2172    /// Initializing linear memory via a copy-on-write mapping can drastically
2173    /// improve instantiation costs of a WebAssembly module because copying
2174    /// memory is deferred. Additionally if a page of memory is only ever read
2175    /// from WebAssembly and never written too then the same underlying page of
2176    /// data will be reused between all instantiations of a module meaning that
2177    /// if a module is instantiated many times this can lower the overall memory
2178    /// required needed to run that module.
2179    ///
2180    /// The main disadvantage of copy-on-write initialization, however, is that
2181    /// it may be possible for highly-parallel scenarios to be less scalable. If
2182    /// a page is read initially by a WebAssembly module then that page will be
2183    /// mapped to a read-only copy shared between all WebAssembly instances. If
2184    /// the same page is then written, however, then a private copy is created
2185    /// and swapped out from the read-only version. This also requires an [IPI],
2186    /// however, which can be a significant bottleneck in high-parallelism
2187    /// situations.
2188    ///
2189    /// This feature is only applicable when a WebAssembly module meets specific
2190    /// criteria to be initialized in this fashion, such as:
2191    ///
2192    /// * Only memories defined in the module can be initialized this way.
2193    /// * Data segments for memory must use statically known offsets.
2194    /// * Data segments for memory must all be in-bounds.
2195    ///
2196    /// Modules which do not meet these criteria will fall back to
2197    /// initialization of linear memory based on copying memory.
2198    ///
2199    /// This feature of Wasmtime is also platform-specific:
2200    ///
2201    /// * Linux - this feature is supported for all instances of [`Module`].
2202    ///   Modules backed by an existing mmap (such as those created by
2203    ///   [`Module::deserialize_file`]) will reuse that mmap to cow-initialize
2204    ///   memory. Other instance of [`Module`] may use the `memfd_create`
2205    ///   syscall to create an initialization image to `mmap`.
2206    /// * Unix (not Linux) - this feature is only supported when loading modules
2207    ///   from a precompiled file via [`Module::deserialize_file`] where there
2208    ///   is a file descriptor to use to map data into the process. Note that
2209    ///   the module must have been compiled with this setting enabled as well.
2210    /// * Windows - there is no support for this feature at this time. Memory
2211    ///   initialization will always copy bytes.
2212    ///
2213    /// By default this option is enabled.
2214    ///
2215    /// [`Module::deserialize_file`]: crate::Module::deserialize_file
2216    /// [`Module`]: crate::Module
2217    /// [IPI]: https://en.wikipedia.org/wiki/Inter-processor_interrupt
2218    pub fn memory_init_cow(&mut self, enable: bool) -> &mut Self {
2219        self.tunables.memory_init_cow = Some(enable);
2220        self
2221    }
2222
2223    /// A configuration option to force the usage of `memfd_create` on Linux to
2224    /// be used as the backing source for a module's initial memory image.
2225    ///
2226    /// When [`Config::memory_init_cow`] is enabled, which is enabled by
2227    /// default, module memory initialization images are taken from a module's
2228    /// original mmap if possible. If a precompiled module was loaded from disk
2229    /// this means that the disk's file is used as an mmap source for the
2230    /// initial linear memory contents. This option can be used to force, on
2231    /// Linux, that instead of using the original file on disk a new in-memory
2232    /// file is created with `memfd_create` to hold the contents of the initial
2233    /// image.
2234    ///
2235    /// This option can be used to avoid possibly loading the contents of memory
2236    /// from disk through a page fault. Instead with `memfd_create` the contents
2237    /// of memory are always in RAM, meaning that even page faults which
2238    /// initially populate a wasm linear memory will only work with RAM instead
2239    /// of ever hitting the disk that the original precompiled module is stored
2240    /// on.
2241    ///
2242    /// This option is disabled by default.
2243    pub fn force_memory_init_memfd(&mut self, enable: bool) -> &mut Self {
2244        self.force_memory_init_memfd = enable;
2245        self
2246    }
2247
2248    /// Configures whether or not a coredump should be generated and attached to
2249    /// the [`Error`](crate::Error) when a trap is raised.
2250    ///
2251    /// This option is disabled by default.
2252    #[cfg(feature = "coredump")]
2253    pub fn coredump_on_trap(&mut self, enable: bool) -> &mut Self {
2254        self.coredump_on_trap = enable;
2255        self
2256    }
2257
2258    /// Enables memory error checking for wasm programs.
2259    ///
2260    /// This option is disabled by default.
2261    ///
2262    /// # Panics
2263    ///
2264    /// Panics if this configuration's compiler was [disabled][Config::enable_compiler].
2265    #[cfg(any(feature = "cranelift", feature = "winch"))]
2266    pub fn wmemcheck(&mut self, enable: bool) -> &mut Self {
2267        self.wmemcheck = enable;
2268        self.compiler_config_mut().wmemcheck = enable;
2269        self
2270    }
2271
2272    /// Configures the "guaranteed dense image size" for copy-on-write
2273    /// initialized memories.
2274    ///
2275    /// When using the [`Config::memory_init_cow`] feature to initialize memory
2276    /// efficiently (which is enabled by default), compiled modules contain an
2277    /// image of the module's initial heap. If the module has a fairly sparse
2278    /// initial heap, with just a few data segments at very different offsets,
2279    /// this could result in a large region of zero bytes in the image. In
2280    /// other words, it's not very memory-efficient.
2281    ///
2282    /// We normally use a heuristic to avoid this: if less than half
2283    /// of the initialized range (first non-zero to last non-zero
2284    /// byte) of any memory in the module has pages with nonzero
2285    /// bytes, then we avoid creating a memory image for the entire module.
2286    ///
2287    /// However, if the embedder always needs the instantiation-time efficiency
2288    /// of copy-on-write initialization, and is otherwise carefully controlling
2289    /// parameters of the modules (for example, by limiting the maximum heap
2290    /// size of the modules), then it may be desirable to ensure a memory image
2291    /// is created even if this could go against the heuristic above. Thus, we
2292    /// add another condition: there is a size of initialized data region up to
2293    /// which we *always* allow a memory image. The embedder can set this to a
2294    /// known maximum heap size if they desire to always get the benefits of
2295    /// copy-on-write images.
2296    ///
2297    /// In the future we may implement a "best of both worlds"
2298    /// solution where we have a dense image up to some limit, and
2299    /// then support a sparse list of initializers beyond that; this
2300    /// would get most of the benefit of copy-on-write and pay the incremental
2301    /// cost of eager initialization only for those bits of memory
2302    /// that are out-of-bounds. However, for now, an embedder desiring
2303    /// fast instantiation should ensure that this setting is as large
2304    /// as the maximum module initial memory content size.
2305    ///
2306    /// By default this value is 16 MiB.
2307    pub fn memory_guaranteed_dense_image_size(&mut self, size_in_bytes: u64) -> &mut Self {
2308        self.memory_guaranteed_dense_image_size = size_in_bytes;
2309        self
2310    }
2311
2312    /// Whether to enable function inlining during compilation or not.
2313    ///
2314    /// This may result in faster execution at runtime, but adds additional
2315    /// compilation time. Inlining may also enlarge the size of compiled
2316    /// artifacts (for example, the size of the result of
2317    /// [`Engine::precompile_component`]).
2318    ///
2319    /// Inlining is not supported by all of Wasmtime's compilation strategies;
2320    /// currently, it only Cranelift supports it. This setting will be ignored
2321    /// when using a compilation strategy that does not support inlining, like
2322    /// Winch.
2323    ///
2324    /// The default value for this is `Inlining::No`.
2325    pub fn compiler_inlining(&mut self, inlining: Inlining) -> &mut Self {
2326        self.tunables.inlining = Some(inlining);
2327        self
2328    }
2329
2330    /// Returns the set of features that the currently selected compiler backend
2331    /// does not support at all and may panic on.
2332    ///
2333    /// Wasmtime strives to reject unknown modules or unsupported modules with
2334    /// first-class errors instead of panics. Not all compiler backends have the
2335    /// same level of feature support on all platforms as well. This method
2336    /// returns a set of features that the currently selected compiler
2337    /// configuration is known to not support and may panic on. This acts as a
2338    /// first-level filter on incoming wasm modules/configuration to fail-fast
2339    /// instead of panicking later on.
2340    ///
2341    /// Note that if a feature is not returned here it does not mean that the
2342    /// backend fully supports the proposal. Instead that means that the backend
2343    /// doesn't ever panic on the proposal, but errors during compilation may
2344    /// still be returned. This means that features listed here are definitely
2345    /// not supported at all, but features not listed here may still be
2346    /// partially supported. For example at the time of this writing the Winch
2347    /// backend partially supports simd so it's not listed here. Winch doesn't
2348    /// fully support simd but unimplemented instructions just return errors.
2349    fn compiler_panicking_wasm_features(&self) -> WasmFeatures {
2350        // First we compute the set of features that Wasmtime itself knows;
2351        // this is a sort of "maximal set" that we invert to create a set
2352        // of features we _definitely can't support_ because wasmtime
2353        // has never heard of them.
2354        let features_known_to_wasmtime = WasmFeatures::WASM3
2355            | WasmFeatures::SHARED_EVERYTHING_THREADS
2356            | WasmFeatures::COMPONENT_MODEL
2357            | WasmFeatures::CUSTOM_PAGE_SIZES
2358            | WasmFeatures::STACK_SWITCHING
2359            | WasmFeatures::WIDE_ARITHMETIC
2360            | WasmFeatures::CM_ASYNC
2361            | WasmFeatures::CM_ASYNC_STACKFUL
2362            | WasmFeatures::CM_MORE_ASYNC_BUILTINS
2363            | WasmFeatures::CM_THREADING
2364            | WasmFeatures::CM_ERROR_CONTEXT
2365            | WasmFeatures::CM_GC
2366            | WasmFeatures::CM_MAP
2367            | WasmFeatures::CM_FIXED_LENGTH_LISTS
2368            | WasmFeatures::CM_IMPLEMENTS;
2369
2370        #[allow(unused_mut, reason = "easier to avoid #[cfg]")]
2371        let mut unsupported = !features_known_to_wasmtime;
2372
2373        #[cfg(any(feature = "cranelift", feature = "winch"))]
2374        match self.compiler_config.as_ref().and_then(|c| c.strategy) {
2375            None | Some(Strategy::Cranelift) => {
2376                // Pulley at this time fundamentally doesn't support the
2377                // `threads` proposal, notably shared memory, because Rust can't
2378                // safely implement loads/stores in the face of shared memory.
2379                // Stack switching is not implemented, either.
2380                if self.compiler_target().is_pulley() {
2381                    unsupported |= WasmFeatures::THREADS;
2382                    unsupported |= WasmFeatures::STACK_SWITCHING;
2383                }
2384
2385                use target_lexicon::*;
2386                match self.compiler_target() {
2387                    Triple {
2388                        architecture: Architecture::X86_64 | Architecture::X86_64h,
2389                        operating_system:
2390                            OperatingSystem::Linux
2391                            | OperatingSystem::MacOSX(_)
2392                            | OperatingSystem::Darwin(_),
2393                        ..
2394                    } => {
2395                        // Stack switching supported on (non-Pulley) Cranelift.
2396                    }
2397
2398                    _ => {
2399                        // On platforms other than x64 Unix-like, we don't
2400                        // support stack switching.
2401                        unsupported |= WasmFeatures::STACK_SWITCHING;
2402                    }
2403                }
2404            }
2405            Some(Strategy::Winch) => {
2406                unsupported |= WasmFeatures::GC
2407                    | WasmFeatures::FUNCTION_REFERENCES
2408                    | WasmFeatures::RELAXED_SIMD
2409                    | WasmFeatures::TAIL_CALL
2410                    | WasmFeatures::GC_TYPES
2411                    | WasmFeatures::EXCEPTIONS
2412                    | WasmFeatures::LEGACY_EXCEPTIONS
2413                    | WasmFeatures::STACK_SWITCHING
2414                    | WasmFeatures::CM_ASYNC;
2415                match self.compiler_target().architecture {
2416                    target_lexicon::Architecture::Aarch64(_) => {
2417                        unsupported |= WasmFeatures::THREADS;
2418                    }
2419
2420                    // Winch doesn't support other non-x64 architectures at this
2421                    // time either but will return an first-class error for
2422                    // them.
2423                    _ => {}
2424                }
2425            }
2426            Some(Strategy::Auto) => unreachable!(),
2427        }
2428        unsupported
2429    }
2430
2431    /// Calculates the set of features that are enabled for this `Config`.
2432    ///
2433    /// This is a bit of a subtle function which takes into account inputs such
2434    /// as the default set of features Wasmtime has enabled, the currently
2435    /// enabled compiler, the currently enabled target, compile-time crate
2436    /// features, and explicitly configured wasm proposals. This function does
2437    /// not return a fixed set of all proposals in all cases as it's a bit more
2438    /// nuanced than that.
2439    ///
2440    /// This method internally will start with an empty set of features to
2441    /// avoid being tied to wasmparser's defaults. Next Wasmtime's set of
2442    /// default features are added to this set, some of which are conditional
2443    /// depending on crate features. Finally explicitly requested features via
2444    /// `wasm_*` methods on `Config` are applied. Everything is then validated
2445    /// later in `Config::validate`.
2446    ///
2447    /// Note that the validation later on in `Config::validate` is a crucial
2448    /// step here. The returned features here might include features unsupported
2449    /// at compile time or unsupported by the selected compiler. In that case
2450    /// `Config::validate` will present a first-class error message indicating
2451    /// what's going on, and users should in theory be able to understand "ok
2452    /// yeah that's why I can't enable that feature here".
2453    fn features(&self) -> WasmFeatures {
2454        // Start with an empty set of wasm features. This notably decouples
2455        // features in Wasmtime from features in wasmparser as the two are
2456        // generally on different timelines.
2457        let mut features = WasmFeatures::empty();
2458
2459        // Next add in all on-by-default features that Wasmtime has which are
2460        // subject to the criteria at
2461        // https://docs.wasmtime.dev/contributing-implementing-wasm-proposals.html
2462        // and https://docs.wasmtime.dev/stability-wasm-proposals.html.
2463        //
2464        // Note that the first entry here, `WASM3`, is a fixed feature set that
2465        // won't change over time in wasmparser which represents the union of
2466        // all on-by-default features in Wasmtime. Also note that this is
2467        // further refined in the conditional section below based on crate
2468        // features.
2469        features |= WasmFeatures::WASM3;
2470
2471        // features |= WasmFeatures::YOUR_WASM_FEATURE;
2472        // ...
2473
2474        // NB: if you add a feature above this line please double-check
2475        // https://docs.wasmtime.dev/stability-wasm-proposals.html
2476        // to ensure all requirements are met and/or update the documentation
2477        // there too.
2478
2479        // Next configure some features further based on compile-time features
2480        // of the wasmtime crate itself. For example if "gc" is disabled then
2481        // `GC_TYPES` are disabled (a wasmparser pseudo-feature) as well as
2482        // exceptions, but reference-types is still available (e.g. new
2483        // encodings/types/etc).
2484        //
2485        // These features are all "on by default" in effect but dependent on
2486        // compile-time support being available.
2487        features.set(WasmFeatures::GC_TYPES, cfg!(feature = "gc"));
2488        features.set(WasmFeatures::EXCEPTIONS, cfg!(feature = "gc"));
2489        features.set(WasmFeatures::THREADS, cfg!(feature = "threads"));
2490        features.set(
2491            WasmFeatures::COMPONENT_MODEL,
2492            cfg!(feature = "component-model"),
2493        );
2494        features.set(
2495            WasmFeatures::CM_ASYNC,
2496            self.tunables
2497                .concurrency_support
2498                .unwrap_or(cfg!(feature = "component-model-async")),
2499        );
2500
2501        // Next disable any features which the current compiler/target do not
2502        // support. This handles cases where Winch, for example, doesn't
2503        // implement a feature yet but Cranelift does. Or maybe Cranelift only
2504        // supports one particular platform and not others. Things like that.
2505        features = features & !self.compiler_panicking_wasm_features();
2506
2507        // And, finally, process all explicitly enabled/disabled features on
2508        // behalf of the embedder's frobbing `Config::wasm_*`. These have the
2509        // highest priority since they were explicitly requested.
2510        debug_assert!((self.enabled_features & self.disabled_features).is_empty());
2511        features &= !self.disabled_features;
2512        features |= self.enabled_features;
2513
2514        features
2515    }
2516
2517    /// Returns the configured compiler target for this `Config`.
2518    pub(crate) fn compiler_target(&self) -> target_lexicon::Triple {
2519        // If a target is explicitly configured, always use that.
2520        if let Some(target) = self.target.clone() {
2521            return target;
2522        }
2523
2524        // If the `build.rs` script determined that this platform uses pulley by
2525        // default, then use Pulley.
2526        if cfg!(default_target_pulley) {
2527            return target_lexicon::Triple::pulley_host();
2528        }
2529
2530        // And at this point the target is for sure the host.
2531        target_lexicon::Triple::host()
2532    }
2533
2534    /// Returns `true` if any of the `gc_heap_*` tunables have been explicitly
2535    /// configured.
2536    fn any_gc_heap_tunables_configured(&self) -> bool {
2537        self.tunables.gc_heap_reservation.is_some()
2538            || self.tunables.gc_heap_guard_size.is_some()
2539            || self.tunables.gc_heap_reservation_for_growth.is_some()
2540            || self.tunables.gc_heap_may_move.is_some()
2541    }
2542
2543    pub(crate) fn validate(&self) -> Result<(Tunables, WasmFeatures)> {
2544        let features = self.features();
2545
2546        // First validate that the selected compiler backend and configuration
2547        // supports the set of `features` that are enabled. This will help
2548        // provide more first class errors instead of panics about unsupported
2549        // features and configurations.
2550        let unsupported = features & self.compiler_panicking_wasm_features();
2551        if !unsupported.is_empty() {
2552            for flag in WasmFeatures::FLAGS.iter() {
2553                if !unsupported.contains(*flag.value()) {
2554                    continue;
2555                }
2556                bail!(
2557                    "the wasm_{} feature is not supported on this compiler configuration",
2558                    flag.name().to_lowercase()
2559                );
2560            }
2561
2562            panic!("should have returned an error by now")
2563        }
2564
2565        if self.max_wasm_stack > self.async_stack_size {
2566            bail!("max_wasm_stack size cannot exceed the async_stack_size");
2567        }
2568        if self.max_wasm_stack == 0 {
2569            bail!("max_wasm_stack size cannot be zero");
2570        }
2571        if !cfg!(feature = "wmemcheck") && self.wmemcheck {
2572            bail!("wmemcheck (memory checker) was requested but is not enabled in this build");
2573        }
2574
2575        if !cfg!(feature = "gc") && features.gc_types() {
2576            bail!("support for GC was disabled at compile time")
2577        }
2578
2579        if !cfg!(feature = "gc") && features.contains(WasmFeatures::EXCEPTIONS) {
2580            bail!("exceptions support requires garbage collection (GC) to be enabled in the build");
2581        }
2582
2583        match &self.rr_config {
2584            #[cfg(feature = "rr")]
2585            RRConfig::Recording | RRConfig::Replaying => {
2586                self.validate_rr_determinism_conflicts()?;
2587            }
2588            RRConfig::None => {}
2589        };
2590
2591        let mut tunables = Tunables::default_for_target(&self.compiler_target())?;
2592
2593        // By default this is enabled with the Cargo feature, and if the feature
2594        // is missing this is disabled.
2595        tunables.concurrency_support = cfg!(feature = "component-model-async");
2596
2597        #[cfg(feature = "rr")]
2598        {
2599            tunables.recording = matches!(self.rr_config, RRConfig::Recording);
2600        }
2601
2602        // If no target is explicitly specified then further refine `tunables`
2603        // for the configuration of this host depending on what platform
2604        // features were found available at compile time. This means that anyone
2605        // cross-compiling for a customized host will need to further refine
2606        // compilation options.
2607        if self.target.is_none() {
2608            // If this platform doesn't have native signals then change some
2609            // defaults to account for that. Note that VM guards are turned off
2610            // here because that's primarily a feature of eliding
2611            // bounds-checks.
2612            if !cfg!(has_native_signals) {
2613                tunables.signals_based_traps = cfg!(has_native_signals);
2614                tunables.memory_guard_size = 0;
2615                tunables.gc_heap_guard_size = 0;
2616            }
2617
2618            // When virtual memory is not available use slightly different
2619            // defaults for tunables to be more amenable to `MallocMemory`.
2620            // Note that these can still be overridden by config options.
2621            if !cfg!(has_virtual_memory) {
2622                tunables.memory_reservation = 0;
2623                tunables.memory_reservation_for_growth = 1 << 20; // 1MB
2624                tunables.memory_init_cow = false;
2625                tunables.gc_heap_reservation = 0;
2626                tunables.gc_heap_reservation_for_growth = 1 << 20; // 1MB
2627            }
2628        }
2629
2630        // If guest-debugging is enabled, we must disable
2631        // signals-based traps. Do this before we process the user's
2632        // provided tunables settings so we can detect a conflict with
2633        // an explicit request to use signals-based traps.
2634        #[cfg(feature = "debug")]
2635        if self.tunables.debug_guest == Some(true) {
2636            tunables.signals_based_traps = false;
2637        }
2638
2639        // Inlining currently falls over with the `stack_switch` instruction.
2640        #[cfg(any(feature = "cranelift", feature = "winch"))]
2641        if features.contains(WasmFeatures::STACK_SWITCHING) {
2642            if let Some(inlining) = self.tunables.inlining
2643                && inlining != Inlining::No
2644            {
2645                bail!("cannot enable compiler inlining when stack switching is enabled");
2646            }
2647            tunables.inlining = Inlining::No;
2648        }
2649
2650        self.tunables.configure(&mut tunables);
2651
2652        // If no GC heap tunables are explicitly configured, copy the memory
2653        // tunables' configured values so that GC heaps default to the same
2654        // configuration as linear memories.
2655        if !self.any_gc_heap_tunables_configured() {
2656            tunables.gc_heap_reservation = tunables.memory_reservation;
2657            tunables.gc_heap_guard_size = tunables.memory_guard_size;
2658            tunables.gc_heap_reservation_for_growth = tunables.memory_reservation_for_growth;
2659            tunables.gc_heap_may_move = tunables.memory_may_move;
2660        }
2661
2662        // If we're going to compile with winch, we must use the winch calling convention.
2663        #[cfg(any(feature = "cranelift", feature = "winch"))]
2664        {
2665            tunables.winch_callable = self
2666                .compiler_config
2667                .as_ref()
2668                .is_some_and(|c| c.strategy == Some(Strategy::Winch));
2669        }
2670
2671        tunables.collector = if features.gc_types() {
2672            #[cfg(feature = "gc")]
2673            {
2674                use wasmtime_environ::Collector as EnvCollector;
2675                Some(match self.collector.try_not_auto()? {
2676                    Collector::DeferredReferenceCounting => EnvCollector::DeferredReferenceCounting,
2677                    Collector::Null => EnvCollector::Null,
2678                    Collector::Copying => EnvCollector::Copying,
2679                    Collector::Auto => unreachable!(),
2680                })
2681            }
2682            #[cfg(not(feature = "gc"))]
2683            bail!("cannot use GC types: the `gc` feature was disabled at compile time")
2684        } else {
2685            None
2686        };
2687
2688        if tunables.debug_guest {
2689            ensure!(
2690                cfg!(feature = "debug"),
2691                "debug instrumentation support was disabled at compile time"
2692            );
2693            ensure!(
2694                !tunables.signals_based_traps,
2695                "cannot use signals-based traps with guest debugging enabled"
2696            );
2697        }
2698
2699        // Concurrency support is required for some component model features.
2700        let requires_concurrency = WasmFeatures::CM_ASYNC
2701            | WasmFeatures::CM_MORE_ASYNC_BUILTINS
2702            | WasmFeatures::CM_ASYNC_STACKFUL
2703            | WasmFeatures::CM_THREADING
2704            | WasmFeatures::CM_ERROR_CONTEXT;
2705        if tunables.concurrency_support && !cfg!(feature = "component-model-async") {
2706            bail!(
2707                "concurrency support was requested but was not \
2708                 compiled into this build of Wasmtime"
2709            )
2710        }
2711        if !tunables.concurrency_support && features.intersects(requires_concurrency) {
2712            bail!(
2713                "concurrency support must be enabled to use the component \
2714                 model async or threading features"
2715            )
2716        }
2717
2718        // If the pooling allocator is used and GC is enabled, check that
2719        // memories and the GC heap are configured identically, since the
2720        // pooling allocator can't support differently-configured heaps.
2721        #[cfg(feature = "pooling-allocator")]
2722        if matches!(
2723            &self.allocation_strategy,
2724            InstanceAllocationStrategy::Pooling(_)
2725        ) && tunables.collector.is_some()
2726        {
2727            if tunables.memory_reservation != tunables.gc_heap_reservation {
2728                bail!(
2729                    "when using the pooling allocator with GC, `memory_reservation` ({}) \
2730                     and `gc_heap_reservation` ({}) must be the same",
2731                    tunables.memory_reservation,
2732                    tunables.gc_heap_reservation,
2733                );
2734            }
2735            if tunables.memory_guard_size != tunables.gc_heap_guard_size {
2736                bail!(
2737                    "when using the pooling allocator with GC, `memory_guard_size` ({}) \
2738                     and `gc_heap_guard_size` ({}) must be the same",
2739                    tunables.memory_guard_size,
2740                    tunables.gc_heap_guard_size,
2741                );
2742            }
2743            if tunables.memory_reservation_for_growth != tunables.gc_heap_reservation_for_growth {
2744                bail!(
2745                    "when using the pooling allocator with GC, \
2746                     `memory_reservation_for_growth` ({}) and \
2747                     `gc_heap_reservation_for_growth` ({}) must be the same",
2748                    tunables.memory_reservation_for_growth,
2749                    tunables.gc_heap_reservation_for_growth,
2750                );
2751            }
2752            if tunables.memory_may_move != tunables.gc_heap_may_move {
2753                bail!(
2754                    "when using the pooling allocator with GC, `memory_may_move` ({}) \
2755                     and `gc_heap_may_move` ({}) must be the same",
2756                    tunables.memory_may_move,
2757                    tunables.gc_heap_may_move,
2758                );
2759            }
2760        }
2761
2762        if tunables.debug_native && !tunables.debug_symbols {
2763            bail!("cannot enable native debug info while debug symbols are disabled");
2764        }
2765
2766        Ok((tunables, features))
2767    }
2768
2769    #[cfg(feature = "runtime")]
2770    pub(crate) fn build_allocator(
2771        &self,
2772        tunables: &Tunables,
2773    ) -> Result<Box<dyn InstanceAllocator + Send + Sync>> {
2774        let _ = tunables;
2775
2776        match &self.allocation_strategy {
2777            InstanceAllocationStrategy::OnDemand => {
2778                let mut _allocator = try_new::<Box<_>>(OnDemandInstanceAllocator::new(
2779                    self.mem_creator.clone(),
2780                    self.async_stack_size,
2781                    self.async_stack_zeroing,
2782                ))?;
2783                #[cfg(feature = "async")]
2784                if let Some(stack_creator) = &self.stack_creator {
2785                    _allocator.set_stack_creator(stack_creator.clone());
2786                }
2787                Ok(_allocator as _)
2788            }
2789            #[cfg(feature = "pooling-allocator")]
2790            InstanceAllocationStrategy::Pooling(config) => {
2791                let mut config = config.clone();
2792                let _ = &mut config;
2793                #[cfg(feature = "async")]
2794                {
2795                    config.stack_size = self.async_stack_size;
2796                    config.async_stack_zeroing = self.async_stack_zeroing;
2797                }
2798                let allocator = try_new::<Box<_>>(
2799                    crate::runtime::vm::PoolingInstanceAllocator::new(&config, tunables)?,
2800                )?;
2801                Ok(allocator as _)
2802            }
2803        }
2804    }
2805
2806    #[cfg(feature = "runtime")]
2807    pub(crate) fn build_gc_runtime(&self) -> Result<Option<Arc<dyn GcRuntime>>> {
2808        if !self.features().gc_types() {
2809            return Ok(None);
2810        }
2811
2812        #[cfg(not(feature = "gc"))]
2813        bail!("cannot create a GC runtime: the `gc` feature was disabled at compile time");
2814
2815        #[cfg(feature = "gc")]
2816        #[cfg_attr(
2817            not(any(feature = "gc-null", feature = "gc-drc", feature = "gc-copying")),
2818            expect(unreachable_code, reason = "definitions known to be dummy")
2819        )]
2820        {
2821            Ok(Some(match self.collector.try_not_auto()? {
2822                #[cfg(feature = "gc-drc")]
2823                Collector::DeferredReferenceCounting => {
2824                    try_new::<Arc<_>>(crate::runtime::vm::DrcCollector::default())? as _
2825                }
2826                #[cfg(not(feature = "gc-drc"))]
2827                Collector::DeferredReferenceCounting => unreachable!(),
2828
2829                #[cfg(feature = "gc-null")]
2830                Collector::Null => {
2831                    try_new::<Arc<_>>(crate::runtime::vm::NullCollector::default())? as _
2832                }
2833                #[cfg(not(feature = "gc-null"))]
2834                Collector::Null => unreachable!(),
2835
2836                #[cfg(feature = "gc-copying")]
2837                Collector::Copying => {
2838                    try_new::<Arc<_>>(crate::runtime::vm::CopyingCollector::default())? as _
2839                }
2840                #[cfg(not(feature = "gc-copying"))]
2841                Collector::Copying => unreachable!(),
2842
2843                Collector::Auto => unreachable!(),
2844            }))
2845        }
2846    }
2847
2848    #[cfg(feature = "runtime")]
2849    pub(crate) fn build_profiler(&self) -> Result<Box<dyn ProfilingAgent>> {
2850        Ok(match self.profiling_strategy {
2851            ProfilingStrategy::PerfMap => profiling_agent::new_perfmap()?,
2852            ProfilingStrategy::JitDump => profiling_agent::new_jitdump()?,
2853            ProfilingStrategy::VTune => profiling_agent::new_vtune()?,
2854            ProfilingStrategy::None => profiling_agent::new_null(),
2855            ProfilingStrategy::Pulley => profiling_agent::new_pulley()?,
2856        })
2857    }
2858
2859    #[cfg(any(feature = "cranelift", feature = "winch"))]
2860    pub(crate) fn build_compiler(
2861        mut self,
2862        tunables: &mut Tunables,
2863        features: WasmFeatures,
2864    ) -> Result<(Self, Box<dyn wasmtime_environ::Compiler>)> {
2865        let target = self.compiler_target();
2866
2867        // The target passed to the builders below is an `Option<Triple>` where
2868        // `None` represents the current host with CPU features inferred from
2869        // the host's CPU itself. The `target` above is not an `Option`, so
2870        // switch it to `None` in the case that a target wasn't explicitly
2871        // specified (which indicates no feature inference) and the target
2872        // matches the host.
2873        let target_for_builder =
2874            if self.target.is_none() && target == target_lexicon::Triple::host() {
2875                None
2876            } else {
2877                Some(target.clone())
2878            };
2879
2880        let mut compiler = match self.compiler_config_mut().strategy {
2881            #[cfg(feature = "cranelift")]
2882            Some(Strategy::Cranelift) => wasmtime_cranelift::builder(target_for_builder)?,
2883            #[cfg(not(feature = "cranelift"))]
2884            Some(Strategy::Cranelift) => bail!("cranelift support not compiled in"),
2885            #[cfg(feature = "winch")]
2886            Some(Strategy::Winch) => wasmtime_winch::builder(target_for_builder)?,
2887            #[cfg(not(feature = "winch"))]
2888            Some(Strategy::Winch) => bail!("winch support not compiled in"),
2889
2890            None | Some(Strategy::Auto) => unreachable!(),
2891        };
2892
2893        if let Some(path) = &self.compiler_config_mut().clif_dir {
2894            compiler.clif_dir(path)?;
2895        }
2896
2897        // If probestack is enabled for a target, Wasmtime will always use the
2898        // inline strategy which doesn't require us to define a `__probestack`
2899        // function or similar.
2900        self.compiler_config_mut().settings.insert(
2901            "probestack_strategy".into(),
2902            ("inline".into(), UserSpecified::No),
2903        );
2904
2905        // We enable stack probing by default on all targets.
2906        // This is required on Windows because of the way Windows
2907        // commits its stacks, but it's also a good idea on other
2908        // platforms to ensure guard pages are hit for large frame
2909        // sizes.
2910        self.compiler_config_mut()
2911            .flags
2912            .insert("enable_probestack".into(), UserSpecified::No);
2913
2914        // The current wasm multivalue implementation depends on this.
2915        // FIXME(#9510) handle this in wasmtime-cranelift instead.
2916        self.compiler_config_mut()
2917            .flags
2918            .insert("enable_multi_ret_implicit_sret".into(), UserSpecified::No);
2919
2920        if let Some(unwind_requested) = self.native_unwind_info {
2921            if !self
2922                .compiler_config_mut()
2923                .ensure_setting_unset_or_given("unwind_info", &unwind_requested.to_string())
2924            {
2925                bail!(
2926                    "incompatible settings requested for Cranelift and Wasmtime `unwind-info` settings"
2927                );
2928            }
2929        }
2930
2931        if target.operating_system == target_lexicon::OperatingSystem::Windows {
2932            if !self
2933                .compiler_config_mut()
2934                .ensure_setting_unset_or_given("unwind_info", "true")
2935            {
2936                bail!("`native_unwind_info` cannot be disabled on Windows");
2937            }
2938        }
2939
2940        // We require frame pointers for correct stack walking, which is safety
2941        // critical in the presence of reference types, and otherwise it is just
2942        // really bad developer experience to get wrong.
2943        self.compiler_config_mut().settings.insert(
2944            "preserve_frame_pointers".into(),
2945            ("true".into(), UserSpecified::No),
2946        );
2947
2948        if !tunables.signals_based_traps {
2949            let mut ok = self
2950                .compiler_config_mut()
2951                .ensure_setting_unset_or_given("enable_table_access_spectre_mitigation", "false");
2952            ok = ok
2953                && self.compiler_config_mut().ensure_setting_unset_or_given(
2954                    "enable_heap_access_spectre_mitigation",
2955                    "false",
2956                );
2957
2958            // Right now spectre-mitigated bounds checks will load from zero so
2959            // if host-based signal handlers are disabled then that's a mismatch
2960            // and doesn't work right now. Fixing this will require more thought
2961            // of how to implement the bounds check in spectre-only mode.
2962            if !ok {
2963                bail!(
2964                    "when signals-based traps are disabled then spectre \
2965                     mitigations must also be disabled"
2966                );
2967            }
2968        }
2969
2970        if features.contains(WasmFeatures::RELAXED_SIMD) && !features.contains(WasmFeatures::SIMD) {
2971            bail!("cannot disable the simd proposal but enable the relaxed simd proposal");
2972        }
2973
2974        if features.contains(WasmFeatures::STACK_SWITCHING) {
2975            use target_lexicon::OperatingSystem;
2976            let model = match target.operating_system {
2977                OperatingSystem::Windows => "update_windows_tib",
2978                OperatingSystem::Linux
2979                | OperatingSystem::MacOSX(_)
2980                | OperatingSystem::Darwin(_) => "basic",
2981                _ => bail!("stack-switching feature not supported on this platform "),
2982            };
2983
2984            if !self
2985                .compiler_config_mut()
2986                .ensure_setting_unset_or_given("stack_switch_model", model)
2987            {
2988                bail!(
2989                    "compiler option 'stack_switch_model' must be set to '{model}' on this platform"
2990                );
2991            }
2992        }
2993
2994        // Apply compiler settings and flags
2995        compiler.set_tunables(tunables.clone())?;
2996        for (k, (v, _)) in self.compiler_config_mut().settings.iter() {
2997            compiler.set(k, v)?;
2998        }
2999        for (flag, _) in self.compiler_config_mut().flags.iter() {
3000            compiler.enable(flag)?;
3001        }
3002        *tunables = compiler.tunables().cloned().unwrap();
3003
3004        #[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
3005        if let Some(cache_store) = &self.compiler_config_mut().cache_store {
3006            compiler.enable_incremental_compilation(cache_store.clone())?;
3007        }
3008
3009        compiler.wmemcheck(self.compiler_config_mut().wmemcheck);
3010
3011        Ok((self, compiler.build()?))
3012    }
3013
3014    /// Internal setting for whether adapter modules for components will have
3015    /// extra WebAssembly instructions inserted performing more debug checks
3016    /// then are necessary.
3017    #[cfg(feature = "component-model")]
3018    pub fn debug_adapter_modules(&mut self, debug: bool) -> &mut Self {
3019        self.tunables.debug_adapter_modules = Some(debug);
3020        self
3021    }
3022
3023    /// Enables clif output when compiling a WebAssembly module.
3024    #[cfg(any(feature = "cranelift", feature = "winch"))]
3025    pub fn emit_clif(&mut self, path: &Path) -> &mut Self {
3026        self.compiler_config_mut().clif_dir = Some(path.to_path_buf());
3027        self
3028    }
3029
3030    /// Configures whether, when on macOS, Mach ports are used for exception
3031    /// handling instead of traditional Unix-based signal handling.
3032    ///
3033    /// WebAssembly traps in Wasmtime are implemented with native faults, for
3034    /// example a `SIGSEGV` will occur when a WebAssembly guest accesses
3035    /// out-of-bounds memory. Handling this can be configured to either use Unix
3036    /// signals or Mach ports on macOS. By default Mach ports are used.
3037    ///
3038    /// Mach ports enable Wasmtime to work by default with foreign
3039    /// error-handling systems such as breakpad which also use Mach ports to
3040    /// handle signals. In this situation Wasmtime will continue to handle guest
3041    /// faults gracefully while any non-guest faults will get forwarded to
3042    /// process-level handlers such as breakpad. Some more background on this
3043    /// can be found in #2456.
3044    ///
3045    /// A downside of using mach ports, however, is that they don't interact
3046    /// well with `fork()`. Forking a Wasmtime process on macOS will produce a
3047    /// child process that cannot successfully run WebAssembly. In this
3048    /// situation traditional Unix signal handling should be used as that's
3049    /// inherited and works across forks.
3050    ///
3051    /// If your embedding wants to use a custom error handler which leverages
3052    /// Mach ports and you additionally wish to `fork()` the process and use
3053    /// Wasmtime in the child process that's not currently possible. Please
3054    /// reach out to us if you're in this bucket!
3055    ///
3056    /// This option defaults to `true`, using Mach ports by default.
3057    pub fn macos_use_mach_ports(&mut self, mach_ports: bool) -> &mut Self {
3058        self.macos_use_mach_ports = mach_ports;
3059        self
3060    }
3061
3062    /// Configures an embedder-provided function, `detect`, which is used to
3063    /// determine if an ISA-specific feature is available on the current host.
3064    ///
3065    /// This function is used to verify that any features enabled for a compiler
3066    /// backend, such as AVX support on x86\_64, are also available on the host.
3067    /// It is undefined behavior to execute an AVX instruction on a host that
3068    /// doesn't support AVX instructions, for example.
3069    ///
3070    /// When the `std` feature is active on this crate then this function is
3071    /// configured to a default implementation that uses the standard library's
3072    /// feature detection. When the `std` feature is disabled then there is no
3073    /// default available and this method must be called to configure a feature
3074    /// probing function.
3075    ///
3076    /// The `detect` function provided is given a string name of an ISA feature.
3077    /// The function should then return:
3078    ///
3079    /// * `Some(true)` - indicates that the feature was found on the host and it
3080    ///   is supported.
3081    /// * `Some(false)` - the feature name was recognized but it was not
3082    ///   detected on the host, for example the CPU is too old.
3083    /// * `None` - the feature name was not recognized and it's not known
3084    ///   whether it's on the host or not.
3085    ///
3086    /// Feature names passed to `detect` match the same feature name used in the
3087    /// Rust standard library. For example `"sse4.2"` is used on x86\_64.
3088    ///
3089    /// # Unsafety
3090    ///
3091    /// This function is `unsafe` because it is undefined behavior to execute
3092    /// instructions that a host does not support. This means that the result of
3093    /// `detect` must be correct for memory safe execution at runtime.
3094    pub unsafe fn detect_host_feature(&mut self, detect: fn(&str) -> Option<bool>) -> &mut Self {
3095        self.detect_host_feature = Some(detect);
3096        self
3097    }
3098
3099    /// Configures Wasmtime to not use signals-based trap handlers, for example
3100    /// disables `SIGILL` and `SIGSEGV` handler registration on Unix platforms.
3101    ///
3102    /// > **Note:** this option has important performance ramifications, be sure
3103    /// > to understand the implications. Wasm programs have been measured to
3104    /// > run up to 2x slower when signals-based traps are disabled.
3105    ///
3106    /// Wasmtime will by default leverage signals-based trap handlers (or the
3107    /// platform equivalent, for example "vectored exception handlers" on
3108    /// Windows) to make generated code more efficient. For example, when
3109    /// Wasmtime can use signals-based traps, it can elide explicit bounds
3110    /// checks for Wasm linear memory accesses, instead relying on virtual
3111    /// memory guard pages to raise a `SIGSEGV` (on Unix) for out-of-bounds
3112    /// accesses, which Wasmtime's runtime then catches and handles. Another
3113    /// example is divide-by-zero: with signals-based traps, Wasmtime can let
3114    /// the hardware raise a trap when the divisor is zero. Without
3115    /// signals-based traps, Wasmtime must explicitly emit additional
3116    /// instructions to check for zero and conditionally branch to a trapping
3117    /// code path.
3118    ///
3119    /// Some environments however may not have access to signal handlers. For
3120    /// example embedded scenarios may not support virtual memory. Other
3121    /// environments where Wasmtime is embedded within the surrounding
3122    /// environment may require that new signal handlers aren't registered due
3123    /// to the global nature of signal handlers. This option exists to disable
3124    /// the signal handler registration when required for these scenarios.
3125    ///
3126    /// When signals-based trap handlers are disabled, then Wasmtime and its
3127    /// generated code will *never* rely on segfaults or other
3128    /// signals. Generated code will be slower because bounds must be explicitly
3129    /// checked along with other conditions like division by zero.
3130    ///
3131    /// The following additional factors can also affect Wasmtime's ability to
3132    /// elide explicit bounds checks and leverage signals-based traps:
3133    ///
3134    /// * The [`Config::memory_reservation`] and [`Config::memory_guard_size`]
3135    ///   settings
3136    /// * The index type of the linear memory (e.g. 32-bit or 64-bit)
3137    /// * The page size of the linear memory
3138    ///
3139    /// When this option is disabled, the
3140    /// `enable_heap_access_spectre_mitigation` and
3141    /// `enable_table_access_spectre_mitigation` Cranelift settings must also be
3142    /// disabled. This means that generated code must have spectre mitigations
3143    /// disabled. This is because spectre mitigations rely on faults from
3144    /// loading from the null address to implement bounds checks.
3145    ///
3146    /// This option defaults to `true`: signals-based trap handlers are enabled
3147    /// by default.
3148    ///
3149    /// > **Note:** Disabling this option is not compatible with the Winch
3150    /// > compiler.
3151    pub fn signals_based_traps(&mut self, enable: bool) -> &mut Self {
3152        self.tunables.signals_based_traps = Some(enable);
3153        self
3154    }
3155
3156    /// Enable/disable GC support in Wasmtime entirely.
3157    ///
3158    /// This flag can be used to gate whether GC infrastructure is enabled or
3159    /// initialized in Wasmtime at all. Wasmtime's GC implementation is required
3160    /// for the [`Self::wasm_gc`] proposal, [`Self::wasm_function_references`],
3161    /// and [`Self::wasm_exceptions`] at this time. None of those proposal can
3162    /// be enabled without also having this option enabled.
3163    ///
3164    /// This option defaults to whether the crate `gc` feature is enabled or
3165    /// not.
3166    pub fn gc_support(&mut self, enable: bool) -> &mut Self {
3167        self.wasm_features(WasmFeatures::GC_TYPES, enable)
3168    }
3169
3170    /// Explicitly indicate or not whether the host is using a hardware float
3171    /// ABI on x86 targets.
3172    ///
3173    /// This configuration option is only applicable on the
3174    /// `x86_64-unknown-none` Rust target and has no effect on other host
3175    /// targets. The `x86_64-unknown-none` Rust target does not support hardware
3176    /// floats by default and uses a "soft float" implementation and ABI. This
3177    /// means that `f32`, for example, is passed in a general-purpose register
3178    /// between functions instead of a floating-point register. This does not
3179    /// match Cranelift's ABI for `f32` where it's passed in floating-point
3180    /// registers.  Cranelift does not have support for a "soft float"
3181    /// implementation where all floating-point operations are lowered to
3182    /// libcalls.
3183    ///
3184    /// This means that for the `x86_64-unknown-none` target the ABI between
3185    /// Wasmtime's libcalls and the host is incompatible when floats are used.
3186    /// This further means that, by default, Wasmtime is unable to load native
3187    /// code when compiled to the `x86_64-unknown-none` target. The purpose of
3188    /// this option is to explicitly allow loading code and bypass this check.
3189    ///
3190    /// Setting this configuration option to `true` indicates that either:
3191    /// (a) the Rust target is compiled with the hard-float ABI manually via
3192    /// `-Zbuild-std` and a custom target JSON configuration, or (b) sufficient
3193    /// x86 features have been enabled in the compiler such that float libcalls
3194    /// will not be used in Wasmtime. For (a) there is no way in Rust at this
3195    /// time to detect whether a hard-float or soft-float ABI is in use on
3196    /// stable Rust, so this manual opt-in is required. For (b) the only
3197    /// instance where Wasmtime passes a floating-point value in a register
3198    /// between the host and compiled wasm code is with libcalls.
3199    ///
3200    /// Float-based libcalls are only used when the compilation target for a
3201    /// wasm module has insufficient target features enabled for native
3202    /// support. For example SSE4.1 is required for the `f32.ceil` WebAssembly
3203    /// instruction to be compiled to a native instruction. If SSE4.1 is not
3204    /// enabled then `f32.ceil` is translated to a "libcall" which is
3205    /// implemented on the host. Float-based libcalls can be avoided with
3206    /// sufficient target features enabled, for example:
3207    ///
3208    /// * `self.cranelift_flag_enable("has_sse3")`
3209    /// * `self.cranelift_flag_enable("has_ssse3")`
3210    /// * `self.cranelift_flag_enable("has_sse41")`
3211    /// * `self.cranelift_flag_enable("has_sse42")`
3212    /// * `self.cranelift_flag_enable("has_fma")`
3213    ///
3214    /// Note that when these features are enabled Wasmtime will perform a
3215    /// runtime check to determine that the host actually has the feature
3216    /// present.
3217    ///
3218    /// For some more discussion see [#11506].
3219    ///
3220    /// [#11506]: https://github.com/bytecodealliance/wasmtime/issues/11506
3221    ///
3222    /// # Safety
3223    ///
3224    /// This method is not safe because it cannot be detected in Rust right now
3225    /// whether the host is compiled with a soft or hard float ABI. Additionally
3226    /// if the host is compiled with a soft float ABI disabling this check does
3227    /// not ensure that the wasm module in question has zero usage of floats
3228    /// in the boundary to the host.
3229    ///
3230    /// Safely using this method requires one of:
3231    ///
3232    /// * The host target is compiled to use hardware floats.
3233    /// * Wasm modules loaded are compiled with enough x86 Cranelift features
3234    ///   enabled to avoid float-related hostcalls.
3235    pub unsafe fn x86_float_abi_ok(&mut self, enable: bool) -> &mut Self {
3236        self.x86_float_abi_ok = Some(enable);
3237        self
3238    }
3239
3240    /// Enable or disable the ability to create a
3241    /// [`SharedMemory`](crate::SharedMemory).
3242    ///
3243    /// The WebAssembly threads proposal, configured by [`Config::wasm_threads`]
3244    /// is on-by-default but there are enough deficiencies in Wasmtime's
3245    /// implementation and API integration that creation of a shared memory is
3246    /// disabled by default. This configuration knob can be used to enable this.
3247    ///
3248    /// When enabling this method be aware that wasm threads are, at this time,
3249    /// a [tier 2
3250    /// feature](https://docs.wasmtime.dev/stability-tiers.html#tier-2) in
3251    /// Wasmtime meaning that it will not receive security updates or fixes to
3252    /// historical releases. Additionally security CVEs will not be issued for
3253    /// bugs in the implementation.
3254    ///
3255    /// This option is `false` by default.
3256    pub fn shared_memory(&mut self, enable: bool) -> &mut Self {
3257        self.shared_memory = enable;
3258        self
3259    }
3260
3261    /// Specifies whether support for concurrent execution of WebAssembly is
3262    /// supported within this store.
3263    ///
3264    /// This configuration option affects whether runtime data structures are
3265    /// initialized within a `Store` on creation to support concurrent execution
3266    /// of WebAssembly guests. This is primarily applicable to the
3267    /// [`Config::wasm_component_model_async`] configuration which is the first
3268    /// time Wasmtime has supported concurrent execution of guests. This
3269    /// configuration option, for example, enables usage of
3270    /// [`Store::run_concurrent`], [`Func::call_concurrent`], [`StreamReader`],
3271    /// etc.
3272    ///
3273    /// This configuration option can be manually disabled to avoid initializing
3274    /// data structures in the [`Store`] related to concurrent execution. When
3275    /// this option is disabled then APIs related to concurrency will all fail
3276    /// with a panic. For example [`Store::run_concurrent`] will panic, creating
3277    /// a [`StreamReader`] will panic, etc.
3278    ///
3279    /// The value of this option additionally affects whether a [`Config`] is
3280    /// valid and the default set of enabled WebAssembly features. If this
3281    /// option is disabled then component-model features related to concurrency
3282    /// will all be disabled. If this option is enabled, then the options will
3283    /// retain their normal defaults. It is not valid to create a [`Config`]
3284    /// with component-model-async explicitly enabled and this option explicitly
3285    /// disabled, however.
3286    ///
3287    /// This option defaults to `true`.
3288    ///
3289    /// [`Store`]: crate::Store
3290    /// [`Store::run_concurrent`]: crate::Store::run_concurrent
3291    /// [`Func::call_concurrent`]: crate::component::Func::call_concurrent
3292    /// [`StreamReader`]: crate::component::StreamReader
3293    pub fn concurrency_support(&mut self, enable: bool) -> &mut Self {
3294        self.tunables.concurrency_support = Some(enable);
3295        self
3296    }
3297
3298    /// Validate if the current configuration has conflicting overrides that prevent
3299    /// execution determinism. Returns an error if a conflict exists.
3300    ///
3301    /// Note: Keep this in sync with [`Config::enforce_determinism`].
3302    #[inline]
3303    #[cfg(feature = "rr")]
3304    pub(crate) fn validate_rr_determinism_conflicts(&self) -> Result<()> {
3305        if let Some(v) = self.tunables.relaxed_simd_deterministic {
3306            if v == false {
3307                bail!("Relaxed deterministic SIMD cannot be disabled when determinism is enforced");
3308            }
3309        }
3310        #[cfg(any(feature = "cranelift", feature = "winch"))]
3311        if let Some((v, _)) = self
3312            .compiler_config
3313            .as_ref()
3314            .and_then(|c| c.settings.get("enable_nan_canonicalization"))
3315        {
3316            if v != "true" {
3317                bail!("NaN canonicalization cannot be disabled when determinism is enforced");
3318            }
3319        }
3320        Ok(())
3321    }
3322
3323    /// Enable execution trace recording or replaying to the configuration.
3324    ///
3325    /// When either recording/replaying are enabled, validation fails if settings
3326    /// that control determinism are not set appropriately. In particular, RR requires
3327    /// doing the following:
3328    /// * Enabling NaN canonicalization with [`Config::cranelift_nan_canonicalization`].
3329    /// * Enabling deterministic relaxed SIMD with [`Config::relaxed_simd_deterministic`].
3330    #[inline]
3331    pub fn rr(&mut self, cfg: RRConfig) -> &mut Self {
3332        self.rr_config = cfg;
3333        self
3334    }
3335
3336    /// Whether or not trap metadata is generated in compiled wasms for internal
3337    /// asserts in the compiled code itself.
3338    ///
3339    /// Wasmtime inserts metadata within compiled artifacts which contain a
3340    /// table of known trap codes for all instructions. If a trap via a signal
3341    /// happens, and it's not listed in these tables, then that's considered a
3342    /// fatal bug that crashes the process. This option controls whether trap
3343    /// codes are inserted into metadata for internal asserts as part of
3344    /// Wasmtime's translation process. These internal asserts should never be
3345    /// triggered, but if they are then the process dies with a signal.
3346    ///
3347    /// Inserting trap metadata into compiled artifacts can take extra space in
3348    /// the final artifact. The trap tables for the artifact will be larger as
3349    /// they contain more trap codes to contain.
3350    ///
3351    /// This is intended as a debugging option and is set to `false` by
3352    /// default.
3353    pub fn metadata_for_internal_asserts(&mut self, enable: bool) -> &mut Self {
3354        self.tunables.metadata_for_internal_asserts = Some(enable);
3355        self
3356    }
3357
3358    /// Whether or not trap metadata is generated in compiled wasms for
3359    /// detection of corruption in the GC heap.
3360    ///
3361    /// For more information about what metadata is in this scenario, see
3362    /// [`Config::metadata_for_internal_asserts`]. Note, though, that this
3363    /// option is enabled by default unlike internal asserts. This is intended
3364    /// as a defense-in-depth option for generated code in the face of GC heap
3365    /// corruption. If the GC heap is corrupted and is detected then the
3366    /// trapping instruction will be gracefully handled and delivered to the
3367    /// embedder. Otherwise if this option were set to `false` then the process
3368    /// would be aborted due to a signal.
3369    pub fn metadata_for_gc_heap_corruption(&mut self, enable: bool) -> &mut Self {
3370        self.tunables.metadata_for_gc_heap_corruption = Some(enable);
3371        self
3372    }
3373}
3374
3375impl Default for Config {
3376    fn default() -> Config {
3377        Config::new()
3378    }
3379}
3380
3381impl fmt::Debug for Config {
3382    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3383        let mut f = f.debug_struct("Config");
3384
3385        // Not every flag in WasmFeatures can be enabled as part of creating
3386        // a Config. This impl gives a complete picture of all WasmFeatures
3387        // enabled, and doesn't require maintenance by hand (which has become out
3388        // of date in the past), at the cost of possible confusion for why
3389        // a flag in this set doesn't have a Config setter.
3390        let features = self.features();
3391        for flag in WasmFeatures::FLAGS.iter() {
3392            f.field(
3393                &format!("wasm_{}", flag.name().to_lowercase()),
3394                &features.contains(*flag.value()),
3395            );
3396        }
3397
3398        f.field("parallel_compilation", &self.parallel_compilation);
3399        #[cfg(any(feature = "cranelift", feature = "winch"))]
3400        {
3401            f.field("compiler_config", &self.compiler_config);
3402        }
3403
3404        self.tunables.format(&mut f);
3405        f.finish()
3406    }
3407}
3408
3409/// Possible Compilation strategies for a wasm module.
3410///
3411/// This is used as an argument to the [`Config::strategy`] method.
3412#[non_exhaustive]
3413#[derive(PartialEq, Eq, Clone, Debug, Copy)]
3414pub enum Strategy {
3415    /// An indicator that the compilation strategy should be automatically
3416    /// selected.
3417    ///
3418    /// This is generally what you want for most projects and indicates that the
3419    /// `wasmtime` crate itself should make the decision about what the best
3420    /// code generator for a wasm module is.
3421    ///
3422    /// Currently this always defaults to Cranelift, but the default value may
3423    /// change over time.
3424    Auto,
3425
3426    /// Currently the default backend, Cranelift aims to be a reasonably fast
3427    /// code generator which generates high quality machine code.
3428    Cranelift,
3429
3430    /// A low-latency baseline compiler for WebAssembly.
3431    /// For more details regarding ISA support and Wasm proposals support
3432    /// see <https://docs.wasmtime.dev/stability-tiers.html#current-tier-status>
3433    Winch,
3434}
3435
3436#[cfg(any(feature = "winch", feature = "cranelift"))]
3437impl Strategy {
3438    fn not_auto(&self) -> Option<Strategy> {
3439        match self {
3440            Strategy::Auto => {
3441                if cfg!(feature = "cranelift") {
3442                    Some(Strategy::Cranelift)
3443                } else if cfg!(feature = "winch") {
3444                    Some(Strategy::Winch)
3445                } else {
3446                    None
3447                }
3448            }
3449            other => Some(*other),
3450        }
3451    }
3452}
3453
3454/// Possible garbage collector implementations for Wasm.
3455///
3456/// This is used as an argument to the [`Config::collector`] method.
3457///
3458/// The properties of Wasmtime's available collectors are summarized in the
3459/// following table:
3460///
3461/// | Collector                   | Collects Garbage[^1]  | Latency[^2] | Throughput[^3] | Allocation Speed[^4] | Heap Utilization[^5] |
3462/// |-----------------------------|-----------------------|-------------|----------------|----------------------|----------------------|
3463/// | `Copying`                   | Yes, including cycles | 🙁         | 🙂             | 🙂                   | 🙁                  |
3464/// | `DeferredReferenceCounting` | Yes, but not cycles   | 🙂         | 🙁             | 😐                   | 😐                  |
3465/// | `Null`                      | No                    | 🙂         | 🙂             | 🙂                   | 🙂                  |
3466///
3467/// [^1]: Whether or not the collector is capable of collecting garbage and cyclic garbage.
3468///
3469/// [^2]: How long the Wasm program is paused during garbage
3470///       collections. Shorter is better. In general, better latency implies
3471///       worse throughput and vice versa.
3472///
3473/// [^3]: How fast the Wasm program runs when using this collector. Roughly
3474///       equivalent to the number of Wasm instructions executed per
3475///       second. Faster is better. In general, better throughput implies worse
3476///       latency and vice versa.
3477///
3478/// [^4]: How fast can individual objects be allocated?
3479///
3480/// [^5]: How many objects can the collector fit into N bytes of memory? That
3481///       is, how much space for bookkeeping and metadata does this collector
3482///       require? Less space taken up by metadata means more space for
3483///       additional objects. Reference counts are larger than mark bits and
3484///       free lists are larger than bump pointers, for example.
3485#[non_exhaustive]
3486#[derive(PartialEq, Eq, Clone, Debug, Copy)]
3487pub enum Collector {
3488    /// An indicator that the garbage collector should be automatically
3489    /// selected.
3490    ///
3491    /// This is generally what you want for most projects and indicates that the
3492    /// `wasmtime` crate itself should make the decision about what the best
3493    /// collector to use is.
3494    ///
3495    /// Currently this always defaults to the copying collector, but the default
3496    /// value may change over time.
3497    Auto,
3498
3499    /// The deferred reference-counting collector.
3500    ///
3501    /// A reference-counting collector, generally trading improved latency for
3502    /// worsened throughput. However, to avoid the largest overheads of
3503    /// reference counting, it avoids manipulating reference counts for Wasm
3504    /// objects on the stack. Instead, it will hold a reference count for an
3505    /// over-approximation of all objects that are currently on the stack, trace
3506    /// the stack during collection to find the precise set of on-stack roots,
3507    /// and decrement the reference count of any object that was in the
3508    /// over-approximation but not the precise set. This improves throughput,
3509    /// compared to "pure" reference counting, by performing many fewer
3510    /// refcount-increment and -decrement operations. The cost is the increased
3511    /// latency associated with tracing the stack.
3512    ///
3513    /// This collector cannot currently collect cycles; they will leak until the
3514    /// GC heap's store is dropped.
3515    DeferredReferenceCounting,
3516
3517    /// The null collector.
3518    ///
3519    /// This collector does not actually collect any garbage. It simply
3520    /// allocates objects until it runs out of memory, at which point further
3521    /// objects allocation attempts will trap.
3522    ///
3523    /// This collector is useful for incredibly short-running Wasm instances
3524    /// where additionally you would rather halt an over-allocating Wasm program
3525    /// than spend time collecting its garbage to allow it to keep running. It
3526    /// is also useful for measuring the overheads associated with other
3527    /// collectors, as this collector imposes as close to zero throughput and
3528    /// latency overhead as possible.
3529    Null,
3530
3531    /// The copying collector.
3532    ///
3533    /// A tracing collector that splits the GC heap in half, bump-allocates
3534    /// objects in one half until it fills up, and then does a GC and copies
3535    /// live objects into the other half, and repeats the process. It has fast
3536    /// allocation, collects cyclic garbage, and good collection throughput,
3537    /// however it suffers from poor latency due to its stop-the-world
3538    /// collections and poor heap utilization due to only using half the GC
3539    /// heap's full capacity at any given time.
3540    ///
3541    /// Note that this collector is still under construction and is not yet
3542    /// functional.
3543    Copying,
3544}
3545
3546impl Default for Collector {
3547    fn default() -> Collector {
3548        Collector::Auto
3549    }
3550}
3551
3552#[cfg(feature = "gc")]
3553impl Collector {
3554    fn not_auto(&self) -> Option<Collector> {
3555        match self {
3556            Collector::Auto => {
3557                if cfg!(feature = "gc-copying") {
3558                    Some(Collector::Copying)
3559                } else if cfg!(feature = "gc-drc") {
3560                    Some(Collector::DeferredReferenceCounting)
3561                } else if cfg!(feature = "gc-null") {
3562                    Some(Collector::Null)
3563                } else {
3564                    None
3565                }
3566            }
3567            other => Some(*other),
3568        }
3569    }
3570
3571    fn try_not_auto(&self) -> Result<Self> {
3572        match self.not_auto() {
3573            #[cfg(feature = "gc-drc")]
3574            Some(c @ Collector::DeferredReferenceCounting) => Ok(c),
3575            #[cfg(not(feature = "gc-drc"))]
3576            Some(Collector::DeferredReferenceCounting) => bail!(
3577                "cannot create an engine using the deferred reference-counting \
3578                 collector because the `gc-drc` feature was not enabled at \
3579                 compile time",
3580            ),
3581
3582            #[cfg(feature = "gc-null")]
3583            Some(c @ Collector::Null) => Ok(c),
3584            #[cfg(not(feature = "gc-null"))]
3585            Some(Collector::Null) => bail!(
3586                "cannot create an engine using the null collector because \
3587                 the `gc-null` feature was not enabled at compile time",
3588            ),
3589
3590            #[cfg(feature = "gc-copying")]
3591            Some(c @ Collector::Copying) => Ok(c),
3592            #[cfg(not(feature = "gc-copying"))]
3593            Some(Collector::Copying) => bail!(
3594                "cannot create an engine using the copying collector because \
3595                 the `gc-copying` feature was not enabled at compile time",
3596            ),
3597
3598            Some(Collector::Auto) => unreachable!(),
3599
3600            None => bail!(
3601                "cannot create an engine with GC support when none of the \
3602                 collectors are available; enable one of the following \
3603                 features: `gc-drc`, `gc-null`, `gc-copying`",
3604            ),
3605        }
3606    }
3607}
3608
3609/// Possible optimization levels for the Cranelift codegen backend.
3610#[non_exhaustive]
3611#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3612pub enum OptLevel {
3613    /// No optimizations performed, minimizes compilation time by disabling most
3614    /// optimizations.
3615    None,
3616    /// Generates the fastest possible code, but may take longer.
3617    Speed,
3618    /// Similar to `speed`, but also performs transformations aimed at reducing
3619    /// code size.
3620    SpeedAndSize,
3621}
3622
3623/// Possible register allocator algorithms for the Cranelift codegen backend.
3624#[non_exhaustive]
3625#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3626pub enum RegallocAlgorithm {
3627    /// Generates the fastest possible code, but may take longer.
3628    ///
3629    /// This algorithm performs "backtracking", which means that it may
3630    /// undo its earlier work and retry as it discovers conflicts. This
3631    /// results in better register utilization, producing fewer spills
3632    /// and moves, but can cause super-linear compile runtime.
3633    Backtracking,
3634    /// Generates acceptable code very quickly.
3635    ///
3636    /// This algorithm performs a single pass through the code,
3637    /// guaranteed to work in linear time.  (Note that the rest of
3638    /// Cranelift is not necessarily guaranteed to run in linear time,
3639    /// however.) It cannot undo earlier decisions, however, and it
3640    /// cannot foresee constraints or issues that may occur further
3641    /// ahead in the code, so the code may have more spills and moves as
3642    /// a result.
3643    ///
3644    /// > **Note**: This algorithm is not yet production-ready and has
3645    /// > historically had known problems. It is not recommended to enable this
3646    /// > algorithm for security-sensitive applications and the Wasmtime project
3647    /// > does not consider this configuration option for issuing security
3648    /// > advisories at this time.
3649    SinglePass,
3650}
3651
3652/// Select which profiling technique to support.
3653#[derive(Debug, Clone, Copy, PartialEq)]
3654pub enum ProfilingStrategy {
3655    /// No profiler support.
3656    None,
3657
3658    /// Collect function name information as the "perf map" file format, used with `perf` on Linux.
3659    PerfMap,
3660
3661    /// Collect profiling info for "jitdump" file format, used with `perf` on
3662    /// Linux.
3663    JitDump,
3664
3665    /// Collect profiling info using the "ittapi", used with `VTune` on Linux.
3666    VTune,
3667
3668    /// Support for profiling Pulley, Wasmtime's interpreter. Note that enabling
3669    /// this at runtime requires enabling the `profile-pulley` Cargo feature at
3670    /// compile time.
3671    Pulley,
3672}
3673
3674/// Select how wasm backtrace detailed information is handled.
3675#[derive(Debug, Clone, Copy)]
3676pub enum WasmBacktraceDetails {
3677    /// Support is unconditionally enabled and wasmtime will parse and read
3678    /// debug information.
3679    Enable,
3680
3681    /// Support is disabled, and wasmtime will not parse debug information for
3682    /// backtrace details.
3683    Disable,
3684
3685    /// Support for backtrace details is conditional on the
3686    /// `WASMTIME_BACKTRACE_DETAILS` environment variable.
3687    Environment,
3688}
3689
3690/// Describe the tri-state configuration of keys such as MPK or PAGEMAP_SCAN.
3691#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
3692pub enum Enabled {
3693    /// Enable this feature if it's detected on the host system, otherwise leave
3694    /// it disabled.
3695    Auto,
3696    /// Enable this feature and fail configuration if the feature is not
3697    /// detected on the host system.
3698    Yes,
3699    /// Do not enable this feature, even if the host system supports it.
3700    No,
3701}
3702
3703/// Configuration options used with [`InstanceAllocationStrategy::Pooling`] to
3704/// change the behavior of the pooling instance allocator.
3705///
3706/// This structure has a builder-style API in the same manner as [`Config`] and
3707/// is configured with [`Config::allocation_strategy`].
3708///
3709/// Note that usage of the pooling allocator does not affect compiled
3710/// WebAssembly code. Compiled `*.cwasm` files, for example, are usable both
3711/// with and without the pooling allocator.
3712///
3713/// ## Advantages of Pooled Allocation
3714///
3715/// The main benefit of the pooling allocator is to make WebAssembly
3716/// instantiation both faster and more scalable in terms of parallelism.
3717/// Allocation is faster because virtual memory is already configured and ready
3718/// to go within the pool, there's no need to [`mmap`] (for example on Unix) a
3719/// new region and configure it with guard pages. By avoiding [`mmap`] this
3720/// avoids whole-process virtual memory locks which can improve scalability and
3721/// performance through avoiding this.
3722///
3723/// Additionally with pooled allocation it's possible to create "affine slots"
3724/// to a particular WebAssembly module or component over time. For example if
3725/// the same module is multiple times over time the pooling allocator will, by
3726/// default, attempt to reuse the same slot. This mean that the slot has been
3727/// pre-configured and can retain virtual memory mappings for a copy-on-write
3728/// image, for example (see [`Config::memory_init_cow`] for more information.
3729/// This means that in a steady state instance deallocation is a single
3730/// [`madvise`] to reset linear memory to its original contents followed by a
3731/// single (optional) [`mprotect`] during the next instantiation to shrink
3732/// memory back to its original size. Compared to non-pooled allocation this
3733/// avoids the need to [`mmap`] a new region of memory, [`munmap`] it, and
3734/// [`mprotect`] regions too.
3735///
3736/// Another benefit of pooled allocation is that it's possible to configure
3737/// things such that no virtual memory management is required at all in a steady
3738/// state. For example a pooling allocator can be configured with:
3739///
3740/// * [`Config::memory_init_cow`] disabled
3741/// * [`Config::memory_guard_size`] disabled
3742/// * [`Config::memory_reservation`] shrunk to minimal size
3743/// * [`PoolingAllocationConfig::table_keep_resident`] sufficiently large
3744/// * [`PoolingAllocationConfig::linear_memory_keep_resident`] sufficiently large
3745///
3746/// With all these options in place no virtual memory tricks are used at all and
3747/// everything is manually managed by Wasmtime (for example resetting memory is
3748/// a `memset(0)`). This is not as fast in a single-threaded scenario but can
3749/// provide benefits in high-parallelism situations as no virtual memory locks
3750/// or IPIs need happen.
3751///
3752/// ## Disadvantages of Pooled Allocation
3753///
3754/// Despite the above advantages to instantiation performance the pooling
3755/// allocator is not enabled by default in Wasmtime. One reason is that the
3756/// performance advantages are not necessarily portable, for example while the
3757/// pooling allocator works on Windows it has not been tuned for performance on
3758/// Windows in the same way it has on Linux.
3759///
3760/// Additionally the main cost of the pooling allocator is that it requires a
3761/// very large reservation of virtual memory (on the order of most of the
3762/// addressable virtual address space). WebAssembly 32-bit linear memories in
3763/// Wasmtime are, by default 4G address space reservations with a small guard
3764/// region both before and after the linear memory. Memories in the pooling
3765/// allocator are contiguous which means that we only need a guard after linear
3766/// memory because the previous linear memory's slot post-guard is our own
3767/// pre-guard. This means that, by default, the pooling allocator uses roughly
3768/// 4G of virtual memory per WebAssembly linear memory slot. 4G of virtual
3769/// memory is 32 bits of a 64-bit address. Many 64-bit systems can only
3770/// actually use 48-bit addresses by default (although this can be extended on
3771/// architectures nowadays too), and of those 48 bits one of them is reserved
3772/// to indicate kernel-vs-userspace. This leaves 47-32=15 bits left,
3773/// meaning you can only have at most 32k slots of linear memories on many
3774/// systems by default. This is a relatively small number and shows how the
3775/// pooling allocator can quickly exhaust all of virtual memory.
3776///
3777/// Another disadvantage of the pooling allocator is that it may keep memory
3778/// alive when nothing is using it. A previously used slot for an instance might
3779/// have paged-in memory that will not get paged out until the
3780/// [`Engine`] owning the pooling allocator is dropped. While
3781/// suitable for some applications this behavior may not be suitable for all
3782/// applications.
3783///
3784/// Finally the last disadvantage of the pooling allocator is that the
3785/// configuration values for the maximum number of instances, memories, tables,
3786/// etc, must all be fixed up-front. There's not always a clear answer as to
3787/// what these values should be so not all applications may be able to work
3788/// with this constraint.
3789///
3790/// [`madvise`]: https://man7.org/linux/man-pages/man2/madvise.2.html
3791/// [`mprotect`]: https://man7.org/linux/man-pages/man2/mprotect.2.html
3792/// [`mmap`]: https://man7.org/linux/man-pages/man2/mmap.2.html
3793/// [`munmap`]: https://man7.org/linux/man-pages/man2/munmap.2.html
3794#[derive(Debug, Clone)]
3795pub struct PoolingAllocationConfig {
3796    /// See `PoolingAllocatorConfig::max_unused_warm_slots` in `wasmtime`
3797    pub(crate) max_unused_warm_slots: u32,
3798    /// The target number of decommits to do per batch. This is not precise, as
3799    /// we can queue up decommits at times when we aren't prepared to
3800    /// immediately flush them, and so we may go over this target size
3801    /// occasionally.
3802    pub(crate) decommit_batch_size: usize,
3803    /// The size, in bytes, of async stacks to allocate (not including the guard
3804    /// page).
3805    #[cfg_attr(
3806        not(all(feature = "async", feature = "pooling-allocator")),
3807        expect(dead_code, reason = "easier to cfg")
3808    )]
3809    pub(crate) stack_size: usize,
3810    /// The limits to apply to instances allocated within this allocator.
3811    pub(crate) limits: InstanceLimits,
3812    /// Whether or not async stacks are zeroed after use.
3813    #[cfg_attr(
3814        not(all(feature = "async", feature = "pooling-allocator")),
3815        expect(dead_code, reason = "easier to cfg")
3816    )]
3817    pub(crate) async_stack_zeroing: bool,
3818    /// If async stack zeroing is enabled and the host platform is Linux this is
3819    /// how much memory to zero out with `memset`.
3820    ///
3821    /// The rest of memory will be zeroed out with `madvise`.
3822    pub(crate) async_stack_keep_resident: usize,
3823    /// How much linear memory, in bytes, to keep resident after resetting for
3824    /// use with the next instance. This much memory will be `memset` to zero
3825    /// when a linear memory is deallocated.
3826    ///
3827    /// Memory exceeding this amount in the wasm linear memory will be released
3828    /// with `madvise` back to the kernel.
3829    ///
3830    /// Only applicable on Linux.
3831    pub(crate) linear_memory_keep_resident: usize,
3832    /// Same as `linear_memory_keep_resident` but for tables.
3833    pub(crate) table_keep_resident: usize,
3834    /// Whether to enable memory protection keys.
3835    pub(crate) memory_protection_keys: Enabled,
3836    /// How many memory protection keys to allocate.
3837    pub(crate) max_memory_protection_keys: usize,
3838    /// Whether to enable PAGEMAP_SCAN on Linux.
3839    pub(crate) pagemap_scan: Enabled,
3840}
3841
3842impl Default for PoolingAllocationConfig {
3843    fn default() -> Self {
3844        Self {
3845            max_unused_warm_slots: 100,
3846            decommit_batch_size: 1,
3847            stack_size: 2 << 20,
3848            limits: InstanceLimits::default(),
3849            async_stack_zeroing: false,
3850            async_stack_keep_resident: 0,
3851            linear_memory_keep_resident: 0,
3852            table_keep_resident: 0,
3853            memory_protection_keys: Enabled::No,
3854            max_memory_protection_keys: 16,
3855            pagemap_scan: Enabled::No,
3856        }
3857    }
3858}
3859
3860/// Instance-related limit configuration for pooling.
3861///
3862/// More docs on this can be found at `wasmtime::PoolingAllocationConfig`.
3863#[derive(Debug, Copy, Clone)]
3864pub(crate) struct InstanceLimits {
3865    /// The maximum number of component instances that may be allocated
3866    /// concurrently.
3867    pub(crate) total_component_instances: u32,
3868
3869    /// The maximum size of a component's `VMComponentContext`, including
3870    /// the aggregate size of all its inner core modules' `VMContext` sizes.
3871    pub(crate) component_instance_size: usize,
3872
3873    /// The maximum number of core module instances that may be allocated
3874    /// concurrently.
3875    pub(crate) total_core_instances: u32,
3876
3877    /// The maximum number of core module instances that a single component may
3878    /// transitively contain.
3879    pub(crate) max_core_instances_per_component: u32,
3880
3881    /// The maximum number of Wasm linear memories that a component may
3882    /// transitively contain.
3883    pub(crate) max_memories_per_component: u32,
3884
3885    /// The maximum number of tables that a component may transitively contain.
3886    pub(crate) max_tables_per_component: u32,
3887
3888    /// The total number of linear memories in the pool, across all instances.
3889    pub(crate) total_memories: u32,
3890
3891    /// The total number of tables in the pool, across all instances.
3892    pub(crate) total_tables: u32,
3893
3894    /// The total number of async stacks in the pool, across all instances.
3895    pub(crate) total_stacks: u32,
3896
3897    /// Maximum size of a core instance's `VMContext`.
3898    pub(crate) core_instance_size: usize,
3899
3900    /// Maximum number of tables per instance.
3901    pub(crate) max_tables_per_module: u32,
3902
3903    /// Maximum number of word-size elements per table.
3904    ///
3905    /// Note that tables for element types such as continuations
3906    /// that use more than one word of storage may store fewer
3907    /// elements.
3908    pub(crate) table_elements: usize,
3909
3910    /// Maximum number of linear memories per instance.
3911    pub(crate) max_memories_per_module: u32,
3912
3913    /// Maximum byte size of a linear memory, must be smaller than
3914    /// `memory_reservation` in `Tunables`.
3915    pub(crate) max_memory_size: usize,
3916
3917    /// The total number of GC heaps in the pool, across all instances.
3918    pub(crate) total_gc_heaps: u32,
3919}
3920
3921impl Default for InstanceLimits {
3922    fn default() -> Self {
3923        let total = if cfg!(target_pointer_width = "32") {
3924            100
3925        } else {
3926            1000
3927        };
3928        // See doc comments for `wasmtime::PoolingAllocationConfig` for these
3929        // default values
3930        Self {
3931            total_component_instances: total,
3932            component_instance_size: 1 << 20, // 1 MiB
3933            total_core_instances: total,
3934            max_core_instances_per_component: u32::MAX,
3935            max_memories_per_component: u32::MAX,
3936            max_tables_per_component: u32::MAX,
3937            total_memories: total,
3938            total_tables: total,
3939            total_stacks: total,
3940            core_instance_size: 1 << 20, // 1 MiB
3941            max_tables_per_module: 1,
3942            // NB: in #8504 it was seen that a C# module in debug module can
3943            // have 10k+ elements.
3944            table_elements: 20_000,
3945            max_memories_per_module: 1,
3946            #[cfg(target_pointer_width = "64")]
3947            max_memory_size: 1 << 32, // 4G,
3948            #[cfg(target_pointer_width = "32")]
3949            max_memory_size: 10 << 20, // 10 MiB
3950            total_gc_heaps: total,
3951        }
3952    }
3953}
3954
3955impl PoolingAllocationConfig {
3956    /// Returns a new configuration builder with all default settings
3957    /// configured.
3958    pub fn new() -> PoolingAllocationConfig {
3959        PoolingAllocationConfig::default()
3960    }
3961
3962    /// Configures the maximum number of "unused warm slots" to retain in the
3963    /// pooling allocator.
3964    ///
3965    /// The pooling allocator operates over slots to allocate from, and each
3966    /// slot is considered "cold" if it's never been used before or "warm" if
3967    /// it's been used by some module in the past. Slots in the pooling
3968    /// allocator additionally track an "affinity" flag to a particular core
3969    /// wasm module. When a module is instantiated into a slot then the slot is
3970    /// considered affine to that module, even after the instance has been
3971    /// deallocated.
3972    ///
3973    /// When a new instance is created then a slot must be chosen, and the
3974    /// current algorithm for selecting a slot is:
3975    ///
3976    /// * If there are slots that are affine to the module being instantiated,
3977    ///   then the most recently used slot is selected to be allocated from.
3978    ///   This is done to improve reuse of resources such as memory mappings and
3979    ///   additionally try to benefit from temporal locality for things like
3980    ///   caches.
3981    ///
3982    /// * Otherwise if there are more than N affine slots to other modules, then
3983    ///   one of those affine slots is chosen to be allocated. The slot chosen
3984    ///   is picked on a least-recently-used basis.
3985    ///
3986    /// * Finally, if there are less than N affine slots to other modules, then
3987    ///   the non-affine slots are allocated from.
3988    ///
3989    /// This setting, `max_unused_warm_slots`, is the value for N in the above
3990    /// algorithm. The purpose of this setting is to have a knob over the RSS
3991    /// impact of "unused slots" for a long-running wasm server.
3992    ///
3993    /// If this setting is set to 0, for example, then affine slots are
3994    /// aggressively reused on a least-recently-used basis. A "cold" slot is
3995    /// only used if there are no affine slots available to allocate from. This
3996    /// means that the set of slots used over the lifetime of a program is the
3997    /// same as the maximum concurrent number of wasm instances.
3998    ///
3999    /// If this setting is set to infinity, however, then cold slots are
4000    /// prioritized to be allocated from. This means that the set of slots used
4001    /// over the lifetime of a program will approach
4002    /// [`PoolingAllocationConfig::total_memories`], or the maximum number of
4003    /// slots in the pooling allocator.
4004    ///
4005    /// Wasmtime does not aggressively decommit all resources associated with a
4006    /// slot when the slot is not in use. For example the
4007    /// [`PoolingAllocationConfig::linear_memory_keep_resident`] option can be
4008    /// used to keep memory associated with a slot, even when it's not in use.
4009    /// This means that the total set of used slots in the pooling instance
4010    /// allocator can impact the overall RSS usage of a program.
4011    ///
4012    /// The default value for this option is `100`.
4013    pub fn max_unused_warm_slots(&mut self, max: u32) -> &mut Self {
4014        self.max_unused_warm_slots = max;
4015        self
4016    }
4017
4018    /// The target number of decommits to do per batch.
4019    ///
4020    /// This is not precise, as we can queue up decommits at times when we
4021    /// aren't prepared to immediately flush them, and so we may go over this
4022    /// target size occasionally.
4023    ///
4024    /// A batch size of one effectively disables batching.
4025    ///
4026    /// Defaults to `1`.
4027    pub fn decommit_batch_size(&mut self, batch_size: usize) -> &mut Self {
4028        self.decommit_batch_size = batch_size;
4029        self
4030    }
4031
4032    /// How much memory, in bytes, to keep resident for async stacks allocated
4033    /// with the pooling allocator.
4034    ///
4035    /// When [`Config::async_stack_zeroing`] is enabled then Wasmtime will reset
4036    /// the contents of async stacks back to zero upon deallocation. This option
4037    /// can be used to perform the zeroing operation with `memset` up to a
4038    /// certain threshold of bytes instead of using system calls to reset the
4039    /// stack to zero.
4040    ///
4041    /// Note that when using this option the memory with async stacks will
4042    /// never be decommitted.
4043    pub fn async_stack_keep_resident(&mut self, size: usize) -> &mut Self {
4044        self.async_stack_keep_resident = size;
4045        self
4046    }
4047
4048    /// How much memory, in bytes, to keep resident for each linear memory
4049    /// after deallocation.
4050    ///
4051    /// This option is only applicable on Linux and has no effect on other
4052    /// platforms.
4053    ///
4054    /// By default Wasmtime will use `madvise` to reset the entire contents of
4055    /// linear memory back to zero when a linear memory is deallocated. This
4056    /// option can be used to use `memset` instead to set memory back to zero
4057    /// which can, in some configurations, reduce the number of page faults
4058    /// taken when a slot is reused.
4059    pub fn linear_memory_keep_resident(&mut self, size: usize) -> &mut Self {
4060        self.linear_memory_keep_resident = size;
4061        self
4062    }
4063
4064    /// How much memory, in bytes, to keep resident for each table after
4065    /// deallocation.
4066    ///
4067    /// This option is only applicable on Linux and has no effect on other
4068    /// platforms.
4069    ///
4070    /// This option is the same as
4071    /// [`PoolingAllocationConfig::linear_memory_keep_resident`] except that it
4072    /// is applicable to tables instead.
4073    pub fn table_keep_resident(&mut self, size: usize) -> &mut Self {
4074        self.table_keep_resident = size;
4075        self
4076    }
4077
4078    /// The maximum number of concurrent component instances supported (default
4079    /// is `1000`).
4080    ///
4081    /// This provides an upper-bound on the total size of component
4082    /// metadata-related allocations, along with
4083    /// [`PoolingAllocationConfig::max_component_instance_size`]. The upper bound is
4084    ///
4085    /// ```text
4086    /// total_component_instances * max_component_instance_size
4087    /// ```
4088    ///
4089    /// where `max_component_instance_size` is rounded up to the size and alignment
4090    /// of the internal representation of the metadata.
4091    pub fn total_component_instances(&mut self, count: u32) -> &mut Self {
4092        self.limits.total_component_instances = count;
4093        self
4094    }
4095
4096    /// The maximum size, in bytes, allocated for a component instance's
4097    /// `VMComponentContext` metadata as well as the aggregate size of this
4098    /// component's core instances `VMContext` metadata.
4099    ///
4100    /// The [`wasmtime::component::Instance`][crate::component::Instance] type
4101    /// has a static size but its internal `VMComponentContext` is dynamically
4102    /// sized depending on the component being instantiated. This size limit
4103    /// loosely correlates to the size of the component, taking into account
4104    /// factors such as:
4105    ///
4106    /// * number of lifted and lowered functions,
4107    /// * number of memories
4108    /// * number of inner instances
4109    /// * number of resources
4110    ///
4111    /// If the allocated size per instance is too small then instantiation of a
4112    /// module will fail at runtime with an error indicating how many bytes were
4113    /// needed.
4114    ///
4115    /// In addition to the memory in the runtime for the component itself,
4116    /// components contain one or more core module instances. Each of these
4117    /// require some memory in the runtime as described in
4118    /// [`PoolingAllocationConfig::max_core_instance_size`]. The limit here
4119    /// applies against the sum of all of these individual allocations.
4120    ///
4121    /// The default value for this is 1MiB.
4122    ///
4123    /// This provides an upper-bound on the total size of all component's
4124    /// metadata-related allocations (for both the component and its embedded
4125    /// core module instances), along with
4126    /// [`PoolingAllocationConfig::total_component_instances`]. The upper bound is
4127    ///
4128    /// ```text
4129    /// total_component_instances * max_component_instance_size
4130    /// ```
4131    ///
4132    /// where `max_component_instance_size` is rounded up to the size and alignment
4133    /// of the internal representation of the metadata.
4134    pub fn max_component_instance_size(&mut self, size: usize) -> &mut Self {
4135        self.limits.component_instance_size = size;
4136        self
4137    }
4138
4139    /// The maximum number of core instances a single component may contain
4140    /// (default is unlimited).
4141    ///
4142    /// This method (along with
4143    /// [`PoolingAllocationConfig::max_memories_per_component`],
4144    /// [`PoolingAllocationConfig::max_tables_per_component`], and
4145    /// [`PoolingAllocationConfig::max_component_instance_size`]) allows you to cap
4146    /// the amount of resources a single component allocation consumes.
4147    ///
4148    /// If a component will instantiate more core instances than `count`, then
4149    /// the component will fail to instantiate.
4150    pub fn max_core_instances_per_component(&mut self, count: u32) -> &mut Self {
4151        self.limits.max_core_instances_per_component = count;
4152        self
4153    }
4154
4155    /// The maximum number of Wasm linear memories that a single component may
4156    /// transitively contain (default is unlimited).
4157    ///
4158    /// This method (along with
4159    /// [`PoolingAllocationConfig::max_core_instances_per_component`],
4160    /// [`PoolingAllocationConfig::max_tables_per_component`], and
4161    /// [`PoolingAllocationConfig::max_component_instance_size`]) allows you to cap
4162    /// the amount of resources a single component allocation consumes.
4163    ///
4164    /// If a component transitively contains more linear memories than `count`,
4165    /// then the component will fail to instantiate.
4166    pub fn max_memories_per_component(&mut self, count: u32) -> &mut Self {
4167        self.limits.max_memories_per_component = count;
4168        self
4169    }
4170
4171    /// The maximum number of tables that a single component may transitively
4172    /// contain (default is unlimited).
4173    ///
4174    /// This method (along with
4175    /// [`PoolingAllocationConfig::max_core_instances_per_component`],
4176    /// [`PoolingAllocationConfig::max_memories_per_component`],
4177    /// [`PoolingAllocationConfig::max_component_instance_size`]) allows you to cap
4178    /// the amount of resources a single component allocation consumes.
4179    ///
4180    /// If a component will transitively contains more tables than `count`, then
4181    /// the component will fail to instantiate.
4182    pub fn max_tables_per_component(&mut self, count: u32) -> &mut Self {
4183        self.limits.max_tables_per_component = count;
4184        self
4185    }
4186
4187    /// The maximum number of concurrent Wasm linear memories supported (default
4188    /// is `1000`).
4189    ///
4190    /// This value has a direct impact on the amount of memory allocated by the pooling
4191    /// instance allocator.
4192    ///
4193    /// The pooling instance allocator allocates a memory pool, where each entry
4194    /// in the pool contains the reserved address space for each linear memory
4195    /// supported by an instance.
4196    ///
4197    /// The memory pool will reserve a large quantity of host process address
4198    /// space to elide the bounds checks required for correct WebAssembly memory
4199    /// semantics. Even with 64-bit address spaces, the address space is limited
4200    /// when dealing with a large number of linear memories.
4201    ///
4202    /// For example, on Linux x86_64, the userland address space limit is 128
4203    /// TiB. That might seem like a lot, but each linear memory will *reserve* 6
4204    /// GiB of space by default.
4205    pub fn total_memories(&mut self, count: u32) -> &mut Self {
4206        self.limits.total_memories = count;
4207        self
4208    }
4209
4210    /// The maximum number of concurrent tables supported (default is `1000`).
4211    ///
4212    /// This value has a direct impact on the amount of memory allocated by the
4213    /// pooling instance allocator.
4214    ///
4215    /// The pooling instance allocator allocates a table pool, where each entry
4216    /// in the pool contains the space needed for each WebAssembly table
4217    /// supported by an instance (see `table_elements` to control the size of
4218    /// each table).
4219    pub fn total_tables(&mut self, count: u32) -> &mut Self {
4220        self.limits.total_tables = count;
4221        self
4222    }
4223
4224    /// The maximum number of execution stacks allowed for asynchronous
4225    /// execution, when enabled (default is `1000`).
4226    ///
4227    /// This value has a direct impact on the amount of memory allocated by the
4228    /// pooling instance allocator.
4229    #[cfg(feature = "async")]
4230    pub fn total_stacks(&mut self, count: u32) -> &mut Self {
4231        self.limits.total_stacks = count;
4232        self
4233    }
4234
4235    /// The maximum number of concurrent core instances supported (default is
4236    /// `1000`).
4237    ///
4238    /// This provides an upper-bound on the total size of core instance
4239    /// metadata-related allocations, along with
4240    /// [`PoolingAllocationConfig::max_core_instance_size`]. The upper bound is
4241    ///
4242    /// ```text
4243    /// total_core_instances * max_core_instance_size
4244    /// ```
4245    ///
4246    /// where `max_core_instance_size` is rounded up to the size and alignment of
4247    /// the internal representation of the metadata.
4248    pub fn total_core_instances(&mut self, count: u32) -> &mut Self {
4249        self.limits.total_core_instances = count;
4250        self
4251    }
4252
4253    /// The maximum size, in bytes, allocated for a core instance's `VMContext`
4254    /// metadata.
4255    ///
4256    /// The [`Instance`][crate::Instance] type has a static size but its
4257    /// `VMContext` metadata is dynamically sized depending on the module being
4258    /// instantiated. This size limit loosely correlates to the size of the Wasm
4259    /// module, taking into account factors such as:
4260    ///
4261    /// * number of functions
4262    /// * number of globals
4263    /// * number of memories
4264    /// * number of tables
4265    /// * number of function types
4266    ///
4267    /// If the allocated size per instance is too small then instantiation of a
4268    /// module will fail at runtime with an error indicating how many bytes were
4269    /// needed.
4270    ///
4271    /// The default value for this is 1MiB.
4272    ///
4273    /// This provides an upper-bound on the total size of core instance
4274    /// metadata-related allocations, along with
4275    /// [`PoolingAllocationConfig::total_core_instances`]. The upper bound is
4276    ///
4277    /// ```text
4278    /// total_core_instances * max_core_instance_size
4279    /// ```
4280    ///
4281    /// where `max_core_instance_size` is rounded up to the size and alignment of
4282    /// the internal representation of the metadata.
4283    pub fn max_core_instance_size(&mut self, size: usize) -> &mut Self {
4284        self.limits.core_instance_size = size;
4285        self
4286    }
4287
4288    /// The maximum number of defined tables for a core module (default is `1`).
4289    ///
4290    /// This value controls the capacity of the `VMTableDefinition` table in
4291    /// each instance's `VMContext` structure.
4292    ///
4293    /// The allocated size of the table will be `tables *
4294    /// sizeof(VMTableDefinition)` for each instance regardless of how many
4295    /// tables are defined by an instance's module.
4296    pub fn max_tables_per_module(&mut self, tables: u32) -> &mut Self {
4297        self.limits.max_tables_per_module = tables;
4298        self
4299    }
4300
4301    /// The maximum table elements for any table defined in a module (default is
4302    /// `20000`).
4303    ///
4304    /// If a table's minimum element limit is greater than this value, the
4305    /// module will fail to instantiate.
4306    ///
4307    /// If a table's maximum element limit is unbounded or greater than this
4308    /// value, the maximum will be `table_elements` for the purpose of any
4309    /// `table.grow` instruction.
4310    ///
4311    /// This value is used to reserve the maximum space for each supported
4312    /// table; table elements are pointer-sized in the Wasmtime runtime.
4313    /// Therefore, the space reserved for each instance is `tables *
4314    /// table_elements * sizeof::<*const ()>`.
4315    pub fn table_elements(&mut self, elements: usize) -> &mut Self {
4316        self.limits.table_elements = elements;
4317        self
4318    }
4319
4320    /// The maximum number of defined linear memories for a module (default is
4321    /// `1`).
4322    ///
4323    /// This value controls the capacity of the `VMMemoryDefinition` table in
4324    /// each core instance's `VMContext` structure.
4325    ///
4326    /// The allocated size of the table will be `memories *
4327    /// sizeof(VMMemoryDefinition)` for each core instance regardless of how
4328    /// many memories are defined by the core instance's module.
4329    pub fn max_memories_per_module(&mut self, memories: u32) -> &mut Self {
4330        self.limits.max_memories_per_module = memories;
4331        self
4332    }
4333
4334    /// The maximum byte size that any WebAssembly linear memory may grow to.
4335    ///
4336    /// This option defaults to 4 GiB meaning that for 32-bit linear memories
4337    /// there is no restrictions. 64-bit linear memories will not be allowed to
4338    /// grow beyond 4 GiB by default.
4339    ///
4340    /// If a memory's minimum size is greater than this value, the module will
4341    /// fail to instantiate.
4342    ///
4343    /// If a memory's maximum size is unbounded or greater than this value, the
4344    /// maximum will be `max_memory_size` for the purpose of any `memory.grow`
4345    /// instruction.
4346    ///
4347    /// This value is used to control the maximum accessible space for each
4348    /// linear memory of a core instance. This can be thought of as a simple
4349    /// mechanism like [`Store::limiter`](crate::Store::limiter) to limit memory
4350    /// at runtime. This value can also affect striping/coloring behavior when
4351    /// used in conjunction with
4352    /// [`memory_protection_keys`](PoolingAllocationConfig::memory_protection_keys).
4353    ///
4354    /// The virtual memory reservation size of each linear memory is controlled
4355    /// by the [`Config::memory_reservation`] setting and this method's
4356    /// configuration cannot exceed [`Config::memory_reservation`].
4357    pub fn max_memory_size(&mut self, bytes: usize) -> &mut Self {
4358        self.limits.max_memory_size = bytes;
4359        self
4360    }
4361
4362    /// Configures whether memory protection keys (MPK) should be used for more
4363    /// efficient layout of pool-allocated memories.
4364    ///
4365    /// When using the pooling allocator (see [`Config::allocation_strategy`],
4366    /// [`InstanceAllocationStrategy::Pooling`]), memory protection keys can
4367    /// reduce the total amount of allocated virtual memory by eliminating guard
4368    /// regions between WebAssembly memories in the pool. It does so by
4369    /// "coloring" memory regions with different memory keys and setting which
4370    /// regions are accessible each time executions switches from host to guest
4371    /// (or vice versa).
4372    ///
4373    /// Leveraging MPK requires configuring a smaller-than-default
4374    /// [`max_memory_size`](PoolingAllocationConfig::max_memory_size) to enable
4375    /// this coloring/striping behavior. For example embeddings might want to
4376    /// reduce the default 4G allowance to 128M.
4377    ///
4378    /// MPK is only available on Linux (called `pku` there) and recent x86
4379    /// systems; we check for MPK support at runtime by examining the `CPUID`
4380    /// register. This configuration setting can be in three states:
4381    ///
4382    /// - `auto`: if MPK support is available the guard regions are removed; if
4383    ///   not, the guard regions remain
4384    /// - `yes`: use MPK to eliminate guard regions; fail if MPK is not
4385    ///   supported
4386    /// - `no`: never use MPK
4387    ///
4388    /// By default this value is `no`, but may become `auto` in future
4389    /// releases.
4390    ///
4391    /// __WARNING__: this configuration options is still experimental--use at
4392    /// your own risk! MPK uses kernel and CPU features to protect memory
4393    /// regions; you may observe segmentation faults if anything is
4394    /// misconfigured.
4395    #[cfg(feature = "memory-protection-keys")]
4396    pub fn memory_protection_keys(&mut self, enable: Enabled) -> &mut Self {
4397        self.memory_protection_keys = enable;
4398        self
4399    }
4400
4401    /// Sets an upper limit on how many memory protection keys (MPK) Wasmtime
4402    /// will use.
4403    ///
4404    /// This setting is only applicable when
4405    /// [`PoolingAllocationConfig::memory_protection_keys`] is set to `enable`
4406    /// or `auto`. Configuring this above the HW and OS limits (typically 15)
4407    /// has no effect.
4408    ///
4409    /// If multiple Wasmtime engines are used in the same process, note that all
4410    /// engines will share the same set of allocated keys; this setting will
4411    /// limit how many keys are allocated initially and thus available to all
4412    /// other engines.
4413    #[cfg(feature = "memory-protection-keys")]
4414    pub fn max_memory_protection_keys(&mut self, max: usize) -> &mut Self {
4415        self.max_memory_protection_keys = max;
4416        self
4417    }
4418
4419    /// Check if memory protection keys (MPK) are available on the current host.
4420    ///
4421    /// This is a convenience method for determining MPK availability using the
4422    /// same method that [`Enabled::Auto`] does. See
4423    /// [`PoolingAllocationConfig::memory_protection_keys`] for more
4424    /// information.
4425    #[cfg(feature = "memory-protection-keys")]
4426    pub fn are_memory_protection_keys_available() -> bool {
4427        crate::runtime::vm::mpk::is_supported()
4428    }
4429
4430    /// The maximum number of concurrent GC heaps supported (default is `1000`).
4431    ///
4432    /// This value has a direct impact on the amount of memory allocated by the
4433    /// pooling instance allocator.
4434    ///
4435    /// The pooling instance allocator allocates a GC heap pool, where each
4436    /// entry in the pool contains the space needed for each GC heap used by a
4437    /// store.
4438    #[cfg(feature = "gc")]
4439    pub fn total_gc_heaps(&mut self, count: u32) -> &mut Self {
4440        self.limits.total_gc_heaps = count;
4441        self
4442    }
4443
4444    /// Configures whether the Linux-specific [`PAGEMAP_SCAN` ioctl][ioctl] is
4445    /// used to help reset linear memory.
4446    ///
4447    /// When [`Self::linear_memory_keep_resident`] or
4448    /// [`Self::table_keep_resident`] options are configured to nonzero values
4449    /// the default behavior is to `memset` the lowest addresses of a table or
4450    /// memory back to their original contents. With the `PAGEMAP_SCAN` ioctl on
4451    /// Linux this can be done to more intelligently scan for resident pages in
4452    /// the region and only reset those pages back to their original contents
4453    /// with `memset` rather than assuming the low addresses are all resident.
4454    ///
4455    /// This ioctl has the potential to provide a number of performance benefits
4456    /// in high-reuse and high concurrency scenarios. Notably this enables
4457    /// Wasmtime to scan the entire region of WebAssembly linear memory and
4458    /// manually reset memory back to its original contents, up to
4459    /// [`Self::linear_memory_keep_resident`] bytes, possibly skipping an
4460    /// `madvise` entirely. This can be more efficient by avoiding removing
4461    /// pages from the address space entirely and additionally ensuring that
4462    /// future use of the linear memory doesn't incur page faults as the pages
4463    /// remain resident.
4464    ///
4465    /// At this time this configuration option is still being evaluated as to
4466    /// how appropriate it is for all use cases. It currently defaults to
4467    /// `no` or disabled but may change to `auto`, enable if supported, in the
4468    /// future. This option is only supported on Linux and requires a kernel
4469    /// version of 6.7 or higher.
4470    ///
4471    /// [ioctl]: https://www.man7.org/linux/man-pages/man2/PAGEMAP_SCAN.2const.html
4472    pub fn pagemap_scan(&mut self, enable: Enabled) -> &mut Self {
4473        self.pagemap_scan = enable;
4474        self
4475    }
4476
4477    /// Returns the configured
4478    /// [`PoolingAllocationConfig::decommit_batch_size`], if enabled.
4479    pub fn get_decommit_batch_size(&self) -> usize {
4480        self.decommit_batch_size
4481    }
4482
4483    /// Returns the configured
4484    /// [`PoolingAllocationConfig::max_unused_warm_slots`], if enabled.
4485    pub fn get_max_unused_warm_slots(&self) -> u32 {
4486        self.max_unused_warm_slots
4487    }
4488
4489    /// Returns the configured
4490    /// [`PoolingAllocationConfig::linear_memory_keep_resident`], if
4491    /// enabled.
4492    pub fn get_memory_keep_resident(&self) -> usize {
4493        self.linear_memory_keep_resident
4494    }
4495
4496    /// Returns the configured
4497    /// [`PoolingAllocationConfig::table_keep_resident`], if enabled.
4498    pub fn get_table_keep_resident(&self) -> usize {
4499        self.table_keep_resident
4500    }
4501
4502    /// Returns the configured
4503    /// [`PoolingAllocationConfig::async_stack_keep_resident`], if
4504    /// enabled.
4505    pub fn get_async_stack_keep_resident(&self) -> usize {
4506        self.async_stack_keep_resident
4507    }
4508
4509    /// Returns the configured
4510    /// [`PoolingAllocationConfig::memory_protection_keys`], if enabled.
4511    pub fn get_memory_protection_keys(&self) -> Enabled {
4512        self.memory_protection_keys
4513    }
4514
4515    /// Returns the configured
4516    /// [`PoolingAllocationConfig::max_memory_protection_keys`], if
4517    /// enabled.
4518    pub fn get_max_memory_protection_keys(&self) -> usize {
4519        self.max_memory_protection_keys
4520    }
4521
4522    /// Returns the configured
4523    /// [`PoolingAllocationConfig::pagemap_scan`], if enabled.
4524    pub fn get_pagemap_scan(&self) -> Enabled {
4525        self.pagemap_scan
4526    }
4527
4528    /// Returns the configured
4529    /// [`PoolingAllocationConfig::total_core_instances`], if enabled.
4530    pub fn get_total_core_instances(&self) -> u32 {
4531        self.limits.total_core_instances
4532    }
4533
4534    /// Returns the configured
4535    /// [`PoolingAllocationConfig::total_component_instances`], if
4536    /// enabled.
4537    pub fn get_total_component_instances(&self) -> u32 {
4538        self.limits.total_component_instances
4539    }
4540
4541    /// Returns the configured
4542    /// [`PoolingAllocationConfig::total_memories`], if enabled.
4543    pub fn get_total_memories(&self) -> u32 {
4544        self.limits.total_memories
4545    }
4546
4547    /// Returns the configured
4548    /// [`PoolingAllocationConfig::total_tables`], if enabled.
4549    pub fn get_total_tables(&self) -> u32 {
4550        self.limits.total_tables
4551    }
4552
4553    /// Returns the configured
4554    /// [`PoolingAllocationConfig::total_stacks`], if enabled.
4555    pub fn get_total_stacks(&self) -> u32 {
4556        self.limits.total_stacks
4557    }
4558
4559    /// Returns the configured
4560    /// [`PoolingAllocationConfig::total_gc_heaps`], if enabled.
4561    pub fn get_total_gc_heaps(&self) -> u32 {
4562        self.limits.total_gc_heaps
4563    }
4564
4565    /// Returns the configured
4566    /// [`PoolingAllocationConfig::max_memory_size`], if enabled.
4567    pub fn get_max_memory_size(&self) -> usize {
4568        self.limits.max_memory_size
4569    }
4570
4571    /// Returns the configured
4572    /// [`PoolingAllocationConfig::table_elements`], if enabled.
4573    pub fn get_table_elements(&self) -> usize {
4574        self.limits.table_elements
4575    }
4576
4577    /// Returns the configured
4578    /// [`PoolingAllocationConfig::max_core_instance_size`], if enabled.
4579    pub fn get_max_core_instance_size(&self) -> usize {
4580        self.limits.core_instance_size
4581    }
4582
4583    /// Returns the configured
4584    /// [`PoolingAllocationConfig::max_component_instance_size`], if
4585    /// enabled.
4586    pub fn get_max_component_instance_size(&self) -> usize {
4587        self.limits.component_instance_size
4588    }
4589
4590    /// Returns the configured
4591    /// [`PoolingAllocationConfig::max_core_instances_per_component`], if
4592    /// enabled.
4593    pub fn get_max_core_instances_per_component(&self) -> u32 {
4594        self.limits.max_core_instances_per_component
4595    }
4596
4597    /// Returns the configured
4598    /// [`PoolingAllocationConfig::max_memories_per_component`], if
4599    /// enabled.
4600    pub fn get_max_memories_per_component(&self) -> u32 {
4601        self.limits.max_memories_per_component
4602    }
4603
4604    /// Returns the configured
4605    /// [`PoolingAllocationConfig::max_tables_per_component`], if enabled.
4606    pub fn get_max_tables_per_component(&self) -> u32 {
4607        self.limits.max_tables_per_component
4608    }
4609
4610    /// Returns the configured
4611    /// [`PoolingAllocationConfig::max_tables_per_module`], if enabled.
4612    pub fn get_max_tables_per_module(&self) -> u32 {
4613        self.limits.max_tables_per_module
4614    }
4615
4616    /// Returns the configured
4617    /// [`PoolingAllocationConfig::max_memories_per_module`], if enabled.
4618    pub fn get_max_memories_per_module(&self) -> u32 {
4619        self.limits.max_memories_per_module
4620    }
4621}
4622
4623#[cfg(feature = "std")]
4624fn detect_host_feature(feature: &str) -> Option<bool> {
4625    #[cfg(target_arch = "aarch64")]
4626    {
4627        return match feature {
4628            "lse" => Some(std::arch::is_aarch64_feature_detected!("lse")),
4629            "paca" => Some(std::arch::is_aarch64_feature_detected!("paca")),
4630            "fp16" => Some(std::arch::is_aarch64_feature_detected!("fp16")),
4631            "dotprod" => Some(std::arch::is_aarch64_feature_detected!("dotprod")),
4632
4633            _ => None,
4634        };
4635    }
4636
4637    // `is_s390x_feature_detected` is nightly only for now, so use the
4638    // STORE FACILITY LIST EXTENDED instruction as a temporary measure.
4639    #[cfg(target_arch = "s390x")]
4640    {
4641        let mut facility_list: [u64; 4] = [0; 4];
4642        unsafe {
4643            core::arch::asm!(
4644                "stfle 0({})",
4645                in(reg_addr) facility_list.as_mut_ptr() ,
4646                inout("r0") facility_list.len() as u64 - 1 => _,
4647                options(nostack)
4648            );
4649        }
4650        let get_facility_bit = |n: usize| {
4651            // NOTE: bits are numbered from the left.
4652            facility_list[n / 64] & (1 << (63 - (n % 64))) != 0
4653        };
4654
4655        return match feature {
4656            "mie3" => Some(get_facility_bit(61)),
4657            "mie4" => Some(get_facility_bit(84)),
4658            "vxrs_ext2" => Some(get_facility_bit(148)),
4659            "vxrs_ext3" => Some(get_facility_bit(198)),
4660
4661            _ => None,
4662        };
4663    }
4664
4665    #[cfg(target_arch = "riscv64")]
4666    {
4667        return match feature {
4668            // due to `is_riscv64_feature_detected` is not stable.
4669            // we cannot use it. For now lie and say all features are always
4670            // found to keep tests working.
4671            _ => Some(true),
4672        };
4673    }
4674
4675    #[cfg(target_arch = "x86_64")]
4676    {
4677        return match feature {
4678            "cmpxchg16b" => Some(std::is_x86_feature_detected!("cmpxchg16b")),
4679            "sse3" => Some(std::is_x86_feature_detected!("sse3")),
4680            "ssse3" => Some(std::is_x86_feature_detected!("ssse3")),
4681            "sse4.1" => Some(std::is_x86_feature_detected!("sse4.1")),
4682            "sse4.2" => Some(std::is_x86_feature_detected!("sse4.2")),
4683            "popcnt" => Some(std::is_x86_feature_detected!("popcnt")),
4684            "avx" => Some(std::is_x86_feature_detected!("avx")),
4685            "avx2" => Some(std::is_x86_feature_detected!("avx2")),
4686            "fma" => Some(std::is_x86_feature_detected!("fma")),
4687            "bmi1" => Some(std::is_x86_feature_detected!("bmi1")),
4688            "bmi2" => Some(std::is_x86_feature_detected!("bmi2")),
4689            "avx512bitalg" => Some(std::is_x86_feature_detected!("avx512bitalg")),
4690            "avx512dq" => Some(std::is_x86_feature_detected!("avx512dq")),
4691            "avx512f" => Some(std::is_x86_feature_detected!("avx512f")),
4692            "avx512vl" => Some(std::is_x86_feature_detected!("avx512vl")),
4693            "avx512vbmi" => Some(std::is_x86_feature_detected!("avx512vbmi")),
4694            "lzcnt" => Some(std::is_x86_feature_detected!("lzcnt")),
4695
4696            _ => None,
4697        };
4698    }
4699
4700    #[allow(
4701        unreachable_code,
4702        reason = "reachable or not depending on if a target above matches"
4703    )]
4704    {
4705        let _ = feature;
4706        return None;
4707    }
4708}
4709
4710// What follows in this impl block is intended to be a somewhat-mechanical
4711// mostly-complete set of getters for relevant configuration options on
4712// `Config`. The `Config` type does not reflect a complete configuration so
4713// default values cannot be directly read from it. An `Engine`, however,
4714// represents a concrete and complete configuration with all default values
4715// fully specified. The purpose of these getters are then to perform a dual
4716// function of reflecting what was explicitly configured above as well as
4717// defaults that Wasmtime sets.
4718//
4719// The current pattern is:
4720//
4721// * All methods are `get_<config_name>`
4722// * Return values return `T` instead of `Option<T>` where possible unless the
4723//   state for `T` is completely missing.
4724//
4725// This impl is primarily in service of
4726// `wasmtime_cli_flags::CommonOptions::from_engine` at this time, and CLI flags
4727// are not as comprehensive as `Config` options, but it's expected that the set
4728// will settle/grow over time.
4729impl Engine {
4730    /// Returns the configured [`Config::memory_may_move`] value.
4731    pub fn get_memory_may_move(&self) -> bool {
4732        self.tunables().memory_may_move
4733    }
4734
4735    /// Returns the configured [`Config::memory_reservation`] value.
4736    pub fn get_memory_reservation(&self) -> u64 {
4737        self.tunables().memory_reservation
4738    }
4739
4740    /// Returns the configured [`Config::memory_reservation_for_growth`] value.
4741    pub fn get_memory_reservation_for_growth(&self) -> u64 {
4742        self.tunables().memory_reservation_for_growth
4743    }
4744
4745    /// Returns the configured [`Config::memory_guard_size`] value.
4746    pub fn get_memory_guard_size(&self) -> u64 {
4747        self.tunables().memory_guard_size
4748    }
4749
4750    /// Returns the configured [`Config::gc_heap_may_move`] value.
4751    pub fn get_gc_heap_may_move(&self) -> bool {
4752        self.tunables().gc_heap_may_move
4753    }
4754
4755    /// Returns the configured [`Config::gc_heap_reservation`] value.
4756    pub fn get_gc_heap_reservation(&self) -> u64 {
4757        self.tunables().gc_heap_reservation
4758    }
4759
4760    /// Returns the configured [`Config::gc_heap_reservation_for_growth`] value.
4761    pub fn get_gc_heap_reservation_for_growth(&self) -> u64 {
4762        self.tunables().gc_heap_reservation_for_growth
4763    }
4764
4765    /// Returns the configured [`Config::gc_heap_guard_size`] value.
4766    pub fn get_gc_heap_guard_size(&self) -> u64 {
4767        self.tunables().gc_heap_guard_size
4768    }
4769
4770    /// Returns the configured [`Config::guard_before_linear_memory`] value.
4771    pub fn get_guard_before_linear_memory(&self) -> bool {
4772        self.tunables().guard_before_linear_memory
4773    }
4774
4775    /// Returns the configured [`Config::table_lazy_init`] value.
4776    pub fn get_table_lazy_init(&self) -> bool {
4777        self.tunables().table_lazy_init
4778    }
4779
4780    /// Returns the configured [`Config::memory_init_cow`] value.
4781    pub fn get_memory_init_cow(&self) -> bool {
4782        self.tunables().memory_init_cow
4783    }
4784
4785    /// Returns the configured [`Config::memory_guaranteed_dense_image_size`] value.
4786    pub fn get_memory_guaranteed_dense_image_size(&self) -> u64 {
4787        self.config().memory_guaranteed_dense_image_size
4788    }
4789
4790    /// Returns the configured [`Config::signals_based_traps`] value.
4791    pub fn get_signals_based_traps(&self) -> bool {
4792        self.tunables().signals_based_traps
4793    }
4794
4795    /// Returns the configured [`Config::gc_zeal_alloc_counter`] value.
4796    pub fn get_gc_zeal_alloc_counter(&self) -> Option<core::num::NonZeroU32> {
4797        self.tunables().gc_zeal_alloc_counter
4798    }
4799
4800    /// Returns the configured [`Config::cranelift_opt_level`] value.
4801    pub fn get_cranelift_opt_level(&self) -> Option<OptLevel> {
4802        #[cfg(any(feature = "cranelift", feature = "winch"))]
4803        if let Some(compiler) = self.compiler() {
4804            let flags = compiler.flags();
4805            let (_, FlagValue::Enum(opt)) = flags.iter().find(|(f, _)| *f == "opt_level")? else {
4806                return None;
4807            };
4808            return match &opt[..] {
4809                "none" => Some(OptLevel::None),
4810                "speed" => Some(OptLevel::Speed),
4811                "speed_and_size" => Some(OptLevel::SpeedAndSize),
4812                _ => None,
4813            };
4814        }
4815        None
4816    }
4817
4818    /// Returns the configured [`Config::cranelift_regalloc_algorithm`] value.
4819    pub fn get_cranelift_regalloc_algorithm(&self) -> Option<RegallocAlgorithm> {
4820        #[cfg(any(feature = "cranelift", feature = "winch"))]
4821        if let Some(compiler) = self.compiler() {
4822            let flags = compiler.flags();
4823            let (_, FlagValue::Enum(opt)) =
4824                flags.iter().find(|(f, _)| *f == "regalloc_algorithm")?
4825            else {
4826                return None;
4827            };
4828            return match &opt[..] {
4829                "backtracking" => Some(RegallocAlgorithm::Backtracking),
4830                "single_pass" => Some(RegallocAlgorithm::SinglePass),
4831                _ => None,
4832            };
4833        }
4834        None
4835    }
4836
4837    /// Returns the configured [`Config::strategy`] value.
4838    pub fn get_strategy(&self) -> Option<Strategy> {
4839        #[cfg(any(feature = "cranelift", feature = "winch"))]
4840        return self.config().compiler_config.as_ref()?.strategy;
4841        #[cfg(not(any(feature = "cranelift", feature = "winch")))]
4842        return None;
4843    }
4844
4845    /// Returns the configured [`Config::collector`] value.
4846    pub fn get_collector(&self) -> Option<Collector> {
4847        #[cfg(feature = "gc")]
4848        return Some(self.config().collector);
4849        #[cfg(not(feature = "gc"))]
4850        return None;
4851    }
4852
4853    /// Returns the configured [`Config::cranelift_debug_verifier`] value.
4854    pub fn get_cranelift_debug_verifier(&self) -> Option<bool> {
4855        #[cfg(any(feature = "cranelift", feature = "winch"))]
4856        if let Some(compiler) = self.compiler() {
4857            let flags = compiler.flags();
4858            let (_, FlagValue::Bool(b)) = flags.iter().find(|(f, _)| *f == "enable_verifier")?
4859            else {
4860                return None;
4861            };
4862            return Some(*b);
4863        }
4864        None
4865    }
4866
4867    /// Returns the configured [`Config::compiler_inlining`] value.
4868    pub fn get_compiler_inlining(&self) -> Inlining {
4869        self.tunables().inlining
4870    }
4871
4872    /// Returns the configured [`Config::native_unwind_info`] value.
4873    pub fn get_native_unwind_info(&self) -> Option<bool> {
4874        #[cfg(any(feature = "cranelift", feature = "winch"))]
4875        if let Some(compiler) = self.compiler() {
4876            let flags = compiler.flags();
4877            let (_, FlagValue::Bool(b)) = flags.iter().find(|(f, _)| *f == "unwind_info")? else {
4878                return None;
4879            };
4880            return Some(*b);
4881        }
4882        None
4883    }
4884
4885    /// Returns the configured [`Config::parallel_compilation`] value.
4886    pub fn get_parallel_compilation(&self) -> bool {
4887        self.config().parallel_compilation
4888    }
4889
4890    /// Returns the configured [`Config::metadata_for_internal_asserts`] value.
4891    pub fn get_metadata_for_internal_asserts(&self) -> bool {
4892        self.tunables().metadata_for_internal_asserts
4893    }
4894
4895    /// Returns the configured [`Config::metadata_for_gc_heap_corruption`] value.
4896    pub fn get_metadata_for_gc_heap_corruption(&self) -> bool {
4897        self.tunables().metadata_for_gc_heap_corruption
4898    }
4899
4900    /// Returns the runtime pooling allocator configuration, if the pooling
4901    /// allocator is in use.
4902    pub fn get_pooling_config(&self) -> Option<&PoolingAllocationConfig> {
4903        #[cfg(feature = "pooling-allocator")]
4904        {
4905            Some(self.allocator().as_pooling()?.config())
4906        }
4907        #[cfg(not(feature = "pooling-allocator"))]
4908        {
4909            None
4910        }
4911    }
4912
4913    /// Returns the configured wasm proposals enabled in this engine.
4914    pub fn get_wasm_features(&self) -> WasmFeatures {
4915        self.features()
4916    }
4917
4918    /// Returns the configured [`Config::async_stack_size`] value.
4919    pub fn get_async_stack_size(&self) -> usize {
4920        self.config().async_stack_size
4921    }
4922
4923    /// Returns the configured [`Config::async_stack_zeroing`] value.
4924    pub fn get_async_stack_zeroing(&self) -> bool {
4925        self.config().async_stack_zeroing
4926    }
4927
4928    /// Returns the configured [`Config::wasm_branch_hinting`] value.
4929    pub fn get_wasm_branch_hinting(&self) -> bool {
4930        self.tunables().branch_hinting
4931    }
4932
4933    /// Returns the configured [`Config::concurrency_support`] value.
4934    pub fn get_concurrency_support(&self) -> bool {
4935        self.tunables().concurrency_support
4936    }
4937
4938    /// Returns the configured [`Config::epoch_interruption`] value.
4939    pub fn get_epoch_interruption(&self) -> bool {
4940        self.tunables().epoch_interruption
4941    }
4942
4943    /// Returns the configured [`Config::consume_fuel`] value.
4944    pub fn get_consume_fuel(&self) -> bool {
4945        self.tunables().consume_fuel
4946    }
4947
4948    /// Returns the configured [`Config::max_wasm_stack`] value.
4949    pub fn get_max_wasm_stack(&self) -> usize {
4950        self.config().max_wasm_stack
4951    }
4952
4953    /// Returns the configured [`Config::cranelift_nan_canonicalization`] value.
4954    pub fn get_cranelift_nan_canonicalization(&self) -> Option<bool> {
4955        #[cfg(any(feature = "cranelift", feature = "winch"))]
4956        if let Some(compiler) = self.compiler() {
4957            let flags = compiler.flags();
4958            let (_, FlagValue::Bool(b)) = flags
4959                .iter()
4960                .find(|(f, _)| *f == "enable_nan_canonicalization")?
4961            else {
4962                return None;
4963            };
4964            return Some(*b);
4965        }
4966        None
4967    }
4968
4969    /// Returns the configured [`Config::relaxed_simd_deterministic`] value.
4970    pub fn get_relaxed_simd_deterministic(&self) -> bool {
4971        self.tunables().relaxed_simd_deterministic
4972    }
4973
4974    /// Returns the configured [`Config::shared_memory`] value.
4975    pub fn get_shared_memory(&self) -> bool {
4976        self.config().shared_memory
4977    }
4978
4979    /// Returns the configured [`Config::generate_address_map`] value.
4980    pub fn get_generate_address_map(&self) -> bool {
4981        self.tunables().generate_address_map
4982    }
4983
4984    /// Returns the configured [`Config::debug_info`] value.
4985    pub fn get_debug_info(&self) -> bool {
4986        self.tunables().debug_native
4987    }
4988
4989    /// Returns the configured [`Config::guest_debug`] value.
4990    pub fn get_guest_debug(&self) -> bool {
4991        self.tunables().debug_guest
4992    }
4993
4994    /// Returns the configured [`Config::debug_symbols`] value.
4995    pub fn get_debug_symbols(&self) -> bool {
4996        self.tunables().debug_symbols
4997    }
4998
4999    /// Returns the configured [`Config::wasm_backtrace_max_frames`] value.
5000    pub fn get_wasm_backtrace_max_frames(&self) -> usize {
5001        self.config()
5002            .wasm_backtrace_max_frames
5003            .map(|f| f.get())
5004            .unwrap_or(0)
5005    }
5006
5007    /// Returns the configured [`Config::target`] value.
5008    pub fn get_target(&self) -> Option<String> {
5009        #[cfg(any(feature = "cranelift", feature = "winch"))]
5010        if let Some(compiler) = self.compiler() {
5011            return Some(compiler.triple().to_string());
5012        }
5013        None
5014    }
5015
5016    /// Returns the enabled flags via [`Config::cranelift_flag_enable`].
5017    pub fn get_cranelift_flags_enabled(&self) -> impl Iterator<Item = &str> {
5018        #[cfg(any(feature = "cranelift", feature = "winch"))]
5019        if let Some(config) = &self.config().compiler_config {
5020            return config
5021                .flags
5022                .iter()
5023                .filter_map(|(k, v)| match v {
5024                    UserSpecified::Yes => Some(k.as_str()),
5025                    UserSpecified::No => None,
5026                })
5027                .collect::<Vec<_>>()
5028                .into_iter();
5029        }
5030
5031        Vec::new().into_iter()
5032    }
5033
5034    /// Returns the enabled flags via [`Config::cranelift_flag_set`].
5035    pub fn get_cranelift_flags_set(&self) -> impl Iterator<Item = (&str, &str)> {
5036        #[cfg(any(feature = "cranelift", feature = "winch"))]
5037        if let Some(config) = &self.config().compiler_config {
5038            return config
5039                .settings
5040                .iter()
5041                .filter_map(|(k, (v, s))| match s {
5042                    UserSpecified::Yes => Some((k.as_str(), v.as_str())),
5043                    UserSpecified::No => None,
5044                })
5045                .collect::<Vec<_>>()
5046                .into_iter();
5047        }
5048
5049        Vec::new().into_iter()
5050    }
5051}