forked from rust-fuzz/libfuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit migrates CI to using the `cargo fuzz` binary with its settings for compiling Rust code to avoid mismatches like the codegen unit issue found in rust-fuzz#89
- Loading branch information
1 parent
2ed340d
commit 8d0d439
Showing
17 changed files
with
156 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
target | ||
Cargo.lock | ||
corpus |
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
[package] | ||
name = "example" | ||
version = "0.1.0" | ||
authors = ["Simonas Kazlauskas <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
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,16 @@ | ||
[package] | ||
name = "example-fuzz" | ||
version = "0.1.0" | ||
authors = ["Simonas Kazlauskas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = { path = "../.." } | ||
example = { path = ".." } | ||
|
||
[[bin]] | ||
name = "bananas" | ||
path = "fuzz_targets/bananas.rs" |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ fuzz_target!(|data: &[u8]| { | |
if data == "banana!".as_bytes() { | ||
panic!("success!"); | ||
} | ||
example::bananas(data); | ||
}); |
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,5 @@ | ||
pub fn bananas(data: &[u8]) { | ||
if data == &b"banana!"[..] { | ||
panic!("success!"); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,5 +4,5 @@ version = "0.1.0" | |
authors = ["Simonas Kazlauskas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
libfuzzer-sys = { path = "..", features = ["arbitrary-derive"] } | ||
[target.'cfg(fuzzing)'.dependencies] | ||
arbitrary = "1" |
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,16 @@ | ||
[package] | ||
name = "example_arbitrary_fuzz" | ||
version = "0.1.0" | ||
authors = ["Simonas Kazlauskas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = { path = "../..", features = ["arbitrary-derive"] } | ||
example_arbitrary = { path = ".." } | ||
|
||
[[bin]] | ||
name = "rgb" | ||
path = "fuzz_targets/rgb.rs" |
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,7 @@ | ||
#![no_main] | ||
|
||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|rgb: example_arbitrary::Rgb| { | ||
example_arbitrary::test(rgb); | ||
}); |
13 changes: 5 additions & 8 deletions
13
example_arbitrary/src/main.rs → example_arbitrary/src/lib.rs
100755 → 100644
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 |
---|---|---|
@@ -1,18 +1,15 @@ | ||
#![no_main] | ||
|
||
use libfuzzer_sys::{arbitrary, fuzz_target}; | ||
|
||
#[derive(arbitrary::Arbitrary, Debug)] | ||
struct Rgb { | ||
#[derive(Debug)] | ||
#[cfg_attr(fuzzing, derive(arbitrary::Arbitrary))] | ||
pub struct Rgb { | ||
r: u8, | ||
g: u8, | ||
b: u8, | ||
} | ||
|
||
fuzz_target!(|rgb: Rgb| { | ||
pub fn test(rgb: Rgb) { | ||
if rgb.r < rgb.g { | ||
if rgb.g < rgb.b { | ||
panic!("success: r < g < b!"); | ||
} | ||
} | ||
}); | ||
} |
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 |
---|---|---|
|
@@ -4,8 +4,5 @@ version = "0.1.0" | |
authors = ["Nick Fitzgerald <[email protected]>"] | ||
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,16 @@ | ||
[package] | ||
name = "example_mutator_fuzz" | ||
version = "0.1.0" | ||
authors = ["Nick Fitzgerald <[email protected]>"] | ||
edition = "2018" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
flate2 = "1.0.20" | ||
libfuzzer-sys = { path = "../.." } | ||
|
||
[[bin]] | ||
name = "boom" | ||
path = "fuzz_targets/boom.rs" |
File renamed without changes.
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,50 @@ | ||
#![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]| { | ||
example_mutator::test(data); | ||
}); | ||
|
||
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, cap); | ||
|
||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use flate2::read::GzDecoder; | ||
use std::io::Read; | ||
|
||
pub fn test(data: &[u8]) { | ||
if let Some(data) = decompress(data) { | ||
if data.starts_with(b"boom") { | ||
panic!(); | ||
} | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} |