puffin/
experiment.rs

1use std::fmt::Display;
2use std::fs::File;
3use std::io::Write;
4use std::path::Path;
5use std::{fs, io};
6
7use chrono::Local;
8use clap::ArgMatches;
9use itertools::Itertools;
10use libafl_bolts::bolts_prelude::Cores;
11use puffin_build::puffin;
12
13use crate::fuzzer::config::{FuzzerConfig, MutationStageConfig};
14use crate::fuzzer::mutations::MutationConfig;
15use crate::protocol::ProtocolBehavior;
16use crate::put_registry::PutRegistry;
17
18#[must_use]
19pub fn format_title<PB: ProtocolBehavior>(
20    title: Option<&str>,
21    index: Option<usize>,
22    put_registry: &PutRegistry<PB>,
23    fuzzer_config: &FuzzerConfig,
24) -> String {
25    let FuzzerConfig {
26        mutation_config,
27        mutation_stage_config,
28        core_definition,
29        put_options,
30        minimizer,
31        ..
32    } = fuzzer_config;
33    let MutationStageConfig {
34        with_truncation,
35        step_locked_stacking,
36        ..
37    } = mutation_stage_config;
38    let MutationConfig {
39        with_bit_level,
40        with_dy,
41        with_focus,
42        ..
43    } = mutation_config;
44    let num_cores = Cores::from_cmdline(core_definition.as_str())
45        .unwrap()
46        .ids
47        .len();
48
49    let date = Local::now().format("%Y-%m-%d");
50    let hour = Local::now().format("%H-%M-%S");
51    let with_bit_level = if *with_bit_level { "_with-bit" } else { "" };
52    let without_dy_mutations = if !*with_dy { "_wo-dy" } else { "" };
53    let without_focus = if !*with_focus { "_wo-focus" } else { "" };
54    let with_truncation = if *with_truncation { "_with-trunc" } else { "" };
55    let step_lock = if *step_locked_stacking {
56        "_steplock"
57    } else {
58        ""
59    };
60    let minimizer = if *minimizer { "_with_minimizer" } else { "" };
61    let option_string = format!(
62        "_put-options-{}",
63        put_options
64            .iter()
65            .map(|(key, val)| format!("{}:{}", key, val))
66            .join("-")
67    );
68    let with_put_options = if !put_options.is_empty() {
69        option_string.as_str()
70    } else {
71        ""
72    };
73    let default_put: &str = &put_registry
74        .default()
75        .versions()
76        .last()
77        .unwrap()
78        .1
79        .split_whitespace()
80        .join("-");
81    format!(
82        "{date}\
83        --{default_put}-{num_cores}c{with_bit_level}{without_dy_mutations}{without_focus}{with_truncation}{step_lock}{minimizer}{with_put_options}\
84        {title}--{hour}--{index}",
85        date = date,
86        title = title.unwrap_or(&puffin::git_ref().unwrap_or_default()),
87        index = index.unwrap_or(0)
88    )
89}
90
91pub fn write_experiment_markdown<PB: ProtocolBehavior>(
92    directory: &Path,
93    title: impl Display,
94    description_text: impl Display,
95    put_registry: &PutRegistry<PB>,
96    commands: &ArgMatches,
97    port: u16,
98) -> Result<String, io::Error> {
99    let full_description = format!(
100        "# Experiment: {title}\n\
101                * PUT Versions: {put_versions}\n\
102                * Default PUT: {default_put}\n\
103                * Date: {date}\n\
104                * Git Ref: {git_ref}\n\
105                * Git Commit: {git_msg}\n\
106                * Launched with: {command:?}\n\
107                * Port: {port}\n\
108                * Log: [tlspuffin.log](./tlspuffin.log)\n\n\
109                {description}\n",
110        title = &title,
111        default_put = &put_registry
112            .default()
113            .versions()
114            .last()
115            .unwrap()
116            .1
117            .split_whitespace()
118            .join("-"),
119        put_versions = put_registry
120            .puts()
121            .map(|(n, p)| format!(
122                "{} ({})",
123                n,
124                p.versions()
125                    .into_iter()
126                    .map(|(c, v)| format!("{c} ({v})"))
127                    .join(" ")
128            ))
129            .join(", "),
130        date = Local::now().to_rfc3339(),
131        git_ref = puffin::git_ref().as_deref().unwrap_or("unknown"),
132        git_msg = puffin::git_msg().as_deref().unwrap_or("unknown"),
133        command = commands,
134        description = description_text
135    );
136
137    fs::create_dir_all(directory)?;
138
139    let mut file = File::create(directory.join("README.md")).unwrap();
140
141    file.write_all(full_description.as_bytes()).unwrap();
142    Ok(full_description)
143}