linera_wasmer_vm/
lib.rs

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