WASI
Definition
WASI (WebAssembly System Interface) is a modular system interface designed to provide WebAssembly applications with a consistent and secure way to perform system-level operations, such as file and network access, across different environments. It aims to enable WebAssembly to run outside the browser, providing a sandboxed execution environment that abstracts away platform-specific details, thus enhancing portability and security.
Secure Settings Example
use wasi_common::WasiCtxBuilder;
use wasmtime::{Config, Engine, Store, Module, Instance};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure the engine with WASI support
let mut config = Config::new();
config.wasi(true);
// Create a new engine and store
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, WasiCtxBuilder::new().build());
// Load and instantiate a WASI module
let module = Module::from_file(&engine, "example.wasm")?;
let instance = Instance::new(&mut store, &module, &[])?;
// Execute the module's main function
let main = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
main.call(&mut store, ())?;
Ok(())
}
Insecure Settings Example
use wasmtime::{Config, Engine, Store, Module, Instance};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Incorrectly configured engine without WASI support
let config = Config::new(); // Missing config.wasi(true)
// Create a new engine and store
let engine = Engine::new(&config)?;
let store = Store::new(&engine, ());
// Load and instantiate a WASI module
let module = Module::from_file(&engine, "example.wasm")?;
let instance = Instance::new(&store, &module, &[])?;
// Execute the module's main function
let main = instance.get_typed_func::<(), ()>(&store, "_start")?;
main.call(&store, ())?;
Ok(())
}