add feedback for decreasing bytes

This commit is contained in:
Alwin Berger 2022-02-11 13:41:42 +01:00
parent d7c0193a5e
commit 5c5f1f77bd
2 changed files with 66 additions and 4 deletions

View File

@ -1,4 +1,5 @@
#![feature(iter_advance_by)] #![feature(iter_advance_by)]
#![feature(is_sorted)]
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub mod worst; pub mod worst;
pub mod freertos; pub mod freertos;

View File

@ -1,3 +1,6 @@
use core::cmp::Ordering::{Greater,Less,Equal};
use libafl::inputs::BytesInput;
use libafl::inputs::HasTargetBytes;
use libafl::feedbacks::MapIndexesMetadata; use libafl::feedbacks::MapIndexesMetadata;
use libafl::corpus::Testcase; use libafl::corpus::Testcase;
use libafl::corpus::FavFactor; use libafl::corpus::FavFactor;
@ -189,7 +192,7 @@ where
let mean_sum_of_squares = (sum_of_square_difference as f64) / (self.target_map.len() as f64); let mean_sum_of_squares = (sum_of_square_difference as f64) / (self.target_map.len() as f64);
let hit_target = mean_sum_of_squares <= self.target_msd; let hit_target = mean_sum_of_squares <= self.target_msd;
if hit_target { if hit_target {
#[cfg(debug_assertions)] eprintln!("Hit Feedback trigger"); // #[cfg(debug_assertions)] eprintln!("Hit Feedback trigger");
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)
@ -317,7 +320,7 @@ where
let mean_sum_of_squares = (sum_of_square_difference as f64) / (self.target_map.len() as f64); let mean_sum_of_squares = (sum_of_square_difference as f64) / (self.target_map.len() as f64);
let hit_target = mean_sum_of_squares < self.best_msd; let hit_target = mean_sum_of_squares < self.best_msd;
if hit_target { if hit_target {
#[cfg(debug_assertions)] eprintln!("Hit Improving: {}",mean_sum_of_squares); // #[cfg(debug_assertions)] eprintln!("Hit Improving: {}",mean_sum_of_squares);
self.best_msd = mean_sum_of_squares; self.best_msd = mean_sum_of_squares;
Ok(true) Ok(true)
} else { } else {
@ -432,7 +435,7 @@ where
EM: EventFirer<I>, EM: EventFirer<I>,
OT: ObserversTuple<I, S>, OT: ObserversTuple<I, S>,
{ {
eprintln!("Input was: {:?}",_input); // eprintln!("Input was: {:?}",_input);
Ok(self.response) Ok(self.response)
} }
} }
@ -506,4 +509,62 @@ where
let execs_times_length_per_hour = execs_per_hour*entry.cached_len()? as u64; let execs_times_length_per_hour = execs_per_hour*entry.cached_len()? as u64;
Ok(execs_times_length_per_hour) Ok(execs_times_length_per_hour)
} }
} }
/// A Feedback reporting if the Input consists of strictly decreasing bytes.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SortedFeedback {
}
impl<S> Feedback<BytesInput, S> for SortedFeedback
where
S: HasClientPerfMonitor,
{
fn is_interesting<EM, OT>(
&mut self,
_state: &mut S,
_manager: &mut EM,
_input: &BytesInput,
_observers: &OT,
_exit_kind: &ExitKind,
) -> Result<bool, Error>
where
EM: EventFirer<BytesInput>,
OT: ObserversTuple<BytesInput, S>,
{
let t = _input.target_bytes();
let tmp = t.as_slice();
if tmp.len()<32 {return Ok(false);}
let tmp = Vec::<u8>::from(&tmp[0..32]);
// tmp.reverse();
if tmp.is_sorted_by(|a,b| match a.partial_cmp(b).unwrap_or(Less) {
Less => Some(Greater),
Equal => Some(Greater),
Greater => Some(Less),
}) {return Ok(true)};
return Ok(false);
}
}
impl Named for SortedFeedback {
#[inline]
fn name(&self) -> &str {
"Sorted"
}
}
impl SortedFeedback {
/// Creates a new [`HitFeedback`]
#[must_use]
pub fn new() -> Self {
Self {}
}
}
impl Default for SortedFeedback {
fn default() -> Self {
Self::new()
}
}
//===================================================================