#wasi #wasm-module #abi #sandbox #env-var

wasmer-wasix

WASI and WASIX implementation library for Wasmer WebAssembly runtime

17 breaking releases

0.18.3 Apr 8, 2024
0.18.1 Mar 4, 2024
0.18.0 Dec 23, 2023
0.16.0 Oct 27, 2023
0.1.0 Mar 23, 2023

#191 in WebAssembly

Download history 3896/week @ 2023-12-23 12494/week @ 2023-12-30 5063/week @ 2024-01-06 13958/week @ 2024-01-13 11086/week @ 2024-01-20 8956/week @ 2024-01-27 10918/week @ 2024-02-03 6811/week @ 2024-02-10 5838/week @ 2024-02-17 4037/week @ 2024-02-24 8157/week @ 2024-03-02 7979/week @ 2024-03-09 7291/week @ 2024-03-16 6657/week @ 2024-03-23 10493/week @ 2024-03-30 8815/week @ 2024-04-06

33,825 downloads per month
Used in 17 crates (11 directly)

MIT license

5MB
84K SLoC

wasmer-wasi Build Status Join Wasmer Slack MIT License crates.io

This crate provides the necessary imports to use WASI easily from Wasmer. WebAssembly System Interface (WASI for short) is a modular system interface for WebAssembly. WASI is being standardized in the WebAssembly subgroup.

Very succinctly, from the user perspective, WASI is a set of WebAssembly module imports under a specific namespace (which varies based on the WASI version). A program compiled for the wasm32-wasi target will be able to support standard I/O, file I/O, filesystem manipulation, memory management, time, string, environment variables, program startup etc.

Wasmer WASI is created with the aim to be fully sandboxed. We are able to achieve that thanks to our Virtual Filesystem implementation (wasmer-vfs) and by only allowing secure systemcalls back to the host.

Note: If you encounter any sandboxing issue please open an issue in the wasmer repo https://github.com/wasmerio/wasmer.

This crate provides the necessary API to create the imports to use WASI easily from the Wasmer runtime, through our ImportObject API.

Supported WASI versions

WASI version Support
wasi_unstable
wasi_snapshot_preview1

The special Latest version points to wasi_snapshot_preview1.

Learn more about the WASI version process (ephemeral, snapshot, old).

Usage

Let's consider the following hello.rs Rust program:

fn main() {
    println!("Hello, {:?}", std::env::args().nth(1));
}

Then, let's compile it to a WebAssembly module with WASI support:

$ rustc --target wasm32-wasi hello.rs

Finally, let's execute it with the wasmer CLI:

$ wasmer run hello.wasm Gordon
Hello, Some("Gordon")

… and programatically with the wasmer and the wasmer-wasi libraries:

use wasmer::{Store, Module, Instance};
use wasmer_wasix::WasiState;

let mut store = Store::default();
let module = Module::from_file(&store, "hello.wasm")?;

// Create the `WasiEnv`.
let wasi_env = WasiState::builder("command-name")
    .args(&["Gordon"])
    .finalize()?;

// Generate an `ImportObject`.
let mut wasi_thread = wasi_env.new_thread();
let import_object = wasi_thread.import_object(&module)?;

// Let's instantiate the module with the imports.
let instance = Instance::new(&module, &import_object)?;

// Let's call the `_start` function, which is our `main` function in Rust.
let start = instance.exports.get_function("_start")?;
start.call(&[])?;

Check the fully working example using WASI.

More resources

This library is about the WASI implementation inside the Wasmer runtime. It contains documentation about the implementation itself. However, if you wish to learn more about WASI, here is a list of links that may help you:

  • wasi-libc, WASI libc implementation for WebAssembly programs built on top of WASI system calls. It provides a wide array of POSIX-compatible C APIs, including support for standard I/O, file I/O, filesystem manipulation, memory management, time, string, environment variables, program startup, and many other APIs,
  • wasi-sdk, WASI-enabled WebAssembly C/C++ toolchain, based on wasi-libc,
  • WASI API documentation,
  • WASI C API header file,
  • WASI Application Binary Interface (ABI), where we learn about _start and _initialize (for reactors) for example.

Dependencies

~24–43MB
~744K SLoC