From e719e85ababfbbc6471627718a7a833d9331947e Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Thu, 20 May 2021 13:16:47 +0200 Subject: [PATCH] clippy --- libafl/src/mutators/token_mutations.rs | 11 ++++--- libafl/src/observers/cmp.rs | 42 +++++++++++++++++++------- libafl_targets/src/cmplog.rs | 2 +- libafl_targets/src/sancov_pcguard.rs | 2 +- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/libafl/src/mutators/token_mutations.rs b/libafl/src/mutators/token_mutations.rs index c50e671c11..5223155a8e 100644 --- a/libafl/src/mutators/token_mutations.rs +++ b/libafl/src/mutators/token_mutations.rs @@ -310,6 +310,7 @@ where S: HasMetadata + HasRand + HasMaxSize, R: Rand, { + #[allow(clippy::too_many_lines)] fn mutate( &mut self, state: &mut S, @@ -343,13 +344,13 @@ where let mut result = MutationResult::Skipped; match cmp_values { CmpValues::U8(v) => { - for i in off..len { - if bytes[i] == v.0 { - bytes[i] = v.1; + for byte in bytes.iter_mut().take(len).skip(off) { + if *byte == v.0 { + *byte = v.1; result = MutationResult::Mutated; break; - } else if bytes[i] == v.1 { - bytes[i] = v.0; + } else if *byte == v.1 { + *byte = v.0; result = MutationResult::Mutated; break; } diff --git a/libafl/src/observers/cmp.rs b/libafl/src/observers/cmp.rs index 0ae3cf4db5..a202b49705 100644 --- a/libafl/src/observers/cmp.rs +++ b/libafl/src/observers/cmp.rs @@ -25,29 +25,28 @@ pub enum CmpValues { } impl CmpValues { + #[must_use] pub fn is_numeric(&self) -> bool { - match self { - CmpValues::U8(_) => true, - CmpValues::U16(_) => true, - CmpValues::U32(_) => true, - CmpValues::U64(_) => true, - _ => false, - } + matches!( + self, + CmpValues::U8(_) | CmpValues::U16(_) | CmpValues::U32(_) | CmpValues::U64(_) + ) } + #[must_use] pub fn to_u64_tuple(&self) -> Option<(u64, u64)> { match self { CmpValues::U8(t) => Some((u64::from(t.0), u64::from(t.1))), CmpValues::U16(t) => Some((u64::from(t.0), u64::from(t.1))), CmpValues::U32(t) => Some((u64::from(t.0), u64::from(t.1))), - CmpValues::U64(t) => Some((u64::from(t.0), u64::from(t.1))), + CmpValues::U64(t) => Some(*t), _ => None, } } } /// A state metadata holding a list of values logged from comparisons -#[derive(Serialize, Deserialize)] +#[derive(Default, Serialize, Deserialize)] pub struct CmpValuesMetadata { /// A `list` of values. pub list: Vec, @@ -76,10 +75,19 @@ pub trait CmpMap: Serialize + DeserializeOwned { /// Get the number of cmps fn len(&self) -> usize; + /// Get if it is empty + #[must_use] + fn is_empty(&self) -> bool { + self.len() == 0 + } + + // Get the number of executions for a cmp fn executions_for(&self, idx: usize) -> usize; + // Get the number of logged executions for a cmp fn usable_executions_for(&self, idx: usize) -> usize; + // Get the logged values for a cmp fn values_of(&self, idx: usize, execution: usize) -> CmpValues; /// Reset the state @@ -94,10 +102,14 @@ where /// Get the number of usable cmps (all by default) fn usable_count(&self) -> usize; + /// Get the `CmpMap` fn map(&self) -> &CM; + /// Get the `CmpMap` (mut) fn map_mut(&mut self) -> &mut CM; + /// Add [`CmpValuesMetadata`] to the State including the logged values. + /// This routine does a basic loop filtering because loop index cmps are not interesting. fn add_cmpvalues_meta(&mut self, state: &mut S) where S: HasMetadata, @@ -221,7 +233,7 @@ impl<'a, CM> StdCmpObserver<'a, CM> where CM: CmpMap, { - /// Creates a new [`StdCmpObserver`] with the given name. + /// Creates a new [`StdCmpObserver`] with the given name and map. #[must_use] pub fn new(name: &'static str, map: &'a mut CM) -> Self { Self { @@ -231,5 +243,13 @@ where } } - // TODO with_size + /// Creates a new [`StdCmpObserver`] with the given name, map and reference to variable size. + #[must_use] + pub fn with_size(name: &'static str, map: &'a mut CM, size: &'a mut usize) -> Self { + Self { + name: name.to_string(), + size: Some(OwnedRefMut::Ref(size)), + map: OwnedRefMut::Ref(map), + } + } } diff --git a/libafl_targets/src/cmplog.rs b/libafl_targets/src/cmplog.rs index e217bfb7ce..8f975c716f 100644 --- a/libafl_targets/src/cmplog.rs +++ b/libafl_targets/src/cmplog.rs @@ -144,7 +144,7 @@ pub static mut libafl_cmplog_enabled: u8 = 0; pub use libafl_cmplog_enabled as CMPLOG_ENABLED; -/// A [`CmpObserver`] observer for CmpLog +/// A [`CmpObserver`] observer for `CmpLog` #[derive(Serialize, Deserialize, Debug)] pub struct CmpLogObserver<'a> { map: OwnedRefMut<'a, CmpLogMap>, diff --git a/libafl_targets/src/sancov_pcguard.rs b/libafl_targets/src/sancov_pcguard.rs index 4c4ffecf7e..6d5f6dcc19 100644 --- a/libafl_targets/src/sancov_pcguard.rs +++ b/libafl_targets/src/sancov_pcguard.rs @@ -1,6 +1,6 @@ //! [`LLVM` `PcGuard`](https://clang.llvm.org/docs/SanitizerCoverage.html#tracing-pcs-with-guards) runtime for `LibAFL`. -use crate::coverage::*; +use crate::coverage::{EDGES_MAP, EDGES_MAP_SIZE, MAX_EDGES_NUM}; #[cfg(all(feature = "sancov_pcguard_edges", feature = "sancov_pcguard_hitcounts"))] #[cfg(not(any(doc, feature = "clippy")))]