From 177135d330220e5430c9ccbfb14b308ac9dcc441 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Fri, 12 Feb 2021 10:50:06 +0100 Subject: [PATCH] fix a bit exec sec in stats --- afl/src/events/mod.rs | 37 ++++++++++++++++++++++++++--------- afl/src/events/stats.rs | 8 +++++++- afl/src/lib.rs | 4 ++-- afl/src/mutators/scheduled.rs | 4 +--- afl/src/stages/mutational.rs | 2 ++ afl/src/state/mod.rs | 15 +------------- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index a838cff8d8..212b67869c 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -101,13 +101,17 @@ where corpus_size: usize, /// The client config for this observers/testcase combination client_config: String, + /// The time of generation of the event + time: Duration, + /// The executions of this client + executions: usize, }, /// New stats. UpdateStats { + /// The time of generation of the event + time: Duration, /// The executions of this client executions: usize, - /// The execs per sec for this client - execs_over_sec: u64, phantom: PhantomData, }, /// A crash was found @@ -146,10 +150,12 @@ where client_config: _, corpus_size: _, observers_buf: _, + time: _, + executions: _, } => "New Testcase", Event::UpdateStats { + time: _, executions: _, - execs_over_sec: _, phantom: _, } => "Stats", Event::Crash { input: _ } => "Crash", @@ -332,18 +338,21 @@ where client_config: _, corpus_size, observers_buf: _, + time, + executions, } => { - stats.client_stats_mut()[0].corpus_size = *corpus_size as u64; + stats.client_stats_mut()[0].update_corpus_size(*corpus_size as u64); + stats.client_stats_mut()[0].update_executions(*executions as u64, *time); stats.display(event.name().to_string()); Ok(BrokerEventResult::Handled) } Event::UpdateStats { + time, executions, - execs_over_sec: _, phantom: _, } => { // TODO: The stats buffer should be added on client add. - stats.client_stats_mut()[0].executions = *executions as u64; + stats.client_stats_mut()[0].update_executions(*executions as u64, *time); stats.display(event.name().to_string()); Ok(BrokerEventResult::Handled) } @@ -568,20 +577,23 @@ where client_config: _, corpus_size, observers_buf: _, + time, + executions } => { let client = stats.client_stats_mut_for(sender_id); - client.corpus_size = *corpus_size as u64; + client.update_corpus_size(*corpus_size as u64); + client.update_executions(*executions as u64, *time); stats.display(event.name().to_string() + " #" + &sender_id.to_string()); Ok(BrokerEventResult::Handled) } Event::UpdateStats { + time, executions, - execs_over_sec: _, phantom: _, } => { // TODO: The stats buffer should be added on client add. let client = stats.client_stats_mut_for(sender_id); - client.executions = *executions as u64; + client.update_executions(*executions as u64, *time); stats.display(event.name().to_string() + " #" + &sender_id.to_string()); Ok(BrokerEventResult::Handled) } @@ -626,6 +638,8 @@ where client_config: _, corpus_size: _, observers_buf, + time: _, + executions: _ } => { // TODO: here u should match client_config, if equal to the current one do not re-execute // we need to pass engine to process() too, TODO @@ -946,6 +960,7 @@ mod tests { use crate::inputs::bytes::BytesInput; use crate::observers::ObserversTuple; use crate::observers::StdMapObserver; + use crate::utils::current_time; static mut MAP: [u32; 4] = [0; 4]; @@ -961,6 +976,8 @@ mod tests { observers_buf, corpus_size: 123, client_config: "conf".into(), + time: current_time(), + executions: 0 }; let serialized = postcard::to_allocvec(&e).unwrap(); @@ -972,6 +989,8 @@ mod tests { observers_buf, corpus_size: _, client_config: _, + time: _, + executions: _ } => { let o = map.deserialize(&observers_buf).unwrap(); let test_observer = o.match_name_type::>("test").unwrap(); diff --git a/afl/src/events/stats.rs b/afl/src/events/stats.rs index 27800bcd11..e75130071e 100644 --- a/afl/src/events/stats.rs +++ b/afl/src/events/stats.rs @@ -27,13 +27,19 @@ impl ClientStats { /// We got a new information about executions for this client, insert them. pub fn update_executions(&mut self, executions: u64, cur_time: time::Duration) { self.executions = executions; - if (cur_time - self.last_window_time).as_secs() > CLIENT_STATS_TIME_WINDOW_SECS { + let diff = cur_time.checked_sub(self.last_window_time).map_or(0, |d| d.as_secs()); + if diff > CLIENT_STATS_TIME_WINDOW_SECS { self.last_execs_per_sec = self.execs_per_sec(cur_time); self.last_window_time = cur_time; self.last_window_executions = executions; } } + /// We got a new information about corpus size for this client, insert them. + pub fn update_corpus_size(&mut self, corpus_size: u64) { + self.corpus_size = corpus_size; + } + /// Get the calculated executions per second for this client pub fn execs_per_sec(&self, cur_time: time::Duration) -> u64 { if self.executions == 0 { diff --git a/afl/src/lib.rs b/afl/src/lib.rs index 8d672d773f..7185129f71 100644 --- a/afl/src/lib.rs +++ b/afl/src/lib.rs @@ -32,7 +32,7 @@ use inputs::Input; use observers::ObserversTuple; use stages::StagesTuple; use state::{HasCorpus, State}; -use utils::{current_milliseconds, Rand}; +use utils::{current_milliseconds, current_time, Rand}; #[cfg(feature = "std")] use std::{env::VarError, io, num::ParseIntError, string::FromUtf8Error}; @@ -86,7 +86,7 @@ where state, Event::UpdateStats { executions: state.executions(), - execs_over_sec: state.executions_over_seconds(), + time: current_time(), phantom: PhantomData, }, )? diff --git a/afl/src/mutators/scheduled.rs b/afl/src/mutators/scheduled.rs index 05603fabd0..587c03aa70 100644 --- a/afl/src/mutators/scheduled.rs +++ b/afl/src/mutators/scheduled.rs @@ -307,9 +307,7 @@ where scheduled.add_mutation(mutation_tokeninsert); scheduled.add_mutation(mutation_tokenreplace); */ - - // TODO: custom dictionary (redqueen etc.) - + scheduled.add_mutation(mutation_splice); HavocBytesMutator { diff --git a/afl/src/stages/mutational.rs b/afl/src/stages/mutational.rs index 4587e946cd..f779f4029b 100644 --- a/afl/src/stages/mutational.rs +++ b/afl/src/stages/mutational.rs @@ -86,6 +86,8 @@ where observers_buf, corpus_size: state.corpus().count() + 1, client_config: "TODO".into(), + time: crate::utils::current_time(), + executions: state.executions(), }, )?; state.add_if_interesting(input_mut, fitness)?; diff --git a/afl/src/state/mod.rs b/afl/src/state/mod.rs index d0aa45713f..5ae936ca42 100644 --- a/afl/src/state/mod.rs +++ b/afl/src/state/mod.rs @@ -68,6 +68,7 @@ where executions: usize, /// The corpus corpus: C, + // TODO use Duration /// At what time the fuzzing started start_time: u64, /// Metadata stored for this state by one of the components @@ -218,20 +219,6 @@ where pub fn set_start_time(&mut self, ms: u64) { self.start_time = ms } - // TODO as this is done in the event manager, we can remove it - #[inline] - pub fn executions_over_seconds(&self) -> u64 { - let elapsed = current_milliseconds() - self.start_time(); - if elapsed == 0 { - return 0; - } - let elapsed = elapsed / 1000; - if elapsed == 0 { - 0 - } else { - (self.executions() as u64) / elapsed - } - } /// Returns vector of feebacks #[inline]