Cleanup last redundant corpus_idx usages (#1930)

* Cleanup last redundant corpus_idx usages

* More cleanup
This commit is contained in:
Dominik Maier 2024-03-13 01:24:11 +01:00 committed by GitHub
parent 9b96149f6c
commit 2763d945a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 53 additions and 84 deletions

View File

@ -138,7 +138,7 @@ pub fn main() -> Result<(), Error> {
state.load_initial_inputs_forced(&mut fuzzer, &mut executor, &mut mgr, &[solution_dir])?;
state.set_corpus_idx(CorpusId::from(0usize))?;
state.set_corpus_idx(CorpusId::from(0_usize))?;
stages.perform_all(&mut fuzzer, &mut executor, &mut state, &mut mgr)?;
Ok(())

View File

@ -6,7 +6,7 @@ use libafl_bolts::impl_serdeany;
use serde::{Deserialize, Serialize};
use crate::{
corpus::{CorpusId, Testcase},
corpus::Testcase,
inputs::BytesInput,
stages::mutational::{MutatedTransform, MutatedTransformPost},
state::{HasCorpus, HasMetadata},
@ -111,17 +111,13 @@ where
{
type Post = Self;
fn try_transform_from(
base: &mut Testcase<BytesInput>,
_state: &S,
corpus_idx: CorpusId,
) -> Result<Self, Error> {
fn try_transform_from(base: &mut Testcase<BytesInput>, _state: &S) -> Result<Self, Error> {
let meta = base
.metadata_map()
.get::<GeneralizedInputMetadata>()
.ok_or_else(|| {
Error::key_not_found(format!(
"Couldn't find the GeneralizedInputMetadata for corpus entry {corpus_idx}",
"Couldn't find the GeneralizedInputMetadata for corpus entry {base:?}",
))
})
.cloned()?;

View File

@ -99,12 +99,13 @@ pub trait Mutator<I, S>: Named {
) -> Result<MutationResult, Error>;
/// Post-process given the outcome of the execution
/// `new_corpus_idx` will be `Some` if a new `Testcase` was created this execution.
#[inline]
fn post_exec(
&mut self,
_state: &mut S,
_stage_idx: i32,
_corpus_idx: Option<CorpusId>,
_new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
Ok(())
}
@ -124,12 +125,13 @@ pub trait MultiMutator<I, S>: Named {
) -> Result<Vec<I>, Error>;
/// Post-process given the outcome of the execution
/// `new_corpus_idx` will be `Some` if a new `Testcase` was created this execution.
#[inline]
fn multi_post_exec(
&mut self,
_state: &mut S,
_stage_idx: i32,
_corpus_idx: Option<CorpusId>,
_new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
Ok(())
}
@ -146,11 +148,12 @@ pub trait MutatorsTuple<I, S>: HasLen {
) -> Result<MutationResult, Error>;
/// Runs the `post_exec` function on all `Mutators` in this `Tuple`.
/// `new_corpus_idx` will be `Some` if a new `Testcase` was created this execution.
fn post_exec_all(
&mut self,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error>;
/// Gets the [`Mutator`] at the given index and runs the `mutate` function on it.
@ -163,6 +166,7 @@ pub trait MutatorsTuple<I, S>: HasLen {
) -> Result<MutationResult, Error>;
/// Gets the [`Mutator`] at the given index and runs the `post_exec` function on it.
/// `new_corpus_idx` will be `Some` if a new `Testcase` was created this execution.
fn get_and_post_exec(
&mut self,
index: usize,
@ -191,7 +195,7 @@ impl<I, S> MutatorsTuple<I, S> for () {
&mut self,
_state: &mut S,
_stage_idx: i32,
_corpus_idx: Option<CorpusId>,
_new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
Ok(())
}
@ -213,7 +217,7 @@ impl<I, S> MutatorsTuple<I, S> for () {
_index: usize,
_state: &mut S,
_stage_idx: i32,
_corpus_idx: Option<CorpusId>,
_new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
Ok(())
}
@ -247,10 +251,10 @@ where
&mut self,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
self.0.post_exec(state, stage_idx, corpus_idx)?;
self.1.post_exec_all(state, stage_idx, corpus_idx)
self.0.post_exec(state, stage_idx, new_corpus_idx)?;
self.1.post_exec_all(state, stage_idx, new_corpus_idx)
}
fn get_and_mutate(
@ -273,13 +277,13 @@ where
index: usize,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
if index == 0 {
self.0.post_exec(state, stage_idx, corpus_idx)
self.0.post_exec(state, stage_idx, new_corpus_idx)
} else {
self.1
.get_and_post_exec(index - 1, state, stage_idx, corpus_idx)
.get_and_post_exec(index - 1, state, stage_idx, new_corpus_idx)
}
}
@ -320,9 +324,9 @@ where
&mut self,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
self.0.post_exec_all(state, stage_idx, corpus_idx)
self.0.post_exec_all(state, stage_idx, new_corpus_idx)
}
fn get_and_mutate(
@ -340,10 +344,10 @@ where
index: usize,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
self.0
.get_and_post_exec(index, state, stage_idx, corpus_idx)
.get_and_post_exec(index, state, stage_idx, new_corpus_idx)
}
fn names(&self) -> Vec<&str> {
@ -381,10 +385,10 @@ impl<I, S> MutatorsTuple<I, S> for Vec<Box<dyn Mutator<I, S>>> {
&mut self,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
for mutator in self.iter_mut() {
mutator.post_exec(state, stage_idx, corpus_idx)?;
mutator.post_exec(state, stage_idx, new_corpus_idx)?;
}
Ok(())
}
@ -407,12 +411,12 @@ impl<I, S> MutatorsTuple<I, S> for Vec<Box<dyn Mutator<I, S>>> {
index: usize,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
let mutator = self
.get_mut(index)
.ok_or_else(|| Error::key_not_found("Mutator with id {index:?} not found."))?;
mutator.post_exec(state, stage_idx, corpus_idx)
mutator.post_exec(state, stage_idx, new_corpus_idx)
}
fn names(&self) -> Vec<&str> {

View File

@ -419,7 +419,7 @@ where
&mut self,
state: &mut S,
_stage_idx: i32,
_corpus_idx: Option<CorpusId>,
_new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
let before = self.finds_before;
let after = state.corpus().count() + state.solutions().count();

View File

@ -55,9 +55,9 @@ where
&mut self,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
M::post_exec(self, state, stage_idx, corpus_idx)
M::post_exec(self, state, stage_idx, new_corpus_idx)
}
}

View File

@ -34,11 +34,7 @@ where
{
type Post = StringIdentificationMetadata;
fn try_transform_from(
base: &mut Testcase<BytesInput>,
state: &S,
_corpus_idx: CorpusId,
) -> Result<Self, Error> {
fn try_transform_from(base: &mut Testcase<BytesInput>, state: &S) -> Result<Self, Error> {
let input = base.load_input(state.corpus())?.clone();
let metadata = base.metadata::<StringIdentificationMetadata>().cloned()?;
Ok((input, metadata))

View File

@ -6,14 +6,17 @@ use core::{any::type_name, marker::PhantomData};
use libafl_bolts::{rands::Rand, Named};
use crate::{
corpus::{Corpus, CorpusId, HasCurrentCorpusIdx, Testcase},
corpus::{Corpus, CorpusId, Testcase},
fuzzer::Evaluator,
inputs::Input,
mark_feature_time,
mutators::{MultiMutator, MutationResult, Mutator},
stages::{ExecutionCountRestartHelper, RetryRestartHelper, Stage},
start_timer,
state::{HasCorpus, HasExecutions, HasMetadata, HasNamedMetadata, HasRand, UsesState},
state::{
HasCorpus, HasCurrentTestcase, HasExecutions, HasMetadata, HasNamedMetadata, HasRand,
UsesState,
},
Error,
};
#[cfg(feature = "introspection")]
@ -30,7 +33,7 @@ pub trait MutatedTransformPost<S>: Sized {
self,
state: &mut S,
stage_idx: i32,
corpus_idx: Option<CorpusId>,
new_corpus_idx: Option<CorpusId>,
) -> Result<(), Error> {
Ok(())
}
@ -51,11 +54,7 @@ where
type Post: MutatedTransformPost<S>;
/// Transform the provided testcase into this type
fn try_transform_from(
base: &mut Testcase<I>,
state: &S,
corpus_idx: CorpusId,
) -> Result<Self, Error>;
fn try_transform_from(base: &mut Testcase<I>, state: &S) -> Result<Self, Error>;
/// Transform this instance back into the original input type
fn try_transform_into(self, state: &S) -> Result<(I, Self::Post), Error>;
@ -70,11 +69,7 @@ where
type Post = ();
#[inline]
fn try_transform_from(
base: &mut Testcase<I>,
state: &S,
_corpus_idx: CorpusId,
) -> Result<Self, Error> {
fn try_transform_from(base: &mut Testcase<I>, state: &S) -> Result<Self, Error> {
state.corpus().load_input_into(base)?;
Ok(base.input().as_ref().unwrap().clone())
}
@ -118,17 +113,11 @@ where
state: &mut Z::State,
manager: &mut EM,
) -> Result<(), Error> {
let Some(corpus_idx) = state.current_corpus_idx()? else {
return Err(Error::illegal_state(
"state is not currently processing a corpus index",
));
};
let num = self.iterations(state)? - self.execs_since_progress_start(state)?;
start_timer!(state);
let mut testcase = state.corpus().get(corpus_idx)?.borrow_mut();
let Ok(input) = I::try_transform_from(&mut testcase, state, corpus_idx) else {
let num = self.iterations(state)? - self.execs_since_progress_start(state)?;
let mut testcase = state.current_testcase_mut()?;
let Ok(input) = I::try_transform_from(&mut testcase, state) else {
return Ok(());
};
drop(testcase);
@ -359,14 +348,8 @@ where
state: &mut Z::State,
manager: &mut EM,
) -> Result<(), Error> {
let Some(corpus_idx) = state.current_corpus_idx()? else {
return Err(Error::illegal_state(
"state is not currently processing a corpus index",
));
};
let mut testcase = state.corpus().get(corpus_idx)?.borrow_mut();
let Ok(input) = I::try_transform_from(&mut testcase, state, corpus_idx) else {
let mut testcase = state.current_testcase_mut()?;
let Ok(input) = I::try_transform_from(&mut testcase, state) else {
return Ok(());
};
drop(testcase);

View File

@ -11,7 +11,7 @@ use crate::{
corpus::HasTestcase,
inputs::{BytesInput, HasBytesVec},
stages::Stage,
state::{HasCorpus, HasMetadata, State, UsesState},
state::{HasCorpus, HasCurrentTestcase, HasMetadata, State, UsesState},
};
/// Metadata which stores the list of pre-computed string-like ranges in the input
@ -111,13 +111,7 @@ where
state: &mut Self::State,
_manager: &mut EM,
) -> Result<(), Error> {
let Some(corpus_idx) = state.current_corpus_idx()? else {
return Err(Error::illegal_state(
"state is not currently processing a corpus index",
));
};
let mut tc = state.testcase_mut(corpus_idx)?;
let mut tc = state.current_testcase_mut()?;
if tc.has_metadata::<StringIdentificationMetadata>() {
return Ok(()); // skip recompute
}

View File

@ -7,7 +7,6 @@ use libafl_bolts::{current_time, impl_serdeany, rands::Rand};
use serde::{Deserialize, Serialize};
use crate::{
corpus::{Corpus, HasCurrentCorpusIdx},
mark_feature_time,
mutators::{MutationResult, Mutator},
stages::{
@ -15,7 +14,10 @@ use crate::{
ExecutionCountRestartHelper, MutationalStage, Stage,
},
start_timer,
state::{HasCorpus, HasExecutions, HasMetadata, HasNamedMetadata, HasRand, UsesState},
state::{
HasCorpus, HasCurrentTestcase, HasExecutions, HasMetadata, HasNamedMetadata, HasRand,
UsesState,
},
Error, Evaluator,
};
#[cfg(feature = "introspection")]
@ -178,18 +180,12 @@ where
state: &mut Z::State,
manager: &mut EM,
) -> Result<(), Error> {
let Some(corpus_idx) = state.current_corpus_idx()? else {
return Err(Error::illegal_state(
"state is not currently processing a corpus index",
));
};
let fuzz_time = self.seed_fuzz_time(state)?;
let iters = self.fixed_iters(state)?;
start_timer!(state);
let mut testcase = state.corpus().get(corpus_idx)?.borrow_mut();
let Ok(input) = I::try_transform_from(&mut testcase, state, corpus_idx) else {
let mut testcase = state.current_testcase_mut()?;
let Ok(input) = I::try_transform_from(&mut testcase, state) else {
return Ok(());
};
drop(testcase);