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