Skip to content

Commit

Permalink
add the pend_fav metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
toseven authored and toseven committed Jul 8, 2023
1 parent 297e848 commit f79240d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
11 changes: 10 additions & 1 deletion libafl/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ where
// default to 0 here to avoid crashes on clock skew
if cur.checked_sub(last_report_time).unwrap_or_default() > monitor_timeout {
let executions = *state.executions();

let pending_size = *state.pending();
let pend_favored_size = *state.pend_favored();
let own_finds_size = *state.own_finds();
let imported_size = *state.imported();
// Default no introspection implmentation
Expand All @@ -494,6 +494,15 @@ where
},
)?;

self.fire(
state,
Event::UpdateUserStats {
name: "pend_fav".to_string(),
value: UserStats::Number(pend_favored_size as u64),
phantom: PhantomData,
},
)?;

self.fire(
state,
Event::UpdateUserStats {
Expand Down
6 changes: 3 additions & 3 deletions libafl/src/schedulers/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
minimizer::{IsFavoredMetadata, MinimizerScheduler, DEFAULT_SKIP_NON_FAVORED_PROB},
LenTimeMulTestcaseScore, Scheduler,
},
state::{HasCorpus, HasMetadata, HasRand, UsesState},
state::{HasAFLStats, HasCorpus, HasMetadata, HasRand, UsesState},
Error,
};

Expand Down Expand Up @@ -123,7 +123,7 @@ where
impl<'a, CS> Scheduler for CoverageAccountingScheduler<'a, CS>
where
CS: Scheduler,
CS::State: HasCorpus + HasMetadata + HasRand + Debug,
CS::State: HasCorpus + HasMetadata + HasRand + Debug + HasAFLStats,
<CS::State as UsesInput>::Input: HasLen,
{
fn on_add(&mut self, state: &mut Self::State, idx: CorpusId) -> Result<(), Error> {
Expand Down Expand Up @@ -185,7 +185,7 @@ where
impl<'a, CS> CoverageAccountingScheduler<'a, CS>
where
CS: Scheduler,
CS::State: HasCorpus + HasMetadata + HasRand + Debug,
CS::State: HasCorpus + HasMetadata + HasRand + Debug + HasAFLStats,
<CS::State as UsesInput>::Input: HasLen,
{
/// Update the `Corpus` score
Expand Down
12 changes: 8 additions & 4 deletions libafl/src/schedulers/minimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
inputs::UsesInput,
observers::ObserversTuple,
schedulers::{LenTimeMulTestcaseScore, RemovableScheduler, Scheduler, TestcaseScore},
state::{HasCorpus, HasMetadata, HasRand, UsesState},
state::{HasAFLStats, HasCorpus, HasMetadata, HasRand, UsesState},
Error,
};

Expand Down Expand Up @@ -80,7 +80,7 @@ where
CS: RemovableScheduler,
F: TestcaseScore<CS::State>,
M: AsSlice<Entry = usize> + SerdeAny + HasRefCnt,
CS::State: HasCorpus + HasMetadata + HasRand,
CS::State: HasCorpus + HasMetadata + HasRand + HasAFLStats,
{
/// Replaces the testcase at the given idx
fn on_replace(
Expand Down Expand Up @@ -187,7 +187,7 @@ where
CS: Scheduler,
F: TestcaseScore<CS::State>,
M: AsSlice<Entry = usize> + SerdeAny + HasRefCnt,
CS::State: HasCorpus + HasMetadata + HasRand,
CS::State: HasCorpus + HasMetadata + HasRand + HasAFLStats,
{
/// Add an entry to the corpus and return its index
fn on_add(&mut self, state: &mut CS::State, idx: CorpusId) -> Result<(), Error> {
Expand Down Expand Up @@ -242,7 +242,7 @@ where
CS: Scheduler,
F: TestcaseScore<CS::State>,
M: AsSlice<Entry = usize> + SerdeAny + HasRefCnt,
CS::State: HasCorpus + HasMetadata + HasRand,
CS::State: HasCorpus + HasMetadata + HasRand + HasAFLStats,
{
/// Update the `Corpus` score using the `MinimizerScheduler`
#[allow(clippy::unused_self)]
Expand Down Expand Up @@ -328,6 +328,7 @@ where

let mut acc = HashSet::new();

let mut pend_favored_counter: usize = 0;
for (key, idx) in &top_rated.map {
if !acc.contains(key) {
let mut entry = state.corpus().get(*idx)?.borrow_mut();
Expand All @@ -342,9 +343,12 @@ where
}

entry.add_metadata(IsFavoredMetadata {});
pend_favored_counter += 1;
}
}

*state.pend_favored_mut() = pend_favored_counter;

Ok(())
}

Expand Down
15 changes: 14 additions & 1 deletion libafl/src/stages/calibrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
inputs::UsesInput,
monitors::UserStats,
observers::{MapObserver, ObserversTuple, UsesObserver},
schedulers::powersched::SchedulerMetadata,
schedulers::{minimizer::IsFavoredMetadata, powersched::SchedulerMetadata},
stages::Stage,
state::{
HasAFLStats, HasClientPerfMonitor, HasCorpus, HasMetadata, HasNamedMetadata, UsesState,
Expand Down Expand Up @@ -106,6 +106,7 @@ where
corpus_idx: CorpusId,
) -> Result<(), Error> {
let mut has_calibration = false;
let mut is_favored = false;
// Run this stage only once for each corpus entry and only if we haven't already inspected it
{
let corpus = state.corpus().get(corpus_idx)?.borrow();
Expand All @@ -114,8 +115,12 @@ where
if corpus.scheduled_count() > 0 {
has_calibration = true;
}
if corpus.has_metadata::<IsFavoredMetadata>() {
is_favored = true;
}
}

// The number of pending testcases decrease if this testcase has been calibrated
if has_calibration {
let pending_size = state.pending_mut();
if *pending_size > 0 {
Expand All @@ -124,6 +129,14 @@ where
return Ok(());
}

// The number of "pend_favored" testcases decrease if this testcase is favored and is firstly calibrated.
if is_favored {
let pend_favored_size = state.pend_favored_mut();
if *pend_favored_size > 0 {
*state.pend_favored_mut() -= 1;
}
}

*state.pending_mut() += 1;

*state.own_finds_mut() = state.corpus().count() - state.imported();
Expand Down

0 comments on commit f79240d

Please sign in to comment.