dusk_wasmtime/
engine.rs

1#[cfg(feature = "runtime")]
2use crate::runtime::type_registry::TypeRegistry;
3use crate::Config;
4use anyhow::{Context, Result};
5#[cfg(any(feature = "cranelift", feature = "winch"))]
6use object::write::{Object, StandardSegment};
7use object::SectionKind;
8use once_cell::sync::OnceCell;
9use std::path::Path;
10use std::sync::atomic::{AtomicU64, Ordering};
11use std::sync::Arc;
12use wasmtime_environ::obj;
13use wasmtime_environ::{FlagValue, ObjectKind, Tunables};
14#[cfg(feature = "runtime")]
15use wasmtime_runtime::GcRuntime;
16
17mod serialization;
18
19/// An `Engine` which is a global context for compilation and management of wasm
20/// modules.
21///
22/// An engine can be safely shared across threads and is a cheap cloneable
23/// handle to the actual engine. The engine itself will be deallocated once all
24/// references to it have gone away.
25///
26/// Engines store global configuration preferences such as compilation settings,
27/// enabled features, etc. You'll likely only need at most one of these for a
28/// program.
29///
30/// ## Engines and `Clone`
31///
32/// Using `clone` on an `Engine` is a cheap operation. It will not create an
33/// entirely new engine, but rather just a new reference to the existing engine.
34/// In other words it's a shallow copy, not a deep copy.
35///
36/// ## Engines and `Default`
37///
38/// You can create an engine with default configuration settings using
39/// `Engine::default()`. Be sure to consult the documentation of [`Config`] for
40/// default settings.
41#[derive(Clone)]
42pub struct Engine {
43    inner: Arc<EngineInner>,
44}
45
46struct EngineInner {
47    config: Config,
48    tunables: Tunables,
49    #[cfg(any(feature = "cranelift", feature = "winch"))]
50    compiler: Box<dyn wasmtime_environ::Compiler>,
51    #[cfg(feature = "runtime")]
52    allocator: Box<dyn wasmtime_runtime::InstanceAllocator + Send + Sync>,
53    #[cfg(feature = "runtime")]
54    gc_runtime: Arc<dyn GcRuntime>,
55    #[cfg(feature = "runtime")]
56    profiler: Box<dyn crate::profiling_agent::ProfilingAgent>,
57    #[cfg(feature = "runtime")]
58    signatures: TypeRegistry,
59    #[cfg(feature = "runtime")]
60    epoch: AtomicU64,
61    #[cfg(feature = "runtime")]
62    unique_id_allocator: wasmtime_runtime::CompiledModuleIdAllocator,
63
64    /// One-time check of whether the compiler's settings, if present, are
65    /// compatible with the native host.
66    compatible_with_native_host: OnceCell<Result<(), String>>,
67}
68
69impl Default for Engine {
70    fn default() -> Engine {
71        Engine::new(&Config::default()).unwrap()
72    }
73}
74
75impl Engine {
76    /// Creates a new [`Engine`] with the specified compilation and
77    /// configuration settings.
78    ///
79    /// # Errors
80    ///
81    /// This method can fail if the `config` is invalid or some
82    /// configurations are incompatible.
83    ///
84    /// For example, feature `reference_types` will need to set
85    /// the compiler setting `enable_safepoints` and `unwind_info`
86    /// to `true`, but explicitly disable these two compiler settings
87    /// will cause errors.
88    pub fn new(config: &Config) -> Result<Engine> {
89        #[cfg(feature = "runtime")]
90        {
91            // Ensure that wasmtime_runtime's signal handlers are configured. This
92            // is the per-program initialization required for handling traps, such
93            // as configuring signals, vectored exception handlers, etc.
94            wasmtime_runtime::init_traps(crate::module::get_wasm_trap, config.macos_use_mach_ports);
95            #[cfg(feature = "debug-builtins")]
96            wasmtime_runtime::debug_builtins::ensure_exported();
97        }
98
99        let config = config.clone();
100        let tunables = config.validate()?;
101
102        #[cfg(any(feature = "cranelift", feature = "winch"))]
103        let (config, compiler) = config.build_compiler(&tunables)?;
104
105        Ok(Engine {
106            inner: Arc::new(EngineInner {
107                #[cfg(any(feature = "cranelift", feature = "winch"))]
108                compiler,
109                #[cfg(feature = "runtime")]
110                allocator: config.build_allocator(&tunables)?,
111                #[cfg(feature = "runtime")]
112                gc_runtime: config.build_gc_runtime()?,
113                #[cfg(feature = "runtime")]
114                profiler: config.build_profiler()?,
115                #[cfg(feature = "runtime")]
116                signatures: TypeRegistry::new(),
117                #[cfg(feature = "runtime")]
118                epoch: AtomicU64::new(0),
119                #[cfg(feature = "runtime")]
120                unique_id_allocator: wasmtime_runtime::CompiledModuleIdAllocator::new(),
121                compatible_with_native_host: OnceCell::new(),
122                config,
123                tunables,
124            }),
125        })
126    }
127
128    /// Returns the configuration settings that this engine is using.
129    #[inline]
130    pub fn config(&self) -> &Config {
131        &self.inner.config
132    }
133
134    pub(crate) fn run_maybe_parallel<
135        A: Send,
136        B: Send,
137        E: Send,
138        F: Fn(A) -> Result<B, E> + Send + Sync,
139    >(
140        &self,
141        input: Vec<A>,
142        f: F,
143    ) -> Result<Vec<B>, E> {
144        if self.config().parallel_compilation {
145            #[cfg(feature = "parallel-compilation")]
146            {
147                use rayon::prelude::*;
148                return input
149                    .into_par_iter()
150                    .map(|a| f(a))
151                    .collect::<Result<Vec<B>, E>>();
152            }
153        }
154
155        // In case the parallel-compilation feature is disabled or the parallel_compilation config
156        // was turned off dynamically fallback to the non-parallel version.
157        input
158            .into_iter()
159            .map(|a| f(a))
160            .collect::<Result<Vec<B>, E>>()
161    }
162
163    /// Take a weak reference to this engine.
164    pub fn weak(&self) -> EngineWeak {
165        EngineWeak {
166            inner: Arc::downgrade(&self.inner),
167        }
168    }
169
170    pub(crate) fn tunables(&self) -> &Tunables {
171        &self.inner.tunables
172    }
173
174    /// Returns whether the engine `a` and `b` refer to the same configuration.
175    #[inline]
176    pub fn same(a: &Engine, b: &Engine) -> bool {
177        Arc::ptr_eq(&a.inner, &b.inner)
178    }
179
180    /// Detects whether the bytes provided are a precompiled object produced by
181    /// Wasmtime.
182    ///
183    /// This function will inspect the header of `bytes` to determine if it
184    /// looks like a precompiled core wasm module or a precompiled component.
185    /// This does not validate the full structure or guarantee that
186    /// deserialization will succeed, instead it helps higher-levels of the
187    /// stack make a decision about what to do next when presented with the
188    /// `bytes` as an input module.
189    ///
190    /// If the `bytes` looks like a precompiled object previously produced by
191    /// [`Module::serialize`](crate::Module::serialize),
192    /// [`Component::serialize`](crate::component::Component::serialize),
193    /// [`Engine::precompile_module`], or [`Engine::precompile_component`], then
194    /// this will return `Some(...)` indicating so. Otherwise `None` is
195    /// returned.
196    pub fn detect_precompiled(&self, bytes: &[u8]) -> Option<Precompiled> {
197        serialization::detect_precompiled_bytes(bytes)
198    }
199
200    /// Like [`Engine::detect_precompiled`], but performs the detection on a file.
201    pub fn detect_precompiled_file(&self, path: impl AsRef<Path>) -> Result<Option<Precompiled>> {
202        serialization::detect_precompiled_file(path)
203    }
204
205    /// Returns the target triple which this engine is compiling code for
206    /// and/or running code for.
207    pub(crate) fn target(&self) -> target_lexicon::Triple {
208        // If a compiler is configured, use that target.
209        #[cfg(any(feature = "cranelift", feature = "winch"))]
210        return self.compiler().triple().clone();
211
212        // ... otherwise it's the native target
213        #[cfg(not(any(feature = "cranelift", feature = "winch")))]
214        return target_lexicon::Triple::host();
215    }
216
217    /// Verify that this engine's configuration is compatible with loading
218    /// modules onto the native host platform.
219    ///
220    /// This method is used as part of `Module::new` to ensure that this
221    /// engine can indeed load modules for the configured compiler (if any).
222    /// Note that if cranelift is disabled this trivially returns `Ok` because
223    /// loaded serialized modules are checked separately.
224    pub(crate) fn check_compatible_with_native_host(&self) -> Result<()> {
225        self.inner
226            .compatible_with_native_host
227            .get_or_init(|| self._check_compatible_with_native_host())
228            .clone()
229            .map_err(anyhow::Error::msg)
230    }
231
232    fn _check_compatible_with_native_host(&self) -> Result<(), String> {
233        #[cfg(any(feature = "cranelift", feature = "winch"))]
234        {
235            let compiler = self.compiler();
236
237            // Check to see that the config's target matches the host
238            let target = compiler.triple();
239            if *target != target_lexicon::Triple::host() {
240                return Err(format!(
241                    "target '{}' specified in the configuration does not match the host",
242                    target
243                ));
244            }
245
246            // Also double-check all compiler settings
247            for (key, value) in compiler.flags().iter() {
248                self.check_compatible_with_shared_flag(key, value)?;
249            }
250            for (key, value) in compiler.isa_flags().iter() {
251                self.check_compatible_with_isa_flag(key, value)?;
252            }
253        }
254        Ok(())
255    }
256
257    /// Checks to see whether the "shared flag", something enabled for
258    /// individual compilers, is compatible with the native host platform.
259    ///
260    /// This is used both when validating an engine's compilation settings are
261    /// compatible with the host as well as when deserializing modules from
262    /// disk to ensure they're compatible with the current host.
263    ///
264    /// Note that most of the settings here are not configured by users that
265    /// often. While theoretically possible via `Config` methods the more
266    /// interesting flags are the ISA ones below. Typically the values here
267    /// represent global configuration for wasm features. Settings here
268    /// currently rely on the compiler informing us of all settings, including
269    /// those disabled. Settings then fall in a few buckets:
270    ///
271    /// * Some settings must be enabled, such as `preserve_frame_pointers`.
272    /// * Some settings must have a particular value, such as
273    ///   `libcall_call_conv`.
274    /// * Some settings do not matter as to their value, such as `opt_level`.
275    pub(crate) fn check_compatible_with_shared_flag(
276        &self,
277        flag: &str,
278        value: &FlagValue,
279    ) -> Result<(), String> {
280        let target = self.target();
281        let ok = match flag {
282            // These settings must all have be enabled, since their value
283            // can affect the way the generated code performs or behaves at
284            // runtime.
285            "libcall_call_conv" => *value == FlagValue::Enum("isa_default".into()),
286            "preserve_frame_pointers" => *value == FlagValue::Bool(true),
287            "enable_probestack" => *value == FlagValue::Bool(crate::config::probestack_supported(target.architecture)),
288            "probestack_strategy" => *value == FlagValue::Enum("inline".into()),
289
290            // Features wasmtime doesn't use should all be disabled, since
291            // otherwise if they are enabled it could change the behavior of
292            // generated code.
293            "enable_llvm_abi_extensions" => *value == FlagValue::Bool(false),
294            "enable_pinned_reg" => *value == FlagValue::Bool(false),
295            "use_colocated_libcalls" => *value == FlagValue::Bool(false),
296            "use_pinned_reg_as_heap_base" => *value == FlagValue::Bool(false),
297
298            // If reference types (or anything that depends on reference types,
299            // like typed function references and GC) are enabled this must be
300            // enabled, otherwise this setting can have any value.
301            "enable_safepoints" => {
302                if self.config().features.reference_types {
303                    *value == FlagValue::Bool(true)
304                } else {
305                    return Ok(())
306                }
307            }
308
309            // Windows requires unwind info as part of its ABI.
310            "unwind_info" => {
311                if target.operating_system == target_lexicon::OperatingSystem::Windows {
312                    *value == FlagValue::Bool(true)
313                } else {
314                    return Ok(())
315                }
316            }
317
318            // These settings don't affect the interface or functionality of
319            // the module itself, so their configuration values shouldn't
320            // matter.
321            "enable_heap_access_spectre_mitigation"
322            | "enable_table_access_spectre_mitigation"
323            | "enable_nan_canonicalization"
324            | "enable_jump_tables"
325            | "enable_float"
326            | "enable_verifier"
327            | "enable_pcc"
328            | "regalloc_checker"
329            | "regalloc_verbose_logs"
330            | "is_pic"
331            | "bb_padding_log2_minus_one"
332            | "machine_code_cfg_info"
333            | "tls_model" // wasmtime doesn't use tls right now
334            | "opt_level" // opt level doesn't change semantics
335            | "enable_alias_analysis" // alias analysis-based opts don't change semantics
336            | "probestack_func_adjusts_sp" // probestack above asserted disabled
337            | "probestack_size_log2" // probestack above asserted disabled
338            | "regalloc" // shouldn't change semantics
339            | "enable_incremental_compilation_cache_checks" // shouldn't change semantics
340            | "enable_atomics" => return Ok(()),
341
342            // Everything else is unknown and needs to be added somewhere to
343            // this list if encountered.
344            _ => {
345                return Err(format!("unknown shared setting {:?} configured to {:?}", flag, value))
346            }
347        };
348
349        if !ok {
350            return Err(format!(
351                "setting {:?} is configured to {:?} which is not supported",
352                flag, value,
353            ));
354        }
355        Ok(())
356    }
357
358    /// Same as `check_compatible_with_native_host` except used for ISA-specific
359    /// flags. This is used to test whether a configured ISA flag is indeed
360    /// available on the host platform itself.
361    pub(crate) fn check_compatible_with_isa_flag(
362        &self,
363        flag: &str,
364        value: &FlagValue,
365    ) -> Result<(), String> {
366        match value {
367            // ISA flags are used for things like CPU features, so if they're
368            // disabled then it's compatible with the native host.
369            FlagValue::Bool(false) => return Ok(()),
370
371            // Fall through below where we test at runtime that features are
372            // available.
373            FlagValue::Bool(true) => {}
374
375            // Only `bool` values are supported right now, other settings would
376            // need more support here.
377            _ => {
378                return Err(format!(
379                    "isa-specific feature {:?} configured to unknown value {:?}",
380                    flag, value
381                ))
382            }
383        }
384
385        #[allow(unused_assignments)]
386        let mut enabled = None;
387
388        #[cfg(target_arch = "aarch64")]
389        {
390            enabled = match flag {
391                "has_lse" => Some(std::arch::is_aarch64_feature_detected!("lse")),
392                // No effect on its own, but in order to simplify the code on a
393                // platform without pointer authentication support we fail if
394                // "has_pauth" is enabled, but "sign_return_address" is not.
395                "has_pauth" => Some(std::arch::is_aarch64_feature_detected!("paca")),
396                // No effect on its own.
397                "sign_return_address_all" => Some(true),
398                // The pointer authentication instructions act as a `NOP` when
399                // unsupported (but keep in mind "has_pauth" as well), so it is
400                // safe to enable them.
401                "sign_return_address" => Some(true),
402                // No effect on its own.
403                "sign_return_address_with_bkey" => Some(true),
404                // The `BTI` instruction acts as a `NOP` when unsupported, so it
405                // is safe to enable it.
406                "use_bti" => Some(true),
407                // fall through to the very bottom to indicate that support is
408                // not enabled to test whether this feature is enabled on the
409                // host.
410                _ => None,
411            };
412        }
413
414        // There is no is_s390x_feature_detected macro yet, so for now
415        // we use getauxval from the libc crate directly.
416        #[cfg(all(target_arch = "s390x", target_os = "linux"))]
417        {
418            let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
419            const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768;
420
421            enabled = match flag {
422                // There is no separate HWCAP bit for mie2, so assume
423                // that any machine with vxrs_ext2 also has mie2.
424                "has_vxrs_ext2" | "has_mie2" => Some((v & HWCAP_S390X_VXRS_EXT2) != 0),
425                // fall through to the very bottom to indicate that support is
426                // not enabled to test whether this feature is enabled on the
427                // host.
428                _ => None,
429            }
430        }
431
432        #[cfg(target_arch = "riscv64")]
433        {
434            enabled = match flag {
435                // make sure `test_isa_flags_mismatch` test pass.
436                "not_a_flag" => None,
437                // due to `is_riscv64_feature_detected` is not stable.
438                // we cannot use it.
439                _ => Some(true),
440            }
441        }
442
443        #[cfg(target_arch = "x86_64")]
444        {
445            enabled = match flag {
446                "has_sse3" => Some(std::is_x86_feature_detected!("sse3")),
447                "has_ssse3" => Some(std::is_x86_feature_detected!("ssse3")),
448                "has_sse41" => Some(std::is_x86_feature_detected!("sse4.1")),
449                "has_sse42" => Some(std::is_x86_feature_detected!("sse4.2")),
450                "has_popcnt" => Some(std::is_x86_feature_detected!("popcnt")),
451                "has_avx" => Some(std::is_x86_feature_detected!("avx")),
452                "has_avx2" => Some(std::is_x86_feature_detected!("avx2")),
453                "has_fma" => Some(std::is_x86_feature_detected!("fma")),
454                "has_bmi1" => Some(std::is_x86_feature_detected!("bmi1")),
455                "has_bmi2" => Some(std::is_x86_feature_detected!("bmi2")),
456                "has_avx512bitalg" => Some(std::is_x86_feature_detected!("avx512bitalg")),
457                "has_avx512dq" => Some(std::is_x86_feature_detected!("avx512dq")),
458                "has_avx512f" => Some(std::is_x86_feature_detected!("avx512f")),
459                "has_avx512vl" => Some(std::is_x86_feature_detected!("avx512vl")),
460                "has_avx512vbmi" => Some(std::is_x86_feature_detected!("avx512vbmi")),
461                "has_lzcnt" => Some(std::is_x86_feature_detected!("lzcnt")),
462
463                // fall through to the very bottom to indicate that support is
464                // not enabled to test whether this feature is enabled on the
465                // host.
466                _ => None,
467            };
468        }
469
470        #[cfg(target_family = "wasm")]
471        {
472            let _ = &mut enabled;
473        }
474
475        match enabled {
476            Some(true) => return Ok(()),
477            Some(false) => {
478                return Err(format!(
479                    "compilation setting {:?} is enabled, but not available on the host",
480                    flag
481                ))
482            }
483            // fall through
484            None => {}
485        }
486
487        Err(format!(
488            "cannot test if target-specific flag {:?} is available at runtime",
489            flag
490        ))
491    }
492}
493
494#[cfg(any(feature = "cranelift", feature = "winch"))]
495#[cfg_attr(docsrs, doc(cfg(any(feature = "cranelift", feature = "winch"))))]
496impl Engine {
497    pub(crate) fn compiler(&self) -> &dyn wasmtime_environ::Compiler {
498        &*self.inner.compiler
499    }
500
501    /// Ahead-of-time (AOT) compiles a WebAssembly module.
502    ///
503    /// The `bytes` provided must be in one of two formats:
504    ///
505    /// * A [binary-encoded][binary] WebAssembly module. This is always supported.
506    /// * A [text-encoded][text] instance of the WebAssembly text format.
507    ///   This is only supported when the `wat` feature of this crate is enabled.
508    ///   If this is supplied then the text format will be parsed before validation.
509    ///   Note that the `wat` feature is enabled by default.
510    ///
511    /// This method may be used to compile a module for use with a different target
512    /// host. The output of this method may be used with
513    /// [`Module::deserialize`](crate::Module::deserialize) on hosts compatible
514    /// with the [`Config`](crate::Config) associated with this [`Engine`].
515    ///
516    /// The output of this method is safe to send to another host machine for later
517    /// execution. As the output is already a compiled module, translation and code
518    /// generation will be skipped and this will improve the performance of constructing
519    /// a [`Module`](crate::Module) from the output of this method.
520    ///
521    /// [binary]: https://webassembly.github.io/spec/core/binary/index.html
522    /// [text]: https://webassembly.github.io/spec/core/text/index.html
523    pub fn precompile_module(&self, bytes: &[u8]) -> Result<Vec<u8>> {
524        crate::CodeBuilder::new(self)
525            .wasm(bytes, None)?
526            .compile_module_serialized()
527    }
528
529    /// Same as [`Engine::precompile_module`] except for a
530    /// [`Component`](crate::component::Component)
531    #[cfg(feature = "component-model")]
532    #[cfg_attr(docsrs, doc(cfg(feature = "component-model")))]
533    pub fn precompile_component(&self, bytes: &[u8]) -> Result<Vec<u8>> {
534        crate::CodeBuilder::new(self)
535            .wasm(bytes, None)?
536            .compile_component_serialized()
537    }
538
539    /// Produces a blob of bytes by serializing the `engine`'s configuration data to
540    /// be checked, perhaps in a different process, with the `check_compatible`
541    /// method below.
542    ///
543    /// The blob of bytes is inserted into the object file specified to become part
544    /// of the final compiled artifact.
545    pub(crate) fn append_compiler_info(&self, obj: &mut Object<'_>) {
546        serialization::append_compiler_info(self, obj, &serialization::Metadata::new(&self))
547    }
548
549    pub(crate) fn append_bti(&self, obj: &mut Object<'_>) {
550        let section = obj.add_section(
551            obj.segment_name(StandardSegment::Data).to_vec(),
552            obj::ELF_WASM_BTI.as_bytes().to_vec(),
553            SectionKind::ReadOnlyData,
554        );
555        let contents = if self.compiler().is_branch_protection_enabled() {
556            1
557        } else {
558            0
559        };
560        obj.append_section_data(section, &[contents], 1);
561    }
562}
563
564/// Return value from the [`Engine::detect_precompiled`] API.
565#[derive(PartialEq, Eq, Copy, Clone, Debug)]
566pub enum Precompiled {
567    /// The input bytes look like a precompiled core wasm module.
568    Module,
569    /// The input bytes look like a precompiled wasm component.
570    Component,
571}
572
573#[cfg(feature = "runtime")]
574#[cfg_attr(docsrs, doc(cfg(feature = "runtime")))]
575impl Engine {
576    /// Eagerly initialize thread-local functionality shared by all [`Engine`]s.
577    ///
578    /// Wasmtime's implementation on some platforms may involve per-thread
579    /// setup that needs to happen whenever WebAssembly is invoked. This setup
580    /// can take on the order of a few hundred microseconds, whereas the
581    /// overhead of calling WebAssembly is otherwise on the order of a few
582    /// nanoseconds. This setup cost is paid once per-OS-thread. If your
583    /// application is sensitive to the latencies of WebAssembly function
584    /// calls, even those that happen first on a thread, then this function
585    /// can be used to improve the consistency of each call into WebAssembly
586    /// by explicitly frontloading the cost of the one-time setup per-thread.
587    ///
588    /// Note that this function is not required to be called in any embedding.
589    /// Wasmtime will automatically initialize thread-local-state as necessary
590    /// on calls into WebAssembly. This is provided for use cases where the
591    /// latency of WebAssembly calls are extra-important, which is not
592    /// necessarily true of all embeddings.
593    pub fn tls_eager_initialize() {
594        wasmtime_runtime::tls_eager_initialize();
595    }
596
597    pub(crate) fn allocator(&self) -> &dyn wasmtime_runtime::InstanceAllocator {
598        self.inner.allocator.as_ref()
599    }
600
601    pub(crate) fn gc_runtime(&self) -> &Arc<dyn GcRuntime> {
602        &self.inner.gc_runtime
603    }
604
605    pub(crate) fn profiler(&self) -> &dyn crate::profiling_agent::ProfilingAgent {
606        self.inner.profiler.as_ref()
607    }
608
609    #[cfg(feature = "cache")]
610    pub(crate) fn cache_config(&self) -> &wasmtime_cache::CacheConfig {
611        &self.config().cache_config
612    }
613
614    pub(crate) fn signatures(&self) -> &TypeRegistry {
615        &self.inner.signatures
616    }
617
618    pub(crate) fn epoch_counter(&self) -> &AtomicU64 {
619        &self.inner.epoch
620    }
621
622    pub(crate) fn current_epoch(&self) -> u64 {
623        self.epoch_counter().load(Ordering::Relaxed)
624    }
625
626    /// Increments the epoch.
627    ///
628    /// When using epoch-based interruption, currently-executing Wasm
629    /// code within this engine will trap or yield "soon" when the
630    /// epoch deadline is reached or exceeded. (The configuration, and
631    /// the deadline, are set on the `Store`.) The intent of the
632    /// design is for this method to be called by the embedder at some
633    /// regular cadence, for example by a thread that wakes up at some
634    /// interval, or by a signal handler.
635    ///
636    /// See [`Config::epoch_interruption`](crate::Config::epoch_interruption)
637    /// for an introduction to epoch-based interruption and pointers
638    /// to the other relevant methods.
639    ///
640    /// When performing `increment_epoch` in a separate thread, consider using
641    /// [`Engine::weak`] to hold an [`EngineWeak`](crate::EngineWeak) and
642    /// performing [`EngineWeak::upgrade`](crate::EngineWeak::upgrade) on each
643    /// tick, so that the epoch ticking thread does not keep an [`Engine`] alive
644    /// longer than any of its consumers.
645    ///
646    /// ## Signal Safety
647    ///
648    /// This method is signal-safe: it does not make any syscalls, and
649    /// performs only an atomic increment to the epoch value in
650    /// memory.
651    pub fn increment_epoch(&self) {
652        self.inner.epoch.fetch_add(1, Ordering::Relaxed);
653    }
654
655    pub(crate) fn unique_id_allocator(&self) -> &wasmtime_runtime::CompiledModuleIdAllocator {
656        &self.inner.unique_id_allocator
657    }
658
659    /// Returns a [`std::hash::Hash`] that can be used to check precompiled WebAssembly compatibility.
660    ///
661    /// The outputs of [`Engine::precompile_module`] and [`Engine::precompile_component`]
662    /// are compatible with a different [`Engine`] instance only if the two engines use
663    /// compatible [`Config`]s. If this Hash matches between two [`Engine`]s then binaries
664    /// from one are guaranteed to deserialize in the other.
665    #[cfg(any(feature = "cranelift", feature = "winch"))]
666    #[cfg_attr(docsrs, doc(cfg(feature = "cranelift")))] // see build.rs
667    pub fn precompile_compatibility_hash(&self) -> impl std::hash::Hash + '_ {
668        crate::compile::HashedEngineCompileEnv(self)
669    }
670
671    /// Executes `f1` and `f2` in parallel if parallel compilation is enabled at
672    /// both runtime and compile time, otherwise runs them synchronously.
673    #[allow(dead_code)] // only used for the component-model feature right now
674    pub(crate) fn join_maybe_parallel<T, U>(
675        &self,
676        f1: impl FnOnce() -> T + Send,
677        f2: impl FnOnce() -> U + Send,
678    ) -> (T, U)
679    where
680        T: Send,
681        U: Send,
682    {
683        if self.config().parallel_compilation {
684            #[cfg(feature = "parallel-compilation")]
685            return rayon::join(f1, f2);
686        }
687        (f1(), f2())
688    }
689
690    /// Loads a `CodeMemory` from the specified in-memory slice, copying it to a
691    /// uniquely owned mmap.
692    ///
693    /// The `expected` marker here is whether the bytes are expected to be a
694    /// precompiled module or a component.
695    pub(crate) fn load_code_bytes(
696        &self,
697        bytes: &[u8],
698        expected: ObjectKind,
699    ) -> Result<Arc<crate::CodeMemory>> {
700        self.load_code(wasmtime_runtime::MmapVec::from_slice(bytes)?, expected)
701    }
702
703    /// Like `load_code_bytes`, but creates a mmap from a file on disk.
704    pub(crate) fn load_code_file(
705        &self,
706        path: &Path,
707        expected: ObjectKind,
708    ) -> Result<Arc<crate::CodeMemory>> {
709        self.load_code(
710            wasmtime_runtime::MmapVec::from_file(path).with_context(|| {
711                format!("failed to create file mapping for: {}", path.display())
712            })?,
713            expected,
714        )
715    }
716
717    pub(crate) fn load_code(
718        &self,
719        mmap: wasmtime_runtime::MmapVec,
720        expected: ObjectKind,
721    ) -> Result<Arc<crate::CodeMemory>> {
722        serialization::check_compatible(self, &mmap, expected)?;
723        let mut code = crate::CodeMemory::new(mmap)?;
724        code.publish()?;
725        Ok(Arc::new(code))
726    }
727}
728
729/// A weak reference to an [`Engine`].
730#[derive(Clone)]
731pub struct EngineWeak {
732    inner: std::sync::Weak<EngineInner>,
733}
734
735impl EngineWeak {
736    /// Upgrade this weak reference into an [`Engine`]. Returns `None` if
737    /// strong references (the [`Engine`] type itself) no longer exist.
738    pub fn upgrade(&self) -> Option<Engine> {
739        std::sync::Weak::upgrade(&self.inner).map(|inner| Engine { inner })
740    }
741}