Skip to content

Commit

Permalink
Introdue SimpleHash separate from MapObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
riesentoaster committed Jan 15, 2025
1 parent ba09cb0 commit bdbf811
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 61 deletions.
17 changes: 11 additions & 6 deletions libafl/src/observers/map/const_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libafl_bolts::{ownedref::OwnedMutSizedSlice, HasLen, Named};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{
observers::{map::MapObserver, ConstLenMapObserver, Observer},
observers::{map::MapObserver, ConstLenMapObserver, Observer, SimpleHash},
Error,
};

Expand Down Expand Up @@ -72,6 +72,16 @@ impl<T, const N: usize> AsMut<Self> for ConstMapObserver<'_, T, N> {
}
}

impl<T, const N: usize> SimpleHash for ConstMapObserver<'_, T, N>
where
T: Hash,
{
#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}
}

impl<T, const N: usize> MapObserver for ConstMapObserver<'_, T, N>
where
T: PartialEq + Copy + Hash + Serialize + DeserializeOwned + Debug + 'static,
Expand Down Expand Up @@ -111,11 +121,6 @@ where
self.len()
}

#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}

/// Reset the map
#[inline]
fn reset_map(&mut self) -> Result<(), Error> {
Expand Down
32 changes: 22 additions & 10 deletions libafl/src/observers/map/hitcount_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use serde::{Deserialize, Serialize};
use crate::{
executors::ExitKind,
observers::{
map::MapObserver, ConstLenMapObserver, DifferentialObserver, Observer, VarLenMapObserver,
map::MapObserver, ConstLenMapObserver, DifferentialObserver, Observer, SimpleHash,
VarLenMapObserver,
},
Error,
};
Expand Down Expand Up @@ -184,6 +185,16 @@ impl<M> AsMut<Self> for HitcountsMapObserver<M> {
}
}

impl<M> SimpleHash for HitcountsMapObserver<M>
where
M: SimpleHash,
{
#[inline]
fn hash_simple(&self) -> u64 {
self.base.hash_simple()
}
}

impl<M> MapObserver for HitcountsMapObserver<M>
where
M: MapObserver<Entry = u8>,
Expand Down Expand Up @@ -221,11 +232,6 @@ where
self.base.reset_map()
}

#[inline]
fn hash_simple(&self) -> u64 {
self.base.hash_simple()
}

fn to_vec(&self) -> Vec<u8> {
self.base.to_vec()
}
Expand Down Expand Up @@ -406,6 +412,16 @@ impl<M> AsMut<Self> for HitcountsIterableMapObserver<M> {
}
}

impl<M> SimpleHash for HitcountsIterableMapObserver<M>
where
M: SimpleHash,
{
#[inline]
fn hash_simple(&self) -> u64 {
self.base.hash_simple()
}
}

impl<M> MapObserver for HitcountsIterableMapObserver<M>
where
M: MapObserver<Entry = u8>,
Expand Down Expand Up @@ -443,10 +459,6 @@ where
self.base.reset_map()
}

#[inline]
fn hash_simple(&self) -> u64 {
self.base.hash_simple()
}
fn to_vec(&self) -> Vec<u8> {
self.base.to_vec()
}
Expand Down
24 changes: 16 additions & 8 deletions libafl/src/observers/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ pub mod macros {
}
}

/// Can calculate a simple hash function, usually used in conjunction with a [`MapObserver`]
pub trait SimpleHash {
/// Compute the hash of the map without needing to provide a hasher
fn hash_simple(&self) -> u64;
}

/// A [`MapObserver`] observes the static map, as oftentimes used for AFL-like coverage information
///
/// When referring to this type in a constraint (e.g. `O: MapObserver`), ensure that you only refer
Expand Down Expand Up @@ -369,9 +375,6 @@ pub trait MapObserver:
/// Count the set bytes in the map
fn count_bytes(&self) -> u64;

/// Compute the hash of the map without needing to provide a hasher
fn hash_simple(&self) -> u64;

/// Get the initial value for `reset()`
fn initial(&self) -> Self::Entry;

Expand Down Expand Up @@ -492,6 +495,16 @@ impl<T, const DIFFERENTIAL: bool> AsMut<Self> for StdMapObserver<'_, T, DIFFEREN
}
}

impl<T, const DIFFERENTIAL: bool> SimpleHash for StdMapObserver<'_, T, DIFFERENTIAL>
where
T: Hash,
{
#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}
}

impl<T, const DIFFERENTIAL: bool> MapObserver for StdMapObserver<'_, T, DIFFERENTIAL>
where
T: PartialEq + Copy + Hash + Serialize + DeserializeOwned + Debug,
Expand Down Expand Up @@ -526,11 +539,6 @@ where
self.as_slice().len()
}

#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}

#[inline]
fn initial(&self) -> T {
self.initial
Expand Down
17 changes: 11 additions & 6 deletions libafl/src/observers/map/multi_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use meminterval::IntervalTree;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{
observers::{map::MapObserver, DifferentialObserver, Observer},
observers::{map::MapObserver, DifferentialObserver, Observer, SimpleHash},
Error,
};

Expand Down Expand Up @@ -84,6 +84,16 @@ impl<T, const DIFFERENTIAL: bool> AsMut<Self> for MultiMapObserver<'_, T, DIFFER
}
}

impl<T, const DIFFERENTIAL: bool> SimpleHash for MultiMapObserver<'_, T, DIFFERENTIAL>
where
T: Hash,
{
#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}
}

impl<T, const DIFFERENTIAL: bool> MapObserver for MultiMapObserver<'_, T, DIFFERENTIAL>
where
T: PartialEq + Copy + Hash + Serialize + DeserializeOwned + Debug,
Expand Down Expand Up @@ -124,11 +134,6 @@ where
res
}

#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}

fn reset_map(&mut self) -> Result<(), Error> {
let initial = self.initial();
for map in &mut self.maps {
Expand Down
17 changes: 11 additions & 6 deletions libafl/src/observers/map/owned_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use libafl_bolts::{AsSlice, AsSliceMut, HasLen, Named};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{
observers::{map::MapObserver, Observer},
observers::{map::MapObserver, Observer, SimpleHash},
Error,
};

Expand Down Expand Up @@ -70,6 +70,16 @@ impl<T> AsMut<Self> for OwnedMapObserver<T> {
}
}

impl<T> SimpleHash for OwnedMapObserver<T>
where
T: Hash,
{
#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}
}

impl<T> MapObserver for OwnedMapObserver<T>
where
T: PartialEq + Copy + Hash + Serialize + DeserializeOwned + Debug,
Expand Down Expand Up @@ -105,11 +115,6 @@ where
self.as_slice().len()
}

#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}

#[inline]
fn initial(&self) -> T {
self.initial
Expand Down
17 changes: 11 additions & 6 deletions libafl/src/observers/map/variable_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use libafl_bolts::{
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{
observers::{map::MapObserver, Observer, VarLenMapObserver},
observers::{map::MapObserver, Observer, SimpleHash, VarLenMapObserver},
Error,
};

Expand Down Expand Up @@ -75,6 +75,16 @@ impl<T> AsMut<Self> for VariableMapObserver<'_, T> {
}
}

impl<T> SimpleHash for VariableMapObserver<'_, T>
where
T: Hash,
{
#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}
}

impl<T> MapObserver for VariableMapObserver<'_, T>
where
T: PartialEq + Copy + Hash + Serialize + DeserializeOwned + Debug,
Expand Down Expand Up @@ -113,11 +123,6 @@ where
res
}

#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}

/// Reset the map
#[inline]
fn reset_map(&mut self) -> Result<(), Error> {
Expand Down
20 changes: 13 additions & 7 deletions libafl/src/observers/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ahash::RandomState;
use libafl_bolts::{ownedref::OwnedRef, AsIter, AsIterMut, AsSlice, AsSliceMut, HasLen, Named};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use super::Observer;
use super::{Observer, SimpleHash};
use crate::{
observers::{MapObserver, ObserverWithHashField},
Error,
Expand Down Expand Up @@ -289,6 +289,18 @@ impl<T> AsMut<Self> for RefCellValueObserver<'_, T> {
self
}
}

impl<T> SimpleHash for RefCellValueObserver<'_, T>
where
T: Hash,
{
/// Panics if the contained value is already mutably borrowed (calls
/// [`RefCell::borrow`]).
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}
}

impl<T, A> MapObserver for RefCellValueObserver<'_, A>
where
T: PartialEq + Copy + Hash + Default + DeserializeOwned + Serialize + Debug,
Expand All @@ -308,12 +320,6 @@ where
self.get_ref_mut()[idx] = val;
}

/// Panics if the contained value is already mutably borrowed (calls
/// [`RefCell::borrow`]).
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
}

/// Panics if the contained value is already mutably borrowed (calls
/// [`RefCell::borrow`]).
fn usable_count(&self) -> usize {
Expand Down
4 changes: 2 additions & 2 deletions libafl/src/schedulers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use tuneable::*;

use crate::{
corpus::{Corpus, CorpusId, HasTestcase, SchedulerTestcaseMetadata, Testcase},
observers::MapObserver,
observers::SimpleHash,
random_corpus_id,
state::{HasCorpus, HasRand},
Error, HasMetadata,
Expand Down Expand Up @@ -109,7 +109,7 @@ where
CS: AflScheduler,
CS::MapObserverRef: AsRef<O>,
S: HasMetadata,
O: MapObserver,
O: SimpleHash,
OT: MatchName,
{
let observer = observers
Expand Down
4 changes: 2 additions & 2 deletions libafl/src/schedulers/powersched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

use crate::{
corpus::{Corpus, CorpusId, HasTestcase, Testcase},
observers::MapObserver,
observers::{MapObserver, SimpleHash},
schedulers::{
on_add_metadata_default, on_evaluation_metadata_default, on_next_metadata_default,
AflScheduler, HasQueueCycles, RemovableScheduler, Scheduler,
Expand Down Expand Up @@ -328,7 +328,7 @@ impl<C, O> HasQueueCycles for PowerQueueScheduler<C, O> {
impl<C, I, O, S> Scheduler<I, S> for PowerQueueScheduler<C, O>
where
S: HasCorpus + HasMetadata + HasTestcase + State,
O: MapObserver,
O: SimpleHash,
C: AsRef<O>,
{
/// Called when a [`Testcase`] is added to the corpus
Expand Down
4 changes: 2 additions & 2 deletions libafl/src/schedulers/weighted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};
use super::powersched::PowerSchedule;
use crate::{
corpus::{Corpus, CorpusId, HasTestcase, Testcase},
observers::MapObserver,
observers::SimpleHash,
random_corpus_id,
schedulers::{
on_add_metadata_default, on_evaluation_metadata_default, on_next_metadata_default,
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<C, F, O, S> Scheduler<<S::Corpus as Corpus>::Input, S> for WeightedSchedule
where
C: AsRef<O> + Named,
F: TestcaseScore<S>,
O: MapObserver,
O: SimpleHash,
S: HasCorpus + HasMetadata + HasRand + HasTestcase,
{
/// Called when a [`Testcase`] is added to the corpus
Expand Down
6 changes: 3 additions & 3 deletions libafl/src/stages/colorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
inputs::{HasMutatorBytes, UsesInput},
mutators::mutations::buffer_copy,
nonzero,
observers::{MapObserver, ObserversTuple},
observers::{ObserversTuple, SimpleHash},
stages::{RetryCountRestartHelper, Stage},
state::{HasCorpus, HasCurrentTestcase, HasRand},
Error, HasMetadata, HasNamedMetadata,
Expand Down Expand Up @@ -86,7 +86,7 @@ where
+ UsesInput<Input = <S::Corpus as Corpus>::Input>,
E::Observers: ObserversTuple<<S::Corpus as Corpus>::Input, S>,
<S::Corpus as Corpus>::Input: HasMutatorBytes + Clone,
O: MapObserver,
O: SimpleHash,
C: AsRef<O> + Named,
{
#[inline]
Expand Down Expand Up @@ -157,7 +157,7 @@ libafl_bolts::impl_serdeany!(TaintMetadata);
impl<C, E, EM, O, S, Z> ColorizationStage<C, E, EM, O, S, Z>
where
EM: EventFirer<State = S>,
O: MapObserver,
O: SimpleHash,
C: AsRef<O> + Named,
E: HasObservers + Executor<EM, <S::Corpus as Corpus>::Input, S, Z>,
E::Observers: ObserversTuple<<S::Corpus as Corpus>::Input, S>,
Expand Down
Loading

0 comments on commit bdbf811

Please sign in to comment.