From 2763d945a334ab08501a19b880286a6b2e595824 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Wed, 13 Mar 2024 01:24:11 +0100 Subject: [PATCH] Cleanup last redundant corpus_idx usages (#1930) * Cleanup last redundant corpus_idx usages * More cleanup --- fuzzers/baby_fuzzer_minimizing/src/main.rs | 2 +- libafl/src/inputs/generalized.rs | 10 ++--- libafl/src/mutators/mod.rs | 42 +++++++++++--------- libafl/src/mutators/mopt_mutator.rs | 2 +- libafl/src/mutators/multi.rs | 4 +- libafl/src/mutators/string.rs | 6 +-- libafl/src/stages/mutational.rs | 45 +++++++--------------- libafl/src/stages/string.rs | 10 +---- libafl/src/stages/tuneable.rs | 16 +++----- 9 files changed, 53 insertions(+), 84 deletions(-) diff --git a/fuzzers/baby_fuzzer_minimizing/src/main.rs b/fuzzers/baby_fuzzer_minimizing/src/main.rs index 945277b3eb..640e8c49e9 100644 --- a/fuzzers/baby_fuzzer_minimizing/src/main.rs +++ b/fuzzers/baby_fuzzer_minimizing/src/main.rs @@ -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(()) diff --git a/libafl/src/inputs/generalized.rs b/libafl/src/inputs/generalized.rs index 6eb4739f05..2252838168 100644 --- a/libafl/src/inputs/generalized.rs +++ b/libafl/src/inputs/generalized.rs @@ -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, - _state: &S, - corpus_idx: CorpusId, - ) -> Result { + fn try_transform_from(base: &mut Testcase, _state: &S) -> Result { let meta = base .metadata_map() .get::() .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()?; diff --git a/libafl/src/mutators/mod.rs b/libafl/src/mutators/mod.rs index cfe503b86f..6cd075d8fa 100644 --- a/libafl/src/mutators/mod.rs +++ b/libafl/src/mutators/mod.rs @@ -99,12 +99,13 @@ pub trait Mutator: Named { ) -> Result; /// 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, + _new_corpus_idx: Option, ) -> Result<(), Error> { Ok(()) } @@ -124,12 +125,13 @@ pub trait MultiMutator: Named { ) -> Result, 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, + _new_corpus_idx: Option, ) -> Result<(), Error> { Ok(()) } @@ -146,11 +148,12 @@ pub trait MutatorsTuple: HasLen { ) -> Result; /// 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, + new_corpus_idx: Option, ) -> Result<(), Error>; /// Gets the [`Mutator`] at the given index and runs the `mutate` function on it. @@ -163,6 +166,7 @@ pub trait MutatorsTuple: HasLen { ) -> Result; /// 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 MutatorsTuple for () { &mut self, _state: &mut S, _stage_idx: i32, - _corpus_idx: Option, + _new_corpus_idx: Option, ) -> Result<(), Error> { Ok(()) } @@ -213,7 +217,7 @@ impl MutatorsTuple for () { _index: usize, _state: &mut S, _stage_idx: i32, - _corpus_idx: Option, + _new_corpus_idx: Option, ) -> Result<(), Error> { Ok(()) } @@ -247,10 +251,10 @@ where &mut self, state: &mut S, stage_idx: i32, - corpus_idx: Option, + new_corpus_idx: Option, ) -> 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, + new_corpus_idx: Option, ) -> 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, + new_corpus_idx: Option, ) -> 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, + new_corpus_idx: Option, ) -> 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 MutatorsTuple for Vec>> { &mut self, state: &mut S, stage_idx: i32, - corpus_idx: Option, + new_corpus_idx: Option, ) -> 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 MutatorsTuple for Vec>> { index: usize, state: &mut S, stage_idx: i32, - corpus_idx: Option, + new_corpus_idx: Option, ) -> 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> { diff --git a/libafl/src/mutators/mopt_mutator.rs b/libafl/src/mutators/mopt_mutator.rs index 9165ec65e6..348269cead 100644 --- a/libafl/src/mutators/mopt_mutator.rs +++ b/libafl/src/mutators/mopt_mutator.rs @@ -419,7 +419,7 @@ where &mut self, state: &mut S, _stage_idx: i32, - _corpus_idx: Option, + _new_corpus_idx: Option, ) -> Result<(), Error> { let before = self.finds_before; let after = state.corpus().count() + state.solutions().count(); diff --git a/libafl/src/mutators/multi.rs b/libafl/src/mutators/multi.rs index 838703a8f6..b6a9847cdb 100644 --- a/libafl/src/mutators/multi.rs +++ b/libafl/src/mutators/multi.rs @@ -55,9 +55,9 @@ where &mut self, state: &mut S, stage_idx: i32, - corpus_idx: Option, + new_corpus_idx: Option, ) -> Result<(), Error> { - M::post_exec(self, state, stage_idx, corpus_idx) + M::post_exec(self, state, stage_idx, new_corpus_idx) } } diff --git a/libafl/src/mutators/string.rs b/libafl/src/mutators/string.rs index a9149b4503..ed8cb66097 100644 --- a/libafl/src/mutators/string.rs +++ b/libafl/src/mutators/string.rs @@ -34,11 +34,7 @@ where { type Post = StringIdentificationMetadata; - fn try_transform_from( - base: &mut Testcase, - state: &S, - _corpus_idx: CorpusId, - ) -> Result { + fn try_transform_from(base: &mut Testcase, state: &S) -> Result { let input = base.load_input(state.corpus())?.clone(); let metadata = base.metadata::().cloned()?; Ok((input, metadata)) diff --git a/libafl/src/stages/mutational.rs b/libafl/src/stages/mutational.rs index 23afeaa4e9..f22b7ada91 100644 --- a/libafl/src/stages/mutational.rs +++ b/libafl/src/stages/mutational.rs @@ -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: Sized { self, state: &mut S, stage_idx: i32, - corpus_idx: Option, + new_corpus_idx: Option, ) -> Result<(), Error> { Ok(()) } @@ -51,11 +54,7 @@ where type Post: MutatedTransformPost; /// Transform the provided testcase into this type - fn try_transform_from( - base: &mut Testcase, - state: &S, - corpus_idx: CorpusId, - ) -> Result; + fn try_transform_from(base: &mut Testcase, state: &S) -> Result; /// 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, - state: &S, - _corpus_idx: CorpusId, - ) -> Result { + fn try_transform_from(base: &mut Testcase, state: &S) -> Result { 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); diff --git a/libafl/src/stages/string.rs b/libafl/src/stages/string.rs index 08d8fcf90e..e9c9841375 100644 --- a/libafl/src/stages/string.rs +++ b/libafl/src/stages/string.rs @@ -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::() { return Ok(()); // skip recompute } diff --git a/libafl/src/stages/tuneable.rs b/libafl/src/stages/tuneable.rs index ec102e4a4c..496c8016ca 100644 --- a/libafl/src/stages/tuneable.rs +++ b/libafl/src/stages/tuneable.rs @@ -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);