Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
R9295 committed Jun 20, 2024
1 parent f9d508d commit d34df09
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
19 changes: 12 additions & 7 deletions src/bin/cargo-ziggy/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ impl Cover {

if !self.keep {
// We remove the previous coverage files
if let Ok(gcda_files) = glob("target/coverage/debug/deps/*.gcda") {
for file in gcda_files.flatten() {
let file_string = &file.display();
fs::remove_file(&file)
.context(format!("⚠️ couldn't find {} during coverage", file_string))?;
}
}
Cover::clean_old_cov()?;
}

let mut shared_corpus = PathBuf::new();
Expand Down Expand Up @@ -135,4 +129,15 @@ impl Cover {
.context("⚠️ couldn't wait for the grcov process")?;
Ok(())
}

pub fn clean_old_cov() -> Result<(), anyhow::Error> {
if let Ok(gcda_files) = glob("target/coverage/debug/deps/*.gcda") {
for file in gcda_files.flatten() {
let file_string = &file.display();
fs::remove_file(&file)
.context(format!("⚠️ couldn't find {} during coverage", file_string))?;
}
}
Ok(())
}
}
53 changes: 32 additions & 21 deletions src/bin/cargo-ziggy/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ impl Fuzz {
let mut afl_output_ok = false;

if self.no_afl && self.coverage_worker {
return Err(anyhow!("cannot use --no-afl with --coverage-worker!"))
return Err(anyhow!("cannot use --no-afl with --coverage-worker!"));
}

let (cov_worker, cov_worker_messages_rx) = match self.coverage_worker {
true => {
info!("cleaning old coverage files");
Cover::clean_old_cov()?;

// build coverage the runner
info!("building coverage worker");
Cover::build_runner()?;
Expand All @@ -154,7 +157,10 @@ impl Fuzz {
.to_string();
let coverage_interval = self.coverage_interval;
let target = self.target.clone();
let main_corpus = PathBuf::from(self.output_target()).join("afl").join("mainaflfuzzer").join("queue");
let main_corpus = PathBuf::from(self.output_target())
.join("afl")
.join("mainaflfuzzer")
.join("queue");
let mut cov_worker_last_run = None;
let (cov_tx, mut cov_rx) = channel::<()>();
let (cov_message_tx, cov_message_rx) = channel::<String>();
Expand All @@ -167,16 +173,13 @@ impl Fuzz {
}
cov_message_tx
.send(format!("sleeping for {} minutes", coverage_interval))?;
/* thread::sleep(Duration::from_secs(coverage_interval * 60)); */
if should_thread_exit(&mut cov_rx)? {
return Ok(());
}
cov_message_tx.send(format!("i am awake"))?;
thread::sleep(Duration::from_secs(coverage_interval * 60));
cov_message_tx.send("i am awake".to_string())?;
let entries = std::fs::read_dir(&main_corpus)?;
let last_run = SystemTime::now();
let mut new_entries = 0;
for entry in entries {
if !entry.is_ok() {
if entry.is_err() {
continue;
}
let entry = entry.unwrap().path();
Expand Down Expand Up @@ -204,7 +207,7 @@ impl Fuzz {
}
}
if new_entries > 0 {
cov_message_tx.send(format!("generating grcov report"))?;
cov_message_tx.send("generating grcov report".to_string())?;
Cover::run_grcov(&target, "html", "coverage", &workspace_root)?;
}
cov_worker_last_run = Some(last_run);
Expand All @@ -219,20 +222,28 @@ impl Fuzz {
}
false => (None, None),
};
let mut cov_worker_messages = VecDeque::new();
let mut cov_worker_messages = VecDeque::<String>::new();
loop {
let sleep_duration = Duration::from_secs(1);
thread::sleep(sleep_duration);
if self.coverage_worker {
match kill_channel.try_recv() {
Ok(_) => {
// We got ctrl-c time to exit!
info!("Waiting for coverage worker to exit!");
let (cov_worker, cov_tx) = cov_worker.unwrap();
cov_tx.send(())?;
cov_worker
.join()
.expect("the cov_worker exited before we could join!")?;
println!("Waiting for coverage worker to exit;");
// If the cov worker is sleeping, we can kill it, no bad side effects will happen.
if !cov_worker_messages
.iter()
.last()
.expect("cov worker should have sent a message!")
.starts_with("sleeping")
{
let (cov_worker, cov_tx) = cov_worker.unwrap();
cov_tx.send(())?;
cov_worker
.join()
.expect("the cov_worker exited before we could join!")?;
}
return Ok(());
}
Err(TryRecvError::Disconnected) => {
Expand All @@ -251,7 +262,7 @@ impl Fuzz {
cov_worker_messages.push_back(message)
}
Err(TryRecvError::Empty) => {
/* break; */
break;
}
Err(TryRecvError::Disconnected) => {
return Err(anyhow!(
Expand Down Expand Up @@ -893,13 +904,13 @@ impl Fuzz {
}
if self.coverage_worker {
screen += &format!(
"β”œβ”€ {blue}coverage-worker{hf_status:0}β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜\n"
"β”œβ”€ {blue}coverage-worker{reset}β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜\n"
);
for message in cov_worker_messages {
screen += &format!("β”œβ”€ {message}\n");
}
}
screen += "β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜";
screen += "β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜";
eprintln!("{screen}");
}
}
Expand Down Expand Up @@ -977,10 +988,10 @@ fn should_thread_exit<T>(rx: &mut Receiver<T>) -> Result<bool, anyhow::Error> {
match rx.try_recv() {
Ok(_) => {
// We got ctrl-c time to exit!
return Ok(true);
Ok(true)
}
Err(TryRecvError::Disconnected) => {
return Err(anyhow!("channel disconnected; this should not happen!"))
Err(anyhow!("channel disconnected; this should not happen!"))
}
Err(TryRecvError::Empty) => Ok(false),
}
Expand Down

0 comments on commit d34df09

Please sign in to comment.