Skip to content

Commit a8f1caf

Browse files
authored
Add coverage file path configuration and rename filename to path in DrCovModule of QEMU (#3525)
* feat(qemu): in drcov module - rename filename to path, add methods * feat(qemu): use generic arg's type for path method of DrCovModule
1 parent 4184985 commit a8f1caf

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

crates/libafl_qemu/src/modules/drcov.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use std::{
33
cmp::{max, min},
44
ops::Range,
55
};
6-
use std::{path::PathBuf, sync::Mutex};
6+
use std::{
7+
path::{Path, PathBuf},
8+
sync::Mutex,
9+
};
710

811
use hashbrown::{HashMap, hash_map::Entry};
912
use libafl::{HasMetadata, executors::ExitKind, observers::ObserversTuple};
@@ -55,7 +58,7 @@ libafl_bolts::impl_serdeany!(DrCovMetadata);
5558
pub struct DrCovModuleBuilder<F> {
5659
filter: Option<F>,
5760
module_mapping: Option<RangeMap<u64, (u16, String)>>,
58-
filename: Option<PathBuf>,
61+
path: Option<PathBuf>,
5962
full_trace: bool,
6063
}
6164

@@ -66,7 +69,7 @@ where
6669
pub fn build(self) -> DrCovModule<F> {
6770
DrCovModule::new(
6871
self.filter.unwrap(),
69-
self.filename.unwrap(),
72+
self.path.unwrap(),
7073
self.module_mapping,
7174
self.full_trace,
7275
)
@@ -76,7 +79,7 @@ where
7679
DrCovModuleBuilder {
7780
filter: Some(filter),
7881
module_mapping: self.module_mapping,
79-
filename: self.filename,
82+
path: self.path,
8083
full_trace: self.full_trace,
8184
}
8285
}
@@ -86,17 +89,17 @@ where
8689
Self {
8790
filter: self.filter,
8891
module_mapping: Some(module_mapping),
89-
filename: self.filename,
92+
path: self.path,
9093
full_trace: self.full_trace,
9194
}
9295
}
9396

9497
#[must_use]
95-
pub fn filename(self, filename: PathBuf) -> Self {
98+
pub fn path<P: Into<PathBuf>>(self, path: P) -> Self {
9699
Self {
97100
filter: self.filter,
98101
module_mapping: self.module_mapping,
99-
filename: Some(filename),
102+
path: Some(path.into()),
100103
full_trace: self.full_trace,
101104
}
102105
}
@@ -106,7 +109,7 @@ where
106109
Self {
107110
filter: self.filter,
108111
module_mapping: self.module_mapping,
109-
filename: self.filename,
112+
path: self.path,
110113
full_trace,
111114
}
112115
}
@@ -116,7 +119,7 @@ where
116119
pub struct DrCovModule<F> {
117120
filter: F,
118121
module_mapping: Option<RangeMap<u64, (u16, String)>>,
119-
filename: PathBuf,
122+
path: PathBuf,
120123
full_trace: bool,
121124
drcov_len: usize,
122125
}
@@ -376,16 +379,16 @@ impl DrCovModule<NopAddressFilter> {
376379
filter: Some(NopAddressFilter),
377380
module_mapping: None,
378381
full_trace: false,
379-
filename: None,
382+
path: None,
380383
}
381384
}
382385
}
383386

384387
impl<F> DrCovModule<F> {
385388
#[must_use]
386-
pub fn new(
389+
pub fn new<P: Into<PathBuf>>(
387390
filter: F,
388-
filename: PathBuf,
391+
path: P,
389392
module_mapping: Option<RangeMap<u64, (u16, String)>>,
390393
full_trace: bool,
391394
) -> Self {
@@ -399,12 +402,20 @@ impl<F> DrCovModule<F> {
399402
Self {
400403
filter,
401404
module_mapping,
402-
filename,
405+
path: path.into(),
403406
full_trace,
404407
drcov_len: 0,
405408
}
406409
}
407410

411+
pub fn set_path<P: Into<PathBuf>>(&mut self, path: P) {
412+
self.path = path.into();
413+
}
414+
415+
pub fn path(&self) -> &Path {
416+
self.path.as_path()
417+
}
418+
408419
pub fn flush(&mut self) {
409420
let lengths_opt = DRCOV_LENGTHS.lock().unwrap();
410421
let lengths = lengths_opt.as_ref().unwrap();
@@ -451,7 +462,7 @@ impl<F> DrCovModule<F> {
451462
// Module mapping is already set. It's checked or filled when the module is first run.
452463
unsafe {
453464
DrCovWriter::new(self.module_mapping.as_ref().unwrap_unchecked())
454-
.write(&self.filename, &drcov_vec)
465+
.write(&self.path, &drcov_vec)
455466
.expect("Failed to write coverage file");
456467
}
457468
}
@@ -499,7 +510,7 @@ impl<F> DrCovModule<F> {
499510
// Module mapping is already set. It's checked or filled when the module is first run.
500511
unsafe {
501512
DrCovWriter::new(self.module_mapping.as_ref().unwrap_unchecked())
502-
.write(&self.filename, &drcov_vec)
513+
.write(&self.path, &drcov_vec)
503514
.expect("Failed to write coverage file");
504515
}
505516
}

0 commit comments

Comments
 (0)