Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced TuiMonitor display to include Current Testcase Index and use… #2810

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions libafl/src/monitors/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,14 @@ impl Monitor for TuiMonitor {
#[expect(clippy::cast_sign_loss)]
fn display(&mut self, event_msg: &str, sender_id: ClientId) {
let cur_time = current_time();

{
// TODO implement floating-point support for TimedStat
let execsec = self.execs_per_sec() as u64;
let totalexec = self.total_execs();
let run_time = cur_time - self.start_time;
let total_process_timing = self.process_timing();

let mut ctx = self.context.write().unwrap();
ctx.total_process_timing = total_process_timing;
ctx.corpus_size_timed.add(run_time, self.corpus_size());
Expand All @@ -452,11 +452,11 @@ impl Monitor for TuiMonitor {
ctx.total_corpus_count = self.corpus_size();
ctx.total_item_geometry = self.item_geometry();
}

self.client_stats_insert(sender_id);
let client = self.client_stats_mut_for(sender_id);
let exec_sec = client.execs_per_sec_pretty(cur_time);

let sender = format!("#{}", sender_id.0);
let pad = if event_msg.len() + sender.len() < 13 {
" ".repeat(13 - event_msg.len() - sender.len())
Expand All @@ -468,13 +468,21 @@ impl Monitor for TuiMonitor {
"[{}] corpus: {}, objectives: {}, executions: {}, exec/sec: {}",
head, client.corpus_size, client.objective_size, client.executions, exec_sec
);

// Display "Current Testcase Index" if available
if let Some(stat) = client.get_user_stats("Current Testcase Index") {
write!(fmt, ", Testcase Index: {}", stat.value()).unwrap();
} else {
write!(fmt, ", Testcase Index: N/A").unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to show anything if it's not available.

Also, this only ends up in the log, not in the TUI, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this code update ensures that the "Testcase Index" information only ends up in the log, not in the TUI. ok will remove else

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think user stats already end up in the log (not 100% sure though)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and really, it'd be nice to have this in the UI for the individual fuzzer, that's the idea of the tui, but we can do that in another PR)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can try in this PR to add this in the UI for the individual fuzzer

}

for (key, val) in &client.user_monitor {
write!(fmt, ", {key}: {val}").unwrap();
}
for (key, val) in &self.aggregator.aggregated {
write!(fmt, ", {key}: {val}").unwrap();
}

{
let client = &self.client_stats()[sender_id.0 as usize];
let mut ctx = self.context.write().unwrap();
Expand All @@ -487,7 +495,7 @@ impl Monitor for TuiMonitor {
}
ctx.client_logs.push_back(fmt);
}

#[cfg(feature = "introspection")]
{
// Print the client performance monitor. Skip the Client IDs that have never sent anything.
Expand All @@ -502,7 +510,7 @@ impl Monitor for TuiMonitor {
}
}
}

fn aggregate(&mut self, name: &str) {
self.aggregator.aggregate(name, &self.client_stats);
}
Expand Down
23 changes: 22 additions & 1 deletion libafl/src/stages/afl_stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//! Stage to compute and report AFL++ stats
use crate::events::Event;
use crate::monitors::UserStats;
use crate::monitors::{AggregatorOps, UserStatsValue};
use crate::state::UsesState;
use alloc::{string::String, vec::Vec};
use core::{marker::PhantomData, time::Duration};
use std::{
Expand Down Expand Up @@ -238,7 +242,7 @@ pub struct AFLPlotData<'a> {
impl<C, E, EM, O, S, Z> Stage<E, EM, S, Z> for AflStatsStage<C, E, EM, O, S, Z>
where
E: HasObservers,
EM: EventFirer,
EM: EventFirer + UsesState<State = S>,
Z: HasScheduler<<S::Corpus as Corpus>::Input, S>,
S: HasImported
+ HasCorpus
Expand Down Expand Up @@ -267,7 +271,24 @@ where
"state is not currently processing a corpus index",
));
};
// Clone or copy the required data from `state`
let corpus_idx_value = corpus_idx.0; // Extract `usize` value from `CorpusId`

// Fire the UpdateUserStats event with the corpus index
_manager.fire(
Copy link
Member

@domenukk domenukk Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optional (i.e., something you set in the builder), it may lead to a lot of events so slow down the fuzzer

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks will look into this

state,
Event::UpdateUserStats {
name: Cow::Borrowed("Current Testcase Index"),
value: UserStats::new(
UserStatsValue::Number(corpus_idx_value as u64),
AggregatorOps::Sum,
),
phantom: PhantomData,
},
)?;

let testcase = state.corpus().get(corpus_idx)?.borrow();

// NOTE: scheduled_count represents the amount of fuzz runs a
// testcase has had. Since this stage is kept at the very end of stage list,
// the entry would have been fuzzed already (and should contain IsFavoredMetadata) but would have a scheduled count of zero
Expand Down
Loading