From ca1be853e133d08d6e846d05de9da00eb3c1218b Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Tue, 27 Oct 2020 11:45:10 +0100 Subject: [PATCH] code format, infos in cargo toml --- Cargo.toml | 13 ++++++++-- src/corpus/mod.rs | 53 +++++++++++++-------------------------- src/engines/aflengine.rs | 14 ++++------- src/engines/mod.rs | 4 +-- src/executors/mod.rs | 3 +-- src/inputs/bytes.rs | 5 ++-- src/inputs/mod.rs | 6 ++--- src/lib.rs | 1 - src/mutators/mod.rs | 6 ++--- src/mutators/scheduled.rs | 6 +---- src/utils.rs | 30 +++++++--------------- 11 files changed, 51 insertions(+), 90 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 45b4ea530a..92d3d3f2f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,15 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dev-dependencies] +criterion = "0.3" # Benchmarking + +[[bench]] +name = "rng_hash_benchmark" +harness = false + [dependencies] -xxhash-rust = { version = "0.8.0-beta.4", features = ["xxh3"] } -thiserror = "1.0" +xxhash-rust = { version = "0.8.0-beta.4", features = ["xxh3"] } # xxh3 hashing for rust +ahash = "0.5.6" # Fast hashing algorithm +thiserror = "1.0" # A nicer way to write Errors +hashbrown = "0.9" # A faster hashmap, nostd compatible \ No newline at end of file diff --git a/src/corpus/mod.rs b/src/corpus/mod.rs index 44d01f5cd1..f2bfc182c0 100644 --- a/src/corpus/mod.rs +++ b/src/corpus/mod.rs @@ -1,25 +1,20 @@ -use std::fmt::Debug; -use std::collections::HashMap; -use crate::Error; -use crate::utils::Rand; use crate::inputs::Input; +use crate::utils::Rand; +use crate::Error; +use hashbrown::HashMap; +use std::fmt::Debug; -pub trait TestcaseMetadata: Debug { - -} +pub trait TestcaseMetadata: Debug {} pub trait Testcase: Debug { - fn load_input(&mut self) -> Result<&Box, Error>; fn is_on_disk(&self) -> bool; fn get_filename(&self) -> &str; fn get_metadatas(&mut self) -> &mut HashMap>; - } /// Corpus with all current testcases pub trait Corpus: Debug { - /// Returns the number of elements fn count(&self) -> usize; @@ -33,16 +28,13 @@ pub trait Corpus: Debug { /// Gets the next entry fn get(&mut self) -> Result<&Box, Error>; - } /// A queue-like corpus pub trait Queue: Corpus { - fn get_cycles(&self) -> u64; fn get_pos(&self) -> usize; - } #[derive(Debug)] @@ -51,12 +43,10 @@ pub struct DefaultQueue<'a> { pos: usize, cycles: u64, entries: Vec>, - dir_path: String - + dir_path: String, } impl Corpus for DefaultQueue<'_> { - /// Returns the number of elements fn count(&self) -> usize { self.entries.len() @@ -92,7 +82,7 @@ impl Corpus for DefaultQueue<'_> { /// Gets the next entry fn get(&mut self) -> Result<&Box, Error> { if self.entries.len() == 0 { - return Err(Error::Unknown) + return Err(Error::Unknown); } self.pos = self.pos + 1; if self.pos >= self.entries.len() { @@ -104,7 +94,6 @@ impl Corpus for DefaultQueue<'_> { } impl Queue for DefaultQueue<'_> { - fn get_cycles(&self) -> u64 { self.cycles } @@ -112,34 +101,28 @@ impl Queue for DefaultQueue<'_> { fn get_pos(&self) -> usize { self.pos } - } impl DefaultQueue<'_> { - pub fn new<'a>(rand: &'a mut dyn Rand, dir_path: &str) -> DefaultQueue<'a> { - DefaultQueue{ - cycles: 0, - dir_path: dir_path.to_owned(), - entries: vec![], - pos: 0, - rand: rand, - } + DefaultQueue { + cycles: 0, + dir_path: dir_path.to_owned(), + entries: vec![], + pos: 0, + rand: rand, + } } - } #[derive(Debug, Default)] struct SimpleTestcase { - is_on_disk: bool, filename: String, metadatas: HashMap>, - } impl Testcase for SimpleTestcase { - fn load_input(&mut self) -> Result<&Box, Error> { // TODO: Implement Err(Error::Unknown) @@ -156,15 +139,14 @@ impl Testcase for SimpleTestcase { fn get_metadatas(&mut self) -> &mut HashMap> { &mut self.metadatas } - } impl SimpleTestcase { fn new(filename: &str) -> Self { - SimpleTestcase{ + SimpleTestcase { filename: filename.to_owned(), is_on_disk: false, - metadatas: HashMap::default() + metadatas: HashMap::default(), } } } @@ -184,6 +166,5 @@ mod tests { let filename = q.get().unwrap().get_filename().to_owned(); assert_eq!(filename, q.get().unwrap().get_filename()); assert_eq!(filename, "fancyfile"); - } -} \ No newline at end of file +} diff --git a/src/engines/aflengine.rs b/src/engines/aflengine.rs index 132a7d145d..c46199be04 100644 --- a/src/engines/aflengine.rs +++ b/src/engines/aflengine.rs @@ -1,14 +1,13 @@ use std::time; -use crate::utils::Rand; -use crate::feedbacks::Feedback; -use crate::stages::Stage; -use crate::executors::Executor; use crate::engines::Engine; +use crate::executors::Executor; +use crate::feedbacks::Feedback; use crate::monitors::Monitor; +use crate::stages::Stage; +use crate::utils::Rand; pub struct AflEngine<'a> { - pub rand: &'a mut dyn Rand, pub feedbacks: Vec>, @@ -24,9 +23,6 @@ pub struct AflEngine<'a> { // TODO: Map pub monitors: Vec>, - } -impl Engine<'_> for AflEngine<'_> { - -} \ No newline at end of file +impl Engine<'_> for AflEngine<'_> {} diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 4a64ecaabc..3df483b1eb 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -1,5 +1,3 @@ pub mod aflengine; -pub trait Engine<'a> { - -} \ No newline at end of file +pub trait Engine<'a> {} diff --git a/src/executors/mod.rs b/src/executors/mod.rs index 0605ed68d8..fdc8e75dc3 100644 --- a/src/executors/mod.rs +++ b/src/executors/mod.rs @@ -1,2 +1 @@ - -pub trait Executor {} \ No newline at end of file +pub trait Executor {} diff --git a/src/inputs/bytes.rs b/src/inputs/bytes.rs index 8f02b72061..de89983ac6 100644 --- a/src/inputs/bytes.rs +++ b/src/inputs/bytes.rs @@ -1,5 +1,5 @@ -use crate::Error; use crate::inputs::Input; +use crate::Error; #[derive(Clone, Debug, Default)] pub struct BytesInput { @@ -7,7 +7,6 @@ pub struct BytesInput { } impl Input for BytesInput { - fn serialize(&self) -> Result<&[u8], Error> { Ok(&self.bytes) } @@ -16,4 +15,4 @@ impl Input for BytesInput { self.bytes.extend_from_slice(buf); Ok(()) } -} \ No newline at end of file +} diff --git a/src/inputs/mod.rs b/src/inputs/mod.rs index 339c2d079e..8ec6a18831 100644 --- a/src/inputs/mod.rs +++ b/src/inputs/mod.rs @@ -1,13 +1,12 @@ pub mod bytes; use std::fs::File; -use std::io::Write; use std::io::Read; +use std::io::Write; use crate::Error; pub trait Input { - fn to_file(&self, path: &str) -> Result<(), Error> { let mut file = File::create(path)?; file.write_all(self.serialize()?)?; @@ -25,5 +24,4 @@ pub trait Input { fn serialize(&self) -> Result<&[u8], Error>; fn deserialize(&mut self, buf: &[u8]) -> Result<(), Error>; - -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index ffc31b1aa7..db651e759b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,6 @@ pub enum Error { Unknown, } - #[cfg(test)] mod tests { #[test] diff --git a/src/mutators/mod.rs b/src/mutators/mod.rs index 9abff03e84..dd176d3c10 100644 --- a/src/mutators/mod.rs +++ b/src/mutators/mod.rs @@ -1,10 +1,9 @@ -use std::io::Error; use crate::inputs::Input; +use std::io::Error; pub mod scheduled; pub trait Mutator { - //fn rand(&self) -> &Box; //fn rand_mut(&self) -> &mut Box; @@ -19,5 +18,4 @@ pub trait Mutator { } fn post_exec_at(&mut self, stage_idx: i32, is_interesting: bool) -> Result<(), Error>; - -} \ No newline at end of file +} diff --git a/src/mutators/scheduled.rs b/src/mutators/scheduled.rs index 0f7d7ed452..c333d4353e 100644 --- a/src/mutators/scheduled.rs +++ b/src/mutators/scheduled.rs @@ -1,12 +1,8 @@ use crate::mutators::Mutator; pub trait ScheduledMutator: Mutator { - fn iterations(&self) -> u64 { - //1 << (1 + self.rand_mut().below(7)) return 0; - } - -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 6176d86b8e..7b87525cbd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,12 +1,11 @@ //! Utility functions for AFL -use std::fmt::Debug; use std::debug_assert; +use std::fmt::Debug; use xxhash_rust::xxh3::xxh3_64_with_seed; /// Ways to get random around here pub trait Rand: Debug { - // Sets the seed of this Rand fn set_seed(&mut self, seed: u64); // Gets the next 64 bit value @@ -21,7 +20,7 @@ pub trait Rand: Debug { Modulo is biased - we don't want our fuzzing to be biased so let's do it right. See https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator - */ + */ let mut unbiased_rnd: u64; loop { unbiased_rnd = self.next(); @@ -31,7 +30,6 @@ pub trait Rand: Debug { } unbiased_rnd % upper_bound_excl - } // Gets a value between the given lower bound (inclusive) and upper bound (inclusive) @@ -39,26 +37,19 @@ pub trait Rand: Debug { debug_assert!(lower_bound_incl <= upper_bound_incl); lower_bound_incl + self.below(upper_bound_incl - lower_bound_incl + 1) } - } const HASH_CONST: u64 = 0xa5b35705; /// XXH3 Based, hopefully speedy, rnd implementation -/// +/// #[derive(Copy, Clone, Debug, Default)] pub struct Xoshiro256StarRand { - - rand_seed: [u64; 4], seeded: bool, - } impl Rand for Xoshiro256StarRand { - - - fn set_seed(&mut self, seed: u64) { self.rand_seed[0] = xxh3_64_with_seed(&HASH_CONST.to_le_bytes(), seed); self.rand_seed[1] = self.rand_seed[0] ^ 0x1234567890abcdef; @@ -69,8 +60,10 @@ impl Rand for Xoshiro256StarRand { } fn next(&mut self) -> u64 { - - let ret: u64 = self.rand_seed[0].wrapping_add(self.rand_seed[3]).rotate_left(23).wrapping_add(self.rand_seed[0]); + let ret: u64 = self.rand_seed[0] + .wrapping_add(self.rand_seed[3]) + .rotate_left(23) + .wrapping_add(self.rand_seed[0]); let t: u64 = self.rand_seed[1] << 17; self.rand_seed[2] ^= self.rand_seed[0]; @@ -83,20 +76,15 @@ impl Rand for Xoshiro256StarRand { self.rand_seed[3] = self.rand_seed[3].rotate_left(45); return ret; - } - } impl Xoshiro256StarRand { - pub fn new() -> Xoshiro256StarRand { - let mut ret: Xoshiro256StarRand = Default::default(); ret.set_seed(0); // TODO: Proper random seed? ret } - } /// Get the next higher power of two @@ -105,7 +93,7 @@ fn next_pow2(val: u64) -> u64 { if val <= 2 { return val; } - let mut out: u64 = val - 1; + let mut out: u64 = val.wrapping_sub(1); out |= out >> 1; out |= out >> 2; out |= out >> 4; @@ -116,7 +104,7 @@ fn next_pow2(val: u64) -> u64 { #[cfg(test)] mod tests { - use crate::utils::{Rand, Xoshiro256StarRand, next_pow2}; + use crate::utils::{next_pow2, Rand, Xoshiro256StarRand}; #[test] fn test_rand() {