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

wasmer-wasix

WASI and WASIX implementation library for Wasmer WebAssembly runtime

31 breaking releases

new 0.32.0 Nov 25, 2024
0.30.0 Oct 29, 2024
0.25.0 Jul 16, 2024
0.18.1 Mar 4, 2024
0.1.0 Mar 23, 2023

#107 in WebAssembly

Download history 20482/week @ 2024-08-05 12964/week @ 2024-08-12 37977/week @ 2024-08-19 10550/week @ 2024-08-26 11285/week @ 2024-09-02 9650/week @ 2024-09-09 6043/week @ 2024-09-16 5661/week @ 2024-09-23 9097/week @ 2024-09-30 14469/week @ 2024-10-07 8803/week @ 2024-10-14 9152/week @ 2024-10-21 11244/week @ 2024-10-28 8437/week @ 2024-11-04 9076/week @ 2024-11-11 6867/week @ 2024-11-18

35,986 downloads per month
Used in 21 crates (13 directly)

MIT license

5MB
94K 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 std::io::Read;
use wasmer::{Module, Store};
use wasmer_wasix::{Pipe, WasiEnv};

let wasm_path = "hello.wasm";

// Let's declare the Wasm module with the text representation.
let wasm_bytes = std::fs::read(wasm_path)?;

// Create a Store.
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
let module = Module::new(&store, wasm_bytes)?;

let (stdout_tx, mut stdout_rx) = Pipe::channel();

// Run the module.
WasiEnv::builder("hello")
     .args(&["Gordon"])
    // .env("KEY", "Value")
    .stdout(Box::new(stdout_tx))
    .run_with_store(module, &mut store)?;

eprintln!("Run complete - reading output");

let mut buf = String::new();
stdout_rx.read_to_string(&mut buf).unwrap();

eprintln!("Output: {buf}");

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

~30–50MB
~870K SLoC