From 397099f0be58e8ffe7da027df9ee7553c2658668 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Sun, 15 Nov 2020 04:34:03 +0100 Subject: [PATCH] started reworking Testcases --- src/corpus/mod.rs | 8 +++---- src/corpus/testcase.rs | 47 ++++++++++++++++++++++----------------- src/executors/inmemory.rs | 8 +++---- src/lib.rs | 10 +++++++++ src/utils.rs | 13 +++++++++++ 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/corpus/mod.rs b/src/corpus/mod.rs index 22d26f99d4..e16b00c0c1 100644 --- a/src/corpus/mod.rs +++ b/src/corpus/mod.rs @@ -18,7 +18,7 @@ where I: Input, { /// Get the entries vector field - fn entries(&self) -> &Vec>>>; + fn entries(&self) -> &[Rc>>]; /// Get the entries vector field (mutable) fn entries_mut(&mut self) -> &mut Vec>>>; @@ -89,7 +89,7 @@ where I: Input, R: Rand, { - fn entries(&self) -> &Vec>>> { + fn entries(&self) -> &[Rc>>] { &self.entries } fn entries_mut(&mut self) -> &mut Vec>>> { @@ -147,7 +147,7 @@ where I: Input, R: Rand, { - fn entries(&self) -> &Vec>>> { + fn entries(&self) -> &[Rc>>] { &self.entries } fn entries_mut(&mut self) -> &mut Vec>>> { @@ -220,7 +220,7 @@ where I: Input, C: Corpus, { - fn entries(&self) -> &Vec>>> { + fn entries(&self) -> &[Rc>>] { self.corpus.entries() } fn entries_mut(&mut self) -> &mut Vec>>> { diff --git a/src/corpus/testcase.rs b/src/corpus/testcase.rs index 24e4b8235a..1e4135e7f2 100644 --- a/src/corpus/testcase.rs +++ b/src/corpus/testcase.rs @@ -24,23 +24,26 @@ pub trait TestcaseMetadata { fn name(&self) -> &'static str; } -/* -pub trait Testcase +pub trait TestcaseTraitTODO where I: Input, T: TestcaseMetadata, { + /// The input associated with this testcase + fn input(&self) -> &Option; - fn input(&mut self) -> Option + /// The input associated with this testcase (mutable) + fn input_mut(&mut self) -> &mut Option; - input: Option, /// Filename, if this testcase is backed by a file in the filesystem - filename: Option, - /// Map of metadatas associated with this testcase - metadatas: HashMap<&'static str, Box>, + fn filename(&self) -> &Option; + /// Map of metadatas associated with this testcase + fn metadatas(&self) -> &HashMap<&'static str, Box>; + + /// Map of metadatas associated with this testcase + fn metadatas_mut(&mut self) -> &mut HashMap<&'static str, Box>; } -*/ #[cfg(feature = "std")] pub enum FileBackedTestcase { @@ -49,10 +52,18 @@ pub enum FileBackedTestcase { /// A testcase that has been loaded, and not yet dirtied. /// The input should be equal to the on-disk state. - Loaded { input: I, filename: P }, + Loaded { + input: I, + filename: P, + //metadatas: HashMap<&'static str, Box>, + }, /// A testcase that has been mutated, but not yet written to disk - Dirty { input: I, filename: P }, + Dirty { + input: I, + filename: P, + //metadatas: HashMap<&'static str, Box>, + }, } #[cfg(feature = "std")] @@ -69,7 +80,10 @@ where match self { Self::Stored { filename } => { let input = I::from_file(&filename)?; - Ok(Self::Loaded { filename, input }) + Ok(Self::Loaded { + filename, + input, + }) } Self::Loaded { input: _, @@ -171,25 +185,18 @@ where pub fn input_mut(&mut self) -> &mut Option { &mut self.input } - /// Set the input - pub fn set_input(&mut self, input: Option) { - self.input = input; - } /// Get the filename, if any pub fn filename(&self) -> &Option { &self.filename } + /// Get the filename, if any (mutable) pub fn filename_mut(&mut self) -> &mut Option { &mut self.filename } - /// Set the filename - pub fn set_filename(&mut self, filename: Option) { - self.filename = filename; - } - /// Get all the metadatas into an HashMap + /// Get all the metadatas into an HashMap (mutable) pub fn metadatas(&mut self) -> &mut HashMap<&'static str, Box> { &mut self.metadatas } diff --git a/src/executors/inmemory.rs b/src/executors/inmemory.rs index 807b9ca393..00622e09a8 100644 --- a/src/executors/inmemory.rs +++ b/src/executors/inmemory.rs @@ -1,5 +1,3 @@ -extern crate alloc; - use alloc::boxed::Box; use alloc::rc::Rc; use alloc::vec::Vec; @@ -201,13 +199,13 @@ compile_error!("InMemoryExecutor not yet supported on this OS"); #[cfg(test)] mod tests { - extern crate alloc; + use alloc::boxed::Box; + use crate::executors::inmemory::InMemoryExecutor; use crate::executors::{Executor, ExitKind}; use crate::inputs::Input; use crate::observers::Observer; use crate::AflError; - use alloc::boxed::Box; #[derive(Clone)] struct NopInput {} @@ -233,7 +231,7 @@ mod tests { #[cfg(feature = "std")] fn test_harness_fn_nop(_executor: &dyn Executor, buf: &[u8]) -> ExitKind { - println! {"Fake exec with buf of len {}", buf.len()}; + println!("Fake exec with buf of len {}", buf.len()); ExitKind::Ok } diff --git a/src/lib.rs b/src/lib.rs index 6f5e6e1a0f..3e3d7c868f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,15 +22,24 @@ use std::io; /// Main error struct for AFL #[derive(Debug)] pub enum AflError { + /// Serialization error Serialize(String), + /// File related error #[cfg(feature = "std")] File(io::Error), + /// Optional val was supposed to be set, but isn't. EmptyOptional(String), + /// Key not in Map KeyNotFound(String), + /// No elements in the current item Empty(String), + /// End of iteration IteratorEnd(String), + /// This is not supported (yet) NotImplemented(String), + /// You're holding it wrong IllegalState(String), + /// Something else happened Unknown(String), } @@ -53,6 +62,7 @@ impl fmt::Display for AflError { } } +/// Create an AFL Error from io Error #[cfg(feature = "std")] impl From for AflError { fn from(err: io::Error) -> Self { diff --git a/src/utils.rs b/src/utils.rs index 3de7d72cb9..39bab1b8b6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -231,6 +231,19 @@ mod tests { assert!(rand.between(11, 20) > 10); } + #[cfg(feature = "std")] + #[test] + fn test_rand_preseeded() { + let mut rand_fixed = DefaultRand::new(0); + let mut rand = DefaultRand::preseeded(); + assert_ne!(rand.next(), rand_fixed.next()); + assert_ne!(rand.next(), rand.next()); + assert!(rand.below(100) < 100); + assert_eq!(rand.below(1), 0); + assert_eq!(rand.between(10, 10), 10); + assert!(rand.between(11, 20) > 10); + } + #[test] fn test_has_rand() { let rand = DefaultRand::new(0).into();