Skip to content

Commit cf01d04

Browse files
domenukksadeli413
andauthored
Report 100% stability if no unstable edges are found (extends #2215) (#2217)
* Report 100% stability if no unstable edges are found * Use metadtata --------- Co-authored-by: sadeli413 <[email protected]>
1 parent dfd3b32 commit cf01d04

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

libafl/src/stages/calibrate.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ impl_serdeany!(UnstableEntriesMetadata);
4040
impl UnstableEntriesMetadata {
4141
#[must_use]
4242
/// Create a new [`struct@UnstableEntriesMetadata`]
43-
pub fn new(entries: HashSet<usize>, map_len: usize) -> Self {
43+
pub fn new() -> Self {
4444
Self {
45-
unstable_entries: entries,
46-
map_len,
45+
unstable_entries: HashSet::new(),
46+
map_len: 0,
4747
}
4848
}
4949

@@ -60,6 +60,12 @@ impl UnstableEntriesMetadata {
6060
}
6161
}
6262

63+
impl Default for UnstableEntriesMetadata {
64+
fn default() -> Self {
65+
Self::new()
66+
}
67+
}
68+
6369
/// Default name for `CalibrationStage`; derived from AFL++
6470
pub const CALIBRATION_STAGE_NAME: &str = "calibration";
6571
/// The calibration stage will measure the average exec time and the target's stability for this input.
@@ -220,25 +226,20 @@ where
220226
i += 1;
221227
}
222228

229+
let mut send_default_stability = false;
223230
let unstable_found = !unstable_entries.is_empty();
224231
if unstable_found {
232+
let metadata = state.metadata_or_insert_with(UnstableEntriesMetadata::new);
233+
225234
// If we see new stable entries executing this new corpus entries, then merge with the existing one
226-
if state.has_metadata::<UnstableEntriesMetadata>() {
227-
let existing = state
228-
.metadata_map_mut()
229-
.get_mut::<UnstableEntriesMetadata>()
230-
.unwrap();
231-
for item in unstable_entries {
232-
existing.unstable_entries.insert(item); // Insert newly found items
233-
}
234-
existing.map_len = map_len;
235-
} else {
236-
state.add_metadata::<UnstableEntriesMetadata>(UnstableEntriesMetadata::new(
237-
HashSet::from_iter(unstable_entries),
238-
map_len,
239-
));
235+
for item in unstable_entries {
236+
metadata.unstable_entries.insert(item); // Insert newly found items
240237
}
241-
};
238+
metadata.map_len = map_len;
239+
} else if !state.has_metadata::<UnstableEntriesMetadata>() {
240+
send_default_stability = true;
241+
state.add_metadata(UnstableEntriesMetadata::new());
242+
}
242243

243244
// If weighted scheduler or powerscheduler is used, update it
244245
if state.has_metadata::<SchedulerMetadata>() {
@@ -300,6 +301,7 @@ where
300301
if let Some(meta) = state.metadata_map().get::<UnstableEntriesMetadata>() {
301302
let unstable_entries = meta.unstable_entries().len();
302303
let map_len = meta.map_len();
304+
debug_assert_ne!(map_len, 0, "The map_len must never be 0");
303305
mgr.fire(
304306
state,
305307
Event::UpdateUserStats {
@@ -315,6 +317,18 @@ where
315317
},
316318
)?;
317319
}
320+
} else if send_default_stability {
321+
mgr.fire(
322+
state,
323+
Event::UpdateUserStats {
324+
name: Cow::from("stability"),
325+
value: UserStats::new(
326+
UserStatsValue::Ratio(map_len as u64, map_len as u64),
327+
AggregatorOps::Avg,
328+
),
329+
phantom: PhantomData,
330+
},
331+
)?;
318332
}
319333

320334
Ok(())

0 commit comments

Comments
 (0)