wasmtime/runtime/
vm.rs

1//! Runtime library support for Wasmtime.
2
3#![deny(missing_docs)]
4// See documentation in crates/wasmtime/src/runtime.rs for why this is
5// selectively enabled here.
6#![warn(clippy::cast_sign_loss)]
7
8// Polyfill `std::simd::i8x16` etc. until they're stable.
9#[cfg(all(target_arch = "x86_64", target_feature = "sse"))]
10#[expect(non_camel_case_types, reason = "matching wasm conventions")]
11pub(crate) type i8x16 = core::arch::x86_64::__m128i;
12#[cfg(all(target_arch = "x86_64", target_feature = "sse"))]
13#[expect(non_camel_case_types, reason = "matching wasm conventions")]
14pub(crate) type f32x4 = core::arch::x86_64::__m128;
15#[cfg(all(target_arch = "x86_64", target_feature = "sse"))]
16#[expect(non_camel_case_types, reason = "matching wasm conventions")]
17pub(crate) type f64x2 = core::arch::x86_64::__m128d;
18
19// On platforms other than x86_64, define i8x16 to a non-constructible type;
20// we need a type because we have a lot of macros for defining builtin
21// functions that are awkward to make conditional on the target, but it
22// doesn't need to actually be constructible unless we're on x86_64.
23#[cfg(not(all(target_arch = "x86_64", target_feature = "sse")))]
24#[expect(non_camel_case_types, reason = "matching wasm conventions")]
25#[derive(Copy, Clone)]
26pub(crate) struct i8x16(crate::uninhabited::Uninhabited);
27#[cfg(not(all(target_arch = "x86_64", target_feature = "sse")))]
28#[expect(non_camel_case_types, reason = "matching wasm conventions")]
29#[derive(Copy, Clone)]
30pub(crate) struct f32x4(crate::uninhabited::Uninhabited);
31#[cfg(not(all(target_arch = "x86_64", target_feature = "sse")))]
32#[expect(non_camel_case_types, reason = "matching wasm conventions")]
33#[derive(Copy, Clone)]
34pub(crate) struct f64x2(crate::uninhabited::Uninhabited);
35
36use crate::StoreContextMut;
37use crate::prelude::*;
38use crate::store::StoreInner;
39use crate::store::StoreOpaque;
40use crate::type_registry::RegisteredType;
41use alloc::sync::Arc;
42use core::fmt;
43use core::ops::Deref;
44use core::ops::DerefMut;
45use core::ptr::NonNull;
46use core::sync::atomic::{AtomicUsize, Ordering};
47use wasmtime_environ::{
48    DefinedFuncIndex, DefinedMemoryIndex, HostPtr, VMOffsets, VMSharedTypeIndex,
49};
50
51#[cfg(feature = "gc")]
52use wasmtime_environ::ModuleInternedTypeIndex;
53
54#[cfg(feature = "component-model")]
55pub mod component;
56mod const_expr;
57mod export;
58mod gc;
59mod imports;
60mod instance;
61mod memory;
62mod mmap_vec;
63mod provenance;
64mod send_sync_ptr;
65mod stack_switching;
66mod store_box;
67mod sys;
68mod table;
69mod traphandlers;
70mod vmcontext;
71
72#[cfg(feature = "threads")]
73mod parking_spot;
74
75// Note that `debug_builtins` here is disabled with a feature or a lack of a
76// native compilation backend because it's only here to assist in debugging
77// natively compiled code.
78#[cfg(all(has_host_compiler_backend, feature = "debug-builtins"))]
79pub mod debug_builtins;
80pub mod libcalls;
81pub mod mpk;
82
83#[cfg(feature = "pulley")]
84pub(crate) mod interpreter;
85#[cfg(not(feature = "pulley"))]
86pub(crate) mod interpreter_disabled;
87#[cfg(not(feature = "pulley"))]
88pub(crate) use interpreter_disabled as interpreter;
89
90#[cfg(feature = "debug-builtins")]
91pub use wasmtime_jit_debug::gdb_jit_int::GdbJitImageRegistration;
92
93pub use crate::runtime::vm::export::*;
94pub use crate::runtime::vm::gc::*;
95pub use crate::runtime::vm::imports::Imports;
96pub use crate::runtime::vm::instance::{
97    GcHeapAllocationIndex, Instance, InstanceAllocationRequest, InstanceAllocator,
98    InstanceAllocatorImpl, InstanceAndStore, InstanceHandle, MemoryAllocationIndex,
99    OnDemandInstanceAllocator, StorePtr, TableAllocationIndex, initialize_instance,
100};
101#[cfg(feature = "pooling-allocator")]
102pub use crate::runtime::vm::instance::{
103    InstanceLimits, PoolConcurrencyLimitError, PoolingInstanceAllocator,
104    PoolingInstanceAllocatorConfig,
105};
106pub use crate::runtime::vm::interpreter::*;
107pub use crate::runtime::vm::memory::{
108    Memory, MemoryBase, RuntimeLinearMemory, RuntimeMemoryCreator, SharedMemory,
109};
110pub use crate::runtime::vm::mmap_vec::MmapVec;
111pub use crate::runtime::vm::provenance::*;
112pub use crate::runtime::vm::stack_switching::*;
113pub use crate::runtime::vm::store_box::*;
114#[cfg(feature = "std")]
115pub use crate::runtime::vm::sys::mmap::open_file_for_mmap;
116#[cfg(has_host_compiler_backend)]
117pub use crate::runtime::vm::sys::unwind::UnwindRegistration;
118pub use crate::runtime::vm::table::{Table, TableElement};
119pub use crate::runtime::vm::traphandlers::*;
120pub use crate::runtime::vm::vmcontext::{
121    VMArrayCallFunction, VMArrayCallHostFuncContext, VMContext, VMFuncRef, VMFunctionImport,
122    VMGlobalDefinition, VMGlobalImport, VMGlobalKind, VMMemoryDefinition, VMMemoryImport,
123    VMOpaqueContext, VMStoreContext, VMTableImport, VMTagImport, VMWasmCallFunction, ValRaw,
124};
125
126pub use send_sync_ptr::SendSyncPtr;
127pub use wasmtime_unwinder::Unwind;
128
129#[cfg(has_host_compiler_backend)]
130pub use wasmtime_unwinder::{UnwindHost, get_stack_pointer};
131
132mod module_id;
133pub use module_id::CompiledModuleId;
134
135#[cfg(has_virtual_memory)]
136mod byte_count;
137#[cfg(has_virtual_memory)]
138mod cow;
139#[cfg(not(has_virtual_memory))]
140mod cow_disabled;
141#[cfg(has_virtual_memory)]
142mod mmap;
143
144#[cfg(feature = "async")]
145mod async_yield;
146#[cfg(feature = "async")]
147pub use crate::runtime::vm::async_yield::*;
148
149#[cfg(feature = "gc-null")]
150mod send_sync_unsafe_cell;
151#[cfg(feature = "gc-null")]
152pub use send_sync_unsafe_cell::SendSyncUnsafeCell;
153
154cfg_if::cfg_if! {
155    if #[cfg(has_virtual_memory)] {
156        pub use crate::runtime::vm::byte_count::*;
157        pub use crate::runtime::vm::mmap::{Mmap, MmapOffset};
158        pub use self::cow::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
159    } else {
160        pub use self::cow_disabled::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
161    }
162}
163
164/// Dynamic runtime functionality needed by this crate throughout the execution
165/// of a wasm instance.
166///
167/// This trait is used to store a raw pointer trait object within each
168/// `VMContext`. This raw pointer trait object points back to the
169/// `wasmtime::Store` internally but is type-erased to avoid needing to
170/// monomorphize the entire runtime on the `T` in `Store<T>`
171///
172/// # Safety
173///
174/// This trait should be implemented by nothing other than `StoreInner<T>` in
175/// this crate. It's not sound to implement it for anything else due to
176/// `unchecked_context_mut` below.
177///
178/// It's also worth nothing that there are various locations where a `*mut dyn
179/// VMStore` is asserted to be both `Send` and `Sync` which disregards the `T`
180/// that's actually stored in the store itself. It's assume that the high-level
181/// APIs using `Store<T>` are correctly inferring send/sync on the returned
182/// values (e.g. futures) and that internally in the runtime we aren't doing
183/// anything "weird" with threads for example.
184pub unsafe trait VMStore: 'static {
185    /// Get a shared borrow of this store's `StoreOpaque`.
186    fn store_opaque(&self) -> &StoreOpaque;
187
188    /// Get an exclusive borrow of this store's `StoreOpaque`.
189    fn store_opaque_mut(&mut self) -> &mut StoreOpaque;
190
191    /// Callback invoked to allow the store's resource limiter to reject a
192    /// memory grow operation.
193    fn memory_growing(
194        &mut self,
195        current: usize,
196        desired: usize,
197        maximum: Option<usize>,
198    ) -> Result<bool, Error>;
199
200    /// Callback invoked to notify the store's resource limiter that a memory
201    /// grow operation has failed.
202    ///
203    /// Note that this is not invoked if `memory_growing` returns an error.
204    fn memory_grow_failed(&mut self, error: Error) -> Result<()>;
205
206    /// Callback invoked to allow the store's resource limiter to reject a
207    /// table grow operation.
208    fn table_growing(
209        &mut self,
210        current: usize,
211        desired: usize,
212        maximum: Option<usize>,
213    ) -> Result<bool, Error>;
214
215    /// Callback invoked to notify the store's resource limiter that a table
216    /// grow operation has failed.
217    ///
218    /// Note that this is not invoked if `table_growing` returns an error.
219    fn table_grow_failed(&mut self, error: Error) -> Result<()>;
220
221    /// Callback invoked whenever fuel runs out by a wasm instance. If an error
222    /// is returned that's raised as a trap. Otherwise wasm execution will
223    /// continue as normal.
224    fn out_of_gas(&mut self) -> Result<(), Error>;
225
226    /// Callback invoked whenever an instance observes a new epoch
227    /// number. Cannot fail; cooperative epoch-based yielding is
228    /// completely semantically transparent. Returns the new deadline.
229    #[cfg(target_has_atomic = "64")]
230    fn new_epoch(&mut self) -> Result<u64, Error>;
231
232    /// Callback invoked whenever an instance needs to grow-or-collect the GC
233    /// heap.
234    ///
235    /// Optionally given a GC reference that is rooted for the collection, and
236    /// then whose updated GC reference is returned.
237    ///
238    /// Optionally given a number of bytes that are needed for an upcoming
239    /// allocation.
240    ///
241    /// Cooperative, async-yielding (if configured) is completely transparent,
242    /// but must be called from a fiber stack in that case.
243    ///
244    /// If the async GC was cancelled, returns an error. This should be raised
245    /// as a trap to clean up Wasm execution.
246    unsafe fn maybe_async_grow_or_collect_gc_heap(
247        &mut self,
248        root: Option<VMGcRef>,
249        bytes_needed: Option<u64>,
250    ) -> Result<Option<VMGcRef>>;
251
252    /// Metadata required for resources for the component model.
253    #[cfg(feature = "component-model")]
254    fn component_calls(&mut self) -> &mut component::CallContexts;
255
256    #[cfg(feature = "component-model-async")]
257    fn component_async_store(
258        &mut self,
259    ) -> &mut dyn crate::runtime::component::VMComponentAsyncStore;
260}
261
262impl Deref for dyn VMStore + '_ {
263    type Target = StoreOpaque;
264
265    fn deref(&self) -> &Self::Target {
266        self.store_opaque()
267    }
268}
269
270impl DerefMut for dyn VMStore + '_ {
271    fn deref_mut(&mut self) -> &mut Self::Target {
272        self.store_opaque_mut()
273    }
274}
275
276impl dyn VMStore + '_ {
277    /// Asserts that this `VMStore` was originally paired with `StoreInner<T>`
278    /// and then casts to the `StoreContextMut` type.
279    ///
280    /// # Unsafety
281    ///
282    /// This method is not safe as there's no static guarantee that `T` is
283    /// correct for this store.
284    pub(crate) unsafe fn unchecked_context_mut<T>(&mut self) -> StoreContextMut<'_, T> {
285        unsafe { StoreContextMut(&mut *(self as *mut dyn VMStore as *mut StoreInner<T>)) }
286    }
287}
288
289/// A newtype wrapper around `NonNull<dyn VMStore>` intended to be a
290/// self-pointer back to the `Store<T>` within raw data structures like
291/// `VMContext`.
292///
293/// This type exists to manually, and unsafely, implement `Send` and `Sync`.
294/// The `VMStore` trait doesn't require `Send` or `Sync` which means this isn't
295/// naturally either trait (e.g. with `SendSyncPtr` instead). Note that this
296/// means that `Instance` is, for example, mistakenly considered
297/// unconditionally `Send` and `Sync`. This is hopefully ok for now though
298/// because from a user perspective the only type that matters is `Store<T>`.
299/// That type is `Send + Sync` if `T: Send + Sync` already so the internal
300/// storage of `Instance` shouldn't matter as the final result is the same.
301/// Note though that this means we need to be extra vigilant about cross-thread
302/// usage of `Instance` and `ComponentInstance` for example.
303#[derive(Copy, Clone)]
304#[repr(transparent)]
305struct VMStoreRawPtr(pub NonNull<dyn VMStore>);
306
307// SAFETY: this is the purpose of `VMStoreRawPtr`, see docs above about safe
308// usage.
309unsafe impl Send for VMStoreRawPtr {}
310unsafe impl Sync for VMStoreRawPtr {}
311
312/// Functionality required by this crate for a particular module. This
313/// is chiefly needed for lazy initialization of various bits of
314/// instance state.
315///
316/// When an instance is created, it holds an `Arc<dyn ModuleRuntimeInfo>`
317/// so that it can get to signatures, metadata on functions, memory and
318/// funcref-table images, etc. All of these things are ordinarily known
319/// by the higher-level layers of Wasmtime. Specifically, the main
320/// implementation of this trait is provided by
321/// `wasmtime::module::ModuleInner`.  Since the runtime crate sits at
322/// the bottom of the dependence DAG though, we don't know or care about
323/// that; we just need some implementor of this trait for each
324/// allocation request.
325#[derive(Clone)]
326pub enum ModuleRuntimeInfo {
327    Module(crate::Module),
328    Bare(Box<BareModuleInfo>),
329}
330
331/// A barebones implementation of ModuleRuntimeInfo that is useful for
332/// cases where a purpose-built environ::Module is used and a full
333/// CompiledModule does not exist (for example, for tests or for the
334/// default-callee instance).
335#[derive(Clone)]
336pub struct BareModuleInfo {
337    module: Arc<wasmtime_environ::Module>,
338    offsets: VMOffsets<HostPtr>,
339    _registered_type: Option<RegisteredType>,
340}
341
342impl ModuleRuntimeInfo {
343    pub(crate) fn bare(module: Arc<wasmtime_environ::Module>) -> Self {
344        ModuleRuntimeInfo::bare_with_registered_type(module, None)
345    }
346
347    pub(crate) fn bare_with_registered_type(
348        module: Arc<wasmtime_environ::Module>,
349        registered_type: Option<RegisteredType>,
350    ) -> Self {
351        ModuleRuntimeInfo::Bare(Box::new(BareModuleInfo {
352            offsets: VMOffsets::new(HostPtr, &module),
353            module,
354            _registered_type: registered_type,
355        }))
356    }
357
358    /// The underlying Module.
359    pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {
360        match self {
361            ModuleRuntimeInfo::Module(m) => m.env_module(),
362            ModuleRuntimeInfo::Bare(b) => &b.module,
363        }
364    }
365
366    /// Translate a module-level interned type index into an engine-level
367    /// interned type index.
368    #[cfg(feature = "gc")]
369    fn engine_type_index(&self, module_index: ModuleInternedTypeIndex) -> VMSharedTypeIndex {
370        match self {
371            ModuleRuntimeInfo::Module(m) => m
372                .code_object()
373                .signatures()
374                .shared_type(module_index)
375                .expect("bad module-level interned type index"),
376            ModuleRuntimeInfo::Bare(_) => unreachable!(),
377        }
378    }
379
380    /// Returns the address, in memory, that the function `index` resides at.
381    fn function(&self, index: DefinedFuncIndex) -> NonNull<VMWasmCallFunction> {
382        let module = match self {
383            ModuleRuntimeInfo::Module(m) => m,
384            ModuleRuntimeInfo::Bare(_) => unreachable!(),
385        };
386        let ptr = module
387            .compiled_module()
388            .finished_function(index)
389            .as_ptr()
390            .cast::<VMWasmCallFunction>()
391            .cast_mut();
392        NonNull::new(ptr).unwrap()
393    }
394
395    /// Returns the address, in memory, of the trampoline that allows the given
396    /// defined Wasm function to be called by the array calling convention.
397    ///
398    /// Returns `None` for Wasm functions which do not escape, and therefore are
399    /// not callable from outside the Wasm module itself.
400    fn array_to_wasm_trampoline(
401        &self,
402        index: DefinedFuncIndex,
403    ) -> Option<NonNull<VMArrayCallFunction>> {
404        let m = match self {
405            ModuleRuntimeInfo::Module(m) => m,
406            ModuleRuntimeInfo::Bare(_) => unreachable!(),
407        };
408        let ptr = NonNull::from(m.compiled_module().array_to_wasm_trampoline(index)?);
409        Some(ptr.cast())
410    }
411
412    /// Returns the `MemoryImage` structure used for copy-on-write
413    /// initialization of the memory, if it's applicable.
414    fn memory_image(
415        &self,
416        memory: DefinedMemoryIndex,
417    ) -> anyhow::Result<Option<&Arc<MemoryImage>>> {
418        match self {
419            ModuleRuntimeInfo::Module(m) => {
420                let images = m.memory_images()?;
421                Ok(images.and_then(|images| images.get_memory_image(memory)))
422            }
423            ModuleRuntimeInfo::Bare(_) => Ok(None),
424        }
425    }
426
427    /// A unique ID for this particular module. This can be used to
428    /// allow for fastpaths to optimize a "re-instantiate the same
429    /// module again" case.
430    #[cfg(feature = "pooling-allocator")]
431    fn unique_id(&self) -> Option<CompiledModuleId> {
432        match self {
433            ModuleRuntimeInfo::Module(m) => Some(m.id()),
434            ModuleRuntimeInfo::Bare(_) => None,
435        }
436    }
437
438    /// A slice pointing to all data that is referenced by this instance.
439    fn wasm_data(&self) -> &[u8] {
440        match self {
441            ModuleRuntimeInfo::Module(m) => m.compiled_module().code_memory().wasm_data(),
442            ModuleRuntimeInfo::Bare(_) => &[],
443        }
444    }
445
446    /// Returns an array, indexed by `ModuleInternedTypeIndex` of all
447    /// `VMSharedSignatureIndex` entries corresponding to the `SignatureIndex`.
448    fn type_ids(&self) -> &[VMSharedTypeIndex] {
449        match self {
450            ModuleRuntimeInfo::Module(m) => m
451                .code_object()
452                .signatures()
453                .as_module_map()
454                .values()
455                .as_slice(),
456            ModuleRuntimeInfo::Bare(_) => &[],
457        }
458    }
459
460    /// Offset information for the current host.
461    pub(crate) fn offsets(&self) -> &VMOffsets<HostPtr> {
462        match self {
463            ModuleRuntimeInfo::Module(m) => m.offsets(),
464            ModuleRuntimeInfo::Bare(b) => &b.offsets,
465        }
466    }
467}
468
469/// Returns the host OS page size, in bytes.
470#[cfg(has_virtual_memory)]
471pub fn host_page_size() -> usize {
472    // NB: this function is duplicated in `crates/fiber/src/unix.rs` so if this
473    // changes that should probably get updated as well.
474    static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
475
476    return match PAGE_SIZE.load(Ordering::Relaxed) {
477        0 => {
478            let size = sys::vm::get_page_size();
479            assert!(size != 0);
480            PAGE_SIZE.store(size, Ordering::Relaxed);
481            size
482        }
483        n => n,
484    };
485}
486
487/// Result of `Memory::atomic_wait32` and `Memory::atomic_wait64`
488#[derive(Copy, Clone, PartialEq, Eq, Debug)]
489pub enum WaitResult {
490    /// Indicates that a `wait` completed by being awoken by a different thread.
491    /// This means the thread went to sleep and didn't time out.
492    Ok = 0,
493    /// Indicates that `wait` did not complete and instead returned due to the
494    /// value in memory not matching the expected value.
495    Mismatch = 1,
496    /// Indicates that `wait` completed with a timeout, meaning that the
497    /// original value matched as expected but nothing ever called `notify`.
498    TimedOut = 2,
499}
500
501/// Description about a fault that occurred in WebAssembly.
502#[derive(Debug)]
503pub struct WasmFault {
504    /// The size of memory, in bytes, at the time of the fault.
505    pub memory_size: usize,
506    /// The WebAssembly address at which the fault occurred.
507    pub wasm_address: u64,
508}
509
510impl fmt::Display for WasmFault {
511    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
512        write!(
513            f,
514            "memory fault at wasm address 0x{:x} in linear memory of size 0x{:x}",
515            self.wasm_address, self.memory_size,
516        )
517    }
518}