wasmer_runtime_core_x/
lib.rs

1//! Wasmer Runtime Core Library
2//!
3//! This crate provides common data structures which are shared by compiler backends
4//! to implement a WebAssembly runtime.
5//!
6//! This crate also provides an API for users who use wasmer as an embedded wasm runtime which
7//! allows operations like compiling, instantiating, providing imports, access exports, memories,
8//! and tables for example.
9//!
10//! Most wasmer users should prefer the API which is re-exported by the `wasmer-runtime`
11//! library by default. This crate provides additional APIs which may be useful to users
12//! that wish to customize the wasmer runtime.
13//!
14
15#![deny(
16    dead_code,
17    missing_docs,
18    nonstandard_style,
19    unused_imports,
20    unused_mut,
21    unused_variables,
22    unused_unsafe,
23    unreachable_patterns
24)]
25#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
26#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]
27
28#[macro_use]
29extern crate serde_derive;
30
31#[allow(unused_imports)]
32#[macro_use]
33extern crate lazy_static;
34
35#[macro_use]
36mod macros;
37#[doc(hidden)]
38pub mod backend;
39mod backing;
40
41pub mod cache;
42pub mod codegen;
43pub mod error;
44pub mod export;
45pub mod global;
46pub mod import;
47pub mod instance;
48pub mod loader;
49pub mod memory;
50pub mod module;
51pub mod parse;
52mod sig_registry;
53pub mod structures;
54mod sys;
55pub mod table;
56#[cfg(all(unix, target_arch = "x86_64"))]
57pub mod trampoline_x64;
58pub mod typed_func;
59pub mod types;
60pub mod units;
61pub mod vm;
62#[doc(hidden)]
63pub mod vmcalls;
64#[cfg(all(unix, target_arch = "x86_64"))]
65pub use trampoline_x64 as trampoline;
66#[cfg(unix)]
67pub mod fault;
68#[cfg(feature = "generate-debug-information")]
69pub mod jit_debug;
70pub mod state;
71#[cfg(feature = "managed")]
72pub mod tiering;
73
74use self::error::CompileResult;
75#[doc(inline)]
76pub use self::error::Result;
77#[doc(inline)]
78pub use self::import::IsExport;
79#[doc(inline)]
80pub use self::instance::{DynFunc, Instance};
81#[doc(inline)]
82pub use self::module::Module;
83#[doc(inline)]
84pub use self::typed_func::Func;
85use std::sync::Arc;
86
87pub use wasmparser;
88
89use self::cache::{Artifact, Error as CacheError};
90
91pub mod prelude {
92    //! The prelude module is a helper module used to bring commonly used runtime core imports into
93    //! scope.
94
95    pub use crate::import::{ImportObject, Namespace};
96    pub use crate::types::{
97        FuncIndex, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
98        ImportedTableIndex, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex,
99        MemoryIndex, TableIndex, Type, Value,
100    };
101    pub use crate::vm;
102    pub use crate::{func, imports};
103}
104
105/// Compile a [`Module`] using the provided compiler from
106/// WebAssembly binary code. This function is useful if it
107/// is necessary to a compile a module before it can be instantiated
108/// and must be used if you wish to use a different backend from the default.
109pub fn compile_with(
110    wasm: &[u8],
111    compiler: &dyn backend::Compiler,
112) -> CompileResult<module::Module> {
113    let token = backend::Token::generate();
114    compiler
115        .compile(wasm, Default::default(), token)
116        .map(|mut inner| {
117            let inner_info: &mut crate::module::ModuleInfo = &mut inner.info;
118            inner_info.import_custom_sections(wasm).unwrap();
119            module::Module::new(Arc::new(inner))
120        })
121}
122
123/// The same as `compile_with` but changes the compiler behavior
124/// with the values in the `CompilerConfig`
125pub fn compile_with_config(
126    wasm: &[u8],
127    compiler: &dyn backend::Compiler,
128    compiler_config: backend::CompilerConfig,
129) -> CompileResult<module::Module> {
130    let token = backend::Token::generate();
131    compiler
132        .compile(wasm, compiler_config, token)
133        .map(|inner| module::Module::new(Arc::new(inner)))
134}
135
136/// Perform validation as defined by the
137/// WebAssembly specification. Returns `true` if validation
138/// succeeded, `false` if validation failed.
139pub fn validate(wasm: &[u8]) -> bool {
140    validate_and_report_errors(wasm).is_ok()
141}
142
143/// The same as `validate` but with an Error message on failure
144pub fn validate_and_report_errors(wasm: &[u8]) -> ::std::result::Result<(), String> {
145    validate_and_report_errors_with_features(wasm, Default::default())
146}
147
148/// The same as `validate_and_report_errors` but with a Features.
149pub fn validate_and_report_errors_with_features(
150    wasm: &[u8],
151    features: backend::Features,
152) -> ::std::result::Result<(), String> {
153    use wasmparser::WasmDecoder;
154    let config = wasmparser::ValidatingParserConfig {
155        operator_config: wasmparser::OperatorValidatorConfig {
156            enable_simd: features.simd,
157            enable_bulk_memory: false,
158            enable_multi_value: false,
159            enable_reference_types: false,
160            enable_threads: features.threads,
161
162            #[cfg(feature = "deterministic-execution")]
163            deterministic_only: true,
164        },
165    };
166    let mut parser = wasmparser::ValidatingParser::new(wasm, Some(config));
167    loop {
168        let state = parser.read();
169        match *state {
170            wasmparser::ParserState::EndWasm => break Ok(()),
171            wasmparser::ParserState::Error(ref e) => break Err(format!("{}", e)),
172            _ => {}
173        }
174    }
175}
176
177/// Creates a new module from the given cache [`Artifact`] for the specified compiler backend
178pub unsafe fn load_cache_with(
179    cache: Artifact,
180    compiler: &dyn backend::Compiler,
181) -> std::result::Result<module::Module, CacheError> {
182    let token = backend::Token::generate();
183    compiler
184        .from_cache(cache, token)
185        .map(|inner| module::Module::new(Arc::new(inner)))
186}
187
188/// The current version of this crate
189pub const VERSION: &str = env!("CARGO_PKG_VERSION");