Executions field in Testcase

This commit is contained in:
Andrea Fioraldi 2021-11-29 14:22:27 +01:00
parent 6cd2d69bfc
commit 6dd107c4ef
3 changed files with 36 additions and 3 deletions

View File

@ -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<Duration>,
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() {

View File

@ -29,6 +29,8 @@ where
exec_time: Option<Duration>,
/// Cached len of the input, if any
cached_len: Option<usize>,
/// Number of executions done at discovery time
executions: usize,
}
impl<I> HasMetadata for Testcase<I>
@ -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,
}
}
}

View File

@ -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)?;