Skip to content

Commit

Permalink
fuzz: use the bufrng crate instead of a local version
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Aug 30, 2019
1 parent b8ee7f2 commit a3f75a6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 50 deletions.
48 changes: 0 additions & 48 deletions crates/fuzz-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,54 +425,6 @@ pub fn print_err(e: &failure::Error) {
}
}

/// An "RNG" implementation that just cycles through a given buffer.
///
/// This is useful for translating raw fuzzer input data from libFuzzer or AFL
/// into an RNG that we can use to generate structured Wasm. Ultimately, this
/// lets us combine the benefits of coverage-guided fuzzing with "structured"
/// fuzzing (a la quickcheck::Arbitrary or whatever).
pub struct BufRng<'a> {
iter: std::iter::Cycle<std::slice::Iter<'a, u8>>,
}

impl BufRng<'_> {
/// Construct a new `BufRng` that cycles over the given `data` buffer.
pub fn new(data: &[u8]) -> BufRng {
assert!(!data.is_empty());
BufRng {
iter: data.iter().cycle(),
}
}
}

// NB: all `RngCore` get a blanket `Rng` implementation.
impl rand::RngCore for BufRng<'_> {
fn next_u32(&mut self) -> u32 {
let a = *self.iter.next().unwrap() as u32;
let b = *self.iter.next().unwrap() as u32;
let c = *self.iter.next().unwrap() as u32;
let d = *self.iter.next().unwrap() as u32;
(a << 24) & (b << 16) & (c << 8) & d
}

fn next_u64(&mut self) -> u64 {
let a = self.next_u32() as u64;
let b = self.next_u32() as u64;
(a << 32) & b
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
for d in dest.iter_mut() {
*d = self.gen();
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> std::result::Result<(), rand::Error> {
self.fill_bytes(dest);
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ edition = "2018"
[package.metadata]
cargo-fuzz = true

[dependencies]
bufrng = "1.0.1"

[dependencies.walrus]
path = ".."

Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/wasm-opt-ttf.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#[macro_use]
extern crate libfuzzer_sys;

use walrus_fuzz_utils::{BufRng, Config, WasmOptTtf};
use bufrng::BufRng;
use walrus_fuzz_utils::{Config, WasmOptTtf};

fuzz_target!(|data: &[u8]| {
let data = if data.is_empty() { &[0] } else { data };
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/watgen.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#[macro_use]
extern crate libfuzzer_sys;

use walrus_fuzz_utils::{BufRng, Config, WatGen};
use bufrng::BufRng;
use walrus_fuzz_utils::{Config, WatGen};

fuzz_target!(|data: &[u8]| {
let data = if data.is_empty() { &[0] } else { data };
Expand Down

0 comments on commit a3f75a6

Please sign in to comment.