diff --git a/libafl/src/common/nautilus/grammartec/context.rs b/libafl/src/common/nautilus/grammartec/context.rs index 77128ca573..35fcd22610 100644 --- a/libafl/src/common/nautilus/grammartec/context.rs +++ b/libafl/src/common/nautilus/grammartec/context.rs @@ -258,7 +258,7 @@ impl Context { max_len: usize, nt: NTermId, p_include_short_rules: usize, - ) -> impl Iterator + 'a { + ) -> impl Iterator + 'a { self.nts_to_rules[&nt] .iter() .take_while(move |r| self.rules_to_min_size[*r] <= max_len) diff --git a/libafl/src/executors/command.rs b/libafl/src/executors/command.rs index 9ecb90ae0d..5854414f8d 100644 --- a/libafl/src/executors/command.rs +++ b/libafl/src/executors/command.rs @@ -620,7 +620,7 @@ mod tests { command::{CommandExecutor, InputLocation}, Executor, }, - fuzzer::test::NopFuzzer, + fuzzer::NopFuzzer, inputs::BytesInput, monitors::SimpleMonitor, state::NopState, diff --git a/libafl/src/executors/inprocess_fork/mod.rs b/libafl/src/executors/inprocess_fork/mod.rs index 9d851d120d..769105ace0 100644 --- a/libafl/src/executors/inprocess_fork/mod.rs +++ b/libafl/src/executors/inprocess_fork/mod.rs @@ -394,7 +394,7 @@ mod tests { hooks::inprocess_fork::InChildProcessHooks, inprocess_fork::GenericInProcessForkExecutor, }, - fuzzer::test::NopFuzzer, + fuzzer::NopFuzzer, state::NopState, }; diff --git a/libafl/src/executors/mod.rs b/libafl/src/executors/mod.rs index 5e9d462b36..9483112ca8 100644 --- a/libafl/src/executors/mod.rs +++ b/libafl/src/executors/mod.rs @@ -165,7 +165,7 @@ pub fn common_signals() -> Vec { } #[cfg(test)] -pub mod test { +mod test { use core::marker::PhantomData; use libafl_bolts::{AsSlice, Error}; @@ -173,7 +173,7 @@ pub mod test { use crate::{ events::NopEventManager, executors::{Executor, ExitKind}, - fuzzer::test::NopFuzzer, + fuzzer::NopFuzzer, inputs::{BytesInput, HasTargetBytes}, state::{HasExecutions, NopState, State, UsesState}, }; @@ -186,6 +186,7 @@ pub mod test { } impl NopExecutor { + /// Creates a new [`NopExecutor`] #[must_use] pub fn new() -> Self { Self { diff --git a/libafl/src/fuzzer/mod.rs b/libafl/src/fuzzer/mod.rs index e9067a139d..3cdca8fc05 100644 --- a/libafl/src/fuzzer/mod.rs +++ b/libafl/src/fuzzer/mod.rs @@ -1,7 +1,7 @@ //! The `Fuzzer` is the main struct for a fuzz campaign. use alloc::{string::ToString, vec::Vec}; -use core::{fmt::Debug, time::Duration}; +use core::{fmt::Debug, marker::PhantomData, time::Duration}; use libafl_bolts::current_time; use serde::{de::DeserializeOwned, Serialize}; @@ -19,7 +19,7 @@ use crate::{ start_timer, state::{ HasCorpus, HasCurrentTestcase, HasExecutions, HasLastFoundTime, HasLastReportTime, - HasSolutions, Stoppable, UsesState, + HasSolutions, State, Stoppable, UsesState, }, Error, HasMetadata, }; @@ -931,62 +931,49 @@ where } } -#[cfg(test)] -pub mod test { - use core::marker::PhantomData; +/// A [`NopFuzzer`] that does nothing +#[derive(Clone, Debug)] +pub struct NopFuzzer { + phantom: PhantomData, +} - use libafl_bolts::Error; - - use crate::{ - corpus::CorpusId, - events::{EventProcessor, ProgressReporter}, - stages::{HasCurrentStageId, StagesTuple}, - state::{HasExecutions, HasLastReportTime, State, UsesState}, - Fuzzer, HasMetadata, - }; - - #[derive(Clone, Debug)] - pub struct NopFuzzer { - phantom: PhantomData, - } - - impl NopFuzzer { - #[must_use] - pub fn new() -> Self { - Self { - phantom: PhantomData, - } - } - } - - impl Default for NopFuzzer { - fn default() -> Self { - Self::new() - } - } - - impl UsesState for NopFuzzer - where - S: State, - { - type State = S; - } - - impl Fuzzer for NopFuzzer - where - E: UsesState, - EM: ProgressReporter + EventProcessor, - ST: StagesTuple, - Self::State: HasMetadata + HasExecutions + HasLastReportTime + HasCurrentStageId, - { - fn fuzz_one( - &mut self, - _stages: &mut ST, - _executor: &mut E, - _state: &mut EM::State, - _manager: &mut EM, - ) -> Result { - unimplemented!() +impl NopFuzzer { + /// Creates a new [`NopFuzzer`] + #[must_use] + pub fn new() -> Self { + Self { + phantom: PhantomData, } } } + +impl Default for NopFuzzer { + fn default() -> Self { + Self::new() + } +} + +impl UsesState for NopFuzzer +where + S: State, +{ + type State = S; +} + +impl Fuzzer for NopFuzzer +where + E: UsesState, + EM: ProgressReporter + EventProcessor, + ST: StagesTuple, + Self::State: HasMetadata + HasExecutions + HasLastReportTime + HasCurrentStageId, +{ + fn fuzz_one( + &mut self, + _stages: &mut ST, + _executor: &mut E, + _state: &mut EM::State, + _manager: &mut EM, + ) -> Result { + unimplemented!("NopFuzzer cannot fuzz"); + } +} diff --git a/libafl/src/stages/mod.rs b/libafl/src/stages/mod.rs index 2d5fad48d0..6c7293d41a 100644 --- a/libafl/src/stages/mod.rs +++ b/libafl/src/stages/mod.rs @@ -699,7 +699,7 @@ impl ExecutionCountRestartHelper { } #[cfg(test)] -pub mod test { +mod test { use alloc::borrow::Cow; use core::marker::PhantomData; @@ -710,15 +710,17 @@ pub mod test { corpus::{Corpus, HasCurrentCorpusId, Testcase}, inputs::NopInput, stages::{RetryCountRestartHelper, Stage}, - state::{test::test_std_state, HasCorpus, State, UsesState}, + state::{HasCorpus, State, StdState, UsesState}, HasMetadata, }; + /// A stage that succeeds to resume #[derive(Debug)] pub struct ResumeSucceededStage { phantom: PhantomData, } + /// A progress state for testing #[derive(Serialize, Deserialize, Debug)] pub struct TestProgress { count: usize, @@ -790,6 +792,7 @@ pub mod test { } } + /// Test to test retries in stages #[test] fn test_tries_progress() -> Result<(), Error> { // # Safety @@ -808,7 +811,7 @@ pub mod test { } } - let mut state = test_std_state(); + let mut state = StdState::nop()?; let stage = StageWithOneTry; let corpus_id = state.corpus_mut().add(Testcase::new(NopInput {}))?; diff --git a/libafl/src/state/mod.rs b/libafl/src/state/mod.rs index bc364fdfb9..ce046b770d 100644 --- a/libafl/src/state/mod.rs +++ b/libafl/src/state/mod.rs @@ -31,12 +31,12 @@ use crate::monitors::ClientPerfMonitor; #[cfg(feature = "scalability_introspection")] use crate::monitors::ScalabilityMonitor; use crate::{ - corpus::{Corpus, CorpusId, HasCurrentCorpusId, HasTestcase, Testcase}, + corpus::{Corpus, CorpusId, HasCurrentCorpusId, HasTestcase, InMemoryCorpus, Testcase}, events::{Event, EventFirer, LogSeverity}, feedbacks::Feedback, fuzzer::{Evaluator, ExecuteInputResult}, generators::Generator, - inputs::{Input, UsesInput}, + inputs::{Input, NopInput, UsesInput}, stages::{HasCurrentStageId, HasNestedStageStatus, StageId}, Error, HasMetadata, HasNamedMetadata, }; @@ -1165,6 +1165,23 @@ where } } +impl StdState, StdRand, InMemoryCorpus> { + /// Create an empty [`StdState`] that has very minimal uses. + /// Potentially good for testing. + pub fn nop() -> Result, StdRand, InMemoryCorpus>, Error> + where + I: Input, + { + StdState::new( + StdRand::with_seed(0), + InMemoryCorpus::::new(), + InMemoryCorpus::new(), + &mut (), + &mut (), + ) + } +} + #[cfg(feature = "introspection")] impl HasClientPerfMonitor for StdState { fn introspection_monitor(&self) -> &ClientPerfMonitor { @@ -1337,22 +1354,11 @@ impl HasScalabilityMonitor for NopState { } #[cfg(test)] -pub mod test { - use libafl_bolts::rands::StdRand; +mod test { + use crate::{inputs::BytesInput, state::StdState}; - use super::StdState; - use crate::{corpus::InMemoryCorpus, inputs::Input}; - - #[must_use] - pub fn test_std_state() -> StdState, StdRand, InMemoryCorpus> - { - StdState::new( - StdRand::with_seed(0), - InMemoryCorpus::::new(), - InMemoryCorpus::new(), - &mut (), - &mut (), - ) - .expect("couldn't instantiate the test state") + #[test] + fn test_std_state() { + StdState::nop::().expect("couldn't instantiate the test state"); } } diff --git a/libafl_bolts/src/llmp.rs b/libafl_bolts/src/llmp.rs index e3514c83a2..4f366bde69 100644 --- a/libafl_bolts/src/llmp.rs +++ b/libafl_bolts/src/llmp.rs @@ -3778,7 +3778,7 @@ mod tests { #[test] #[serial] #[cfg_attr(miri, ignore)] - pub fn test_llmp_connection() { + fn test_llmp_connection() { #[allow(unused_variables)] let shmem_provider = StdShMemProvider::new().unwrap(); let mut broker = match LlmpConnection::on_port(shmem_provider.clone(), 1337).unwrap() { diff --git a/libafl_bolts/src/minibsod.rs b/libafl_bolts/src/minibsod.rs index 179e57d5b2..de550d3fe3 100644 --- a/libafl_bolts/src/minibsod.rs +++ b/libafl_bolts/src/minibsod.rs @@ -1163,7 +1163,7 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] - pub fn test_dump_registers() { + fn test_dump_registers() { let ucontext = ucontext().unwrap(); let mut writer = BufWriter::new(stdout()); dump_registers(&mut writer, &ucontext).unwrap(); @@ -1193,7 +1193,7 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] - pub fn test_dump_registers() { + fn test_dump_registers() { let (tx, rx) = mpsc::channel(); let (evt_tx, evt_rx) = mpsc::channel(); let t = std::thread::spawn(move || { diff --git a/libafl_bolts/src/tuples.rs b/libafl_bolts/src/tuples.rs index 3ada1d2e5f..e3b7a6aa05 100644 --- a/libafl_bolts/src/tuples.rs +++ b/libafl_bolts/src/tuples.rs @@ -824,22 +824,6 @@ macro_rules! tuple_for_each_mut { }; } -#[cfg(test)] -#[cfg(feature = "std")] -#[test] -#[allow(clippy::items_after_statements)] -pub fn test_macros() { - let mut t = tuple_list!(1, "a"); - - tuple_for_each!(f1, std::fmt::Display, t, |x| { - log::info!("{x}"); - }); - - tuple_for_each_mut!(f2, std::fmt::Display, t, |x| { - log::info!("{x}"); - }); -} - /* // Define trait and implement it for several primitive types. @@ -944,4 +928,20 @@ mod test { #[allow(clippy::no_effect_underscore_binding)] let _type_assert: tuple_list_type!(W, W, W) = mapped; } + + /// Function that tests the tuple macros + #[test] + #[cfg(feature = "std")] + #[allow(clippy::items_after_statements)] + fn test_macros() { + let mut t = tuple_list!(1, "a"); + + tuple_for_each!(f1, std::fmt::Display, t, |x| { + log::info!("{x}"); + }); + + tuple_for_each_mut!(f2, std::fmt::Display, t, |x| { + log::info!("{x}"); + }); + } }