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 export;
57mod gc;
58mod imports;
59mod instance;
60mod memory;
61mod mmap_vec;
62#[cfg(has_virtual_memory)]
63mod pagemap_disabled;
64mod provenance;
65mod send_sync_ptr;
66mod stack_switching;
67mod store_box;
68mod sys;
69mod table;
70#[cfg(feature = "gc")]
71mod throw;
72mod traphandlers;
73mod vmcontext;
74
75#[cfg(feature = "threads")]
76mod parking_spot;
77
78#[cfg(all(has_host_compiler_backend, feature = "debug-builtins"))]
82pub mod debug_builtins;
83pub mod libcalls;
84pub mod mpk;
85
86#[cfg(feature = "pulley")]
87pub(crate) mod interpreter;
88#[cfg(not(feature = "pulley"))]
89pub(crate) mod interpreter_disabled;
90#[cfg(not(feature = "pulley"))]
91pub(crate) use interpreter_disabled as interpreter;
92
93#[cfg(feature = "component-model-async")]
94pub(crate) use sys::{component_async_tls_get, component_async_tls_set};
95
96#[cfg(feature = "debug-builtins")]
97pub use wasmtime_jit_debug::gdb_jit_int::GdbJitImageRegistration;
98
99pub use crate::runtime::vm::always_mut::*;
100pub use crate::runtime::vm::export::*;
101pub use crate::runtime::vm::gc::*;
102pub use crate::runtime::vm::imports::Imports;
103pub use crate::runtime::vm::instance::{
104 GcHeapAllocationIndex, Instance, InstanceAllocationRequest, InstanceAllocator, InstanceHandle,
105 MemoryAllocationIndex, OnDemandInstanceAllocator, TableAllocationIndex,
106};
107#[cfg(feature = "pooling-allocator")]
108pub use crate::runtime::vm::instance::{
109 InstanceLimits, PoolConcurrencyLimitError, PoolingAllocatorMetrics, PoolingInstanceAllocator,
110 PoolingInstanceAllocatorConfig,
111};
112pub use crate::runtime::vm::interpreter::*;
113pub use crate::runtime::vm::memory::{
114 Memory, MemoryBase, RuntimeLinearMemory, RuntimeMemoryCreator, SharedMemory,
115};
116pub use crate::runtime::vm::mmap_vec::MmapVec;
117pub use crate::runtime::vm::provenance::*;
118pub use crate::runtime::vm::stack_switching::*;
119pub use crate::runtime::vm::store_box::*;
120#[cfg(feature = "std")]
121pub use crate::runtime::vm::sys::mmap::open_file_for_mmap;
122#[cfg(has_host_compiler_backend)]
123pub use crate::runtime::vm::sys::unwind::UnwindRegistration;
124pub use crate::runtime::vm::table::{Table, TableElementType};
125#[cfg(feature = "gc")]
126pub use crate::runtime::vm::throw::*;
127pub use crate::runtime::vm::traphandlers::*;
128#[cfg(feature = "component-model")]
129pub use crate::runtime::vm::vmcontext::VMArrayCallFunction;
130pub use crate::runtime::vm::vmcontext::{
131 VMArrayCallHostFuncContext, VMContext, VMFuncRef, VMFunctionImport, VMGlobalDefinition,
132 VMGlobalImport, VMGlobalKind, VMMemoryDefinition, VMMemoryImport, VMOpaqueContext,
133 VMStoreContext, VMTableImport, VMTagImport, VMWasmCallFunction, ValRaw,
134};
135#[cfg(has_custom_sync)]
136pub(crate) use sys::capi;
137
138pub use send_sync_ptr::SendSyncPtr;
139pub use wasmtime_unwinder::Unwind;
140
141#[cfg(has_host_compiler_backend)]
142pub use wasmtime_unwinder::{UnwindHost, get_stack_pointer};
143
144mod module_id;
145pub use module_id::CompiledModuleId;
146
147#[cfg(has_virtual_memory)]
148mod byte_count;
149#[cfg(has_virtual_memory)]
150mod cow;
151#[cfg(not(has_virtual_memory))]
152mod cow_disabled;
153#[cfg(has_virtual_memory)]
154mod mmap;
155
156#[allow(unused, reason = "hard to cfg on/off, weird feature interactions")]
157mod send_sync_unsafe_cell;
158#[allow(unused, reason = "hard to cfg on/off, weird feature interactions")]
159pub use send_sync_unsafe_cell::SendSyncUnsafeCell;
160
161cfg_if::cfg_if! {
162 if #[cfg(has_virtual_memory)] {
163 pub use crate::runtime::vm::byte_count::*;
164 pub use crate::runtime::vm::mmap::{Mmap, MmapOffset};
165 pub use self::cow::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
166 } else {
167 pub use self::cow_disabled::{MemoryImage, MemoryImageSlot, ModuleMemoryImages};
168 }
169}
170
171pub trait ModuleMemoryImageSource: Send + Sync + 'static {
173 fn wasm_data(&self) -> &[u8];
176
177 fn mmap(&self) -> Option<&MmapVec>;
180}
181
182pub unsafe trait VMStore: 'static {
203 fn store_opaque(&self) -> &StoreOpaque;
205
206 fn store_opaque_mut(&mut self) -> &mut StoreOpaque;
208
209 fn resource_limiter_and_store_opaque(
212 &mut self,
213 ) -> (Option<StoreResourceLimiter<'_>>, &mut StoreOpaque);
214
215 #[cfg(target_has_atomic = "64")]
219 fn new_epoch_updated_deadline(&mut self) -> Result<crate::UpdateDeadline>;
220
221 #[cfg(feature = "component-model")]
223 fn component_task_state_mut(&mut self) -> &mut crate::component::store::ComponentTaskState;
224
225 #[cfg(feature = "component-model-async")]
226 fn component_async_store(
227 &mut self,
228 ) -> &mut dyn crate::runtime::component::VMComponentAsyncStore;
229
230 #[cfg(feature = "debug")]
232 fn block_on_debug_handler(&mut self, event: crate::DebugEvent) -> crate::Result<()>;
233}
234
235impl Deref for dyn VMStore + '_ {
236 type Target = StoreOpaque;
237
238 fn deref(&self) -> &Self::Target {
239 self.store_opaque()
240 }
241}
242
243impl DerefMut for dyn VMStore + '_ {
244 fn deref_mut(&mut self) -> &mut Self::Target {
245 self.store_opaque_mut()
246 }
247}
248
249impl dyn VMStore + '_ {
250 pub(crate) unsafe fn unchecked_context_mut<T>(&mut self) -> StoreContextMut<'_, T> {
258 unsafe { StoreContextMut(&mut *(self as *mut dyn VMStore as *mut StoreInner<T>)) }
259 }
260}
261
262#[derive(Copy, Clone)]
277#[repr(transparent)]
278struct VMStoreRawPtr(pub NonNull<dyn VMStore>);
279
280unsafe impl Send for VMStoreRawPtr {}
283unsafe impl Sync for VMStoreRawPtr {}
284
285#[derive(Clone)]
288pub enum ModuleRuntimeInfo {
289 Module(crate::Module),
290 Bare(Arc<BareModuleInfo>),
291}
292
293#[derive(Clone)]
298pub struct BareModuleInfo {
299 module: Arc<wasmtime_environ::Module>,
300 offsets: VMOffsets<HostPtr>,
301 _registered_type: Option<RegisteredType>,
302}
303
304impl ModuleRuntimeInfo {
305 pub(crate) fn bare(module: Arc<wasmtime_environ::Module>) -> Result<Self, OutOfMemory> {
306 ModuleRuntimeInfo::bare_with_registered_type(module, None)
307 }
308
309 pub(crate) fn bare_with_registered_type(
310 module: Arc<wasmtime_environ::Module>,
311 registered_type: Option<RegisteredType>,
312 ) -> Result<Self, OutOfMemory> {
313 let info = try_new(BareModuleInfo {
314 offsets: VMOffsets::new(HostPtr, &module),
315 module,
316 _registered_type: registered_type,
317 })?;
318 Ok(ModuleRuntimeInfo::Bare(info))
319 }
320
321 pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {
323 match self {
324 ModuleRuntimeInfo::Module(m) => m.env_module(),
325 ModuleRuntimeInfo::Bare(b) => &b.module,
326 }
327 }
328
329 #[cfg(feature = "gc")]
332 fn engine_type_index(&self, module_index: ModuleInternedTypeIndex) -> VMSharedTypeIndex {
333 match self {
334 ModuleRuntimeInfo::Module(m) => m
335 .engine_code()
336 .signatures()
337 .shared_type(module_index)
338 .expect("bad module-level interned type index"),
339 ModuleRuntimeInfo::Bare(_) => unreachable!(),
340 }
341 }
342
343 fn memory_image(&self, memory: DefinedMemoryIndex) -> crate::Result<Option<&Arc<MemoryImage>>> {
346 match self {
347 ModuleRuntimeInfo::Module(m) => {
348 let images = m.memory_images()?;
349 Ok(images.and_then(|images| images.get_memory_image(memory)))
350 }
351 ModuleRuntimeInfo::Bare(_) => Ok(None),
352 }
353 }
354
355 #[cfg(feature = "pooling-allocator")]
359 fn unique_id(&self) -> Option<CompiledModuleId> {
360 match self {
361 ModuleRuntimeInfo::Module(m) => Some(m.id()),
362 ModuleRuntimeInfo::Bare(_) => None,
363 }
364 }
365
366 fn wasm_data(&self) -> &[u8] {
368 match self {
369 ModuleRuntimeInfo::Module(m) => m.engine_code().wasm_data(),
370 ModuleRuntimeInfo::Bare(_) => &[],
371 }
372 }
373
374 fn type_ids(&self) -> &[VMSharedTypeIndex] {
377 match self {
378 ModuleRuntimeInfo::Module(m) => m
379 .engine_code()
380 .signatures()
381 .as_module_map()
382 .values()
383 .as_slice(),
384 ModuleRuntimeInfo::Bare(_) => &[],
385 }
386 }
387
388 pub(crate) fn offsets(&self) -> &VMOffsets<HostPtr> {
390 match self {
391 ModuleRuntimeInfo::Module(m) => m.offsets(),
392 ModuleRuntimeInfo::Bare(b) => &b.offsets,
393 }
394 }
395}
396
397#[cfg(has_virtual_memory)]
399pub fn host_page_size() -> usize {
400 static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
403
404 return match PAGE_SIZE.load(Ordering::Relaxed) {
405 0 => {
406 let size = sys::vm::get_page_size();
407 assert!(size != 0);
408 PAGE_SIZE.store(size, Ordering::Relaxed);
409 size
410 }
411 n => n,
412 };
413}
414
415#[derive(Copy, Clone, PartialEq, Eq, Debug)]
417pub enum WaitResult {
418 Ok = 0,
421 Mismatch = 1,
424 TimedOut = 2,
427}
428
429#[derive(Debug)]
431pub struct WasmFault {
432 pub memory_size: usize,
434 pub wasm_address: u64,
436}
437
438impl fmt::Display for WasmFault {
439 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
440 write!(
441 f,
442 "memory fault at wasm address 0x{:x} in linear memory of size 0x{:x}",
443 self.wasm_address, self.memory_size,
444 )
445 }
446}
447
448pub fn assert_ready<F: Future>(f: F) -> F::Output {
467 one_poll(f).unwrap()
468}
469
470fn one_poll<F: Future>(f: F) -> Option<F::Output> {
482 let mut context = Context::from_waker(&Waker::noop());
483 match pin!(f).poll(&mut context) {
484 Poll::Ready(output) => Some(output),
485 Poll::Pending => None,
486 }
487}