generic map observer

This commit is contained in:
Andrea Fioraldi 2020-10-29 13:18:09 +01:00
parent b492051448
commit 604e278664
3 changed files with 27 additions and 25 deletions

View File

@ -14,3 +14,4 @@ xxhash-rust = { version = "0.8.0-beta.4", features = ["xxh3"] } # xxh3 hashing f
thiserror = "1.0" # A nicer way to write Errors thiserror = "1.0" # A nicer way to write Errors
hashbrown = "0.9" # A faster hashmap, nostd compatible hashbrown = "0.9" # A faster hashmap, nostd compatible
libc = "0.2" # For (*nix) libc libc = "0.2" # For (*nix) libc
num = "*"

View File

@ -1,19 +1,22 @@
extern crate num;
use crate::corpus::Testcase; use crate::corpus::Testcase;
use crate::executors::Executor; use crate::executors::Executor;
use crate::observers::MapObserver; use crate::observers::MapObserver;
use num::Integer;
pub trait Feedback { pub trait Feedback {
/// is_interesting should return the "Interestingness" from 0 to 255 (percent times 2.55) /// is_interesting should return the "Interestingness" from 0 to 255 (percent times 2.55)
fn is_interesting(&mut self, executor: &dyn Executor, entry: &dyn Testcase) -> u8; fn is_interesting(&mut self, executor: &dyn Executor, entry: &dyn Testcase) -> u8;
} }
use crate::observers::StaticMapObserver; pub struct CovFeedback<'a, MapT: Integer + Copy> {
pub struct CovFeedback<'a> { virgin_bits: Vec<MapT>,
virgin_bits: Vec<u8>, smo: &'a MapObserver<'a, MapT>,
smo: &'a StaticMapObserver,
} }
impl<'a> Feedback for CovFeedback<'a> { impl<'a, MapT: Integer + Copy> Feedback for CovFeedback<'a, MapT> {
fn is_interesting(&mut self, _executor: &dyn Executor, _entry: &dyn Testcase) -> u8 { fn is_interesting(&mut self, _executor: &dyn Executor, _entry: &dyn Testcase) -> u8 {
let mut interesting = 0; let mut interesting = 0;
// TODO: impl. correctly, optimize // TODO: impl. correctly, optimize
@ -30,12 +33,12 @@ impl<'a> Feedback for CovFeedback<'a> {
} }
} }
impl<'a> CovFeedback<'a> { impl<'a, MapT: Integer + Copy> CovFeedback<'a, MapT> {
/// Create new CovFeedback using a static map observer /// Create new CovFeedback using a static map observer
pub fn new(smo: &'a StaticMapObserver) -> Self { pub fn new(smo: &'a MapObserver<MapT>) -> Self {
CovFeedback { CovFeedback {
smo: smo, smo: smo,
virgin_bits: vec![0; smo.get_map().len()], virgin_bits: vec![MapT::zero(); smo.get_map().len()],
} }
} }
} }

View File

@ -1,6 +1,10 @@
extern crate num;
use crate::AflError; use crate::AflError;
use std::slice::from_raw_parts_mut; use std::slice::from_raw_parts_mut;
use num::Integer;
/// Observers observe different information about the target. /// Observers observe different information about the target.
/// They can then be used by various sorts of feedback. /// They can then be used by various sorts of feedback.
pub trait Observer { pub trait Observer {
@ -15,42 +19,36 @@ pub trait Observer {
} }
} }
/// A MapObserver contains a map with values, collected from the child.
pub trait MapObserver<MapT>: Observer {
fn get_map(&self) -> &[MapT];
fn get_map_mut(&mut self) -> &mut [MapT];
}
/// A staticMapObserver observes the static map, as oftentimes used for afl-like coverage information /// A staticMapObserver observes the static map, as oftentimes used for afl-like coverage information
pub struct StaticMapObserver { pub struct MapObserver<'a, MapT: Integer + Copy> {
map: &'static mut [u8], map: &'a mut [MapT],
} }
impl Observer for StaticMapObserver { impl<'a, MapT: Integer + Copy> Observer for MapObserver<'a, MapT> {
fn reset(&mut self) -> Result<(), AflError> { fn reset(&mut self) -> Result<(), AflError> {
// Normal memset, see https://rust.godbolt.org/z/Trs5hv // Normal memset, see https://rust.godbolt.org/z/Trs5hv
for i in self.map.iter_mut() { for i in self.map.iter_mut() {
*i = 0; *i = MapT::zero();
} }
Ok(()) Ok(())
} }
} }
impl MapObserver<u8> for StaticMapObserver { impl<'a, MapT: Integer + Copy> MapObserver<'a, MapT> {
fn get_map(&self) -> &[u8] { pub fn get_map(&self) -> &[MapT] {
self.map self.map
} }
fn get_map_mut(&mut self) -> &mut [u8] { pub fn get_map_mut(&mut self) -> &mut [MapT] {
self.map self.map
} }
} }
impl StaticMapObserver { impl<'a, MapT: Integer + Copy> MapObserver<'a, MapT> {
/// Creates a new StaticMapObserver from a raw pointer. /// Creates a new MapObserver from a raw pointer.
pub fn new(map_ptr: *mut u8, len: usize) -> Self { pub fn new(map_ptr: *mut MapT, len: usize) -> Self {
unsafe { unsafe {
StaticMapObserver { MapObserver {
map: from_raw_parts_mut(map_ptr, len), map: from_raw_parts_mut(map_ptr, len),
} }
} }