Skip to content

Commit 5854fd0

Browse files
authored
fix(libafl_frida): correctly calculate the coverage using DrCov (#1579)
1 parent ee9eb3e commit 5854fd0

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

libafl_frida/src/helper.rs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use capstone::{
1313
};
1414
#[cfg(unix)]
1515
use frida_gum::instruction_writer::InstructionWriter;
16-
#[cfg(unix)]
17-
use frida_gum::CpuContext;
1816
use frida_gum::{
1917
stalker::{StalkerIterator, StalkerOutput, Transformer},
2018
Gum, Module, ModuleDetails, ModuleMap, PageProtection,
@@ -379,16 +377,6 @@ pub fn get_module_size(module_name: &str) -> usize {
379377
code_size
380378
}
381379

382-
#[cfg(target_arch = "aarch64")]
383-
fn pc(context: &CpuContext) -> usize {
384-
context.pc() as usize
385-
}
386-
387-
#[cfg(all(target_arch = "x86_64", unix))]
388-
fn pc(context: &CpuContext) -> usize {
389-
context.rip() as usize
390-
}
391-
392380
fn pathlist_contains_module<I, P>(list: I, module: &ModuleDetails) -> bool
393381
where
394382
I: IntoIterator<Item = P>,
@@ -490,6 +478,8 @@ where
490478
#[cfg(any(target_arch = "aarch64", all(target_arch = "x86_64", unix)))] capstone: &Capstone,
491479
) {
492480
let mut first = true;
481+
let mut basic_block_start = 0;
482+
let mut basic_block_size = 0;
493483
for instruction in basic_block {
494484
let instr = instruction.instr();
495485
#[cfg(unix)]
@@ -511,16 +501,8 @@ where
511501
}
512502

513503
#[cfg(unix)]
514-
if let Some(rt) = runtimes.match_first_type_mut::<DrCovRuntime>() {
515-
instruction.put_callout(|context| {
516-
let real_address = rt.real_address_for_stalked(pc(&context));
517-
//let (range, (id, name)) = helper.ranges.get_key_value(&real_address).unwrap();
518-
//log::trace!("{}:0x{:016x}", name, real_address - range.start);
519-
rt.drcov_basic_blocks.push(DrCovBasicBlock::new(
520-
real_address,
521-
real_address + instr_size,
522-
));
523-
});
504+
if let Some(_rt) = runtimes.match_first_type_mut::<DrCovRuntime>() {
505+
basic_block_start = address;
524506
}
525507
}
526508

@@ -582,15 +564,22 @@ where
582564
}
583565

584566
#[cfg(unix)]
585-
if let Some(rt) = runtimes.match_first_type_mut::<DrCovRuntime>() {
586-
rt.add_stalked_address(
587-
output.writer().pc() as usize - instr_size,
588-
address as usize,
589-
);
567+
if let Some(_rt) = runtimes.match_first_type_mut::<DrCovRuntime>() {
568+
basic_block_size += instr_size;
590569
}
591570
}
592571
instruction.keep();
593572
}
573+
#[cfg(unix)]
574+
if basic_block_size != 0 {
575+
if let Some(rt) = runtimes.borrow_mut().match_first_type_mut::<DrCovRuntime>() {
576+
log::trace!("{basic_block_start:#016X}:{basic_block_size:X}");
577+
rt.drcov_basic_blocks.push(DrCovBasicBlock::new(
578+
basic_block_start as usize,
579+
basic_block_start as usize + basic_block_size,
580+
));
581+
}
582+
}
594583
}
595584

596585
/*

0 commit comments

Comments
 (0)