Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for defining custom mutators
This adds support for defining custom mutators, as described in https://github.com/google/fuzzing/blob/master/docs/structure-aware-fuzzing.md
- Loading branch information
Showing
5 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "example_mutator" | ||
version = "0.1.0" | ||
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
flate2 = "1.0.20" | ||
libfuzzer-sys = { path = ".." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#![no_main] | ||
|
||
use flate2::{read::GzDecoder, write::GzEncoder, Compression}; | ||
use libfuzzer_sys::{fuzz_mutator, fuzz_target}; | ||
use std::io::{Read, Write}; | ||
|
||
fuzz_target!(|data: &[u8]| { | ||
// Decompress the input data and crash if it starts with "boom". | ||
if let Some(data) = decompress(data) { | ||
if data.starts_with(b"boom") { | ||
panic!(); | ||
} | ||
} | ||
}); | ||
|
||
fuzz_mutator!( | ||
|data: &mut [u8], size: usize, max_size: usize, _seed: u32| { | ||
// Decompress the input data. If that fails, use a dummy value. | ||
let mut decompressed = decompress(&data[..size]).unwrap_or_else(|| b"hi".to_vec()); | ||
|
||
// Mutate the decompressed data with `libFuzzer`'s default mutator. Make | ||
// the `decompressed` vec's extra capacity available for insertion | ||
// mutations via `resize`. | ||
let len = decompressed.len(); | ||
let cap = decompressed.capacity(); | ||
decompressed.resize(cap, 0); | ||
let new_decompressed_size = libfuzzer_sys::fuzzer_mutate(&mut decompressed, len); | ||
|
||
// Recompress the mutated data. | ||
let compressed = compress(&decompressed[..new_decompressed_size]); | ||
|
||
// Copy the recompressed mutated data into `data` and return the new size. | ||
let new_size = std::cmp::min(max_size, compressed.len()); | ||
data[..new_size].copy_from_slice(&compressed[..new_size]); | ||
new_size | ||
} | ||
); | ||
|
||
fn decompress(data: &[u8]) -> Option<Vec<u8>> { | ||
let mut decoder = GzDecoder::new(data); | ||
let mut decompressed = Vec::new(); | ||
if decoder.read_to_end(&mut decompressed).is_ok() { | ||
Some(decompressed) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn compress(data: &[u8]) -> Vec<u8> { | ||
let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); | ||
encoder | ||
.write_all(data) | ||
.expect("writing into a vec is infallible"); | ||
encoder.finish().expect("writing into a vec is infallible") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters