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
use cargo_metadata::MetadataCommand;
use std::{any::type_name, env, path::PathBuf};

pub fn corpus_directory_from_args_type<T>() -> PathBuf {
    corpus_directory().join(path_from_args_type::<T>())
}

pub fn corpus_directory_from_target(krate: &str, target: &str) -> PathBuf {
    corpus_directory().join(path_from_target(krate, target))
}

pub fn crashes_directory_from_target(krate: &str, target: &str) -> PathBuf {
    output_directory_from_target(krate, target).join("crashes")
}

pub fn queue_directory_from_target(krate: &str, target: &str) -> PathBuf {
    output_directory_from_target(krate, target).join("queue")
}

pub fn output_directory_from_target(krate: &str, target: &str) -> PathBuf {
    output_directory().join(path_from_target(krate, target))
}

fn corpus_directory() -> PathBuf {
    target_directory(false).join("corpus")
}

fn output_directory() -> PathBuf {
    target_directory(true).join("output")
}

pub fn target_directory(instrumented: bool) -> PathBuf {
    let mut command = MetadataCommand::new();
    if let Ok(path) = env::var("TEST_FUZZ_MANIFEST_PATH") {
        command.manifest_path(path);
    }
    let mut target_dir = command.exec().unwrap().target_directory;
    if instrumented {
        let file_name = target_dir
            .file_name()
            .unwrap()
            .to_string_lossy()
            .to_string();
        target_dir.set_file_name(file_name + "-afl");
    }
    target_dir
}

fn path_from_args_type<T>() -> String {
    type_name::<T>()
        .strip_suffix("_fuzz::Args")
        .unwrap()
        .to_owned()
}

fn path_from_target(krate: &str, target: &str) -> String {
    krate.replace("-", "_") + "::" + target
}