From 6dd107c4ef1d77b83c9120ad77cc4114627372f1 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Mon, 29 Nov 2021 14:22:27 +0100 Subject: [PATCH] Executions field in Testcase --- libafl/src/corpus/ondisk.rs | 15 +++++++++++++++ libafl/src/corpus/testcase.rs | 18 ++++++++++++++++++ libafl/src/fuzzer/mod.rs | 6 +++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/libafl/src/corpus/ondisk.rs b/libafl/src/corpus/ondisk.rs index be5cf68a47..1086e57163 100644 --- a/libafl/src/corpus/ondisk.rs +++ b/libafl/src/corpus/ondisk.rs @@ -22,6 +22,15 @@ pub enum OnDiskMetadataFormat { JsonPretty, } +/// A corpus able to store testcases to disk, and load them from disk, when they are being used. +#[cfg(feature = "std")] +#[derive(Serialize)] +pub struct OnDiskMetadata<'a> { + metadata: &'a SerdeAnyMap, + exec_time: &'a Option, + executions: &'a usize, +} + /// A corpus able to store testcases to disk, and load them from disk, when they are being used. #[cfg(feature = "std")] #[derive(Default, Serialize, Deserialize, Clone, Debug)] @@ -91,6 +100,12 @@ where tmpfile_name.file_name().unwrap().to_string_lossy() )); + let ondisk_meta = OnDiskMetadata { + metadata: testcase.metadata(), + exec_time: testcase.exec_time(), + executions: testcase.executions(), + }; + let mut tmpfile = File::create(&tmpfile_name)?; let serialized = match self.meta_format.as_ref().unwrap() { diff --git a/libafl/src/corpus/testcase.rs b/libafl/src/corpus/testcase.rs index 0f1d1df4db..346fb9720d 100644 --- a/libafl/src/corpus/testcase.rs +++ b/libafl/src/corpus/testcase.rs @@ -29,6 +29,8 @@ where exec_time: Option, /// Cached len of the input, if any cached_len: Option, + /// Number of executions done at discovery time + executions: usize, } impl HasMetadata for Testcase @@ -148,6 +150,7 @@ where metadata: SerdeAnyMap::new(), exec_time: None, cached_len: None, + executions: 0, } } @@ -160,6 +163,20 @@ where metadata: SerdeAnyMap::new(), exec_time: None, cached_len: None, + executions: 0, + } + } + + /// Create a new Testcase instance given an [`Input`] and the number of executions + #[inline] + pub fn with_executions(input: I, executions: usize) -> Self { + Testcase { + input: Some(input), + filename: None, + metadata: SerdeAnyMap::new(), + exec_time: None, + cached_len: None, + executions, } } @@ -173,6 +190,7 @@ where metadata: SerdeAnyMap::new(), exec_time: None, cached_len: None, + executions: 0, } } } diff --git a/libafl/src/fuzzer/mod.rs b/libafl/src/fuzzer/mod.rs index e487edcb27..608f166475 100644 --- a/libafl/src/fuzzer/mod.rs +++ b/libafl/src/fuzzer/mod.rs @@ -361,7 +361,7 @@ where self.objective_mut().discard_metadata(state, &input)?; // Add the input to the main corpus - let mut testcase = Testcase::new(input.clone()); + let mut testcase = Testcase::with_executions(input.clone(), *state.executions()); self.feedback_mut().append_metadata(state, &mut testcase)?; let idx = state.corpus_mut().add(testcase)?; self.scheduler_mut().on_add(state, idx)?; @@ -393,7 +393,7 @@ where self.feedback_mut().discard_metadata(state, &input)?; // The input is a solution, add it to the respective corpus - let mut testcase = Testcase::new(input); + let mut testcase = Testcase::with_executions(input, *state.executions()); self.objective_mut().append_metadata(state, &mut testcase)?; state.solutions_mut().add(testcase)?; @@ -487,7 +487,7 @@ where self.objective_mut().discard_metadata(state, &input)?; // Add the input to the main corpus - let mut testcase = Testcase::new(input.clone()); + let mut testcase = Testcase::with_executions(input.clone(), *state.executions()); self.feedback_mut().append_metadata(state, &mut testcase)?; let idx = state.corpus_mut().add(testcase)?; self.scheduler_mut().on_add(state, idx)?;