swasm

WebAssembly binary format serialization/deserialization/interpreter


Keywords
serde, interpreter, webassembly, swasm, bytecode
Licenses
MIT/Apache-2.0

Documentation

swasm

Low-level WebAssembly format library.

Build Status crates.io link

Documentation

Rust WebAssembly format serializing/deserializing

Add to Cargo.toml

[dependencies]
swasm = "0.31"

and then

extern crate swasm;

let module = swasm::deserialize_file("./res/cases/v1/hello.swasm").unwrap();
assert!(module.code_section().is_some());

let code_section = module.code_section().unwrap(); // Part of the module with functions code

println!("Function count in swasm file: {}", code_section.bodies().len());

Wabt Test suite

swasm supports full wabt testsuite (https://github.com/WebAssembly/testsuite), running asserts that involves deserialization.

To run testsuite:

  • make sure you have all prerequisites to build wabt (since swasm builds it internally using wabt-rs, see https://github.com/WebAssembly/wabt)
  • checkout with submodules (git submodule update --init --recursive)
  • run cargo test --release --manifest-path=spec/Cargo.toml

Decoder can be fuzzed with cargo-fuzz using swasm-opt (https://github.com/WebAssembly/binaryen):

  • make sure you have all prerequisites to build binaryen and cargo-fuzz (cmake and a C++11 toolchain)
  • checkout with submodules (git submodule update --init --recursive)
  • install cargo fuzz subcommand with cargo install cargo-fuzz
  • set rustup to use a nightly toolchain, because cargo fuzz uses a rust compiler plugin: rustup override set nightly
  • run cargo fuzz run deserialize

no_std crates

This crate has a feature, std, that is enabled by default. To use this crate in a no_std context, add the following to your Cargo.toml:

[dependencies]
swasm = { version = "0.31", default-features = false }

Until allocator api is stabilized, this type of use is limited to nightly Rust.

License

swasm is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), at your choice.

See LICENSE-APACHE, and LICENSE-MIT for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in swasm by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.