Skip to content

Commit

Permalink
add AFL stage names for calibration, colorization, power and sync sta…
Browse files Browse the repository at this point in the history
…ges (AFLplusplus#2209)

* add AFL stage names for calibration, colorization, power and sync stages

* clippy

* add missing name field in sync stage

* use consts instead of hardcoding in functions.
change set_name to with_name for PowerMutationalStage
remove irrelevant fn transforming

* make AFL++ name default for all stages
  • Loading branch information
R9295 authored May 18, 2024
1 parent 22d8e92 commit baf0744
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
11 changes: 11 additions & 0 deletions libafl/src/stages/calibrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ impl UnstableEntriesMetadata {
}
}

/// Default name for `CalibrationStage`; derived from AFL++
pub const CALIBRATION_STAGE_NAME: &str = "calibration";
/// The calibration stage will measure the average exec time and the target's stability for this input.
#[derive(Clone, Debug)]
pub struct CalibrationStage<C, O, OT, S> {
map_observer_handle: Handle<C>,
map_name: Cow<'static, str>,
name: Cow<'static, str>,
stage_max: usize,
/// If we should track stability
track_stability: bool,
Expand Down Expand Up @@ -349,6 +352,7 @@ where
track_stability: true,
restart_helper: ExecutionCountRestartHelper::default(),
phantom: PhantomData,
name: Cow::Borrowed(CALIBRATION_STAGE_NAME),
}
}

Expand All @@ -365,6 +369,13 @@ where
track_stability: false,
restart_helper: ExecutionCountRestartHelper::default(),
phantom: PhantomData,
name: Cow::Borrowed(CALIBRATION_STAGE_NAME),
}
}
}

impl<C, O, OT, S> Named for CalibrationStage<C, O, OT, S> {
fn name(&self) -> &Cow<'static, str> {
&self.name
}
}
6 changes: 5 additions & 1 deletion libafl/src/stages/colorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ impl Ord for Earlier {
}
}

/// Default name for `ColorizationStage`; derived from ALF++
pub const COLORIZATION_STAGE_NAME: &str = "colorization";
/// The mutational stage using power schedules
#[derive(Clone, Debug)]
pub struct ColorizationStage<C, E, EM, O, Z> {
map_observer_handle: Handle<C>,
name: Cow<'static, str>,
#[allow(clippy::type_complexity)]
phantom: PhantomData<(E, EM, O, E, Z)>,
}
Expand All @@ -72,7 +75,7 @@ where
E: UsesState,
{
fn name(&self) -> &Cow<'static, str> {
self.map_observer_handle.name()
&self.name
}
}

Expand Down Expand Up @@ -308,6 +311,7 @@ where
pub fn new(map_observer: &C) -> Self {
Self {
map_observer_handle: map_observer.handle(),
name: Cow::Borrowed(COLORIZATION_STAGE_NAME),
phantom: PhantomData,
}
}
Expand Down
29 changes: 13 additions & 16 deletions libafl/src/stages/power.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! The power schedules. This stage should be invoked after the calibration stage.
use alloc::borrow::Cow;
use core::{fmt::Debug, marker::PhantomData};

use libafl_bolts::Named;

use crate::{
executors::{Executor, HasObservers},
fuzzer::Evaluator,
Expand All @@ -11,10 +14,12 @@ use crate::{
state::{HasCorpus, HasCurrentTestcase, HasExecutions, HasRand, UsesState},
Error, HasMetadata,
};

/// Default name for `PowerMutationalStage`; derived from AFL++
pub const POWER_MUTATIONAL_STAGE_NAME: &str = "power";
/// The mutational stage using power schedules
#[derive(Clone, Debug)]
pub struct PowerMutationalStage<E, F, EM, I, M, Z> {
name: Cow<'static, str>,
/// The mutators we use
mutator: M,
/// Helper for restarts
Expand All @@ -30,6 +35,12 @@ where
type State = E::State;
}

impl<E, F, EM, I, M, Z> Named for PowerMutationalStage<E, F, EM, I, M, Z> {
fn name(&self) -> &Cow<'static, str> {
&self.name
}
}

impl<E, F, EM, I, M, Z> MutationalStage<E, EM, I, M, Z> for PowerMutationalStage<E, F, EM, I, M, Z>
where
E: Executor<EM, Z> + HasObservers,
Expand Down Expand Up @@ -112,22 +123,8 @@ where
{
/// Creates a new [`PowerMutationalStage`]
pub fn new(mutator: M) -> Self {
Self::transforming(mutator)
}
}

impl<E, F, EM, I, M, Z> PowerMutationalStage<E, F, EM, I, M, Z>
where
E: Executor<EM, Z> + HasObservers,
EM: UsesState<State = E::State>,
F: TestcaseScore<E::State>,
M: Mutator<I, E::State>,
E::State: HasCorpus + HasMetadata + HasRand,
Z: Evaluator<E, EM, State = E::State>,
{
/// Creates a new transforming [`PowerMutationalStage`]
pub fn transforming(mutator: M) -> Self {
Self {
name: Cow::Borrowed(POWER_MUTATIONAL_STAGE_NAME),
mutator,
phantom: PhantomData,
restart_helper: ExecutionCountRestartHelper::default(),
Expand Down
11 changes: 8 additions & 3 deletions libafl/src/stages/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ impl SyncFromDiskMetadata {
}
}

/// Default name for `SyncFromDiskStage`; derived from AFL++
pub const SYNC_FROM_DISK_STAGE_NAME: &str = "sync";

/// A stage that loads testcases from disk to sync with other fuzzers such as AFL++
#[derive(Debug)]
pub struct SyncFromDiskStage<CB, E, EM, Z> {
name: Cow<'static, str>,
sync_dir: PathBuf,
load_callback: CB,
phantom: PhantomData<(E, EM, Z)>,
Expand All @@ -65,8 +69,7 @@ where
E: UsesState,
{
fn name(&self) -> &Cow<'static, str> {
static NAME: Cow<'static, str> = Cow::Borrowed("SyncFromDiskStage");
&NAME
&self.name
}
}

Expand Down Expand Up @@ -138,9 +141,10 @@ where
#[must_use]
pub fn new(sync_dir: PathBuf, load_callback: CB) -> Self {
Self {
name: Cow::Borrowed(SYNC_FROM_DISK_STAGE_NAME),
phantom: PhantomData,
sync_dir,
load_callback,
phantom: PhantomData,
}
}

Expand Down Expand Up @@ -211,6 +215,7 @@ where
Input::from_file(p)
}
Self {
name: Cow::Borrowed(SYNC_FROM_DISK_STAGE_NAME),
sync_dir,
load_callback: load_callback::<_, _>,
phantom: PhantomData,
Expand Down

0 comments on commit baf0744

Please sign in to comment.