Add StdState::nop() for simple state creation, make CI happy again on latest nightly. (#2521)

* Make CI happy again on latest nightly

* Fix build

* Unneeded comment

* fix tests

* clippy
This commit is contained in:
Dominik Maier 2024-09-12 00:33:55 +02:00 committed by GitHub
parent 25624d8eec
commit 8ccff4b77f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 99 additions and 102 deletions

View File

@ -258,7 +258,7 @@ impl Context {
max_len: usize, max_len: usize,
nt: NTermId, nt: NTermId,
p_include_short_rules: usize, p_include_short_rules: usize,
) -> impl Iterator<Item = &RuleId> + 'a { ) -> impl Iterator<Item = &'a RuleId> + 'a {
self.nts_to_rules[&nt] self.nts_to_rules[&nt]
.iter() .iter()
.take_while(move |r| self.rules_to_min_size[*r] <= max_len) .take_while(move |r| self.rules_to_min_size[*r] <= max_len)

View File

@ -620,7 +620,7 @@ mod tests {
command::{CommandExecutor, InputLocation}, command::{CommandExecutor, InputLocation},
Executor, Executor,
}, },
fuzzer::test::NopFuzzer, fuzzer::NopFuzzer,
inputs::BytesInput, inputs::BytesInput,
monitors::SimpleMonitor, monitors::SimpleMonitor,
state::NopState, state::NopState,

View File

@ -394,7 +394,7 @@ mod tests {
hooks::inprocess_fork::InChildProcessHooks, hooks::inprocess_fork::InChildProcessHooks,
inprocess_fork::GenericInProcessForkExecutor, inprocess_fork::GenericInProcessForkExecutor,
}, },
fuzzer::test::NopFuzzer, fuzzer::NopFuzzer,
state::NopState, state::NopState,
}; };

View File

@ -165,7 +165,7 @@ pub fn common_signals() -> Vec<Signal> {
} }
#[cfg(test)] #[cfg(test)]
pub mod test { mod test {
use core::marker::PhantomData; use core::marker::PhantomData;
use libafl_bolts::{AsSlice, Error}; use libafl_bolts::{AsSlice, Error};
@ -173,7 +173,7 @@ pub mod test {
use crate::{ use crate::{
events::NopEventManager, events::NopEventManager,
executors::{Executor, ExitKind}, executors::{Executor, ExitKind},
fuzzer::test::NopFuzzer, fuzzer::NopFuzzer,
inputs::{BytesInput, HasTargetBytes}, inputs::{BytesInput, HasTargetBytes},
state::{HasExecutions, NopState, State, UsesState}, state::{HasExecutions, NopState, State, UsesState},
}; };
@ -186,6 +186,7 @@ pub mod test {
} }
impl<S> NopExecutor<S> { impl<S> NopExecutor<S> {
/// Creates a new [`NopExecutor`]
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {

View File

@ -1,7 +1,7 @@
//! The `Fuzzer` is the main struct for a fuzz campaign. //! The `Fuzzer` is the main struct for a fuzz campaign.
use alloc::{string::ToString, vec::Vec}; 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 libafl_bolts::current_time;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
@ -19,7 +19,7 @@ use crate::{
start_timer, start_timer,
state::{ state::{
HasCorpus, HasCurrentTestcase, HasExecutions, HasLastFoundTime, HasLastReportTime, HasCorpus, HasCurrentTestcase, HasExecutions, HasLastFoundTime, HasLastReportTime,
HasSolutions, Stoppable, UsesState, HasSolutions, State, Stoppable, UsesState,
}, },
Error, HasMetadata, Error, HasMetadata,
}; };
@ -931,54 +931,42 @@ where
} }
} }
#[cfg(test)] /// A [`NopFuzzer`] that does nothing
pub mod test { #[derive(Clone, Debug)]
use core::marker::PhantomData; pub struct NopFuzzer<S> {
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<S> {
phantom: PhantomData<S>, phantom: PhantomData<S>,
} }
impl<S> NopFuzzer<S> { impl<S> NopFuzzer<S> {
/// Creates a new [`NopFuzzer`]
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
phantom: PhantomData, phantom: PhantomData,
} }
} }
} }
impl<S> Default for NopFuzzer<S> { impl<S> Default for NopFuzzer<S> {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl<S> UsesState for NopFuzzer<S> impl<S> UsesState for NopFuzzer<S>
where where
S: State, S: State,
{ {
type State = S; type State = S;
} }
impl<ST, E, EM> Fuzzer<E, EM, ST> for NopFuzzer<E::State> impl<ST, E, EM> Fuzzer<E, EM, ST> for NopFuzzer<E::State>
where where
E: UsesState, E: UsesState,
EM: ProgressReporter<State = Self::State> + EventProcessor<E, Self>, EM: ProgressReporter<State = Self::State> + EventProcessor<E, Self>,
ST: StagesTuple<E, EM, Self::State, Self>, ST: StagesTuple<E, EM, Self::State, Self>,
Self::State: HasMetadata + HasExecutions + HasLastReportTime + HasCurrentStageId, Self::State: HasMetadata + HasExecutions + HasLastReportTime + HasCurrentStageId,
{ {
fn fuzz_one( fn fuzz_one(
&mut self, &mut self,
_stages: &mut ST, _stages: &mut ST,
@ -986,7 +974,6 @@ pub mod test {
_state: &mut EM::State, _state: &mut EM::State,
_manager: &mut EM, _manager: &mut EM,
) -> Result<CorpusId, Error> { ) -> Result<CorpusId, Error> {
unimplemented!() unimplemented!("NopFuzzer cannot fuzz");
}
} }
} }

View File

@ -699,7 +699,7 @@ impl ExecutionCountRestartHelper {
} }
#[cfg(test)] #[cfg(test)]
pub mod test { mod test {
use alloc::borrow::Cow; use alloc::borrow::Cow;
use core::marker::PhantomData; use core::marker::PhantomData;
@ -710,15 +710,17 @@ pub mod test {
corpus::{Corpus, HasCurrentCorpusId, Testcase}, corpus::{Corpus, HasCurrentCorpusId, Testcase},
inputs::NopInput, inputs::NopInput,
stages::{RetryCountRestartHelper, Stage}, stages::{RetryCountRestartHelper, Stage},
state::{test::test_std_state, HasCorpus, State, UsesState}, state::{HasCorpus, State, StdState, UsesState},
HasMetadata, HasMetadata,
}; };
/// A stage that succeeds to resume
#[derive(Debug)] #[derive(Debug)]
pub struct ResumeSucceededStage<S> { pub struct ResumeSucceededStage<S> {
phantom: PhantomData<S>, phantom: PhantomData<S>,
} }
/// A progress state for testing
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct TestProgress { pub struct TestProgress {
count: usize, count: usize,
@ -790,6 +792,7 @@ pub mod test {
} }
} }
/// Test to test retries in stages
#[test] #[test]
fn test_tries_progress() -> Result<(), Error> { fn test_tries_progress() -> Result<(), Error> {
// # Safety // # Safety
@ -808,7 +811,7 @@ pub mod test {
} }
} }
let mut state = test_std_state(); let mut state = StdState::nop()?;
let stage = StageWithOneTry; let stage = StageWithOneTry;
let corpus_id = state.corpus_mut().add(Testcase::new(NopInput {}))?; let corpus_id = state.corpus_mut().add(Testcase::new(NopInput {}))?;

View File

@ -31,12 +31,12 @@ use crate::monitors::ClientPerfMonitor;
#[cfg(feature = "scalability_introspection")] #[cfg(feature = "scalability_introspection")]
use crate::monitors::ScalabilityMonitor; use crate::monitors::ScalabilityMonitor;
use crate::{ use crate::{
corpus::{Corpus, CorpusId, HasCurrentCorpusId, HasTestcase, Testcase}, corpus::{Corpus, CorpusId, HasCurrentCorpusId, HasTestcase, InMemoryCorpus, Testcase},
events::{Event, EventFirer, LogSeverity}, events::{Event, EventFirer, LogSeverity},
feedbacks::Feedback, feedbacks::Feedback,
fuzzer::{Evaluator, ExecuteInputResult}, fuzzer::{Evaluator, ExecuteInputResult},
generators::Generator, generators::Generator,
inputs::{Input, UsesInput}, inputs::{Input, NopInput, UsesInput},
stages::{HasCurrentStageId, HasNestedStageStatus, StageId}, stages::{HasCurrentStageId, HasNestedStageStatus, StageId},
Error, HasMetadata, HasNamedMetadata, Error, HasMetadata, HasNamedMetadata,
}; };
@ -1165,6 +1165,23 @@ where
} }
} }
impl StdState<NopInput, InMemoryCorpus<NopInput>, StdRand, InMemoryCorpus<NopInput>> {
/// Create an empty [`StdState`] that has very minimal uses.
/// Potentially good for testing.
pub fn nop<I>() -> Result<StdState<I, InMemoryCorpus<I>, StdRand, InMemoryCorpus<I>>, Error>
where
I: Input,
{
StdState::new(
StdRand::with_seed(0),
InMemoryCorpus::<I>::new(),
InMemoryCorpus::new(),
&mut (),
&mut (),
)
}
}
#[cfg(feature = "introspection")] #[cfg(feature = "introspection")]
impl<I, C, R, SC> HasClientPerfMonitor for StdState<I, C, R, SC> { impl<I, C, R, SC> HasClientPerfMonitor for StdState<I, C, R, SC> {
fn introspection_monitor(&self) -> &ClientPerfMonitor { fn introspection_monitor(&self) -> &ClientPerfMonitor {
@ -1337,22 +1354,11 @@ impl<I> HasScalabilityMonitor for NopState<I> {
} }
#[cfg(test)] #[cfg(test)]
pub mod test { mod test {
use libafl_bolts::rands::StdRand; use crate::{inputs::BytesInput, state::StdState};
use super::StdState; #[test]
use crate::{corpus::InMemoryCorpus, inputs::Input}; fn test_std_state() {
StdState::nop::<BytesInput>().expect("couldn't instantiate the test state");
#[must_use]
pub fn test_std_state<I: Input>() -> StdState<I, InMemoryCorpus<I>, StdRand, InMemoryCorpus<I>>
{
StdState::new(
StdRand::with_seed(0),
InMemoryCorpus::<I>::new(),
InMemoryCorpus::new(),
&mut (),
&mut (),
)
.expect("couldn't instantiate the test state")
} }
} }

View File

@ -3778,7 +3778,7 @@ mod tests {
#[test] #[test]
#[serial] #[serial]
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore)]
pub fn test_llmp_connection() { fn test_llmp_connection() {
#[allow(unused_variables)] #[allow(unused_variables)]
let shmem_provider = StdShMemProvider::new().unwrap(); let shmem_provider = StdShMemProvider::new().unwrap();
let mut broker = match LlmpConnection::on_port(shmem_provider.clone(), 1337).unwrap() { let mut broker = match LlmpConnection::on_port(shmem_provider.clone(), 1337).unwrap() {

View File

@ -1163,7 +1163,7 @@ mod tests {
#[test] #[test]
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore)]
pub fn test_dump_registers() { fn test_dump_registers() {
let ucontext = ucontext().unwrap(); let ucontext = ucontext().unwrap();
let mut writer = BufWriter::new(stdout()); let mut writer = BufWriter::new(stdout());
dump_registers(&mut writer, &ucontext).unwrap(); dump_registers(&mut writer, &ucontext).unwrap();
@ -1193,7 +1193,7 @@ mod tests {
#[test] #[test]
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore)]
pub fn test_dump_registers() { fn test_dump_registers() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let (evt_tx, evt_rx) = mpsc::channel(); let (evt_tx, evt_rx) = mpsc::channel();
let t = std::thread::spawn(move || { let t = std::thread::spawn(move || {

View File

@ -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. // Define trait and implement it for several primitive types.
@ -944,4 +928,20 @@ mod test {
#[allow(clippy::no_effect_underscore_binding)] #[allow(clippy::no_effect_underscore_binding)]
let _type_assert: tuple_list_type!(W<A>, W<B>, W<C>) = mapped; let _type_assert: tuple_list_type!(W<A>, W<B>, W<C>) = 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}");
});
}
} }