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