libafl 0.16.0

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
//! The [`PrometheusMonitor`] exposes fuzzer progress as 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.
//! Pair it with a [`MultiMonitor`](crate::monitors::MultiMonitor) for console output.
//! In your fuzzer:
//!
//! ```rust
//! // First, include:
//! use libafl::monitors::{MultiMonitor, PrometheusMonitor};
//!
//! // Then, create the monitor:
//! let listener = "127.0.0.1:8080"; // point prometheus to scrape here in your prometheus.yml
//! let mon = (
//!     PrometheusMonitor::new(listener),
//!     MultiMonitor::new(|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::Debug, net::SocketAddr, sync::atomic::AtomicU64, time::Duration};
use std::net::ToSocketAddrs;

// using axum for the HTTP server library (fast, async, modular)
use axum::{
    Router, extract::State as AxumState, http::header, response::IntoResponse, routing::get,
};
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,
};
use tokio::net::TcpListener;

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<ClientLabels, Gauge>,
    objective_count: Family<ClientLabels, Gauge>,
    executions: Family<ClientLabels, Gauge>,
    exec_rate: Family<ClientLabels, Gauge<f64, AtomicU64>>,
    runtime: Family<ClientLabels, Gauge>,
    clients_count: Family<ClientLabels, Gauge>,
    custom_stat: Family<CustomStatLabels, Gauge<f64, AtomicU64>>,
}

impl PrometheusStats {
    fn init_global(&self) {
        let global = ClientLabels {
            client: Cow::from("global"),
        };
        self.corpus_count.get_or_create(&global).set(0);
        self.objective_count.get_or_create(&global).set(0);
        self.executions.get_or_create(&global).set(0);
        self.exec_rate.get_or_create(&global).set(0.0);
        self.runtime.get_or_create(&global).set(0);
        self.clients_count.get_or_create(&global).set(0);
    }
}

/// Tracking monitor during fuzzing.
#[derive(Clone, Debug)]
pub struct PrometheusMonitor {
    stats: PrometheusStats, // unified prometheus metrics
    listener: SocketAddr,   // server address
    runtime: Arc<std::sync::Mutex<Option<tokio::runtime::Runtime>>>, // background tokio runtime
}

impl Monitor for PrometheusMonitor {
    fn display(
        &mut self,
        client_stats_manager: &mut ClientStatsManager,
        event_msg: &str,
        sender_id: ClientId,
    ) -> Result<(), Error> {
        let _ = event_msg; // logging is handled by a paired monitor (e.g., MultiMonitor)

        let mut runtime_lock = self.runtime.lock().unwrap();
        if runtime_lock.is_none() {
            let runtime = tokio::runtime::Builder::new_multi_thread()
                .enable_all()
                .build()
                .unwrap();
            let listener = self.listener;
            let stats = self.stats.clone();
            runtime.spawn(async move {
                serve_metrics(listener, stats)
                    .await
                    .map_err(|err| log::error!("{err:?}"))
                    .ok();
            });
            *runtime_lock = Some(runtime);
        }
        drop(runtime_lock);

        let global_stats = client_stats_manager.global_stats();
        let global = ClientLabels {
            client: Cow::from("global"),
        };

        // Global (aggregated) metrics
        self.stats
            .corpus_count
            .get_or_create(&global)
            .set(global_stats.corpus_size.try_into().unwrap());

        self.stats
            .objective_count
            .get_or_create(&global)
            .set(global_stats.objective_size.try_into().unwrap());

        self.stats
            .executions
            .get_or_create(&global)
            .set(global_stats.total_execs.try_into().unwrap());

        self.stats
            .exec_rate
            .get_or_create(&global)
            .set(global_stats.execs_per_sec);

        self.stats
            .runtime
            .get_or_create(&global)
            .set(global_stats.run_time.as_secs().try_into().unwrap());

        let total_clients: i64 = global_stats.client_stats_count.try_into().unwrap();
        self.stats
            .clients_count
            .get_or_create(&global)
            .set(total_clients);

        // Global aggregated custom stats
        for (key, val) in client_stats_manager.aggregated() {
            #[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.stats
                            .custom_stat
                            .get_or_create(&CustomStatLabels {
                                client: Cow::from("global"),
                                stat: Cow::from("edges_total"),
                            })
                            .set(*b as f64);
                        self.stats
                            .custom_stat
                            .get_or_create(&CustomStatLabels {
                                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.stats
                .custom_stat
                .get_or_create(&CustomStatLabels {
                    client: Cow::from("global"),
                    stat: key.clone(),
                })
                .set(value);
        }

        // 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();

        let client_label = ClientLabels {
            client: Cow::from(sender_id.0.to_string()),
        };

        self.stats
            .corpus_count
            .get_or_create(&client_label)
            .set(cur_client_clone.corpus_size().try_into().unwrap());

        self.stats
            .objective_count
            .get_or_create(&client_label)
            .set(cur_client_clone.objective_size().try_into().unwrap());

        self.stats
            .executions
            .get_or_create(&client_label)
            .set(cur_client_clone.executions().try_into().unwrap());

        self.stats
            .exec_rate
            .get_or_create(&client_label)
            .set(cur_client_clone.execs_per_sec(current_time()));

        let client_run_time = current_time()
            .saturating_sub(cur_client_clone.start_time())
            .as_secs();
        self.stats
            .runtime
            .get_or_create(&client_label)
            .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.stats
            .clients_count
            .get_or_create(&client_label)
            .set(total_clients);

        // Per-client custom stats
        for (key, val) in cur_client_clone.user_stats() {
            #[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.stats
                            .custom_stat
                            .get_or_create(&CustomStatLabels {
                                client: Cow::from(sender_id.0.to_string()),
                                stat: Cow::from("edges_total"),
                            })
                            .set(*b as f64);
                        self.stats
                            .custom_stat
                            .get_or_create(&CustomStatLabels {
                                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.stats
                .custom_stat
                .get_or_create(&CustomStatLabels {
                    client: Cow::from(sender_id.0.to_string()),
                    stat: key.clone(),
                })
                .set(value);
        }
        Ok(())
    }
}

impl PrometheusMonitor {
    /// Create a new [`PrometheusMonitor`].
    /// The `listener` is the address to bind the metrics HTTP endpoint to.
    /// Pair with a [`MultiMonitor`](crate::monitors::MultiMonitor) for console output.
    pub fn new<T>(listener: T) -> Self
    where
        T: ToSocketAddrs,
    {
        let addr = listener
            .to_socket_addrs()
            .expect("Failed to resolve socket address")
            .next()
            .expect("No socket addresses resolved");
        let stats = PrometheusStats::default();
        stats.init_global();

        Self {
            stats,
            listener: addr,
            runtime: Arc::new(std::sync::Mutex::new(None)),
        }
    }

    /// 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<T>(listener: T, _start_time: Duration) -> Self
    where
        T: ToSocketAddrs,
    {
        Self::new(listener)
    }
}

pub(crate) async fn serve_metrics(
    listener: SocketAddr,
    stats: PrometheusStats,
) -> Result<(), std::io::Error> {
    let mut registry = Registry::default();

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

    let state = State {
        registry: Arc::new(registry),
    };

    let app = Router::new()
        .route("/", get(get_root))
        .route("/metrics", get(get_metrics))
        .with_state(state);

    let listener = TcpListener::bind(&listener).await?;
    axum::serve(listener, app).await?;

    Ok(())
}

/// Labels for standard per-client metrics (corpus, executions, etc.).
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelSet, Debug)]
pub struct ClientLabels {
    /// The `sender_id` helps to differentiate between clients when multiple are spawned.
    client: Cow<'static, str>,
}

/// Labels for custom user-defined stats, adding a `stat` dimension for filtering.
#[derive(Clone, Hash, PartialEq, Eq, EncodeLabelSet, Debug)]
pub struct CustomStatLabels {
    /// The `sender_id` helps to differentiate between clients when multiple are spawned.
    client: Cow<'static, str>,
    /// The name of the custom stat (e.g. `edges`, `edges_hit`, `edges_total`).
    stat: Cow<'static, str>,
}

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

async fn get_root() -> &'static str {
    "LibAFL Prometheus Monitor"
}

async fn get_metrics(AxumState(state): AxumState<State>) -> impl IntoResponse {
    let mut encoded = String::new();
    encode(&mut encoded, &state.registry).unwrap();
    (
        [(
            header::CONTENT_TYPE,
            "application/openmetrics-text; version=1.0.0; charset=utf-8",
        )],
        encoded,
    )
}

#[cfg(test)]
mod tests {
    use alloc::string::String;
    use core::time::Duration;
    use std::{
        io::{Read, Write},
        net::TcpStream,
        thread::sleep,
    };

    use libafl_bolts::ClientId;

    use crate::monitors::{Monitor, PrometheusMonitor, stats::ClientStatsManager};

    #[test]
    fn test_prometheus_monitor() {
        let mut client_stats = ClientStatsManager::new();
        let mut mon = PrometheusMonitor::new("127.0.0.1:18081");
        mon.display(&mut client_stats, "test", ClientId(0)).unwrap();

        // Give the server a moment to start up in the background thread
        sleep(Duration::from_millis(500));

        let mut stream =
            TcpStream::connect("127.0.0.1:18081").expect("Failed to connect to prometheus monitor");
        stream
            .set_read_timeout(Some(Duration::from_millis(500)))
            .unwrap();
        stream
            .write_all(b"GET /metrics HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
            .unwrap();

        let mut response = String::new();
        stream.read_to_string(&mut response).unwrap();

        assert!(response.contains("executions_total"));
        assert!(response.contains("execution_rate"));
    }
}