Skip to content

Commit acadc8e

Browse files
authored
ListDumpFeedback: Add modified ListFeedback that will dump newly observed addresses (#3394)
* ListDumpFeedback: Add modified ListFeedback that will dump newly observed addresses * ListFeedback: move functionality of ListDumpFeedback to ListFeedback Adds a second constructor `with_coverage_dump` to ListFeedback. This will create an Option<File> from a path parameter. `has_interesting_list_observer_feedback` will than write any new observed coverage to the file. For the already existing `new` constructor, this Option<File> is set to `None`. Removes the `Clone` trait from ListFeedback and adds necessary traits `ToString` and `LowerHex` to `T`. * ListFeedback: move write of new addresses to `append_list_observer_metadata` * ListFeedback: make `std` usage depending on target environment * ListFeedback: make `file` field depending on std * ListFeedback: make clippy happy * ListFeedback: delete as per @totatoka's comment in #3394
1 parent 5340051 commit acadc8e

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

crates/libafl/src/feedbacks/list.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use alloc::borrow::Cow;
2-
use core::{fmt::Debug, hash::Hash};
2+
use core::{
3+
fmt::{Debug, LowerHex},
4+
hash::Hash,
5+
};
6+
#[cfg(feature = "std")]
7+
use std::{fs::File, io::Write, path::Path};
38

49
use hashbrown::HashSet;
510
use libafl_bolts::{
@@ -59,10 +64,12 @@ impl<T> HasRefCnt for ListFeedbackMetadata<T> {
5964
}
6065

6166
/// Consider interesting a testcase if the list in `ListObserver` is not empty.
62-
#[derive(Debug, Clone)]
67+
#[derive(Debug)]
6368
pub struct ListFeedback<T> {
6469
observer_handle: Handle<ListObserver<T>>,
6570
novelty: HashSet<T>,
71+
#[cfg(feature = "std")]
72+
file: Option<File>,
6673
}
6774

6875
libafl_bolts::impl_serdeany!(
@@ -72,7 +79,7 @@ libafl_bolts::impl_serdeany!(
7279

7380
impl<T> ListFeedback<T>
7481
where
75-
T: Debug + Eq + Hash + for<'a> Deserialize<'a> + Serialize + 'static + Copy,
82+
T: Debug + Eq + Hash + for<'a> Deserialize<'a> + Serialize + 'static + Copy + LowerHex,
7683
{
7784
fn has_interesting_list_observer_feedback<OT, S>(
7885
&mut self,
@@ -99,6 +106,15 @@ where
99106
!self.novelty.is_empty()
100107
}
101108

109+
#[cfg(feature = "std")]
110+
fn dump_coverage(&mut self) {
111+
if let Some(mut file) = self.file.as_ref() {
112+
for line in &self.novelty {
113+
file.write_all(format!("0x{line:x}\n").as_bytes()).unwrap();
114+
}
115+
}
116+
}
117+
102118
fn append_list_observer_metadata<S: HasNamedMetadata>(&mut self, state: &mut S) {
103119
let history_set = state
104120
.named_metadata_map_mut()
@@ -108,6 +124,9 @@ where
108124
for v in &self.novelty {
109125
history_set.set.insert(*v);
110126
}
127+
128+
#[cfg(feature = "std")]
129+
self.dump_coverage();
111130
}
112131
}
113132

@@ -126,7 +145,15 @@ impl<EM, I, OT, S, T> Feedback<EM, I, OT, S> for ListFeedback<T>
126145
where
127146
OT: MatchName,
128147
S: HasNamedMetadata,
129-
T: Debug + Eq + Hash + for<'a> Deserialize<'a> + Serialize + Default + Copy + 'static,
148+
T: Debug
149+
+ Eq
150+
+ Hash
151+
+ for<'a> Deserialize<'a>
152+
+ Serialize
153+
+ Default
154+
+ Copy
155+
+ 'static
156+
+ LowerHex,
130157
{
131158
fn is_interesting(
132159
&mut self,
@@ -170,6 +197,21 @@ impl<T> ListFeedback<T> {
170197
Self {
171198
observer_handle: observer.handle(),
172199
novelty: HashSet::new(),
200+
#[cfg(feature = "std")]
201+
file: None,
202+
}
203+
}
204+
205+
/// Creates a new [`ListFeedback`], deciding if the given [`ListObserver`] value of a run is interesting.
206+
/// Will dump newly observed addresses to `path`. If `path` exists, the file will be truncated.
207+
#[cfg(feature = "std")]
208+
pub fn with_coverage_dump<P: AsRef<Path>>(observer: &ListObserver<T>, path: P) -> Self {
209+
let file = Some(File::create(path).unwrap());
210+
211+
Self {
212+
observer_handle: observer.handle(),
213+
novelty: HashSet::new(),
214+
file,
173215
}
174216
}
175217
}

0 commit comments

Comments
 (0)