1#![deny(missing_docs)]
4#![warn(clippy::cast_sign_loss)]
7
8#[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#[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(core::convert::Infallible);
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(core::convert::Infallible);
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(core::convert::Infallible);
35
36use crate::StoreContextMut;
37use crate::prelude::*;
38use crate::store::{StoreInner, StoreOpaque, StoreResourceLimiter};
39use crate::type_registry::RegisteredType;
40use alloc::sync::Arc;
41use core::fmt;
42use core::ops::{Deref, DerefMut};
43use core::pin::pin;
44use core::ptr::NonNull;
45use core::sync::atomic::{AtomicUsize, Ordering};
46use core::task::{Context, Poll, Waker};
47use wasmtime_environ::error::OutOfMemory;
48use wasmtime_environ::{DefinedMemoryIndex, HostPtr, VMOffsets, VMSharedTypeIndex};
49
50#[cfg(feature = "gc")]
51use wasmtime_environ::ModuleInternedTypeIndex;
52
53mod always_mut;
54#[cfg(feature = "component-model")]
55pub mod component;
56mod const_expr;
57mod export;
58mod gc;
59mod imports;
60mod instance;
61mod memory;
62mod mmap_vec;
63#[cfg(has_virtual_memory)]
64mod pagemap_disabled;
65mod provenance;
66mod send_sync_ptr;
67mod stack_switching;
68mod store_box;
69mod sys;
70mod table;
71#[cfg(feature = "gc")]
72mod throw;
73mod traphandlers;
74mod vmcontext;
75
76#[cfg(feature = "threads")]
77mod parking_spot;
78
79#[cfg(all(has_host_compiler_backend, feature = "debug-builtins"))]
83pub mod debug_builtins;
84pub mod libcalls;
85pub mod mpk;
86
87#[cfg(feature = "pulley")]
88pub(crate) mod interpreter;
89#[cfg(not(feature = "pulley"))]
90pub(crate) mod interpreter_disabled;
91#[cfg(not(feature = "pulley"))]
92pub(crate) use interpreter_disabled as interpreter;
93
94#[cfg(feature = "debug-builtins")]
95pub use wasmtime_jit_debug::gdb_jit_int::GdbJitImageRegistration;
96
97pub use crate::runtime::vm::always_mut::*;
98pub use crate::runtime::vm::export::*;
99pub use crate::runtime::vm::gc::*;
100pub use crate::runtime::vm::imports::Imports;
101pub use crate::runtime::vm::instance::{
102 GcHeapAllocationIndex, Instance, InstanceAllocationRequest, InstanceAllocator, InstanceHandle,
103 MemoryAllocationIndex, OnDemandInstanceAllocator, TableAllocationIndex, initialize_instance,
104};
105#[cfg(feature = "pooling-allocator")]
106pub use crate::runtime::vm::instance::{
107 InstanceLimits, PoolConcurrencyLimitError, PoolingAllocatorMetrics, PoolingInstanceAllocator,
108 PoolingInstanceAllocatorConfig,
109};
110pub use crate::runtime::vm::interpreter::*;
111pub use crate::runtime::vm::memory::{
112 Memory, MemoryBase, RuntimeLinearMemory, RuntimeMemoryCreator, SharedMemory,
113};
114pub use crate::runtime::vm::mmap_vec::MmapVec;
115pub use crate::runtime::vm::provenance::*;
116pub use crate::runtime::vm::stack_switching::*;
117pub use crate::runtime::vm::store_box::*;
118#[cfg(feature = "std")]
119pub use crate::runtime::vm::sys::mmap::open_file_for_mmap;
120#[cfg(has_host_compiler_backend)]
121pub use crate::runtime::vm::sys::unwind::UnwindRegistration;
122pub use crate::runtime::vm::table::{Table, TableElementType};
123#[cfg(feature = "gc")]
124pub use crate::runtime::vm::throw::*;
125pub use crate::runtime::vm::traphandlers::*;
126#[cfg(feature = "component-model")]
127pub use crate::runtime::vm::vmcontext::VMArrayCallFunction;
128pub use crate::runtime::vm::vmcontext::{
129 VMArrayCallHostFuncContext, VMContext, VMFuncRef, VMFunctionImport, VMGlobalDefinition,
130 VMGlobalImport, VMGlobalKind, VMMemoryDefinition, VMMemoryImport, VMOpaqueContext,
131 VMStoreContext, VMTableImport, VMTagImport, VMWasmCallFunction, ValRaw,
132};
133#[cfg(has_custom_sync)]
134pub(crate) use sys::capi;
135
136pub use send_sync_ptr::SendSyncPtr;
137pub use wasmtime_unwinder::Unwind;
138
139#[cfg(has_host_compiler_backend)]
140pub use wasmtime_unwinder::{UnwindHost, get_stack_pointer};
141
142mod module_id;
143pub use module_id::CompiledModuleId;
144
145#[cfg(has_virtual_memory)]
146mod byte_count;
147#[cfg(has_virtual_memory)]
148mod cow;
149#[cfg(not(has_virtual_memory))]
150mod cow_disabled;
151#[cfg(has_virtual_memory)]
152mod mmap;
153
154#[cfg(any(feature = "async", feature = "gc"))]
155mod async_yield;
156#[cfg(any(feature = "async", feature = "gc"))]
157pub use crate::runtime::vm::async_yield::*;
158
159#[cfg(feature = "gc-null")]
160mod send_sync_unsafe_cell;
161#[cfg(feature = "gc-null")]
162pub use send_sync_unsafe_cell::SendSyncUnsafeCell;
163
164cfg_if::cfg_if! {
165 if #[cfg(has_virtual_memory)] {
166 pub use crate::runtime::vm::byte_count::*;
167 pub use crate::runtime::vm::mmap::{Mmap, MmapOffset};
168 pub use self::cow::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
169 } else {
170 pub use self::cow_disabled::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
171 }
172}
173
174pub trait ModuleMemoryImageSource: Send + Sync + 'static {
176 fn wasm_data(&self) -> &[u8];
179
180 fn mmap(&self) -> Option<&MmapVec>;
183}
184
185pub unsafe trait VMStore: 'static {
206 fn store_opaque(&self) -> &StoreOpaque;
208
209 fn store_opaque_mut(&mut self) -> &mut StoreOpaque;
211
212 fn resource_limiter_and_store_opaque(
215 &mut self,
216 ) -> (Option<StoreResourceLimiter<'_>>, &mut StoreOpaque);
217
218 #[cfg(target_has_atomic = "64")]
222 fn new_epoch_updated_deadline(&mut self) -> Result<crate::UpdateDeadline>;
223
224 #[cfg(feature = "component-model")]
226 fn component_calls(&mut self) -> &mut component::CallContexts;
227
228 #[cfg(feature = "component-model-async")]
229 fn component_async_store(
230 &mut self,
231 ) -> &mut dyn crate::runtime::component::VMComponentAsyncStore;
232
233 #[cfg(feature = "debug")]
235 fn block_on_debug_handler(&mut self, event: crate::DebugEvent) -> crate::Result<()>;
236}
237
238impl Deref for dyn VMStore + '_ {
239 type Target = StoreOpaque;
240
241 fn deref(&self) -> &Self::Target {
242 self.store_opaque()
243 }
244}
245
246impl DerefMut for dyn VMStore + '_ {
247 fn deref_mut(&mut self) -> &mut Self::Target {
248 self.store_opaque_mut()
249 }
250}
251
252impl dyn VMStore + '_ {
253 pub(crate) unsafe fn unchecked_context_mut<T>(&mut self) -> StoreContextMut<'_, T> {
261 unsafe { StoreContextMut(&mut *(self as *mut dyn VMStore as *mut StoreInner<T>)) }
262 }
263}
264
265#[derive(Copy, Clone)]
280#[repr(transparent)]
281struct VMStoreRawPtr(pub NonNull<dyn VMStore>);
282
283unsafe impl Send for VMStoreRawPtr {}
286unsafe impl Sync for VMStoreRawPtr {}
287
288#[derive(Clone)]
291pub enum ModuleRuntimeInfo {
292 Module(crate::Module),
293 Bare(Arc<BareModuleInfo>),
294}
295
296#[derive(Clone)]
301pub struct BareModuleInfo {
302 module: Arc<wasmtime_environ::Module>,
303 offsets: VMOffsets<HostPtr>,
304 _registered_type: Option<RegisteredType>,
305}
306
307impl ModuleRuntimeInfo {
308 pub(crate) fn bare(module: Arc<wasmtime_environ::Module>) -> Result<Self, OutOfMemory> {
309 ModuleRuntimeInfo::bare_with_registered_type(module, None)
310 }
311
312 pub(crate) fn bare_with_registered_type(
313 module: Arc<wasmtime_environ::Module>,
314 registered_type: Option<RegisteredType>,
315 ) -> Result<Self, OutOfMemory> {
316 let info = try_new(BareModuleInfo {
317 offsets: VMOffsets::new(HostPtr, &module),
318 module,
319 _registered_type: registered_type,
320 })?;
321 Ok(ModuleRuntimeInfo::Bare(info))
322 }
323
324 pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {
326 match self {
327 ModuleRuntimeInfo::Module(m) => m.env_module(),
328 ModuleRuntimeInfo::Bare(b) => &b.module,
329 }
330 }
331
332 #[cfg(feature = "gc")]
335 fn engine_type_index(&self, module_index: ModuleInternedTypeIndex) -> VMSharedTypeIndex {
336 match self {
337 ModuleRuntimeInfo::Module(m) => m
338 .engine_code()
339 .signatures()
340 .shared_type(module_index)
341 .expect("bad module-level interned type index"),
342 ModuleRuntimeInfo::Bare(_) => unreachable!(),
343 }
344 }
345
346 fn memory_image(&self, memory: DefinedMemoryIndex) -> crate::Result<Option<&Arc<MemoryImage>>> {
349 match self {
350 ModuleRuntimeInfo::Module(m) => {
351 let images = m.memory_images()?;
352 Ok(images.and_then(|images| images.get_memory_image(memory)))
353 }
354 ModuleRuntimeInfo::Bare(_) => Ok(None),
355 }
356 }
357
358 #[cfg(feature = "pooling-allocator")]
362 fn unique_id(&self) -> Option<CompiledModuleId> {
363 match self {
364 ModuleRuntimeInfo::Module(m) => Some(m.id()),
365 ModuleRuntimeInfo::Bare(_) => None,
366 }
367 }
368
369 fn wasm_data(&self) -> &[u8] {
371 match self {
372 ModuleRuntimeInfo::Module(m) => m.engine_code().wasm_data(),
373 ModuleRuntimeInfo::Bare(_) => &[],
374 }
375 }
376
377 fn type_ids(&self) -> &[VMSharedTypeIndex] {
380 match self {
381 ModuleRuntimeInfo::Module(m) => m
382 .engine_code()
383 .signatures()
384 .as_module_map()
385 .values()
386 .as_slice(),
387 ModuleRuntimeInfo::Bare(_) => &[],
388 }
389 }
390
391 pub(crate) fn offsets(&self) -> &VMOffsets<HostPtr> {
393 match self {
394 ModuleRuntimeInfo::Module(m) => m.offsets(),
395 ModuleRuntimeInfo::Bare(b) => &b.offsets,
396 }
397 }
398}
399
400#[cfg(has_virtual_memory)]
402pub fn host_page_size() -> usize {
403 static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
406
407 return match PAGE_SIZE.load(Ordering::Relaxed) {
408 0 => {
409 let size = sys::vm::get_page_size();
410 assert!(size != 0);
411 PAGE_SIZE.store(size, Ordering::Relaxed);
412 size
413 }
414 n => n,
415 };
416}
417
418#[derive(Copy, Clone, PartialEq, Eq, Debug)]
420pub enum WaitResult {
421 Ok = 0,
424 Mismatch = 1,
427 TimedOut = 2,
430}
431
432#[derive(Debug)]
434pub struct WasmFault {
435 pub memory_size: usize,
437 pub wasm_address: u64,
439}
440
441impl fmt::Display for WasmFault {
442 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
443 write!(
444 f,
445 "memory fault at wasm address 0x{:x} in linear memory of size 0x{:x}",
446 self.wasm_address, self.memory_size,
447 )
448 }
449}
450
451pub fn assert_ready<F: Future>(f: F) -> F::Output {
470 one_poll(f).unwrap()
471}
472
473fn one_poll<F: Future>(f: F) -> Option<F::Output> {
485 let mut context = Context::from_waker(&Waker::noop());
486 match pin!(f).poll(&mut context) {
487 Poll::Ready(output) => Some(output),
488 Poll::Pending => None,
489 }
490}