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