README.md
1# The WebAssembly binary file decoder in Rust
2
3**A [Bytecode Alliance](https://bytecodealliance.org/) project**
4
5![CI](https://github.com/bytecodealliance/wasmparser/workflows/CI/badge.svg)
6[![crates.io link](https://img.shields.io/crates/v/wasmparser.svg)](https://crates.io/crates/wasmparser)
7
8The decoder library provides lightweight and fast decoding/parsing of WebAssembly binary files.
9
10The other goal is minimal memory footprint. For this reason, there is no AST or IR of WebAssembly data.
11
12See also its sibling at https://github.com/wasdk/wasmparser
13
14
15## Documentation
16
17The documentation and examples can be found at the https://docs.rs/wasmparser/
18
19
20## Example
21
22```rust
23use wasmparser::WasmDecoder;
24use wasmparser::Parser;
25use wasmparser::ParserState;
26
27fn get_name(bytes: &[u8]) -> &str {
28 str::from_utf8(bytes).ok().unwrap()
29}
30
31fn main() {
32 let ref buf: Vec<u8> = read_wasm_bytes();
33 let mut parser = Parser::new(buf);
34 loop {
35 let state = parser.read();
36 match *state {
37 ParserState::BeginWasm { .. } => {
38 println!("====== Module");
39 }
40 ParserState::ExportSectionEntry { field, ref kind, .. } => {
41 println!(" Export {} {:?}", get_name(field), kind);
42 }
43 ParserState::ImportSectionEntry { module, field, .. } => {
44 println!(" Import {}::{}", get_name(module), get_name(field))
45 }
46 ParserState::EndWasm => break,
47 _ => ( /* println!(" Other {:?}", state) */ )
48 }
49 }
50}
51```
52
53
54## Fuzzing
55
56To fuzz test wasmparser.rs, switch to a nightly Rust compiler and install [cargo-fuzz]:
57
58```
59cargo install cargo-fuzz
60```
61
62Then, from the root of the repository, run:
63
64```
65cargo fuzz run parse
66```
67
68If you want to use files as seeds for the fuzzer, add them to `fuzz/corpus/parse/` and restart cargo-fuzz.
69
70[cargo-fuzz]: https://github.com/rust-fuzz/cargo-fuzz
71