Skip to content

Commit abe9551

Browse files
Remove base-pattern for Monitor (#2953)
* Remove base-pattern for Monitor * Fix runtime parameter for log_record --------- Co-authored-by: Dongjia "toka" Zhang <[email protected]>
1 parent 075fb0d commit abe9551

File tree

5 files changed

+44
-80
lines changed

5 files changed

+44
-80
lines changed

MIGRATION.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- There is a `ClientStatsManager` to manage client statistics, and is owned by `EventManager`. Most of previous `Monitor`'s trait methods have been moved to the `ClientStatsManager`.
1111
- `user_monitor` has been renamed to `user_stats`, and its structure definitions have been moved to `statistics` module as well.
1212
- `introspection_monitor` has been renamed to `introspection_stats`, and perf-related structure definitions have been renamed and moved to `statistics` module as well.
13+
- `OnDiskTomlMonitor`, `OnDiskJsonMonitor`, `OnDiskJsonAggregateMonitor` are now no longer takes a base monitor to wrap. If you want to use multiple monitors together, use `libafl::combine_monitor!`.
1314

1415
# 0.14.1 -> 0.15.0
1516
- `MmapShMem::new` and `MmapShMemProvider::new_shmem_with_id` now take `AsRef<Path>` instead of a byte array for the filename/id.

fuzzers/inprocess/libfuzzer_libpng_launcher/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{env, net::SocketAddr, path::PathBuf};
77

88
use clap::{self, Parser};
99
use libafl::{
10+
combine_monitor,
1011
corpus::{Corpus, InMemoryCorpus, OnDiskCorpus},
1112
events::{launcher::Launcher, EventConfig},
1213
executors::{inprocess::InProcessExecutor, ExitKind},
@@ -140,9 +141,9 @@ pub extern "C" fn libafl_main() {
140141

141142
let shmem_provider = StdShMemProvider::new().expect("Failed to init shared memory");
142143

143-
let monitor = OnDiskTomlMonitor::new(
144-
"./fuzzer_stats.toml",
145-
MultiMonitor::new(|s| println!("{s}")),
144+
let monitor = combine_monitor!(
145+
OnDiskTomlMonitor::new("./fuzzer_stats.toml"),
146+
MultiMonitor::new(|s| println!("{s}"))
146147
);
147148

148149
let mut run_client = |state: Option<_>, mut restarting_mgr, _client_description| {

fuzzers/inprocess/libfuzzer_libpng_norestart/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{env, net::SocketAddr, path::PathBuf};
88

99
use clap::Parser;
1010
use libafl::{
11+
combine_monitor,
1112
corpus::{Corpus, InMemoryOnDiskCorpus, OnDiskCorpus},
1213
events::{
1314
launcher::{ClientDescription, Launcher},
@@ -156,9 +157,9 @@ pub extern "C" fn libafl_main() {
156157

157158
let shmem_provider = MmapShMemProvider::new().expect("Failed to init shared memory");
158159

159-
let monitor = OnDiskTomlMonitor::new(
160-
"./fuzzer_stats.toml",
161-
MultiMonitor::new(|s| println!("{s}")),
160+
let monitor = combine_monitor!(
161+
OnDiskTomlMonitor::new("./fuzzer_stats.toml"),
162+
MultiMonitor::new(|s| println!("{s}"))
162163
);
163164

164165
let mut run_client = |state: Option<_>,

libafl/src/monitors/disk.rs

+25-51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Monitors that wrap a base monitor and also log to disk using different formats like `JSON` and `TOML`.
1+
//! Monitors that log to disk using different formats like `JSON` and `TOML`.
22
33
use alloc::string::String;
44
use core::time::Duration;
@@ -11,32 +11,22 @@ use std::{
1111
use libafl_bolts::{current_time, format_duration_hms, ClientId};
1212
use serde_json::json;
1313

14-
use crate::{
15-
monitors::{Monitor, NopMonitor},
16-
statistics::manager::ClientStatsManager,
17-
};
14+
use crate::{monitors::Monitor, statistics::manager::ClientStatsManager};
1815

1916
/// Wrap a monitor and log the current state of the monitor into a Toml file.
2017
#[derive(Debug, Clone)]
21-
pub struct OnDiskTomlMonitor<M>
22-
where
23-
M: Monitor,
24-
{
25-
base: M,
18+
pub struct OnDiskTomlMonitor {
2619
filename: PathBuf,
2720
last_update: Duration,
2821
update_interval: Duration,
2922
}
3023

31-
impl<M> Monitor for OnDiskTomlMonitor<M>
32-
where
33-
M: Monitor,
34-
{
24+
impl Monitor for OnDiskTomlMonitor {
3525
fn display(
3626
&mut self,
3727
client_stats_manager: &mut ClientStatsManager,
38-
event_msg: &str,
39-
sender_id: ClientId,
28+
_event_msg: &str,
29+
_sender_id: ClientId,
4030
) {
4131
let cur_time = current_time();
4232

@@ -100,96 +90,82 @@ exec_sec = {}
10090

10191
drop(file);
10292
}
103-
104-
self.base
105-
.display(client_stats_manager, event_msg, sender_id);
10693
}
10794
}
10895

109-
impl<M> OnDiskTomlMonitor<M>
110-
where
111-
M: Monitor,
112-
{
96+
impl OnDiskTomlMonitor {
11397
/// Create new [`OnDiskTomlMonitor`]
11498
#[must_use]
115-
pub fn new<P>(filename: P, base: M) -> Self
99+
pub fn new<P>(filename: P) -> Self
116100
where
117101
P: Into<PathBuf>,
118102
{
119-
Self::with_update_interval(filename, base, Duration::from_secs(60))
103+
Self::with_update_interval(filename, Duration::from_secs(60))
120104
}
121105

122106
/// Create new [`OnDiskTomlMonitor`] with custom update interval
123107
#[must_use]
124-
pub fn with_update_interval<P>(filename: P, base: M, update_interval: Duration) -> Self
108+
pub fn with_update_interval<P>(filename: P, update_interval: Duration) -> Self
125109
where
126110
P: Into<PathBuf>,
127111
{
128112
Self {
129-
base,
130113
filename: filename.into(),
131114
last_update: current_time() - update_interval,
132115
update_interval,
133116
}
134117
}
135118
}
136119

137-
impl OnDiskTomlMonitor<NopMonitor> {
120+
impl OnDiskTomlMonitor {
138121
/// Create new [`OnDiskTomlMonitor`] without a base
139122
#[must_use]
123+
#[deprecated(since = "0.16.0", note = "Use new directly")]
140124
pub fn nop<P>(filename: P) -> Self
141125
where
142126
P: Into<PathBuf>,
143127
{
144-
Self::new(filename, NopMonitor::new())
128+
Self::new(filename)
145129
}
146130
}
147131

148132
#[derive(Debug, Clone)]
149-
/// Wraps a base monitor and continuously appends the current statistics to a Json lines file.
150-
pub struct OnDiskJsonMonitor<F, M>
133+
/// Continuously appends the current statistics to a Json lines file.
134+
pub struct OnDiskJsonMonitor<F>
151135
where
152-
F: FnMut(&mut M) -> bool,
153-
M: Monitor,
136+
F: FnMut(&mut ClientStatsManager) -> bool,
154137
{
155-
base: M,
156138
path: PathBuf,
157139
/// A function that has the current runtime as argument and decides, whether a record should be logged
158140
log_record: F,
159141
}
160142

161-
impl<F, M> OnDiskJsonMonitor<F, M>
143+
impl<F> OnDiskJsonMonitor<F>
162144
where
163-
F: FnMut(&mut M) -> bool,
164-
M: Monitor,
145+
F: FnMut(&mut ClientStatsManager) -> bool,
165146
{
166147
/// Create a new [`OnDiskJsonMonitor`]
167-
pub fn new<P>(filename: P, base: M, log_record: F) -> Self
148+
pub fn new<P>(filename: P, log_record: F) -> Self
168149
where
169150
P: Into<PathBuf>,
170151
{
171152
let path = filename.into();
172153

173-
Self {
174-
base,
175-
path,
176-
log_record,
177-
}
154+
Self { path, log_record }
178155
}
179156
}
180157

181-
impl<F, M> Monitor for OnDiskJsonMonitor<F, M>
158+
impl<F> Monitor for OnDiskJsonMonitor<F>
182159
where
183-
F: FnMut(&mut M) -> bool,
184-
M: Monitor,
160+
F: FnMut(&mut ClientStatsManager) -> bool,
185161
{
186162
fn display(
187163
&mut self,
188164
client_stats_manager: &mut ClientStatsManager,
189-
event_msg: &str,
190-
sender_id: ClientId,
165+
_event_msg: &str,
166+
_sender_id: ClientId,
191167
) {
192-
if (self.log_record)(&mut self.base) {
168+
if (self.log_record)(client_stats_manager) {
193169
let file = OpenOptions::new()
194170
.append(true)
195171
.create(true)
@@ -207,7 +183,5 @@ where
207183
});
208184
writeln!(&file, "{line}").expect("Unable to write Json to file");
209185
}
210-
self.base
211-
.display(client_stats_manager, event_msg, sender_id);
212186
}
213187
}

libafl/src/monitors/disk_aggregate.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,31 @@ use serde_json::json;
1111

1212
use crate::{monitors::Monitor, statistics::manager::ClientStatsManager};
1313

14-
/// A monitor that wraps another monitor and logs aggregated stats to a JSON file.
14+
/// A monitor that logs aggregated stats to a JSON file.
1515
#[derive(Clone)]
16-
pub struct OnDiskJsonAggregateMonitor<M> {
17-
base: M,
16+
pub struct OnDiskJsonAggregateMonitor {
1817
json_path: PathBuf,
1918
last_update: Duration,
2019
update_interval: Duration,
2120
}
2221

23-
impl<M> Debug for OnDiskJsonAggregateMonitor<M>
24-
where
25-
M: Debug,
26-
{
22+
impl Debug for OnDiskJsonAggregateMonitor {
2723
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
2824
f.debug_struct("OnDiskJsonAggregateMonitor")
29-
.field("base", &self.base)
3025
.field("last_update", &self.last_update)
3126
.field("update_interval", &self.update_interval)
3227
.field("json_path", &self.json_path)
3328
.finish_non_exhaustive()
3429
}
3530
}
3631

37-
impl<M> Monitor for OnDiskJsonAggregateMonitor<M>
38-
where
39-
M: Monitor,
40-
{
32+
impl Monitor for OnDiskJsonAggregateMonitor {
4133
fn display(
4234
&mut self,
4335
client_stats_manager: &mut ClientStatsManager,
44-
event_msg: &str,
45-
sender_id: ClientId,
36+
_event_msg: &str,
37+
_sender_id: ClientId,
4638
) {
47-
// First let the base monitor handle its display
48-
self.base
49-
.display(client_stats_manager, event_msg, sender_id);
50-
5139
// Write JSON stats if update interval has elapsed
5240
let cur_time = current_time();
5341
if cur_time - self.last_update >= self.update_interval {
@@ -83,22 +71,21 @@ where
8371
}
8472
}
8573

86-
impl<M> OnDiskJsonAggregateMonitor<M> {
74+
impl OnDiskJsonAggregateMonitor {
8775
/// Creates a new [`OnDiskJsonAggregateMonitor`]
88-
pub fn new<P>(base: M, json_path: P) -> Self
76+
pub fn new<P>(json_path: P) -> Self
8977
where
9078
P: Into<PathBuf>,
9179
{
92-
Self::with_interval(json_path, base, Duration::from_secs(10))
80+
Self::with_interval(json_path, Duration::from_secs(10))
9381
}
9482

9583
/// Creates a new [`OnDiskJsonAggregateMonitor`] with custom update interval
96-
pub fn with_interval<P>(json_path: P, base: M, update_interval: Duration) -> Self
84+
pub fn with_interval<P>(json_path: P, update_interval: Duration) -> Self
9785
where
9886
P: Into<PathBuf>,
9987
{
10088
Self {
101-
base,
10289
json_path: json_path.into(),
10390
last_update: current_time() - update_interval,
10491
update_interval,

0 commit comments

Comments
 (0)