From 0a396424358e547cae95f792e0b18ff04d0eb3fa Mon Sep 17 00:00:00 2001 From: hkctkuy Date: Wed, 11 Oct 2023 15:25:40 +0300 Subject: [PATCH] Add force-remove option --- casr/src/bin/casr-afl.rs | 7 +++++++ casr/src/bin/casr-libfuzzer.rs | 7 +++++++ casr/src/bin/casr-ubsan.rs | 16 +++++++++++++++- casr/src/util.rs | 9 ++++++++- docs/usage.md | 4 ++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/casr/src/bin/casr-afl.rs b/casr/src/bin/casr-afl.rs index 2199e753..5cee25bd 100644 --- a/casr/src/bin/casr-afl.rs +++ b/casr/src/bin/casr-afl.rs @@ -75,6 +75,13 @@ fn main() -> Result<()> { .value_parser(clap::value_parser!(PathBuf)) .help("Output directory with triaged reports") ) + .arg( + Arg::new("force-remove") + .short('f') + .long("force-remove") + .action(ArgAction::SetTrue) + .help("Remove output project directory if it exists") + ) .arg( Arg::new("ignore-cmdline") .action(ArgAction::SetTrue) diff --git a/casr/src/bin/casr-libfuzzer.rs b/casr/src/bin/casr-libfuzzer.rs index 465a4657..38626a93 100644 --- a/casr/src/bin/casr-libfuzzer.rs +++ b/casr/src/bin/casr-libfuzzer.rs @@ -74,6 +74,13 @@ fn main() -> Result<()> { .value_name("OUTPUT_DIR") .help("Output directory with triaged reports") ) + .arg( + Arg::new("force-remove") + .short('f') + .long("force-remove") + .action(ArgAction::SetTrue) + .help("Remove output project directory if it exists") + ) .arg( Arg::new("no-cluster") .action(ArgAction::SetTrue) diff --git a/casr/src/bin/casr-ubsan.rs b/casr/src/bin/casr-ubsan.rs index 80823055..00d1b483 100644 --- a/casr/src/bin/casr-ubsan.rs +++ b/casr/src/bin/casr-ubsan.rs @@ -274,6 +274,13 @@ fn main() -> Result<()> { .value_parser(clap::value_parser!(PathBuf)) .help("Output directory with triaged reports") ) + .arg( + Arg::new("force-remove") + .short('f') + .long("force-remove") + .action(ArgAction::SetTrue) + .help("Remove output project directory if it exists") + ) .arg( Arg::new("ARGS") .action(ArgAction::Set) @@ -298,7 +305,14 @@ fn main() -> Result<()> { } else if !output_dir.is_dir() { bail!("Output directory must be a directory"); } else if output_dir.read_dir()?.next().is_some() { - bail!("Output directory is not empty."); + if matches.get_flag("force-remove") { + fs::remove_dir_all(output_dir)?; + fs::create_dir_all(output_dir).with_context(|| { + format!("Couldn't create output directory {}", output_dir.display()) + })?; + } else { + bail!("Output directory is not empty."); + } } // Get program args. let argv: Vec<&str> = if let Some(argvs) = matches.get_many::("ARGS") { diff --git a/casr/src/util.rs b/casr/src/util.rs index fbb3f591..7275db62 100644 --- a/casr/src/util.rs +++ b/casr/src/util.rs @@ -318,7 +318,14 @@ pub fn initialize_dirs(matches: &clap::ArgMatches) -> Result<&PathBuf> { format!("Couldn't create output directory {}", output_dir.display()) })?; } else if output_dir.read_dir()?.next().is_some() { - bail!("Output directory is not empty."); + if matches.get_flag("force-remove") { + fs::remove_dir_all(output_dir)?; + fs::create_dir_all(output_dir).with_context(|| { + format!("Couldn't create output directory {}", output_dir.display()) + })?; + } else { + bail!("Output directory is not empty."); + } } // Get oom dir let oom_dir = output_dir.join("oom"); diff --git a/docs/usage.md b/docs/usage.md index 884473c5..a7759fd6 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -94,6 +94,7 @@ Triage errors found by UndefinedBehaviorSanitizer and create CASR reports (.casr that timeout is disabled [default: 0] -i, --input ... Target input directory list -o, --output Output directory with triaged reports + -f, --force-remove Remove output project directory if it exists -h, --help Print help -V, --version Print version @@ -363,6 +364,7 @@ Triage crashes found by AFL++ that timeout is disabled [default: 0] -i, --input AFL++ work directory -o, --output Output directory with triaged reports + -f, --force-remove Remove output project directory if it exists --ignore-cmdline Force usage to run target instead of searching for cmdline files in AFL fuzzing directory --no-cluster Do not cluster CASR reports @@ -474,6 +476,8 @@ Triage crashes found by libFuzzer based fuzzer (C/C++/go-fuzz/Atheris/Jazzer) Directory containing crashes found by libFuzzer [default: .] -o, --output Output directory with triaged reports + -f, --force-remove + Remove output project directory if it exists --no-cluster Do not cluster CASR reports --casr-gdb-args