Debian Codegraph @

crates/wasm-mutate/README.md

raw
1 <div align="center">
2 <h1><code>wasm-mutate</code></h1>
3
4 <strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong>
5
6 <p>
7 <strong>wasm-mutate is a new tool for fuzzing Wasm compilers, runtimes, validators, and other Wasm-consuming programs.</strong>
8 </p>
9
10 <!-- TODO add proper links -->
11 <p>
12 <a href="https://crates.io/crates/wasm-mutate"><img src="https://img.shields.io/crates/v/wasm-mutate.svg?style=flat-square" alt="Crates.io version" /></a>
13 <a href="https://crates.io/crates/wasm-mutate"><img src="https://img.shields.io/crates/d/wasm-mutate.svg?style=flat-square" alt="Download" /></a>
14 <a href="https://docs.rs/wasm-mutate/"><img src="https://img.shields.io/static/v1?label=docs&message=wasm-mutate&color=blue&style=flat-square" alt="docs.rs docs" /></a>
15 </p>
16 </div>
17
18 <!-- . -->
19
20
21 ## Usage
22
23 Add `wasm-mutate` to your `Cargo.toml`:
24
25 ```shell
26 $ cargo add wasm-mutate
27 ```
28
29 You can also mutate a WebAssembly binary by using the cli tool:
30
31 ```bash
32 wasm-tools mutate original.wasm --seed 0 -o out.wasm --preserve-semantics
33 ```
34
35 ## Features
36
37 * **semantically equivalent transformations:** `wasm-mutate` has the ability to
38 only apply semantics-preserving changes to the input Wasm module. When it is
39 used in this mode, the mutated Wasm computes identical results when
40 given the same inputs as the original Wasm module.
41 * **determinism:** `wasm-mutate` is deterministic, i.e., given the same input
42 Wasm module and the same seed, it always produces the same mutated
43 output Wasm module.
44 * **libfuzzer integration**: `wasm-mutate` integrates well with mutation-based fuzzers like libFuzzer. It
45 reuses the fuzzer's raw input strings. `wasm-mutate` works with the
46 `LLVMFuzzerCustomMutator` hook and the
47 `libfuzzer_sys::fuzz_mutator!` macro.
48
49 ### Example
50
51 ```rust
52 #![no_main]
53
54 use libfuzzer_sys::{fuzz_mutator, fuzz_target};
55 use std::io::{BufRead, Read, Write};
56 use wasmparser::WasmFeatures;
57
58 fuzz_target!(|bytes: &[u8]| {
59 // Initialize the Wasm for example
60 });
61
62 fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
63 // Generate a random Wasm module with `wasm-smith` as well as a RNG seed for
64 let wasm = &data[..size];
65 let features = WasmFeatures::default();
66 let mut validator = wasmparser::Validator::new();
67 validator.wasm_features(features);
68 let validation_result = validator.validate_all(&wasm);
69
70 // Mutate the data if its a valid Wasm file, otherwise, create a random one
71 let wasm = if validation_result.is_ok() {
72 wasm.to_vec()
73 } else {
74 let (w, _) = match wasm_tools_fuzz::generate_valid_module_from_seed(seed, |config, u| {
75 config.exceptions_enabled = false;
76 config.simd_enabled = false;
77 config.reference_types_enabled = false;
78 config.memory64_enabled = false;
79 config.max_memories = 1;
80 Ok(())
81 }) {
82 Ok(m) => m,
83 Err(_) => {
84 return size;
85 }
86 };
87 w
88 };
89
90 let mutated_wasm = wasm_mutate::WasmMutate::default()
91 .seed(seed.into())
92 .fuel(1000)
93 .preserve_semantics(true)
94 .run(&wasm);
95
96 let mutated_wasm = match mutated_wasm {
97 Ok(w) => w,
98 Err(_) => wasm,
99 };
100
101 // The mutated Wasm should still be valid, since the input Wasm was valid.
102 let newsize = mutated_wasm.len();
103 data[..newsize].copy_from_slice(&mutated_wasm[..newsize]);
104 newsize
105 });
106
107
108 ```
109
110 * **test case reduction (WIP):** `wasm-mutate` can have the ability to restrict
111 mutations to only those that shrink the size of the Wasm module. If it is used
112 in this mode, `wasm-mutate` essentially becomes a Wasm test-case reducer. We
113 are currently working to provide a prototype of this feature as a separate
114 binary. The following pseudo-Rust provides the general picture of it as an
115 standard hill-climbing algorithm.
116
117 ```rust
118 let wasmmutate = wasm_mutate::WasmMutate::default()
119 .seed(seed)
120 .fuel(1000)
121 .preserve_semantics(true)
122 .reduce(true);
123
124 while MAX_ITERATIONS > 0 {
125 let new_wasm = wasmmutate.run(&wasm);
126 wasm = if check_equivalence(new_wasm, wasm) {
127 wasm
128 } else{
129 panic!("No valid transformations")
130 }
131 MAX_ITERATIONS -= 1;
132 }
133
134 return wasm
135
136 ```
137
138 # License
139
140 This project is licensed under the Apache 2.0 license with the LLVM exception.
141 See [LICENSE](../../LICENSE) for more details.
142
143 ### Contribution
144
145 Unless you explicitly state otherwise, any contribution intentionally submitted
146 for inclusion in this project by you, as defined in the Apache-2.0 license,
147 shall be licensed as above, without any additional terms or conditions.
148
149 ### Special contribution
150
151 * Javier Cabrera Arteaga (Phd. student at KTH)