From dec37fcfdde188b0c77276674b5d98925a48b7b4 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 13 Nov 2020 03:58:38 +0100 Subject: [PATCH] tidying code --- src/corpus/testcase.rs | 7 +++++-- src/engines/mod.rs | 4 ++-- src/inputs/bytes.rs | 14 ++++++++++++++ src/lib.rs | 1 + src/mutators/scheduled.rs | 1 + src/utils.rs | 13 ++++++++++++- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/corpus/testcase.rs b/src/corpus/testcase.rs index 92a5156a27..4400d62d37 100644 --- a/src/corpus/testcase.rs +++ b/src/corpus/testcase.rs @@ -98,8 +98,11 @@ where } /// Create a new Testcase instace given an input behind a Rc RefCell - pub fn new_rr(input: I) -> Rc> { - Rc::new(RefCell::new(Self::new(input))) + pub fn new_rr(input: T) -> Rc> + where + T: Into, + { + Rc::new(RefCell::new(Self::new(input.into()))) } /// Create a new Testcase instace given an input and a filename behind a Rc RefCell diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 3ac56c26b0..014013b5f5 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -110,10 +110,10 @@ mod tests { #[test] fn test_engine() { - let rand = DefaultRand::preseeded_rr(); + let rand: Rc<_> = DefaultRand::preseeded().into(); let mut corpus = InMemoryCorpus::::new(&rand); - let testcase = Testcase::new_rr(BytesInput::new(vec![0; 4])); + let testcase = Testcase::new_rr(vec![0; 4]); corpus.add(testcase); let executor: Rc>> = InMemoryExecutor::new_rr(harness); let mut engine = DefaultEngine::new(); diff --git a/src/inputs/bytes.rs b/src/inputs/bytes.rs index 089f0199ac..3f07a3e4d2 100644 --- a/src/inputs/bytes.rs +++ b/src/inputs/bytes.rs @@ -1,5 +1,7 @@ extern crate alloc; +use core::convert::From; + use crate::inputs::{HasBytesVec, HasTargetBytes, Input}; use crate::AflError; @@ -35,6 +37,18 @@ impl HasTargetBytes for BytesInput { } } +impl From> for BytesInput { + fn from(bytes: Vec) -> Self { + Self::new(bytes) + } +} + +impl From<&[u8]> for BytesInput { + fn from(bytes: &[u8]) -> Self { + Self::new(bytes.to_owned()) + } +} + impl BytesInput { /// Creates a new bytes input using the given bytes pub fn new(bytes: Vec) -> Self { diff --git a/src/lib.rs b/src/lib.rs index 88f0387016..01a9fcca56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub mod utils; use std::io; use thiserror::Error; +/// Main error struct for AFL #[derive(Error, Debug)] pub enum AflError { #[error("Error in Serialization: `{0}`")] diff --git a/src/mutators/scheduled.rs b/src/mutators/scheduled.rs index e211dd578f..b20d76ff1d 100644 --- a/src/mutators/scheduled.rs +++ b/src/mutators/scheduled.rs @@ -281,6 +281,7 @@ where I: Input + HasBytesVec, S: ScheduledMutator, { + /// Mutate bytes fn mutate(&mut self, corpus: &mut C, input: &mut I, stage_idx: i32) -> Result<(), AflError> { self.scheduled.mutate(corpus, input, stage_idx) } diff --git a/src/utils.rs b/src/utils.rs index 0b6d3eac91..ff4b8fa132 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -109,6 +109,13 @@ impl Rand for Xoshiro256StarRand { } } +use std::convert::Into; +impl Into>> for Xoshiro256StarRand { + fn into(self) -> Rc> { + Rc::new(RefCell::new(self)) + } +} + impl Xoshiro256StarRand { /// Creates a new Xoshiro rand with the given seed pub fn new(seed: u64) -> Self { @@ -117,6 +124,10 @@ impl Xoshiro256StarRand { ret } + pub fn to_rc_refcell(self) -> Rc> { + self.into() + } + /// Creates a new Xoshiro rand with the given seed, wrapped in a Rc>. pub fn new_rr(seed: u64) -> Rc> { Rc::new(RefCell::new(Self::new(seed))) @@ -137,8 +148,8 @@ impl Xoshiro256StarRand { } } -#[cfg(test)] /// fake rand, for testing purposes +#[cfg(test)] #[derive(Copy, Clone, Debug, Default)] pub struct XKCDRand { val: u64,