wasmer_vm/
lib.rs

1//! Runtime library support for Wasmer.
2
3#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
4#![warn(unused_import_braces)]
5#![allow(clippy::new_without_default, ambiguous_wide_pointer_comparisons)]
6#![warn(
7    clippy::float_arithmetic,
8    clippy::mut_mut,
9    clippy::nonminimal_bool,
10    clippy::map_unwrap_or,
11    clippy::print_stdout,
12    clippy::unicode_not_nfc,
13    clippy::use_self
14)]
15#![cfg_attr(docsrs, feature(doc_cfg))]
16
17mod exception;
18mod export;
19mod extern_ref;
20mod function_env;
21mod global;
22mod imports;
23mod instance;
24mod memory;
25mod mmap;
26mod probestack;
27mod sig_registry;
28mod store;
29mod table;
30mod threadconditions;
31mod trap;
32mod vmcontext;
33
34#[cfg(feature = "experimental-host-interrupt")]
35pub mod interrupt_registry;
36pub mod libcalls;
37
38use std::ptr::NonNull;
39
40pub use crate::exception::{VMExceptionObj, VMExceptionRef};
41pub use crate::export::*;
42pub use crate::extern_ref::{VMExternObj, VMExternRef};
43pub use crate::function_env::VMFunctionEnvironment;
44pub use crate::global::*;
45pub use crate::imports::Imports;
46pub use crate::instance::{InstanceAllocator, VMInstance};
47pub use crate::memory::{
48    LinearMemory, NotifyLocation, VMMemory, VMOwnedMemory, VMSharedMemory,
49    initialize_memory_with_data,
50};
51pub use crate::mmap::{Mmap, MmapType};
52pub use crate::probestack::PROBESTACK;
53pub use crate::sig_registry::SignatureRegistry;
54pub use crate::store::{InternalStoreHandle, MaybeInstanceOwned, StoreHandle, StoreObjects};
55pub use crate::table::{TableElement, VMTable};
56#[doc(hidden)]
57pub use crate::threadconditions::{
58    ExpectedValue, ThreadConditions, ThreadConditionsHandle, WaiterError,
59};
60pub use crate::trap::*;
61pub use crate::vmcontext::{
62    VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionContext,
63    VMFunctionImport, VMFunctionKind, VMGlobalDefinition, VMGlobalImport, VMMemoryDefinition,
64    VMMemoryImport, VMSharedTagIndex, VMSignatureHash, VMTableDefinition, VMTableImport,
65    VMTrampoline,
66};
67pub use store::StoreObject;
68pub use wasmer_types::LibCall;
69pub use wasmer_types::MemoryError;
70pub use wasmer_types::MemoryStyle;
71use wasmer_types::RawValue;
72pub use wasmer_types::TableStyle;
73pub use wasmer_types::{StoreId, VMBuiltinFunctionIndex, VMOffsets};
74
75/// Version number of this crate.
76pub const VERSION: &str = env!("CARGO_PKG_VERSION");
77
78/// Pointers to section data.
79#[derive(Clone, Copy, Debug)]
80#[repr(transparent)]
81pub struct SectionBodyPtr(pub *const u8);
82
83impl std::ops::Deref for SectionBodyPtr {
84    type Target = *const u8;
85
86    fn deref(&self) -> &Self::Target {
87        &self.0
88    }
89}
90
91/// A placeholder byte-sized type which is just used to provide some amount of type
92/// safety when dealing with pointers to JIT-compiled function bodies. Note that it's
93/// deliberately not Copy, as we shouldn't be carelessly copying function body bytes
94/// around.
95#[repr(C)]
96pub struct VMFunctionBody(u8);
97
98/// A safe wrapper around `VMFunctionBody`.
99#[derive(Clone, Copy, Debug)]
100#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
101#[repr(transparent)]
102pub struct FunctionBodyPtr(pub *const VMFunctionBody);
103
104impl std::ops::Deref for FunctionBodyPtr {
105    type Target = *const VMFunctionBody;
106
107    fn deref(&self) -> &Self::Target {
108        &self.0
109    }
110}
111
112/// # Safety
113/// The VMFunctionBody that this points to is opaque, so there's no data to
114/// read or write through this pointer. This is essentially a usize.
115unsafe impl Send for FunctionBodyPtr {}
116/// # Safety
117/// The VMFunctionBody that this points to is opaque, so there's no data to
118/// read or write through this pointer. This is essentially a usize.
119unsafe impl Sync for FunctionBodyPtr {}
120
121/// A function reference. A single word that points to metadata about a function.
122#[repr(transparent)]
123#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
124pub struct VMFuncRef(pub NonNull<VMCallerCheckedAnyfunc>);
125
126impl VMFuncRef {
127    /// Converts the `VMFuncRef` into a `RawValue`.
128    pub fn into_raw(self) -> RawValue {
129        RawValue {
130            funcref: self.0.as_ptr() as usize,
131        }
132    }
133
134    /// Extracts a `VMFuncRef` from a `RawValue`.
135    ///
136    /// # Safety
137    /// `raw.funcref` must be a valid pointer.
138    pub unsafe fn from_raw(raw: RawValue) -> Option<Self> {
139        unsafe { NonNull::new(raw.funcref as *mut VMCallerCheckedAnyfunc).map(Self) }
140    }
141}
142
143#[cfg(test)]
144mod test_vmfunction_body {
145    use super::VMFunctionBody;
146    use std::mem::size_of;
147
148    #[test]
149    fn check_vmfunction_body_offsets() {
150        assert_eq!(size_of::<VMFunctionBody>(), 1);
151    }
152}