This repository is a fork of Wasmtime extended with support for the WasmFX instruction set. WasmFX adds stack switching capabilities to core Wasm, thereby enabling interleaving of computation. The design of the instruction set extension is based on effect handlers, which provide a structured facility for handling non-local control flow.
Additional key resources are
- An explainer document with an informal introduction to the instruction set extension along with example programs.
- A formal description of the extension.
- A collection of benchmark programs and a prototype library for asynchronous I/O programming leveraging the instruction extension.
The build steps are equivalent to the standard steps for building Wasmtime from source, but using this repository instead. There is no need to build or install the original version of Wasmtime to use this fork.
Concretely, the steps are as follows:
- Make sure that you have a Rust toolchain installed, for example using rustup.
- Check out this repository:
git clone https://github.com/wasmfx/wasmfxtime.git
cd wasmfxtime
git submodule update --init
- Build in debug mode:
cargo build
As a result, a debug build of the wasmtime
executable will be created at
target/debug/wasmtime
.
Analogously, to build in release mode run cargo build --release
, which places the wasmtime
artifact in target/release/
.
The wasmtime
executable can compile and run WebAssembly modules in the usual way. However, if a module contains WasmFX instructions, then it is necessary to enable additional features, e.g.
wasmtime -W=exceptions,function-references,stack-switching my_module.wat
The first two features are prerequisite features, i.e. the exceptions
feature is required to support WebAssembly tags and the function-references
feature is required to support typed references. The stack-switching
feature enables support for the stack switching (viz. WasmFX) instruction set.
To run an arbitrary function exported as foo
by the module type
wasmtime -W=exceptions,function-references,stack-switching --invoke=foo my_module.wat
The following module implements a generator and consumer using stack switching.
(module
(type $ft (func))
(type $ct (cont $ft))
;; Tag used by generator, the i32 payload corresponds to the generated values
(tag $yield (param i32))
;; Printing function for unsigned integers.
;; This function is unrelated to stack switching.
(func $println_u32 (param $value i32)
;; See examples/generator.wat for actual implementation
)
;; Simple generator yielding values from 100 down to 1
(func $generator
(local $i i32)
(local.set $i (i32.const 100))
(loop $l
;; Suspend generator, yield current value of $i to consumer
(suspend $yield (local.get $i))
;; Decrement $i and exit loop once $i reaches 0
(local.tee $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l)
)
)
(elem declare func $generator)
(func $consumer
(local $c (ref $ct))
;; Create continuation executing function $generator
(local.set $c (cont.new $ct (ref.func $generator)))
(loop $loop
(block $on_yield (result i32 (ref $ct))
;; Resume continuation $c
(resume $ct (on $yield $on_yield) (local.get $c))
;; Generator returned: no more data
(return)
)
;; Generator suspend, stack contains [i32 (ref $ct)]
(local.set $c)
;; Stack now contains the i32 value yielded by generator
(call $println_u32)
(br $loop)
)
)
(func $start (export "_start")
(call $consumer)
)
)
See
examples/generator.wat
for the full version of the file, including the definition of $println_u32
.
Running the full version with
wasmtime -W=exceptions,function-references,stack-switching generator.wat
then prints the numbers 100 down to 1 in the terminal.
The implementation of the WasmFX proposal is currently limited in a few ways:
- The only supported platform is x64 Linux.
- Only a single module can be executed. In particular, providing additional
modules using the
--preload
option ofwasmtime
can lead to unexpected behavior.
A standalone runtime for WebAssembly
A Bytecode Alliance project
Guide | Contributing | Website | Chat
The Wasmtime CLI can be installed on Linux and macOS (locally) with a small install script:
curl https://wasmtime.dev/install.sh -sSf | bash
This script installs into $WASMTIME_HOME
(defaults to $HOME/.wasmtime
), and executable is placed in $WASMTIME_HOME/bin
.
Windows or otherwise interested users can download installers and binaries directly from the GitHub Releases page.
If you've got the Rust compiler installed then you can take some Rust source code:
fn main() {
println!("Hello, world!");
}
and compile/run it with:
$ rustup target add wasm32-wasip1
$ rustc hello.rs --target wasm32-wasip1
$ wasmtime hello.wasm
Hello, world!
(Note: make sure you installed Rust using the rustup
method in the official
instructions above, and do not have a copy of the Rust toolchain installed on
your system in some other way as well (e.g. the system package manager). Otherwise, the rustup target add...
command may not install the target for the correct copy of Rust.)
-
Fast. Wasmtime is built on the optimizing Cranelift code generator to quickly generate high-quality machine code either at runtime or ahead-of-time. Wasmtime is optimized for efficient instantiation, low-overhead calls between the embedder and wasm, and scalability of concurrent instances.
-
Secure. Wasmtime's development is strongly focused on correctness and security. Building on top of Rust's runtime safety guarantees, each Wasmtime feature goes through careful review and consideration via an RFC process. Once features are designed and implemented, they undergo 24/7 fuzzing donated by Google's OSS Fuzz. As features stabilize they become part of a release, and when things go wrong we have a well-defined security policy in place to quickly mitigate and patch any issues. We follow best practices for defense-in-depth and integrate protections and mitigations for issues like Spectre. Finally, we're working to push the state-of-the-art by collaborating with academic researchers to formally verify critical parts of Wasmtime and Cranelift.
-
Configurable. Wasmtime uses sensible defaults, but can also be configured to provide more fine-grained control over things like CPU and memory consumption. Whether you want to run Wasmtime in a tiny environment or on massive servers with many concurrent instances, we've got you covered.
-
WASI. Wasmtime supports a rich set of APIs for interacting with the host environment through the WASI standard.
-
Standards Compliant. Wasmtime passes the official WebAssembly test suite, implements the official C API of wasm, and implements future proposals to WebAssembly as well. Wasmtime developers are intimately engaged with the WebAssembly standards process all along the way too.
You can use Wasmtime from a variety of different languages through embeddings of the implementation.
Languages supported by the Bytecode Alliance:
- Rust - the
wasmtime
crate - C - the
wasm.h
,wasi.h
, andwasmtime.h
headers, CMake orwasmtime
Conan package - C++ - the
wasmtime-cpp
repository or usewasmtime-cpp
Conan package - Python - the
wasmtime
PyPI package - .NET - the
Wasmtime
NuGet package - Go - the
wasmtime-go
repository - Ruby - the
wasmtime
gem
Languages supported by the community:
- Elixir - the
wasmex
hex package - Perl - the
Wasm
Perl package'sWasm::Wasmtime
π Read the Wasmtime guide here! π
The wasmtime guide is the best starting point to learn about what Wasmtime can do for you or help answer your questions about Wasmtime. If you're curious in contributing to Wasmtime, it can also help you do that!
It's Wasmtime.