Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ Released YYYY-MM-DD.

### Changed

* TODO (or remove section if none)
* Recommend `SmallRng` over `StdRng` in the examples for faster, more lightweight
seeding and sampling
* Updated `rand` dependency from 0.8.5 to 0.10
* Updated `flate2` dependency from 1.0.24 to 1.1
* Rename `gen` variable to `rng` for better 2024 Edition compatability

### Deprecated

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ members = [
]

[dev-dependencies]
flate2 = "1.0.24"
rand = "0.8.5"
flate2 = "1.1"
rand = { version = "0.10", default-features = false }
2 changes: 1 addition & 1 deletion example_crossover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ authors = ["R. Elliott Childre"]
edition = "2021"

[target.'cfg(fuzzing)'.dependencies]
rand = "0.8"
rand = { version = "0.10", default-features = false }
2 changes: 1 addition & 1 deletion example_crossover/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
cargo-fuzz = true

[dependencies]
rand = "0.8"
rand = { version = "0.10", default-features = false }
libfuzzer-sys = { path = "../.." }
example_crossover = { path = ".." }

Expand Down
26 changes: 13 additions & 13 deletions example_crossover/fuzz/fuzz_targets/boom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use example_crossover::sum;
use libfuzzer_sys::{fuzz_crossover, fuzz_mutator, fuzz_target};
use rand::distributions::{Bernoulli, Distribution, Uniform};
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use rand::distr::{Bernoulli, Distribution, Uniform};
use rand::{rngs::SmallRng, seq::SliceRandom, Rng, SeedableRng};
use std::mem::size_of;

fuzz_target!(|data: &[u8]| {
Expand All @@ -17,8 +17,8 @@ fuzz_target!(|data: &[u8]| {
);
});

fn rfp(rng: &mut StdRng) -> f64 {
match Uniform::new_inclusive(0, 10).sample(rng) {
fn rfp<T: Rng>(rng: &mut T) -> f64 {
match Uniform::new_inclusive(0, 10).unwrap().sample(rng) {
0 => f64::NAN,
1 => f64::MIN,
2 => f64::MAX,
Expand All @@ -29,24 +29,24 @@ fn rfp(rng: &mut StdRng) -> f64 {
7 => f64::INFINITY,
8 => f64::NEG_INFINITY,
9 => 0.0,
10 => Uniform::new_inclusive(-1.0, 1.0).sample(rng),
10 => Uniform::new_inclusive(-1.0, 1.0).unwrap().sample(rng),
_ => 0.0,
}
}

fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
let mut gen = StdRng::seed_from_u64(seed.into());
let mut rng = SmallRng::seed_from_u64(seed.into());

match Uniform::new_inclusive(0, 3).sample(&mut gen) {
match Uniform::new_inclusive(0, 3).unwrap().sample(&mut rng) {
0 => {
// "Change [an] element"

// Not altering the size, so decode the intended space (i.e. `size`) as floats
let (_, floats, _) = unsafe { data[..size].align_to_mut::<f64>() };

if !floats.is_empty() {
let d = Uniform::new(0, floats.len());
floats[d.sample(&mut gen)] = rfp(&mut gen);
let d = Uniform::new(0, floats.len()).unwrap();
floats[d.sample(&mut rng)] = rfp(&mut rng);
}
}
1 => {
Expand All @@ -58,7 +58,7 @@ fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
let (_, floats, _) = unsafe { data[..plus_one].align_to_mut::<f64>() };

let last = floats.last_mut().unwrap();
*last = rfp(&mut gen);
*last = rfp(&mut rng);

return plus_one;
}
Expand All @@ -79,7 +79,7 @@ fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {

// Not altering the size, so decode the intended space (i.e. `size`) as floats
let (_, floats, _) = unsafe { data[..size].align_to_mut::<f64>() };
floats.shuffle(&mut gen);
floats.shuffle(&mut rng);
}
_ => unreachable!(),
};
Expand All @@ -88,7 +88,7 @@ fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
});

fuzz_crossover!(|data1: &[u8], data2: &[u8], out: &mut [u8], seed: u32| {
let mut gen = StdRng::seed_from_u64(seed.into());
let mut rng = SmallRng::seed_from_u64(seed.into());

let bd = Bernoulli::new(0.5).unwrap();

Expand All @@ -113,7 +113,7 @@ fuzz_crossover!(|data1: &[u8], data2: &[u8], out: &mut [u8], seed: u32| {
// Put into the destination, floats from either data1 or data2 if the
// Bernoulli distribution succeeds or fails
for i in 0..n {
out_floats[i] = if bd.sample(&mut gen) {
out_floats[i] = if bd.sample(&mut rng) {
d1_floats[i]
} else {
d2_floats[i]
Expand Down
2 changes: 1 addition & 1 deletion example_mutator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
edition = "2018"

[dependencies]
flate2 = "1.0.20"
flate2 = "1.1"
2 changes: 1 addition & 1 deletion example_mutator/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
cargo-fuzz = true

[dependencies]
flate2 = "1.0.24"
flate2 = "1.1"
libfuzzer-sys = { path = "../.." }

[[bin]]
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,17 @@ macro_rules! fuzz_target {
/// ```no_run
/// #![no_main]
///
/// use rand::{rngs::StdRng, Rng, SeedableRng};
/// use rand::{rngs::SmallRng, RngExt, SeedableRng};
///
/// libfuzzer_sys::fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
/// let mut rng = StdRng::seed_from_u64(seed as u64);
/// let mut rng = SmallRng::seed_from_u64(seed as u64);
///
/// # let first_mutation = |_, _, _, _| todo!();
/// # let second_mutation = |_, _, _, _| todo!();
/// # let third_mutation = |_, _, _, _| todo!();
/// # let fourth_mutation = |_, _, _, _| todo!();
/// // Choose which of our four supported kinds of mutations we want to make.
/// match rng.gen_range(0..4) {
/// match rng.random_range(0..4) {
/// 0 => first_mutation(rng, data, size, max_size),
/// 1 => second_mutation(rng, data, size, max_size),
/// 2 => third_mutation(rng, data, size, max_size),
Expand Down Expand Up @@ -632,7 +632,7 @@ pub fn fuzzer_mutate(data: &mut [u8], size: usize, max_size: usize) -> usize {
/// #![no_main]
///
/// use libfuzzer_sys::{fuzz_crossover, fuzz_mutator, fuzz_target, fuzzer_mutate};
/// use rand::{rngs::StdRng, Rng, SeedableRng};
/// use rand::{rngs::SmallRng, RngExt, SeedableRng};
/// use std::mem::size_of;
///
/// fuzz_target!(|data: &[u8]| {
Expand All @@ -651,11 +651,11 @@ pub fn fuzzer_mutate(data: &mut [u8], size: usize, max_size: usize) -> usize {
/// // Inject some ...potentially problematic values to make the example close
/// // more quickly.
/// fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
/// let mut gen = StdRng::seed_from_u64(seed.into());
/// let mut rng = SmallRng::seed_from_u64(seed.into());
///
/// let (_, floats, _) = unsafe { data[..size].align_to_mut::<f64>() };
///
/// let x = gen.gen_range(0..=1000);
/// let x = rng.random_range(0..=1000);
/// if x == 0 && !floats.is_empty() {
/// floats[0] = f64::INFINITY;
/// } else if x == 1000 && floats.len() > 1 {
Expand Down