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