Skip to content

Commit

Permalink
Use the macros for havoc_mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
riesentoaster committed Dec 21, 2024
1 parent 0faa01b commit 7b16d45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 92 deletions.
104 changes: 16 additions & 88 deletions libafl/src/mutators/havoc_mutations.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! [`crate::mutators::Mutator`] collection equivalent to AFL++'s havoc mutations
use libafl_bolts::tuples::{Map, Merge};
use libafl_bolts::{
map_tuple_list_type, merge_tuple_list_type,
tuples::{Map, Merge},
};
use tuple_list::{tuple_list, tuple_list_type};

use super::{MappingMutator, ToMappingMutator};
use crate::mutators::{
mapping::{OptionalMutator, ToOptionalMutator},
mapping::{ToMappingMutator, ToOptionalMutator},
mutations::{
BitFlipMutator, ByteAddMutator, ByteDecMutator, ByteFlipMutator, ByteIncMutator,
ByteInterestingMutator, ByteNegMutator, ByteRandMutator, BytesCopyMutator,
Expand Down Expand Up @@ -56,96 +58,22 @@ pub type MappedHavocCrossoverType<F, O> = tuple_list_type!(
);

/// Tuple type of the mutations that compose the Havoc mutator
pub type HavocMutationsType = tuple_list_type!(
BitFlipMutator,
ByteFlipMutator,
ByteIncMutator,
ByteDecMutator,
ByteNegMutator,
ByteRandMutator,
ByteAddMutator,
WordAddMutator,
DwordAddMutator,
QwordAddMutator,
ByteInterestingMutator,
WordInterestingMutator,
DwordInterestingMutator,
BytesDeleteMutator,
BytesDeleteMutator,
BytesDeleteMutator,
BytesDeleteMutator,
BytesExpandMutator,
BytesInsertMutator,
BytesRandInsertMutator,
BytesSetMutator,
BytesRandSetMutator,
BytesCopyMutator,
BytesInsertCopyMutator,
BytesSwapMutator,
CrossoverInsertMutator,
CrossoverReplaceMutator,
);
pub type HavocMutationsType =
merge_tuple_list_type!(HavocMutationsNoCrossoverType, HavocCrossoverType);

/// Tuple type of the mutations that compose the Havoc mutator for mapped input types
pub type MappedHavocMutationsType<F1, F2, O> = tuple_list_type!(
MappingMutator<BitFlipMutator, F1>,
MappingMutator<ByteFlipMutator, F1>,
MappingMutator<ByteIncMutator, F1>,
MappingMutator<ByteDecMutator, F1>,
MappingMutator<ByteNegMutator, F1>,
MappingMutator<ByteRandMutator, F1>,
MappingMutator<ByteAddMutator, F1>,
MappingMutator<WordAddMutator, F1>,
MappingMutator<DwordAddMutator, F1>,
MappingMutator<QwordAddMutator, F1>,
MappingMutator<ByteInterestingMutator, F1>,
MappingMutator<WordInterestingMutator, F1>,
MappingMutator<DwordInterestingMutator, F1>,
MappingMutator<BytesDeleteMutator, F1>,
MappingMutator<BytesDeleteMutator, F1>,
MappingMutator<BytesDeleteMutator, F1>,
MappingMutator<BytesDeleteMutator, F1>,
MappingMutator<BytesExpandMutator, F1>,
MappingMutator<BytesInsertMutator, F1>,
MappingMutator<BytesRandInsertMutator, F1>,
MappingMutator<BytesSetMutator, F1>,
MappingMutator<BytesRandSetMutator, F1>,
MappingMutator<BytesCopyMutator, F1>,
MappingMutator<BytesInsertCopyMutator, F1>,
MappingMutator<BytesSwapMutator, F1>,
MappingMutator<MappedCrossoverInsertMutator<F2, O>, F1>,
MappingMutator<MappedCrossoverReplaceMutator<F2, O>, F1>,
pub type MappedHavocMutationsType<F1, F2, O> = map_tuple_list_type!(
merge_tuple_list_type!(HavocMutationsNoCrossoverType, MappedHavocCrossoverType<F2,O>),
ToMappingMutator<F1>
);

/// Tuple type of the mutations that compose the Havoc mutator for mapped input types, for optional byte array input parts
pub type OptionMappedHavocMutationsType<F1, F2, O> = tuple_list_type!(
MappingMutator<OptionalMutator<BitFlipMutator>, F1>,
MappingMutator<OptionalMutator<ByteFlipMutator>, F1>,
MappingMutator<OptionalMutator<ByteIncMutator>, F1>,
MappingMutator<OptionalMutator<ByteDecMutator>, F1>,
MappingMutator<OptionalMutator<ByteNegMutator>, F1>,
MappingMutator<OptionalMutator<ByteRandMutator>, F1>,
MappingMutator<OptionalMutator<ByteAddMutator>, F1>,
MappingMutator<OptionalMutator<WordAddMutator>, F1>,
MappingMutator<OptionalMutator<DwordAddMutator>, F1>,
MappingMutator<OptionalMutator<QwordAddMutator>, F1>,
MappingMutator<OptionalMutator<ByteInterestingMutator>, F1>,
MappingMutator<OptionalMutator<WordInterestingMutator>, F1>,
MappingMutator<OptionalMutator<DwordInterestingMutator>, F1>,
MappingMutator<OptionalMutator<BytesDeleteMutator>, F1>,
MappingMutator<OptionalMutator<BytesDeleteMutator>, F1>,
MappingMutator<OptionalMutator<BytesDeleteMutator>, F1>,
MappingMutator<OptionalMutator<BytesDeleteMutator>, F1>,
MappingMutator<OptionalMutator<BytesExpandMutator>, F1>,
MappingMutator<OptionalMutator<BytesInsertMutator>, F1>,
MappingMutator<OptionalMutator<BytesRandInsertMutator>, F1>,
MappingMutator<OptionalMutator<BytesSetMutator>, F1>,
MappingMutator<OptionalMutator<BytesRandSetMutator>, F1>,
MappingMutator<OptionalMutator<BytesCopyMutator>, F1>,
MappingMutator<OptionalMutator<BytesInsertCopyMutator>, F1>,
MappingMutator<OptionalMutator<BytesSwapMutator>, F1>,
MappingMutator<OptionalMutator<MappedCrossoverInsertMutator<F2, O>>, F1>,
MappingMutator<OptionalMutator<MappedCrossoverReplaceMutator<F2, O>>, F1>,
pub type OptionMappedHavocMutationsType<F1, F2, O> = map_tuple_list_type!(
map_tuple_list_type!(
merge_tuple_list_type!(HavocMutationsNoCrossoverType, MappedHavocCrossoverType<F2,O>),
ToOptionalMutator
),
ToMappingMutator<F1>
);

/// Get the mutations that compose the Havoc mutator (only applied to single inputs)
Expand Down
10 changes: 6 additions & 4 deletions libafl_bolts/src/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,8 @@ impl<Head, Tail> PlusOne for (Head, Tail) where

#[cfg(test)]
mod test {
use core::marker::PhantomData;

use tuple_list::{tuple_list, tuple_list_type};

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -977,9 +979,9 @@ mod test {
#[test]
fn test_mapper() {
struct W<T>(T);
struct MyMapper;
struct MyMapper<P>(PhantomData<P>);

impl<T> MappingFunctor<T> for MyMapper {
impl<T, P> MappingFunctor<T> for MyMapper<P> {
type Output = W<T>;

fn apply(&mut self, from: T) -> Self::Output {
Expand All @@ -992,9 +994,9 @@ mod test {
struct C;

type OrigType = tuple_list_type!(A, B, C);
type MappedType = map_tuple_list_type!(OrigType, MyMapper);
type MappedType = map_tuple_list_type!(OrigType, MyMapper<usize>);
let orig: OrigType = tuple_list!(A, B, C);
let _mapped: MappedType = orig.map(MyMapper);
let _mapped: MappedType = orig.map(MyMapper(PhantomData::<usize>));
}

#[test]
Expand Down

0 comments on commit 7b16d45

Please sign in to comment.