From a182101700b8f3d098e66bf231ab9b811aad6059 Mon Sep 17 00:00:00 2001 From: hkctkuy Date: Mon, 21 Aug 2023 16:37:28 +0300 Subject: [PATCH] add error handling --- casr/src/bin/casr-afl.rs | 56 +++++++++++++++++++++------------- casr/src/bin/casr-libfuzzer.rs | 21 ++++++++----- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/casr/src/bin/casr-afl.rs b/casr/src/bin/casr-afl.rs index 27d72063..908be059 100644 --- a/casr/src/bin/casr-afl.rs +++ b/casr/src/bin/casr-afl.rs @@ -275,16 +275,22 @@ fn main() -> Result<()> { info!("Using {} threads", num_of_threads - 1); let counter = RwLock::new(0_usize); let total = crashes.len(); - custom_pool.join( - || { - let _ = crashes.par_iter().try_for_each(|(_, crash)| { - crash.run_casr(output_dir.as_path(), timeout)?; - *counter.write().unwrap() += 1; - Ok::<(), anyhow::Error>(()) - }); - }, - || util::log_progress(&counter, total), - ); + custom_pool + .join( + || { + crashes.par_iter().try_for_each(|(_, crash)| { + let Ok(_) = crash.run_casr(output_dir.as_path(), timeout) else { + // Disable util::log_progress + *counter.write().unwrap() = total; + bail!("Casr run error"); + }; + *counter.write().unwrap() += 1; + Ok::<(), anyhow::Error>(()) + }) + }, + || util::log_progress(&counter, total), + ) + .0?; // Deduplicate reports. if output_dir.read_dir()?.count() < 2 { @@ -385,7 +391,7 @@ fn summarize_results( .filter(|e| e.extension().is_none() || e.extension().unwrap() != "casrep") .filter(|e| !Path::new(format!("{}.gdb.casrep", e.display()).as_str()).exists()) .collect(); - let num_of_threads = jobs.min(crashes.len()); + let num_of_threads = jobs.min(crashes.len() + 1); if num_of_threads > 1 { info!("casr-gdb: adding crash reports..."); info!("Using {} threads", num_of_threads - 1); @@ -396,22 +402,28 @@ fn summarize_results( .build() .unwrap(); let at_index = gdb_args.iter().skip(1).position(|s| s.contains("@@")); - custom_pool.join( - || { - let _ = crashes.par_iter().try_for_each(|crash| { - AflCrashInfo { + custom_pool + .join( + || { + crashes.par_iter().try_for_each(|crash| { + let Ok(_) = AflCrashInfo { path: crash.to_path_buf(), target_args: gdb_args.clone(), at_index, is_asan: false, } - .run_casr(None, timeout)?; - *counter.write().unwrap() += 1; - Ok::<(), anyhow::Error>(()) - }); - }, - || util::log_progress(&counter, total), - ); + .run_casr(None, timeout) else { + // Disable util::log_progress + *counter.write().unwrap() = total; + bail!("Casr run error"); + }; + *counter.write().unwrap() += 1; + Ok::<(), anyhow::Error>(()) + }) + }, + || util::log_progress(&counter, total), + ) + .0?; } } diff --git a/casr/src/bin/casr-libfuzzer.rs b/casr/src/bin/casr-libfuzzer.rs index c44c92af..ee250657 100644 --- a/casr/src/bin/casr-libfuzzer.rs +++ b/casr/src/bin/casr-libfuzzer.rs @@ -177,9 +177,10 @@ fn main() -> Result<()> { } else { "casr-san" }; - custom_pool.join( - || { - let _ = crashes.par_iter().try_for_each(|(crash, fname)| { + custom_pool + .join( + || { + let Ok (_) = crashes.par_iter().try_for_each(|(crash, fname)| { let mut casr_cmd = Command::new(tool); if timeout != 0 { casr_cmd.args(["-t".to_string(), timeout.to_string()]); @@ -212,10 +213,16 @@ fn main() -> Result<()> { } *counter.write().unwrap() += 1; Ok::<(), anyhow::Error>(()) - }); - }, - || util::log_progress(&counter, total), - ); + }) else { + // Disable util::log_progress + *counter.write().unwrap() = total; + bail!("Casr run error"); + }; + Ok(()) + }, + || util::log_progress(&counter, total), + ) + .0?; // Deduplicate reports. if output_dir.read_dir()?.count() < 2 {