wasmtime_runtime/lib.rs
1//! Runtime library support for Wasmtime.
2
3#![deny(missing_docs)]
4#![warn(clippy::cast_sign_loss)]
5
6use anyhow::{Error, Result};
7use std::fmt;
8use std::ptr::NonNull;
9use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
10use std::sync::Arc;
11use wasmtime_environ::{
12 DefinedFuncIndex, DefinedMemoryIndex, HostPtr, ModuleInternedTypeIndex, VMOffsets,
13};
14
15mod arch;
16mod async_yield;
17#[cfg(feature = "component-model")]
18pub mod component;
19mod export;
20mod gc;
21mod imports;
22mod instance;
23mod memory;
24mod mmap;
25mod mmap_vec;
26mod send_sync_ptr;
27mod store_box;
28mod sys;
29mod table;
30mod traphandlers;
31mod vmcontext;
32
33mod threads;
34pub use self::threads::*;
35
36#[cfg(feature = "debug-builtins")]
37pub mod debug_builtins;
38pub mod libcalls;
39pub mod mpk;
40
41#[cfg(feature = "debug-builtins")]
42pub use wasmtime_jit_debug::gdb_jit_int::GdbJitImageRegistration;
43
44pub use crate::arch::{get_stack_pointer, V128Abi};
45pub use crate::async_yield::*;
46pub use crate::export::*;
47pub use crate::gc::*;
48pub use crate::imports::Imports;
49pub use crate::instance::{
50 GcHeapAllocationIndex, Instance, InstanceAllocationRequest, InstanceAllocator,
51 InstanceAllocatorImpl, InstanceHandle, MemoryAllocationIndex, OnDemandInstanceAllocator,
52 StorePtr, TableAllocationIndex,
53};
54#[cfg(feature = "pooling-allocator")]
55pub use crate::instance::{
56 InstanceLimits, PoolingInstanceAllocator, PoolingInstanceAllocatorConfig,
57};
58pub use crate::memory::{DefaultMemoryCreator, Memory, RuntimeLinearMemory, RuntimeMemoryCreator};
59pub use crate::mmap::Mmap;
60pub use crate::mmap_vec::MmapVec;
61pub use crate::mpk::MpkEnabled;
62pub use crate::store_box::*;
63pub use crate::sys::unwind::UnwindRegistration;
64pub use crate::table::{Table, TableElement};
65pub use crate::traphandlers::*;
66pub use crate::vmcontext::{
67 VMArrayCallFunction, VMArrayCallHostFuncContext, VMContext, VMFuncRef, VMFunctionBody,
68 VMFunctionImport, VMGlobalDefinition, VMGlobalImport, VMInvokeArgument, VMMemoryDefinition,
69 VMMemoryImport, VMNativeCallFunction, VMNativeCallHostFuncContext, VMOpaqueContext,
70 VMRuntimeLimits, VMSharedTypeIndex, VMTableDefinition, VMTableImport, VMWasmCallFunction,
71 ValRaw,
72};
73pub use send_sync_ptr::SendSyncPtr;
74
75mod module_id;
76pub use module_id::{CompiledModuleId, CompiledModuleIdAllocator};
77
78mod cow;
79pub use crate::cow::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
80
81/// Version number of this crate.
82pub const VERSION: &str = env!("CARGO_PKG_VERSION");
83
84/// Dynamic runtime functionality needed by this crate throughout the execution
85/// of a wasm instance.
86///
87/// This trait is used to store a raw pointer trait object within each
88/// `VMContext`. This raw pointer trait object points back to the
89/// `wasmtime::Store` internally but is type-erased so this `wasmtime_runtime`
90/// crate doesn't need the entire `wasmtime` crate to build.
91///
92/// Note that this is an extra-unsafe trait because no heed is paid to the
93/// lifetime of this store or the Send/Sync-ness of this store. All of that must
94/// be respected by embedders (e.g. the `wasmtime::Store` structure). The theory
95/// is that `wasmtime::Store` handles all this correctly.
96pub unsafe trait Store {
97 /// Returns the raw pointer in memory where this store's shared
98 /// `VMRuntimeLimits` structure is located.
99 ///
100 /// Used to configure `VMContext` initialization and store the right pointer
101 /// in the `VMContext`.
102 fn vmruntime_limits(&self) -> *mut VMRuntimeLimits;
103
104 /// Returns a pointer to the global epoch counter.
105 ///
106 /// Used to configure the `VMContext` on initialization.
107 fn epoch_ptr(&self) -> *const AtomicU64;
108
109 /// Get this store's GC heap.
110 fn gc_store(&mut self) -> &mut GcStore {
111 self.maybe_gc_store()
112 .expect("attempt to access the GC store before it has been allocated")
113 }
114
115 /// Get this store's GC heap, if it has been allocated.
116 fn maybe_gc_store(&mut self) -> Option<&mut GcStore>;
117
118 /// Callback invoked to allow the store's resource limiter to reject a
119 /// memory grow operation.
120 fn memory_growing(
121 &mut self,
122 current: usize,
123 desired: usize,
124 maximum: Option<usize>,
125 ) -> Result<bool, Error>;
126
127 /// Callback invoked to notify the store's resource limiter that a memory
128 /// grow operation has failed.
129 ///
130 /// Note that this is not invoked if `memory_growing` returns an error.
131 fn memory_grow_failed(&mut self, error: Error) -> Result<()>;
132
133 /// Callback invoked to allow the store's resource limiter to reject a
134 /// table grow operation.
135 fn table_growing(
136 &mut self,
137 current: u32,
138 desired: u32,
139 maximum: Option<u32>,
140 ) -> Result<bool, Error>;
141
142 /// Callback invoked to notify the store's resource limiter that a table
143 /// grow operation has failed.
144 ///
145 /// Note that this is not invoked if `table_growing` returns an error.
146 fn table_grow_failed(&mut self, error: Error) -> Result<()>;
147
148 /// Callback invoked whenever fuel runs out by a wasm instance. If an error
149 /// is returned that's raised as a trap. Otherwise wasm execution will
150 /// continue as normal.
151 fn out_of_gas(&mut self) -> Result<(), Error>;
152
153 /// Callback invoked whenever an instance observes a new epoch
154 /// number. Cannot fail; cooperative epoch-based yielding is
155 /// completely semantically transparent. Returns the new deadline.
156 fn new_epoch(&mut self) -> Result<u64, Error>;
157
158 /// Callback invoked whenever an instance needs to trigger a GC.
159 ///
160 /// Optionally given a GC reference that is rooted for the collection, and
161 /// then whose updated GC reference is returned.
162 ///
163 /// Cooperative, async-yielding (if configured) is completely transparent.
164 ///
165 /// If the async GC was cancelled, returns an error. This should be raised
166 /// as a trap to clean up Wasm execution.
167 fn gc(&mut self, root: Option<VMGcRef>) -> Result<Option<VMGcRef>>;
168
169 /// Metadata required for resources for the component model.
170 #[cfg(feature = "component-model")]
171 fn component_calls(&mut self) -> &mut component::CallContexts;
172}
173
174/// Functionality required by this crate for a particular module. This
175/// is chiefly needed for lazy initialization of various bits of
176/// instance state.
177///
178/// When an instance is created, it holds an `Arc<dyn ModuleRuntimeInfo>`
179/// so that it can get to signatures, metadata on functions, memory and
180/// funcref-table images, etc. All of these things are ordinarily known
181/// by the higher-level layers of Wasmtime. Specifically, the main
182/// implementation of this trait is provided by
183/// `wasmtime::module::ModuleInner`. Since the runtime crate sits at
184/// the bottom of the dependence DAG though, we don't know or care about
185/// that; we just need some implementor of this trait for each
186/// allocation request.
187pub trait ModuleRuntimeInfo: Send + Sync + 'static {
188 /// The underlying Module.
189 fn module(&self) -> &Arc<wasmtime_environ::Module>;
190
191 /// Translate a module-level interned type index into an engine-level
192 /// interned type index.
193 fn engine_type_index(&self, module_index: ModuleInternedTypeIndex) -> VMSharedTypeIndex;
194
195 /// Returns the address, in memory, that the function `index` resides at.
196 fn function(&self, index: DefinedFuncIndex) -> NonNull<VMWasmCallFunction>;
197
198 /// Returns the address, in memory, of the trampoline that allows the given
199 /// defined Wasm function to be called by the native calling convention.
200 ///
201 /// Returns `None` for Wasm functions which do not escape, and therefore are
202 /// not callable from outside the Wasm module itself.
203 fn native_to_wasm_trampoline(
204 &self,
205 index: DefinedFuncIndex,
206 ) -> Option<NonNull<VMNativeCallFunction>>;
207
208 /// Returns the address, in memory, of the trampoline that allows the given
209 /// defined Wasm function to be called by the array calling convention.
210 ///
211 /// Returns `None` for Wasm functions which do not escape, and therefore are
212 /// not callable from outside the Wasm module itself.
213 fn array_to_wasm_trampoline(&self, index: DefinedFuncIndex) -> Option<VMArrayCallFunction>;
214
215 /// Return the address, in memory, of the trampoline that allows Wasm to
216 /// call a native function of the given signature.
217 fn wasm_to_native_trampoline(
218 &self,
219 signature: VMSharedTypeIndex,
220 ) -> Option<NonNull<VMWasmCallFunction>>;
221
222 /// Returns the `MemoryImage` structure used for copy-on-write
223 /// initialization of the memory, if it's applicable.
224 fn memory_image(&self, memory: DefinedMemoryIndex)
225 -> anyhow::Result<Option<&Arc<MemoryImage>>>;
226
227 /// A unique ID for this particular module. This can be used to
228 /// allow for fastpaths to optimize a "re-instantiate the same
229 /// module again" case.
230 fn unique_id(&self) -> Option<CompiledModuleId>;
231
232 /// A slice pointing to all data that is referenced by this instance.
233 fn wasm_data(&self) -> &[u8];
234
235 /// Returns an array, indexed by `ModuleInternedTypeIndex` of all
236 /// `VMSharedSignatureIndex` entries corresponding to the `SignatureIndex`.
237 fn type_ids(&self) -> &[VMSharedTypeIndex];
238
239 /// Offset information for the current host.
240 fn offsets(&self) -> &VMOffsets<HostPtr>;
241}
242
243/// Returns the host OS page size, in bytes.
244pub fn page_size() -> usize {
245 static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
246
247 return match PAGE_SIZE.load(Ordering::Relaxed) {
248 0 => {
249 let size = sys::vm::get_page_size();
250 assert!(size != 0);
251 PAGE_SIZE.store(size, Ordering::Relaxed);
252 size
253 }
254 n => n,
255 };
256}
257
258/// Result of [`Memory::atomic_wait32`] and [`Memory::atomic_wait64`]
259#[derive(Copy, Clone, PartialEq, Eq, Debug)]
260pub enum WaitResult {
261 /// Indicates that a `wait` completed by being awoken by a different thread.
262 /// This means the thread went to sleep and didn't time out.
263 Ok = 0,
264 /// Indicates that `wait` did not complete and instead returned due to the
265 /// value in memory not matching the expected value.
266 Mismatch = 1,
267 /// Indicates that `wait` completed with a timeout, meaning that the
268 /// original value matched as expected but nothing ever called `notify`.
269 TimedOut = 2,
270}
271
272/// Description about a fault that occurred in WebAssembly.
273#[derive(Debug)]
274pub struct WasmFault {
275 /// The size of memory, in bytes, at the time of the fault.
276 pub memory_size: usize,
277 /// The WebAssembly address at which the fault occurred.
278 pub wasm_address: u64,
279}
280
281impl fmt::Display for WasmFault {
282 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283 write!(
284 f,
285 "memory fault at wasm address 0x{:x} in linear memory of size 0x{:x}",
286 self.wasm_address, self.memory_size,
287 )
288 }
289}