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