trident_fuzz/config/
afl.rs

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
use std::collections::HashMap;

use crate::config::constants::*;
use rand::RngCore;
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone, Hash, PartialEq, Eq)]
pub enum BuildArgument {
    CargoTargetDir,
}

#[derive(Debug, Deserialize, Clone, Hash, PartialEq, Eq)]
pub enum FuzzArgument {
    AflWorkspaceIn,
    AflWorkspaceOut,
    Execs,
    Seconds,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Afl {
    pub build_args: HashMap<BuildArgument, AflArg>,
    pub fuzz_args: HashMap<FuzzArgument, AflArg>,
    pub seeds: Vec<AflSeed>,
}

impl Afl {
    pub fn get_cargo_build_dir(&self) -> Option<&AflArg> {
        self.build_args.get(&BuildArgument::CargoTargetDir)
    }
    pub fn get_workspace_in(&self) -> Option<&AflArg> {
        self.fuzz_args.get(&FuzzArgument::AflWorkspaceIn)
    }
    pub fn get_workspace_out(&self) -> Option<&AflArg> {
        self.fuzz_args.get(&FuzzArgument::AflWorkspaceOut)
    }
    pub fn get_execs(&self) -> Option<&AflArg> {
        self.fuzz_args.get(&FuzzArgument::Execs)
    }
    pub fn get_seconds(&self) -> Option<&AflArg> {
        self.fuzz_args.get(&FuzzArgument::Seconds)
    }
    pub fn get_collect_build_args(&self) -> Vec<String> {
        self.build_args
            .values()
            .flat_map(|arg| {
                let val = arg.val.clone().unwrap_or_default();
                if let Some(opt) = &arg.short_opt {
                    vec![opt.clone(), val]
                } else if let Some(opt) = &arg.long_opt {
                    vec![opt.clone(), val]
                } else {
                    vec![]
                }
            })
            .collect()
    }
    pub fn get_collect_fuzz_args(&self) -> Vec<String> {
        self.fuzz_args
            .values()
            .flat_map(|arg| {
                let val = arg.val.clone().unwrap_or_default();
                if let Some(opt) = &arg.short_opt {
                    vec![opt.clone(), val]
                } else if let Some(opt) = &arg.long_opt {
                    vec![opt.clone(), val]
                } else {
                    vec![]
                }
            })
            .collect()
    }
}

#[derive(Default, Debug, Deserialize, Clone)]
pub struct _Afl {
    #[serde(default)]
    pub cargo_target_dir: Option<String>,
    #[serde(default)]
    pub afl_workspace_in: Option<String>,
    #[serde(default)]
    pub afl_workspace_out: Option<String>,
    #[serde(default)]
    pub execs: Option<u64>,
    #[serde(default)]
    pub seconds: Option<u64>,
    #[serde(default)]
    pub seeds: Option<Vec<_AflSeed>>,
}

impl From<_Afl> for Afl {
    fn from(_f: _Afl) -> Self {
        let mut _self = Self {
            seeds: vec![],
            fuzz_args: HashMap::new(),
            build_args: HashMap::new(),
        };

        // cargo_target_dir
        let cargo_target_dir = _f
            .cargo_target_dir
            .and_then(|value| if value.is_empty() { None } else { Some(value) })
            .unwrap_or(CARGO_TARGET_DIR_DEFAULT_AFL.to_owned());

        _self.build_args.insert(
            BuildArgument::CargoTargetDir,
            AflArg::new("", "--target-dir", &cargo_target_dir),
        );

        // afl_workspace_in
        let afl_workspace_in = _f
            .afl_workspace_in
            .and_then(|value| if value.is_empty() { None } else { Some(value) })
            .unwrap_or(AFL_WORKSPACE_DEFAULT_IN.to_owned());

        _self.fuzz_args.insert(
            FuzzArgument::AflWorkspaceIn,
            AflArg::new("-i", "", &afl_workspace_in),
        );

        // afl_workspace_out
        let afl_workspace_out = _f
            .afl_workspace_out
            .and_then(|value| if value.is_empty() { None } else { Some(value) })
            .unwrap_or(AFL_WORKSPACE_DEFAULT_OUT.to_owned());

        _self.fuzz_args.insert(
            FuzzArgument::AflWorkspaceOut,
            AflArg::new("-o", "", &afl_workspace_out),
        );

        // execs
        let execs = _f.execs.unwrap_or(0);
        if execs > 0 {
            _self.fuzz_args.insert(
                FuzzArgument::Execs,
                AflArg::new("-E", "", &execs.to_string()),
            );
        }

        // seconds
        let seconds = _f.seconds.unwrap_or(0);
        if seconds > 0 {
            _self.fuzz_args.insert(
                FuzzArgument::Seconds,
                AflArg::new("-V", "", &seconds.to_string()),
            );
        }

        if let Some(seeds) = _f.seeds {
            for x in seeds {
                _self.seeds.push(x.into());
            }
        } else {
            _self.seeds.push(_AflSeed::default().into());
        }

        _self
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct AflArg {
    pub short_opt: Option<String>,
    pub long_opt: Option<String>,
    pub val: Option<String>,
}
impl AflArg {
    pub fn new(short_opt: &str, long_opt: &str, val: &str) -> Self {
        let short_opt = if short_opt.is_empty() {
            None
        } else {
            Some(short_opt.to_owned())
        };
        let long_opt = if long_opt.is_empty() {
            None
        } else {
            Some(long_opt.to_owned())
        };
        let val = if val.is_empty() {
            None
        } else {
            Some(val.to_owned())
        };
        Self {
            short_opt,
            long_opt,
            val,
        }
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct _AflSeed {
    pub file_name: String,
    pub seed: Option<String>,
    pub override_file: Option<bool>,
    pub bytes_count: Option<usize>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct AflSeed {
    pub file_name: String,
    pub seed: Vec<u8>,
    pub override_file: bool,
}

impl Default for _AflSeed {
    fn default() -> Self {
        Self {
            file_name: DEFAULT_SEED_FILENAME.to_string(),
            seed: Some(DEFAULT_SEED.to_string()),
            override_file: Some(false),
            bytes_count: None,
        }
    }
}

impl From<_AflSeed> for AflSeed {
    fn from(value: _AflSeed) -> Self {
        match value.bytes_count {
            Some(number_of_random_bytes) => {
                if number_of_random_bytes > 0 {
                    let mut rng = rand::rngs::OsRng;
                    let mut seed = vec![0u8; number_of_random_bytes];
                    rng.fill_bytes(&mut seed);
                    Self {
                        file_name: value.file_name,
                        seed,
                        override_file: value.override_file.unwrap_or_default(),
                    }
                } else {
                    let seed_as_bytes = value
                        .seed
                        .and_then(|value| if value.is_empty() { None } else { Some(value) })
                        .unwrap_or(DEFAULT_SEED.to_string())
                        .as_bytes()
                        .to_vec();
                    Self {
                        file_name: value.file_name,
                        seed: seed_as_bytes,
                        override_file: value.override_file.unwrap_or_default(),
                    }
                }
            }
            None => {
                let seed_as_bytes = value
                    .seed
                    .and_then(|value| if value.is_empty() { None } else { Some(value) })
                    .unwrap_or(DEFAULT_SEED.to_string())
                    .as_bytes()
                    .to_vec();
                Self {
                    file_name: value.file_name,
                    seed: seed_as_bytes,
                    override_file: value.override_file.unwrap_or_default(),
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    impl Afl {
        fn clean() -> Self {
            Self {
                fuzz_args: HashMap::new(),
                build_args: HashMap::new(),
                seeds: vec![],
            }
        }
    }

    #[test]
    fn test_cargo_target_dir() {
        let mut afl = Afl::clean();

        afl.build_args.insert(
            BuildArgument::CargoTargetDir,
            AflArg::new("", "--target-dir", CARGO_TARGET_DIR_DEFAULT_AFL),
        );

        let arg = afl.get_collect_build_args();
        assert_eq!(arg, vec!["--target-dir", CARGO_TARGET_DIR_DEFAULT_AFL]);
    }
    #[test]
    fn test_workspace_in() {
        let mut afl = Afl::clean();

        // afl_workspace_in
        afl.fuzz_args.insert(
            FuzzArgument::AflWorkspaceIn,
            AflArg::new("-i", "", AFL_WORKSPACE_DEFAULT_IN),
        );

        let arg = afl.get_collect_fuzz_args();
        assert_eq!(arg, vec!["-i", AFL_WORKSPACE_DEFAULT_IN]);
    }
    #[test]
    fn test_workspace_out() {
        let mut afl = Afl::clean();

        // afl_workspace_out
        afl.fuzz_args.insert(
            FuzzArgument::AflWorkspaceOut,
            AflArg::new("-o", "", AFL_WORKSPACE_DEFAULT_OUT),
        );

        let arg = afl.get_collect_fuzz_args();
        assert_eq!(arg, vec!["-o", AFL_WORKSPACE_DEFAULT_OUT]);
    }
    #[test]
    fn test_execs() {
        let mut afl = Afl::clean();

        // execs
        afl.fuzz_args
            .insert(FuzzArgument::Execs, AflArg::new("-E", "", &555.to_string()));

        let arg = afl.get_collect_fuzz_args();
        assert_eq!(arg, vec!["-E", "555"]);
    }
    #[test]
    fn test_seconds() {
        let mut afl = Afl::clean();

        // seconds
        afl.fuzz_args.insert(
            FuzzArgument::Seconds,
            AflArg::new("-V", "", &15.to_string()),
        );

        let arg = afl.get_collect_fuzz_args();
        assert_eq!(arg, vec!["-V", "15"]);
    }
}