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(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#[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
164pub unsafe trait VMStore: 'static {
185 fn store_opaque(&self) -> &StoreOpaque;
187
188 fn store_opaque_mut(&mut self) -> &mut StoreOpaque;
190
191 fn memory_growing(
194 &mut self,
195 current: usize,
196 desired: usize,
197 maximum: Option<usize>,
198 ) -> Result<bool, Error>;
199
200 fn memory_grow_failed(&mut self, error: Error) -> Result<()>;
205
206 fn table_growing(
209 &mut self,
210 current: usize,
211 desired: usize,
212 maximum: Option<usize>,
213 ) -> Result<bool, Error>;
214
215 fn table_grow_failed(&mut self, error: Error) -> Result<()>;
220
221 fn out_of_gas(&mut self) -> Result<(), Error>;
225
226 #[cfg(target_has_atomic = "64")]
230 fn new_epoch(&mut self) -> Result<u64, Error>;
231
232 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 #[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 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#[derive(Copy, Clone)]
304#[repr(transparent)]
305struct VMStoreRawPtr(pub NonNull<dyn VMStore>);
306
307unsafe impl Send for VMStoreRawPtr {}
310unsafe impl Sync for VMStoreRawPtr {}
311
312#[derive(Clone)]
326pub enum ModuleRuntimeInfo {
327 Module(crate::Module),
328 Bare(Box<BareModuleInfo>),
329}
330
331#[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 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 #[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 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 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 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 #[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 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 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 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#[cfg(has_virtual_memory)]
471pub fn host_page_size() -> usize {
472 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#[derive(Copy, Clone, PartialEq, Eq, Debug)]
489pub enum WaitResult {
490 Ok = 0,
493 Mismatch = 1,
496 TimedOut = 2,
499}
500
501#[derive(Debug)]
503pub struct WasmFault {
504 pub memory_size: usize,
506 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}