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