ziggy 1.7.0

A multi-fuzzer management utility for all of your Rust fuzzing needs 🧑‍🎤
Documentation
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
use std::{
    fs,
    path::PathBuf,
    process::{self, ExitStatus},
    sync::{Mutex, MutexGuard},
    thread,
    time::Duration,
};

static EXCLUSIVE: Mutex<()> = Mutex::new(());

fn exclusive_guard() -> MutexGuard<'static, ()> {
    EXCLUSIVE.lock().unwrap_or_else(|e| {
        EXCLUSIVE.clear_poison();
        e.into_inner()
    })
}

fn kill_subprocesses_recursively(pid: &str) {
    let subprocesses = process::Command::new("pgrep")
        .arg(format!("-P{pid}"))
        .output()
        .unwrap();

    for subprocess in std::str::from_utf8(&subprocesses.stdout)
        .unwrap()
        .split('\n')
    {
        if subprocess.is_empty() {
            continue;
        }

        kill_subprocesses_recursively(subprocess);
    }

    println!("Killing pid {pid}");
    unsafe {
        libc::kill(pid.parse::<i32>().unwrap(), libc::SIGTERM);
    }
}

#[allow(clippy::zombie_processes)]
#[test]
fn integration() {
    let _guard = exclusive_guard();
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_dir_path = temp_dir.path();
    let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
    let workspace_root: PathBuf = metadata.workspace_root.into();
    let target_directory: PathBuf = metadata.target_directory.into();
    let cargo_ziggy = target_directory.join("debug").join("cargo-ziggy");
    let fuzzer_directory = workspace_root.join("examples").join("url");

    // TODO Custom target path

    // cargo ziggy build
    let build_status = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("build")
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy build`");

    assert!(build_status.success(), "`cargo ziggy build` failed");

    // cargo ziggy fuzz -j 2 -t 5
    let fuzzer = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("fuzz")
        .arg("-j2")
        .arg("-t5")
        .arg("-G100")
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .env("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES", "1")
        .env("AFL_SKIP_CPUFREQ", "1")
        .current_dir(&fuzzer_directory)
        .spawn()
        .expect("failed to run `cargo ziggy fuzz`");
    thread::sleep(Duration::from_secs(30));
    kill_subprocesses_recursively(&format!("{}", fuzzer.id()));

    assert!(
        temp_dir_path
            .join("url-fuzz")
            .join("afl")
            .join("mainaflfuzzer")
            .join("fuzzer_stats")
            .is_file()
    );
    assert!(
        temp_dir_path
            .join("url-fuzz")
            .join("honggfuzz")
            .join("url-fuzz")
            .join("input")
            .is_dir()
    );

    // We resume fuzzing
    // cargo ziggy fuzz -j 2 -t 5
    let fuzzer = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("fuzz")
        .arg("-j2")
        .arg("-t5")
        .arg("-G100")
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .env("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES", "1")
        .env("AFL_SKIP_CPUFREQ", "1")
        .current_dir(&fuzzer_directory)
        .spawn()
        .expect("failed to run `cargo ziggy fuzz`");
    thread::sleep(Duration::from_secs(30));
    kill_subprocesses_recursively(&format!("{}", fuzzer.id()));

    // cargo ziggy minimize
    let minimization = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("minimize")
        .arg("-j2")
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy minimize`");

    assert!(minimization.success());
    assert!(
        temp_dir_path
            .join("url-fuzz")
            .join("logs")
            .join("minimization_afl.log")
            .is_file()
    );

    fs::remove_dir_all(temp_dir_path.join("url-fuzz").join("corpus_minimized")).unwrap();

    // cargo ziggy minimize -e honggfuzz
    let minimization = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("minimize")
        .arg("-ehonggfuzz")
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy minimize`");

    assert!(minimization.success());
    assert!(
        temp_dir_path
            .join("url-fuzz")
            .join("logs")
            .join("minimization_honggfuzz.log")
            .is_file()
    );

    // cargo ziggy cover
    let coverage = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("cover")
        .args(["-t", "html", "-t", "json", "-t", "text", "-t", "lcov"])
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy cover`");

    assert!(coverage.success());
    for name in ["index.html", "index.txt", "coverage.json", "coverage.lcov"] {
        assert!(
            temp_dir_path
                .join("url-fuzz")
                .join("coverage")
                .join(name)
                .is_file()
        );
    }

    // cargo ziggy plot
    let plot = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("plot")
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy plot`");

    assert!(plot.success());
    assert!(
        temp_dir_path
            .join("url-fuzz")
            .join("plot")
            .join("index.html")
            .is_file()
    );
}

#[allow(clippy::zombie_processes)]
#[test]
fn coverage_regression() {
    let _guard = exclusive_guard();
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_dir_path = temp_dir.path();
    let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
    let workspace_root: PathBuf = metadata.workspace_root.into();
    let target_directory: PathBuf = metadata.target_directory.into();
    let cargo_ziggy = target_directory.join("debug").join("cargo-ziggy");
    let fuzzer_directory = workspace_root.join("examples").join("url");

    // cargo ziggy build
    let build_status = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("build")
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy build`");

    assert!(build_status.success(), "`cargo ziggy build` failed");

    // cargo ziggy fuzz -j 2 -t 5
    let fuzzer = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("fuzz")
        .arg("-j2")
        .arg("-t5")
        .arg("-G100")
        .arg("--no-honggfuzz")
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .env("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES", "1")
        .env("AFL_SKIP_CPUFREQ", "1")
        .current_dir(&fuzzer_directory)
        .spawn()
        .expect("failed to run `cargo ziggy fuzz`");
    thread::sleep(Duration::from_secs(30));
    kill_subprocesses_recursively(&format!("{}", fuzzer.id()));

    // cargo ziggy cover lcov regression test
    let coverage: ExitStatus = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("cover")
        .arg("--output-type")
        .arg("lcov")
        .arg("--output")
        .arg(temp_dir_path.join("url-fuzz").join("cover_lcov"))
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .current_dir(&fuzzer_directory)
        .status()
        .expect("Failed to run lcov coverage");

    let coverage_second: ExitStatus = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("cover")
        .arg("--output-type")
        .arg("lcov")
        .arg("--output")
        .arg(temp_dir_path.join("url-fuzz").join("cover_lcov"))
        .env("ZIGGY_OUTPUT", format!("{}", temp_dir_path.display()))
        .current_dir(&fuzzer_directory)
        .status()
        .expect("Failed to run lcov coverage");

    assert!(coverage.success());
    assert!(coverage_second.success());
    assert!(
        temp_dir_path
            .join("url-fuzz")
            .join("cover_lcov/coverage.lcov")
            .is_file()
    );
}

#[allow(clippy::zombie_processes)]
#[test]
fn fuzz_binary() {
    let _guard = exclusive_guard();
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_dir_path = temp_dir.path();
    let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
    let workspace_root: PathBuf = metadata.workspace_root.into();
    let target_directory: PathBuf = metadata.target_directory.into();
    let cargo_ziggy = target_directory.join("debug/cargo-ziggy");
    let fuzzer_directory = workspace_root.join("examples/url");
    let binary_path = temp_dir_path.join("binary");
    let output_path = temp_dir_path.join("output");

    // cargo ziggy build
    let build_status = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("build")
        .arg("--no-honggfuzz")
        .current_dir(&fuzzer_directory)
        .status()
        .expect("failed to run `cargo ziggy build`");

    assert!(build_status.success(), "`cargo ziggy build` failed");

    std::fs::create_dir_all(temp_dir_path).expect("failed creating output dir");
    std::fs::copy(target_directory.join("afl/debug/url-fuzz"), &binary_path)
        .expect("failed to move instrumented binary into output dir");

    // cargo ziggy fuzz -j 2 -t 5
    let fuzzer = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("fuzz")
        .arg("-b")
        .arg(&binary_path)
        .arg("--no-honggfuzz")
        .arg("-j2")
        .arg("-t5")
        .arg("-G100")
        .env("ZIGGY_OUTPUT", &output_path)
        .env("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES", "1")
        .env("AFL_SKIP_CPUFREQ", "1")
        .current_dir(temp_dir_path)
        .spawn()
        .expect("failed to run `cargo ziggy fuzz`");
    thread::sleep(Duration::from_secs(30));
    kill_subprocesses_recursively(&format!("{}", fuzzer.id()));

    assert!(
        output_path
            .join("binary/afl/mainaflfuzzer/fuzzer_stats")
            .is_file()
    );

    // We resume fuzzing
    // cargo ziggy fuzz -j 2 -t 5
    let fuzzer = process::Command::new(&cargo_ziggy)
        .arg("ziggy")
        .arg("fuzz")
        .arg("-b")
        .arg(&binary_path)
        .arg("--no-honggfuzz")
        .arg("-j2")
        .arg("-t5")
        .arg("-G100")
        .env("ZIGGY_OUTPUT", &output_path)
        .env("AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES", "1")
        .env("AFL_SKIP_CPUFREQ", "1")
        .current_dir(temp_dir_path)
        .spawn()
        .expect("failed to run `cargo ziggy fuzz`");
    thread::sleep(Duration::from_secs(30));
    kill_subprocesses_recursively(&format!("{}", fuzzer.id()));
}

#[allow(clippy::zombie_processes)]
#[test]
fn clean() {
    let _guard = exclusive_guard();
    let temp_dir = tempfile::tempdir().unwrap();
    let temp_dir_path = temp_dir.path();
    let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
    let workspace_root: PathBuf = metadata.workspace_root.into();
    let target_directory: PathBuf = metadata.target_directory.into();
    let cargo_ziggy = target_directory.join("debug/cargo-ziggy");
    let fuzzer_directory = workspace_root.join("examples/url");

    {
        let afl_build_path = target_directory.join("afl/debug/url-fuzz");
        let hfuzz_build_path = target_directory.join(format!(
            "honggfuzz/{}/release/url-fuzz",
            target_triple::TARGET
        ));

        // cargo ziggy build
        let build_status = process::Command::new(&cargo_ziggy)
            .arg("ziggy")
            .arg("build")
            .current_dir(&fuzzer_directory)
            .status()
            .expect("failed to run `cargo ziggy build`");

        assert!(build_status.success(), "`cargo ziggy build` failed");
        assert!(afl_build_path.is_file(), "no afl harness");
        assert!(hfuzz_build_path.is_file(), "no honggfuzz harness");

        let clean_status = process::Command::new(&cargo_ziggy)
            .args(["ziggy", "clean", "-p", "url-fuzz"])
            .current_dir(&fuzzer_directory)
            .status()
            .expect("failed to run `cargo ziggy clean`");
        assert!(clean_status.success(), "`cargo ziggy clean` failed");
        assert!(!afl_build_path.exists(), "afl harness not cleaned");
        assert!(!hfuzz_build_path.exists(), "honggfuzz harness not cleaned");
    }

    {
        // use temp_dir_path as target-dir
        let afl_build_path = temp_dir_path.join("afl/debug/url-fuzz");
        let hfuzz_build_path = temp_dir_path.join(format!(
            "honggfuzz/{}/release/url-fuzz",
            target_triple::TARGET
        ));

        // cargo ziggy build
        let build_status = process::Command::new(&cargo_ziggy)
            .arg("ziggy")
            .arg("build")
            .env("CARGO_TARGET_DIR", temp_dir_path)
            .current_dir(&fuzzer_directory)
            .status()
            .expect("failed to run `cargo ziggy build`");

        assert!(build_status.success(), "`cargo ziggy build` failed");
        assert!(afl_build_path.is_file(), "no afl harness");
        assert!(hfuzz_build_path.is_file(), "no honggfuzz harness");

        let clean_status = process::Command::new(&cargo_ziggy)
            .args(["ziggy", "clean", "-p", "url-fuzz"])
            .env("CARGO_TARGET_DIR", temp_dir_path)
            .current_dir(&fuzzer_directory)
            .status()
            .expect("failed to run `cargo ziggy clean`");
        assert!(clean_status.success(), "`cargo ziggy clean` failed");
        assert!(!afl_build_path.exists(), "afl harness not cleaned");
        assert!(!hfuzz_build_path.exists(), "honggfuzz harness not cleaned");
    }
}