wasmer_runtime_unc/
lib.rs

1#![deny(
2    dead_code,
3    missing_docs,
4    nonstandard_style,
5    unused_imports,
6    unused_mut,
7    unused_variables,
8    unused_unsafe,
9    unreachable_patterns
10)]
11#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
12#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]
13
14//! Wasmer-runtime is a library that makes embedding WebAssembly
15//! in your application easy, efficient, and safe.
16//!
17//! # How to use Wasmer-Runtime
18//!
19//! The easiest way is to use the [`instantiate`] function to create an [`Instance`].
20//! Then you can use [`call`] or [`func`] and then [`call`][func.call] to call an exported function safely.
21//!
22//! [`instantiate`]: fn.instantiate.html
23//! [`Instance`]: struct.Instance.html
24//! [`call`]: struct.Instance.html#method.call
25//! [`func`]: struct.Instance.html#method.func
26//! [func.call]: struct.Function.html#method.call
27//!
28//! ## Here's an example:
29//!
30//! Given this WebAssembly:
31//!
32//! ```wat
33//! (module
34//!   (type $t0 (func (param i32) (result i32)))
35//!   (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)
36//!     get_local $p0
37//!     i32.const 1
38//!     i32.add))
39//! ```
40//!
41//! compiled into wasm bytecode, we can call the exported "add_one" function:
42//!
43//! ```ignore
44//! static WASM: &'static [u8] = &[
45//!     // The module above compiled to bytecode goes here.
46//!     // ...
47//! #   0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60,
48//! #   0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0b, 0x01, 0x07,
49//! #   0x61, 0x64, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01,
50//! #   0x07, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x0b, 0x00, 0x1a, 0x04, 0x6e,
51//! #   0x61, 0x6d, 0x65, 0x01, 0x0a, 0x01, 0x00, 0x07, 0x61, 0x64, 0x64, 0x5f,
52//! #   0x6f, 0x6e, 0x65, 0x02, 0x07, 0x01, 0x00, 0x01, 0x00, 0x02, 0x70, 0x30,
53//! ];
54//!
55//! use wasmer_runtime::{
56//!     instantiate,
57//!     Value,
58//!     imports,
59//!     error,
60//!     Func,
61//! };
62//!
63//! fn main() -> error::Result<()> {
64//!     // We're not importing anything, so make an empty import object.
65//!     let import_object = imports! {};
66//!
67//!     let mut instance = instantiate(WASM, &import_object)?;
68//!
69//!     let add_one: Func<i32, i32> = instance.exports.get("add_one")?;
70//!
71//!     let value = add_one.call(42)?;
72//!
73//!     assert_eq!(value, 43);
74//!
75//!     Ok(())
76//! }
77//! ```
78//!
79//! # Additional Notes:
80//!
81//! `wasmer-runtime` is built to support multiple compiler backends.
82//! Currently, we support the Singlepass, [Cranelift], and LLVM compilers
83//! with the [`wasmer-singlepass-backend`], [`wasmer-clif-backend`], and
84//! wasmer-llvm-backend crates, respectively.
85//!
86//! You can specify the compiler you wish to use with the [`compile_with`]
87//! function or use the default with the [`compile`] function.
88//!
89//! [Cranelift]: https://github.com/CraneStation/cranelift
90//! [LLVM]: https://llvm.org
91//! [`wasmer-singlepass-backend`]: https://crates.io/crates/wasmer-singlepass-backend
92//! [`wasmer-clif-backend`]: https://crates.io/crates/wasmer-clif-backend
93
94#[macro_use]
95extern crate serde_derive;
96
97pub use wasmer_runtime_core::backend::{ExceptionCode, Features};
98pub use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
99pub use wasmer_runtime_core::export::Export;
100pub use wasmer_runtime_core::global::Global;
101pub use wasmer_runtime_core::import::{ImportObject, LikeNamespace};
102pub use wasmer_runtime_core::instance::{DynFunc, Instance};
103pub use wasmer_runtime_core::memory::ptr::{Array, Item, WasmPtr};
104pub use wasmer_runtime_core::memory::Memory;
105pub use wasmer_runtime_core::module::Module;
106pub use wasmer_runtime_core::table::Table;
107pub use wasmer_runtime_core::types::Value;
108pub use wasmer_runtime_core::vm::Ctx;
109
110pub use wasmer_runtime_core::Func;
111pub use wasmer_runtime_core::{compile_with, validate};
112pub use wasmer_runtime_core::{func, imports};
113
114#[cfg(unix)]
115pub use wasmer_runtime_core::{
116    fault::{pop_code_version, push_code_version},
117    state::CodeVersion,
118};
119
120pub mod memory {
121    //! The memory module contains the implementation data structures and helper functions used to
122    //! manipulate and access wasm memory.
123    pub use wasmer_runtime_core::memory::{Atomically, Memory, MemoryView};
124}
125
126pub mod wasm {
127    //! Various types exposed by the Wasmer Runtime.
128    pub use wasmer_runtime_core::global::Global;
129    pub use wasmer_runtime_core::table::Table;
130    pub use wasmer_runtime_core::types::{
131        FuncSig, GlobalDescriptor, MemoryDescriptor, TableDescriptor, Type, Value,
132    };
133}
134
135pub mod error {
136    //! The error module contains the data structures and helper functions used to implement errors that
137    //! are produced and returned from the wasmer runtime.
138    pub use wasmer_runtime_core::cache::Error as CacheError;
139    pub use wasmer_runtime_core::error::*;
140}
141
142pub mod units {
143    //! Various unit types.
144    pub use wasmer_runtime_core::units::{Bytes, Pages};
145}
146
147pub mod types {
148    //! Types used in the Wasm runtime and conversion functions.
149    pub use wasmer_runtime_core::types::*;
150}
151
152pub mod cache;
153
154pub use wasmer_runtime_core::backend::{Compiler, CompilerConfig};
155
156/// Enum used to select which compiler should be used to generate code.
157#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
158pub enum Backend {
159    #[cfg(feature = "singlepass")]
160    /// Singlepass backend
161    Singlepass,
162    #[cfg(feature = "cranelift")]
163    /// Cranelift backend
164    Cranelift,
165    #[cfg(feature = "llvm")]
166    /// LLVM backend
167    LLVM,
168    /// Auto backend
169    Auto,
170}
171
172impl Backend {
173    /// Get a list of the currently enabled (via feature flag) backends.
174    pub fn variants() -> &'static [&'static str] {
175        &[
176            #[cfg(feature = "singlepass")]
177            "singlepass",
178            #[cfg(feature = "cranelift")]
179            "cranelift",
180            #[cfg(feature = "llvm")]
181            "llvm",
182            "auto",
183        ]
184    }
185
186    /// Stable string representation of the backend.
187    /// It can be used as part of a cache key, for example.
188    pub fn to_string(&self) -> &'static str {
189        match self {
190            #[cfg(feature = "singlepass")]
191            Backend::Singlepass => "singlepass",
192            #[cfg(feature = "cranelift")]
193            Backend::Cranelift => "cranelift",
194            #[cfg(feature = "llvm")]
195            Backend::LLVM => "llvm",
196            Backend::Auto => "auto",
197        }
198    }
199}
200
201impl Default for Backend {
202    fn default() -> Self {
203        #[cfg(all(feature = "default-backend-singlepass", not(feature = "docs")))]
204        return Backend::Singlepass;
205
206        #[cfg(any(feature = "default-backend-cranelift", feature = "docs"))]
207        return Backend::Cranelift;
208
209        #[cfg(all(feature = "default-backend-llvm", not(feature = "docs")))]
210        return Backend::LLVM;
211
212        #[cfg(not(any(
213            feature = "default-backend-singlepass",
214            feature = "default-backend-cranelift",
215            feature = "default-backend-llvm",
216        )))]
217        panic!("There is no default-backend set.");
218    }
219}
220
221impl std::str::FromStr for Backend {
222    type Err = String;
223    fn from_str(s: &str) -> Result<Backend, String> {
224        match s.to_lowercase().as_str() {
225            #[cfg(feature = "singlepass")]
226            "singlepass" => Ok(Backend::Singlepass),
227            #[cfg(feature = "cranelift")]
228            "cranelift" => Ok(Backend::Cranelift),
229            #[cfg(feature = "llvm")]
230            "llvm" => Ok(Backend::LLVM),
231            "auto" => Ok(Backend::Auto),
232            _ => Err(format!("The backend {} doesn't exist", s)),
233        }
234    }
235}
236
237/// Compile WebAssembly binary code into a [`Module`].
238/// This function is useful if it is necessary to
239/// compile a module before it can be instantiated
240/// (otherwise, the [`instantiate`] function should be used).
241///
242/// [`Module`]: struct.Module.html
243/// [`instantiate`]: fn.instantiate.html
244///
245/// # Params:
246/// * `wasm`: A `&[u8]` containing the
247///   binary code of the wasm module you want to compile.
248/// # Errors:
249/// If the operation fails, the function returns `Err(error::CompileError::...)`.
250///
251/// This function only exists if one of `default-backend-llvm`, `default-backend-cranelift`,
252/// or `default-backend-singlepass` is set.
253#[cfg(any(
254    feature = "default-backend-singlepass",
255    feature = "default-backend-cranelift",
256    feature = "default-backend-llvm",
257))]
258pub fn compile(wasm: &[u8]) -> error::CompileResult<Module> {
259    wasmer_runtime_core::compile_with(&wasm[..], &default_compiler())
260}
261
262/// The same as `compile` but takes a `CompilerConfig` for the purpose of
263/// changing the compiler's behavior
264///
265/// This function only exists if one of `default-backend-llvm`, `default-backend-cranelift`,
266/// or `default-backend-singlepass` is set.
267#[cfg(any(
268    feature = "default-backend-singlepass",
269    feature = "default-backend-cranelift",
270    feature = "default-backend-llvm",
271))]
272pub fn compile_with_config(
273    wasm: &[u8],
274    compiler_config: CompilerConfig,
275) -> error::CompileResult<Module> {
276    wasmer_runtime_core::compile_with_config(&wasm[..], &default_compiler(), compiler_config)
277}
278
279/// The same as `compile_with_config` but takes a `Compiler` for the purpose of
280/// changing the backend.
281pub fn compile_with_config_with(
282    wasm: &[u8],
283    compiler_config: CompilerConfig,
284    compiler: &dyn Compiler,
285) -> error::CompileResult<Module> {
286    wasmer_runtime_core::compile_with_config(&wasm[..], compiler, compiler_config)
287}
288
289/// Compile and instantiate WebAssembly code without
290/// creating a [`Module`].
291///
292/// [`Module`]: struct.Module.html
293///
294/// # Params:
295/// * `wasm`: A `&[u8]` containing the
296///   binary code of the wasm module you want to compile.
297/// * `import_object`: An object containing the values to be imported
298///   into the newly-created Instance, such as functions or
299///   Memory objects. There must be one matching property
300///   for each declared import of the compiled module or else a
301///   LinkError is thrown.
302/// # Errors:
303/// If the operation fails, the function returns a
304/// `error::CompileError`, `error::LinkError`, or
305/// `error::RuntimeError` (all combined into an `error::Error`),
306/// depending on the cause of the failure.
307///
308/// This function only exists if one of `default-backend-llvm`, `default-backend-cranelift`,
309/// or `default-backend-singlepass` is set.
310#[cfg(any(
311    feature = "default-backend-singlepass",
312    feature = "default-backend-cranelift",
313    feature = "default-backend-llvm",
314))]
315pub fn instantiate(wasm: &[u8], import_object: &ImportObject) -> error::Result<Instance> {
316    let module = compile(wasm)?;
317    module.instantiate(import_object)
318}
319
320/// Get a single instance of the default compiler to use.
321///
322/// The output of this function can be controlled by the mutually
323/// exclusive `default-backend-llvm`, `default-backend-singlepass`,
324/// and `default-backend-cranelift` feature flags.
325///
326/// This function only exists if one of `default-backend-llvm`, `default-backend-cranelift`,
327/// or `default-backend-singlepass` is set.
328#[cfg(any(
329    feature = "default-backend-singlepass",
330    feature = "default-backend-cranelift",
331    feature = "default-backend-llvm",
332))]
333pub fn default_compiler() -> impl Compiler {
334    #[cfg(any(
335        all(
336            feature = "default-backend-llvm",
337            not(feature = "docs"),
338            any(
339                feature = "default-backend-cranelift",
340                feature = "default-backend-singlepass"
341            )
342        ),
343        all(
344            not(feature = "docs"),
345            feature = "default-backend-cranelift",
346            feature = "default-backend-singlepass"
347        )
348    ))]
349    compile_error!(
350        "The `default-backend-X` features are mutually exclusive.  Please choose just one"
351    );
352
353    #[cfg(all(feature = "default-backend-llvm", not(feature = "docs")))]
354    use wasmer_llvm_backend::LLVMCompiler as DefaultCompiler;
355
356    #[cfg(all(feature = "default-backend-singlepass", not(feature = "docs")))]
357    use wasmer_singlepass_backend::SinglePassCompiler as DefaultCompiler;
358
359    #[cfg(any(feature = "default-backend-cranelift", feature = "docs"))]
360    use wasmer_clif_backend::CraneliftCompiler as DefaultCompiler;
361
362    return DefaultCompiler::new();
363}
364
365/// Get the `Compiler` as a trait object for the given `Backend`.
366/// Returns `Option` because support for the requested `Compiler` may
367/// not be enabled by feature flags.
368///
369/// To get a list of the enabled backends as strings, call `Backend::variants()`.
370pub fn compiler_for_backend(backend: Backend) -> Option<Box<dyn Compiler>> {
371    match backend {
372        #[cfg(feature = "cranelift")]
373        Backend::Cranelift => Some(Box::new(wasmer_clif_backend::CraneliftCompiler::new())),
374
375        #[cfg(any(feature = "singlepass"))]
376        Backend::Singlepass => Some(Box::new(
377            wasmer_singlepass_backend::SinglePassCompiler::new(),
378        )),
379
380        #[cfg(feature = "llvm")]
381        Backend::LLVM => Some(Box::new(wasmer_llvm_backend::LLVMCompiler::new())),
382
383        Backend::Auto => {
384            #[cfg(feature = "default-backend-singlepass")]
385            return Some(Box::new(
386                wasmer_singlepass_backend::SinglePassCompiler::new(),
387            ));
388            #[cfg(feature = "default-backend-cranelift")]
389            return Some(Box::new(wasmer_clif_backend::CraneliftCompiler::new()));
390            #[cfg(feature = "default-backend-llvm")]
391            return Some(Box::new(wasmer_llvm_backend::LLVMCompiler::new()));
392
393            #[cfg(not(any(
394                feature = "default-backend-singlepass",
395                feature = "default-backend-cranelift",
396                feature = "default-backend-llvm",
397            )))]
398            panic!("There is no default-compiler set.");
399        }
400    }
401}
402
403/// The current version of this crate.
404pub const VERSION: &str = env!("CARGO_PKG_VERSION");
405
406#[cfg(test)]
407mod test {
408    use super::*;
409    use std::str::FromStr;
410
411    #[test]
412    fn str_repr_matches() {
413        // if this test breaks, think hard about why it's breaking
414        // can we avoid having these be different?
415
416        for &backend in &[
417            #[cfg(feature = "cranelift")]
418            Backend::Cranelift,
419            #[cfg(feature = "llvm")]
420            Backend::LLVM,
421            #[cfg(feature = "singlepass")]
422            Backend::Singlepass,
423        ] {
424            assert_eq!(backend, Backend::from_str(backend.to_string()).unwrap());
425        }
426    }
427}