Skip to content
A simple event-driven library for parsing WebAssembly binary files
Rust Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows Use actions/checkout@v1 to support submodules checkout (#154) Dec 3, 2019
benches
examples
fuzz Update to Rust 2018 Edition. Jun 27, 2019
src Remove `std` and `core` features (#137) Dec 4, 2019
tests Move from `wabt` to `wast` to parsing `*.wast` files (#146) Nov 18, 2019
testsuite @ 6d29716 Enable SIMD proposals spec tests; fix some V128 validation. (#130) Sep 9, 2019
.gitignore Add a rusty-tags entry to .gitignore. Sep 28, 2018
.gitmodules
.rustfmt.toml Add a minimal .rustfmt.toml so that IDEs know to apply rustfmt. Sep 28, 2018
CODE_OF_CONDUCT.md Make it a Bytecode Alliance project (#136) Nov 12, 2019
Cargo.toml Version 0.44 Dec 4, 2019
LICENSE Change wasmparser's license to "Apache-2.0 WITH LLVM-exception". Oct 9, 2018
ORG_CODE_OF_CONDUCT.md
README.md Make it a Bytecode Alliance project (#136) Nov 12, 2019
SECURITY.md
compare-master.sh
format-all.sh
test-all.sh enable/disable floats configuration (#121) Aug 21, 2019

README.md

The WebAssembly binary file decoder in Rust

A Bytecode Alliance project

Build Status crates.io link

The decoder library provides lightweight and fast decoding/parsing of WebAssembly binary files.

The other goal is minimal memory footprint. For this reason, there is no AST or IR of WebAssembly data.

See also its sibling at https://github.com/wasdk/wasmparser

Documentation

The documentation and examples can be found at the https://docs.rs/wasmparser/

Example

use wasmparser::WasmDecoder;
use wasmparser::Parser;
use wasmparser::ParserState;

fn get_name(bytes: &[u8]) -> &str {
  str::from_utf8(bytes).ok().unwrap()
}

fn main() {
  let ref buf: Vec<u8> = read_wasm_bytes();
  let mut parser = Parser::new(buf);
  loop {
    let state = parser.read();
    match *state {
        ParserState::BeginWasm { .. } => {
            println!("====== Module");
        }
        ParserState::ExportSectionEntry { field, ref kind, .. } => {
            println!("  Export {} {:?}", get_name(field), kind);
        }
        ParserState::ImportSectionEntry { module, field, .. } => {
            println!("  Import {}::{}", get_name(module), get_name(field))
        }
        ParserState::EndWasm => break,
        _ => ( /* println!(" Other {:?}", state) */ )
    }
  }
}

Fuzzing

To fuzz test wasmparser.rs, switch to a nightly Rust compiler and install cargo-fuzz:

cargo install cargo-fuzz

Then, from the root of the repository, run:

cargo fuzz run parse

If you want to use files as seeds for the fuzzer, add them to fuzz/corpus/parse/ and restart cargo-fuzz.

You can’t perform that action at this time.