From 65ddfa6acf9518cecc2c6c3b960a3f251abb5cd4 Mon Sep 17 00:00:00 2001 From: Abc Xyz Date: Sat, 4 Nov 2023 19:54:35 +0300 Subject: [PATCH] drcov_rt: make coverage file names unique (#1581) * fix(drcov_rt): coverage files are overwritten if have the same names Make it unique. * fix(drcov_rt): use coverage and input as a filename, skip empty covs --- libafl_frida/src/drcov_rt.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/libafl_frida/src/drcov_rt.rs b/libafl_frida/src/drcov_rt.rs index 1148f39c1f..6c76d31f26 100644 --- a/libafl_frida/src/drcov_rt.rs +++ b/libafl_frida/src/drcov_rt.rs @@ -22,13 +22,13 @@ use crate::helper::FridaRuntime; pub struct DrCovRuntime { /// The basic blocks of this execution pub drcov_basic_blocks: Vec, - /// The memory ragnes of this target + /// The memory ranges of this target ranges: RangeMap, coverage_directory: PathBuf, } impl FridaRuntime for DrCovRuntime { - /// initializes this runtime wiith the given `ranges` + /// initializes this runtime with the given `ranges` fn init( &mut self, _gum: &frida_gum::Gum, @@ -46,14 +46,27 @@ impl FridaRuntime for DrCovRuntime { } /// Called after execution, writes the trace to a unique `DrCov` file for this trace - /// into `./coverage/.drcov` + /// into `./coverage/_.drcov`. Empty coverages will be skipped. fn post_exec(&mut self, input: &I) -> Result<(), Error> { - let mut hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher(); - hasher.write(input.target_bytes().as_slice()); + // We don't need empty coverage files + if self.drcov_basic_blocks.is_empty() { + return Ok(()); + } + + let mut input_hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher(); + input_hasher.write(input.target_bytes().as_slice()); + let input_hash = input_hasher.finish(); + + let mut coverage_hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher(); + for bb in &self.drcov_basic_blocks { + coverage_hasher.write_usize(bb.start); + coverage_hasher.write_usize(bb.end); + } + let coverage_hash = coverage_hasher.finish(); let filename = self .coverage_directory - .join(format!("{:016x}.drcov", hasher.finish(),)); + .join(format!("{input_hash:016x}_{coverage_hash:016x}.drcov")); DrCovWriter::new(&self.ranges).write(filename, &self.drcov_basic_blocks)?; self.drcov_basic_blocks.clear();