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