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