libafl 0.15.4

Slot your own fuzzers together and extend their features using Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! The [`PrometheusMonitor`] logs fuzzer progress to a prometheus endpoint.
//!
//! ## Overview
//!
//! The client (i.e., the fuzzer) sets up an HTTP endpoint (/metrics).
//! The endpoint contains metrics such as execution rate.
//!
//! A prometheus server (can use a precompiled binary or docker) then scrapes
//! the endpoint at regular intervals (configurable via prometheus.yml file).
//!
//! ## How to use it
//!
//! Create a [`PrometheusMonitor`] and plug it into any fuzzer similar to other monitors.
//! In your fuzzer:
//!
//! ```rust
//! // First, include:
//! use libafl::monitors::PrometheusMonitor;
//!
//! // Then, create the monitor:
//! let listener = "127.0.0.1:8080".to_string(); // point prometheus to scrape here in your prometheus.yml
//! let mon = PrometheusMonitor::new(listener, |s| log::info!("{s}"));
//!
//! // and finally, like with any other monitor, pass it into the event manager like so:
//! // let mgr = SimpleEventManager::new(mon);
//! ```
//!
//! When using docker, you may need to point `prometheus.yml` to the `docker0` interface or `host.docker.internal`

use alloc::{
    borrow::Cow,
    string::{String, ToString},
    sync::Arc,
};
use core::{
    fmt,
    fmt::{Debug, Write},
    sync::atomic::AtomicU64,
    time::Duration,
};
use std::thread;

// using thread in order to start the HTTP server in a separate thread
use futures::executor::block_on;
use libafl_bolts::{ClientId, Error, current_time};
// using the official rust client library for Prometheus: https://github.com/prometheus/client_rust
use prometheus_client::{
    encoding::{EncodeLabelSet, text::encode},
    metrics::{family::Family, gauge::Gauge},
    registry::Registry,
};
// using tide for the HTTP server library (fast, async, simple)
use tide::Request;

use crate::monitors::{
    Monitor,
    stats::{manager::ClientStatsManager, user_stats::UserStatsValue},
};

/// Prometheus metrics for global and each client.
#[derive(Debug, Clone, Default)]
pub struct PrometheusStats {
    corpus_count: Family<Labels, Gauge>,
    objective_count: Family<Labels, Gauge>,
    executions: Family<Labels, Gauge>,
    exec_rate: Family<Labels, Gauge<f64, AtomicU64>>,
    runtime: Family<Labels, Gauge>,
    clients_count: Family<Labels, Gauge>,
    custom_stat: Family<Labels, Gauge<f64, AtomicU64>>,
}

/// Tracking monitor during fuzzing.
#[derive(Clone)]
pub struct PrometheusMonitor<F>
where
    F: FnMut(&str),
{
    print_fn: F,
    prometheus_global_stats: PrometheusStats, // global prometheus metrics
    prometheus_client_stats: PrometheusStats, // per-client prometheus metrics
}

impl<F> Debug for PrometheusMonitor<F>
where
    F: FnMut(&str),
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PrometheusMonitor").finish_non_exhaustive()
    }
}

impl<F> Monitor for PrometheusMonitor<F>
where
    F: FnMut(&str),
{
    fn display(
        &mut self,
        client_stats_manager: &mut ClientStatsManager,
        event_msg: &str,
        sender_id: ClientId,
    ) -> Result<(), Error> {
        // Update the prometheus metrics
        // The gauges must take signed i64's, with max value of 2^63-1 so it is
        // probably fair to error out at a count of nine quintillion across any
        // of these counts.
        // realistically many of these metrics should be counters but would
        // require a fair bit of logic to handle "amount to increment given
        // time since last observation"

        let global_stats = client_stats_manager.global_stats();
        // Global (aggregated) metrics
        let corpus_size = global_stats.corpus_size;
        self.prometheus_global_stats
            .corpus_count
            .get_or_create(&Labels {
                client: Cow::from("global"),
                stat: Cow::from(""),
            })
            .set(corpus_size.try_into().unwrap());

        let objective_size = global_stats.objective_size;
        self.prometheus_global_stats
            .objective_count
            .get_or_create(&Labels {
                client: Cow::from("global"),
                stat: Cow::from(""),
            })
            .set(objective_size.try_into().unwrap());

        let total_execs = global_stats.total_execs;
        self.prometheus_global_stats
            .executions
            .get_or_create(&Labels {
                client: Cow::from("global"),
                stat: Cow::from(""),
            })
            .set(total_execs.try_into().unwrap());

        let execs_per_sec = global_stats.execs_per_sec;
        self.prometheus_global_stats
            .exec_rate
            .get_or_create(&Labels {
                client: Cow::from("global"),
                stat: Cow::from(""),
            })
            .set(execs_per_sec);

        let run_time = global_stats.run_time.as_secs();
        self.prometheus_global_stats
            .runtime
            .get_or_create(&Labels {
                client: Cow::from("global"),
                stat: Cow::from(""),
            })
            .set(run_time.try_into().unwrap()); // run time in seconds, which can be converted to a time format by Grafana or similar

        let total_clients = global_stats.client_stats_count.try_into().unwrap(); // convert usize to u64 (unlikely that # of clients will be > 2^64 -1...)
        self.prometheus_global_stats
            .clients_count
            .get_or_create(&Labels {
                client: Cow::from("global"),
                stat: Cow::from(""),
            })
            .set(total_clients);

        // display stats in a SimpleMonitor format
        let mut global_fmt = format!(
            "[Prometheus] [{} #GLOBAL] run time: {}, clients: {}, corpus: {}, objectives: {}, executions: {}, exec/sec: {}",
            event_msg,
            global_stats.run_time_pretty,
            global_stats.client_stats_count,
            global_stats.corpus_size,
            global_stats.objective_size,
            global_stats.total_execs,
            global_stats.execs_per_sec_pretty
        );
        for (key, val) in client_stats_manager.aggregated() {
            // print global aggregated custom stats
            write!(global_fmt, ", {key}: {val}").unwrap();
            #[expect(clippy::cast_precision_loss)]
            let value: f64 = match val {
                UserStatsValue::Number(n) => *n as f64,
                UserStatsValue::Float(f) => *f,
                UserStatsValue::String(_s) => 0.0,
                UserStatsValue::Ratio(a, b) => {
                    if key == "edges" {
                        self.prometheus_global_stats
                            .custom_stat
                            .get_or_create(&Labels {
                                client: Cow::from("global"),
                                stat: Cow::from("edges_total"),
                            })
                            .set(*b as f64);
                        self.prometheus_global_stats
                            .custom_stat
                            .get_or_create(&Labels {
                                client: Cow::from("global"),
                                stat: Cow::from("edges_hit"),
                            })
                            .set(*a as f64);
                    }
                    (*a as f64 / *b as f64) * 100.0
                }
                UserStatsValue::Percent(p) => *p * 100.0,
            };
            self.prometheus_global_stats
                .custom_stat
                .get_or_create(&Labels {
                    client: Cow::from("global"),
                    stat: key.clone(),
                })
                .set(value);
        }

        (self.print_fn)(&global_fmt);

        // Client-specific metrics

        client_stats_manager.client_stats_insert(sender_id)?;
        let client = client_stats_manager.client_stats_for(sender_id)?;
        let mut cur_client_clone = client.clone();

        self.prometheus_client_stats
            .corpus_count
            .get_or_create(&Labels {
                client: Cow::from(sender_id.0.to_string()),
                stat: Cow::from(""),
            })
            .set(cur_client_clone.corpus_size().try_into().unwrap());

        self.prometheus_client_stats
            .objective_count
            .get_or_create(&Labels {
                client: Cow::from(sender_id.0.to_string()),
                stat: Cow::from(""),
            })
            .set(cur_client_clone.objective_size().try_into().unwrap());

        self.prometheus_client_stats
            .executions
            .get_or_create(&Labels {
                client: Cow::from(sender_id.0.to_string()),
                stat: Cow::from(""),
            })
            .set(cur_client_clone.executions().try_into().unwrap());

        self.prometheus_client_stats
            .exec_rate
            .get_or_create(&Labels {
                client: Cow::from(sender_id.0.to_string()),
                stat: Cow::from(""),
            })
            .set(cur_client_clone.execs_per_sec(current_time()));

        let client_run_time = current_time()
            .checked_sub(cur_client_clone.start_time())
            .unwrap_or_default()
            .as_secs();
        self.prometheus_client_stats
            .runtime
            .get_or_create(&Labels {
                client: Cow::from(sender_id.0.to_string()),
                stat: Cow::from(""),
            })
            .set(client_run_time.try_into().unwrap()); // run time in seconds per-client, which can be converted to a time format by Grafana or similar

        self.prometheus_global_stats
            .clients_count
            .get_or_create(&Labels {
                client: Cow::from(sender_id.0.to_string()),
                stat: Cow::from(""),
            })
            .set(total_clients);

        let mut fmt = format!(
            "[Prometheus] [{} #{}] corpus: {}, objectives: {}, executions: {}, exec/sec: {}",
            event_msg,
            sender_id.0,
            client.corpus_size(),
            client.objective_size(),
            client.executions(),
            cur_client_clone.execs_per_sec_pretty(current_time())
        );

        for (key, val) in cur_client_clone.user_stats() {
            // print the custom stats for each client
            write!(fmt, ", {key}: {val}").unwrap();
            // Update metrics added to the user_stats hashmap by feedback event-fires
            // You can filter for each custom stat in promQL via labels of both the stat name and client id
            #[expect(clippy::cast_precision_loss)]
            let value: f64 = match val.value() {
                UserStatsValue::Number(n) => *n as f64,
                UserStatsValue::Float(f) => *f,
                UserStatsValue::String(_s) => 0.0,
                UserStatsValue::Ratio(a, b) => {
                    if key == "edges" {
                        self.prometheus_client_stats
                            .custom_stat
                            .get_or_create(&Labels {
                                client: Cow::from(sender_id.0.to_string()),
                                stat: Cow::from("edges_total"),
                            })
                            .set(*b as f64);
                        self.prometheus_client_stats
                            .custom_stat
                            .get_or_create(&Labels {
                                client: Cow::from(sender_id.0.to_string()),
                                stat: Cow::from("edges_hit"),
                            })
                            .set(*a as f64);
                    }
                    (*a as f64 / *b as f64) * 100.0
                }
                UserStatsValue::Percent(p) => *p * 100.0,
            };
            self.prometheus_client_stats
                .custom_stat
                .get_or_create(&Labels {
                    client: Cow::from(sender_id.0.to_string()),
                    stat: key.clone(),
                })
                .set(value);
        }
        (self.print_fn)(&fmt);
        Ok(())
    }
}

impl<F> PrometheusMonitor<F>
where
    F: FnMut(&str),
{
    /// Create a new [`PrometheusMonitor`].
    /// The `listener` is the address to send logs to.
    /// The `print_fn` is the printing function that can output the logs otherwise.
    pub fn new(listener: String, print_fn: F) -> Self {
        let prometheus_global_stats = PrometheusStats::default();
        let prometheus_global_stats_clone = prometheus_global_stats.clone();
        let prometheus_client_stats = PrometheusStats::default();
        let prometheus_client_stats_clone = prometheus_client_stats.clone();

        // Need to run the metrics server in a different thread to avoid blocking
        thread::spawn(move || {
            block_on(serve_metrics(
                listener,
                prometheus_global_stats_clone,
                prometheus_client_stats_clone,
            ))
            .map_err(|err| log::error!("{err:?}"))
            .ok();
        });
        Self {
            print_fn,
            prometheus_global_stats,
            prometheus_client_stats,
        }
    }
    /// Creates the monitor with a given `start_time`.
    #[deprecated(
        since = "0.16.0",
        note = "Please use new to create. start_time is useless here."
    )]
    pub fn with_time(listener: String, print_fn: F, _start_time: Duration) -> Self {
        Self::new(listener, print_fn)
    }
}

/// Set up an HTTP endpoint /metrics
pub(crate) async fn serve_metrics(
    listener: String,
    global_stats: PrometheusStats,
    client_stats: PrometheusStats,
) -> Result<(), std::io::Error> {
    let mut registry = Registry::default();

    // Register the global stats
    registry.register(
        "global_corpus_count",
        "Number of test cases in the corpus",
        global_stats.corpus_count,
    );
    registry.register(
        "global_objective_count",
        "Number of times the objective has been achieved (e.g., crashes)",
        global_stats.objective_count,
    );
    registry.register(
        "global_executions_total",
        "Total number of executions",
        global_stats.executions,
    );
    registry.register(
        "execution_rate",
        "Rate of executions per second",
        global_stats.exec_rate,
    );
    registry.register(
        "global_runtime",
        "How long the fuzzer has been running for (seconds)",
        global_stats.runtime,
    );
    registry.register(
        "global_clients_count",
        "How many clients have been spawned for the fuzzing job",
        global_stats.clients_count,
    );
    registry.register(
        "global_custom_stat",
        "A metric to contain custom stats returned by feedbacks, filterable by label (aggregated)",
        global_stats.custom_stat,
    );

    // Register the client stats
    registry.register(
        "corpus_count",
        "Number of test cases in the client's corpus",
        client_stats.corpus_count,
    );
    registry.register(
        "objective_count",
        "Number of client's objectives (e.g., crashes)",
        client_stats.objective_count,
    );
    registry.register(
        "executions_total",
        "Total number of client executions",
        client_stats.executions,
    );
    registry.register(
        "execution_rate",
        "Rate of executions per second",
        client_stats.exec_rate,
    );
    registry.register(
        "runtime",
        "How long the client has been running for (seconds)",
        client_stats.runtime,
    );
    registry.register(
        "clients_count",
        "How many clients have been spawned for the fuzzing job",
        client_stats.clients_count,
    );
    registry.register(
        "custom_stat",
        "A metric to contain custom stats returned by feedbacks, filterable by label",
        client_stats.custom_stat,
    );

    let mut app = tide::with_state(State {
        registry: Arc::new(registry),
    });

    app.at("/")
        .get(|_| async { Ok("LibAFL Prometheus Monitor") });
    app.at("/metrics").get(|req: Request<State>| async move {
        let mut encoded = String::new();
        encode(&mut encoded, &req.state().registry).unwrap();
        let response = tide::Response::builder(200)
            .body(encoded)
            .content_type("application/openmetrics-text; version=1.0.0; charset=utf-8")
            .build();
        Ok(response)
    });
    app.listen(listener).await?;

    Ok(())
}

/// Struct used to define the labels in `prometheus`.
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelSet, Debug)]
pub struct Labels {
    /// The `sender_id` helps to differentiate between clients when multiple are spawned.
    client: Cow<'static, str>,
    /// Used for `custom_stat` filtering.
    stat: Cow<'static, str>,
}

/// The state for this monitor.
#[derive(Clone)]
struct State {
    registry: Arc<Registry>,
}