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