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
use crate::event::{USER_STAT_EDGES, USER_STAT_NODES};
use libafl::{
    bolts::{current_time, format_duration_hms},
    monitors::{ClientStats, Monitor, UserStats},
};
use std::time::Duration;

#[cfg(feature = "graphviz")]
use {crate::event::USER_STAT_STATEGRAPH, std::fs::File, std::io::Write, std::path::PathBuf};

/// Adds capabilities to a Monitor to get information about the state-graph.
///
/// All functions are already provided.   
/// You just need to do
/// ```
/// impl HasStateStats for YourMonitor {}
/// ```
/// and then you can invoke the given functions in `YourMonitor::display()`.
pub trait HasStateStats: Monitor {
    /// Helper function used by the other functions.
    fn calculate_average(&mut self, stat: &str) -> u64 {
        let mut sum = 0;
        let stats = self.client_stats_mut();

        for client_stat in stats.iter_mut() {
            if let Some(UserStats::Number(val)) = client_stat.get_user_stats(stat) {
                sum += val;
            }
        }

        sum / stats.len() as u64
    }

    /// Get the average number of vertices in the state-graphs across all instances.
    fn avg_statemachine_nodes(&mut self) -> u64 {
        self.calculate_average(USER_STAT_NODES)
    }

    /// Get the average number of edges in the state-graphs across all instances.
    fn avg_statemachine_edges(&mut self) -> u64 {
        self.calculate_average(USER_STAT_EDGES)
    }
}

/// A monitor that prints information about the state-graph in addition to all other info.
///
/// Works as a drop-in replacement for all other monitors.
#[derive(Clone, Debug)]
pub struct StateMonitor {
    client_stats: Vec<ClientStats>,
    start_time: Duration,
}
impl StateMonitor {
    /// Create a new StateMonitor
    pub fn new() -> Self {
        Self {
            client_stats: Vec::<ClientStats>::new(),
            start_time: current_time(),
        }
    }

    fn max_corpus_size(&self) -> u64 {
        let mut val = 0;

        for client_stat in &self.client_stats {
            val = std::cmp::max(val, client_stat.corpus_size);
        }

        val
    }
}

impl HasStateStats for StateMonitor {}

impl Monitor for StateMonitor {
    fn client_stats_mut(&mut self) -> &mut Vec<ClientStats> {
        &mut self.client_stats
    }

    fn client_stats(&self) -> &[ClientStats] {
        &self.client_stats
    }

    fn start_time(&mut self) -> Duration {
        self.start_time
    }

    fn display(&mut self, msg: String, _sender: u32) {
        let num_nodes = self.avg_statemachine_nodes();
        let num_edges = self.avg_statemachine_edges();
        let corpus_size = self.max_corpus_size();
        let objective_size = self.objective_size();
        let execs = self.total_execs();
        let execs_per_sec = self.execs_per_sec();
        let cores = std::cmp::max(1, self.client_stats.len().saturating_sub(1));

        println!(
            "[butterfly::{}] uptime: {} | cores: {} | corpus: {} | objectives: {} | total execs: {} | exec/s: {} | nodes: {} | edges: {}",
            msg,
            format_duration_hms(&(current_time() - self.start_time)),
            cores,
            corpus_size,
            objective_size,
            execs,
            execs_per_sec,
            num_nodes,
            num_edges,
        );
    }
}

/// A monitor that periodically outputs a DOT representation of the state graph.
///
/// __Only available with feature__: `graphviz`
///
/// If there are multiple fuzzer instances this monitor writes the state graph of
/// each instance to the file separated by linebreaks.
///
/// # Example
/// ```
/// // Writes every 60 seconds into stategraph.dot
/// let monitor = GraphvizMonitor::new(
///    StateMonitor::new(),
///    "stategraph.dot",
///    60,
/// );
/// ```
#[cfg(feature = "graphviz")]
#[derive(Clone, Debug)]
pub struct GraphvizMonitor<M>
where
    M: Monitor,
{
    base: M,
    filename: PathBuf,
    last_update: Duration,
    interval: u64,
}

#[cfg(feature = "graphviz")]
impl<M> GraphvizMonitor<M>
where
    M: Monitor,
{
    /// Creates a new GraphvizMonitor.
    ///
    /// # Arguments
    /// - `monitor`: Other monitor that shall be wrapped
    /// - `filename`: Filename of the dot file
    /// - `interval`: Interval in seconds at which to write to the file
    pub fn new<P>(monitor: M, filename: P, interval: u64) -> Self
    where
        P: Into<PathBuf>,
    {
        Self {
            base: monitor,
            filename: filename.into(),
            last_update: current_time(),
            interval,
        }
    }
}

#[cfg(feature = "graphviz")]
impl<M> Monitor for GraphvizMonitor<M>
where
    M: Monitor,
{
    fn client_stats_mut(&mut self) -> &mut Vec<ClientStats> {
        self.base.client_stats_mut()
    }

    fn client_stats(&self) -> &[ClientStats] {
        self.base.client_stats()
    }

    fn start_time(&mut self) -> Duration {
        self.base.start_time()
    }

    fn display(&mut self, event_msg: String, sender_id: u32) {
        let cur_time = current_time();

        if (cur_time - self.last_update).as_secs() >= self.interval {
            self.last_update = cur_time;

            let mut file = File::create(&self.filename).expect("Failed to open DOT file");

            for stats in self.client_stats_mut() {
                if let Some(UserStats::String(graph)) = stats.get_user_stats(USER_STAT_STATEGRAPH) {
                    writeln!(&mut file, "{}", graph).expect("Failed to write DOT file");
                }
            }
        }

        self.base.display(event_msg, sender_id);
    }
}