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