From ec55a03ec15b75c651d3b3165454fda26f096fab Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Wed, 7 Apr 2021 19:53:14 +0200 Subject: [PATCH] more clippy --- clippy.sh | 6 +++++- libafl/src/bolts/llmp.rs | 12 ++++++------ libafl/src/bolts/os/unix_signals.rs | 4 ++-- libafl/src/bolts/serdeany.rs | 4 ++-- libafl/src/bolts/shmem.rs | 9 ++++----- libafl/src/corpus/minimizer.rs | 4 ++++ libafl/src/corpus/testcase.rs | 9 +++++---- libafl/src/events/llmp.rs | 9 +++++++-- libafl/src/events/simple.rs | 1 + libafl/src/executors/inprocess.rs | 12 ++++++------ libafl/src/mutators/mutations.rs | 14 ++++++++------ libafl/src/mutators/token_mutations.rs | 8 ++++---- libafl/src/observers/map.rs | 6 ++++-- libafl/src/stats/mod.rs | 3 ++- libafl/src/utils.rs | 14 +++++++------- libafl_cc/src/lib.rs | 2 +- 16 files changed, 68 insertions(+), 49 deletions(-) diff --git a/clippy.sh b/clippy.sh index 17a8503766..7f4e6ac479 100755 --- a/clippy.sh +++ b/clippy.sh @@ -1,14 +1,18 @@ #!/bin/sh # Clippy checks -cargo clean +cargo clean -p libafl RUST_BACKTRACE=full cargo clippy --all -- \ -D clippy::pedantic \ -W clippy::cast_sign_loss \ -W clippy::similar-names \ -W clippy::cast_ptr_alignment \ + -W clippy::cast_possible_wrap \ + -W clippy::unused_self \ + -W clippy::too_many_lines \ -A missing-docs \ -A clippy::doc_markdown \ -A clippy::must-use-candidate \ + -A clippy::type_repetition_in_bounds \ -A clippy::missing-errors-doc \ -A clippy::cast-possible-truncation \ -A clippy::used-underscore-binding \ diff --git a/libafl/src/bolts/llmp.rs b/libafl/src/bolts/llmp.rs index 4a2062fe42..ba40bd8d7e 100644 --- a/libafl/src/bolts/llmp.rs +++ b/libafl/src/bolts/llmp.rs @@ -1223,6 +1223,7 @@ where /// Will return IllegalArgument error if msg is not on page. /// # Safety /// This dereferences msg, make sure to pass a proper pointer to it. + #[allow(clippy::cast_sign_loss)] pub unsafe fn msg_to_offset(&self, msg: *const LlmpMsg) -> Result { let page = self.page(); if llmp_msg_in_page(page, msg) { @@ -1266,19 +1267,17 @@ where /// Gets this message from this page, at the indicated offset. /// Will return IllegalArgument error if the offset is out of bounds. pub fn msg_from_offset(&mut self, offset: u64) -> Result<*mut LlmpMsg, Error> { + let offset = offset as usize; unsafe { let page = self.page_mut(); let page_size = self.shmem.map().len() - size_of::(); - if offset as isize > page_size as isize { + if offset > page_size { Err(Error::IllegalArgument(format!( "Msg offset out of bounds (size: {}, requested offset: {})", page_size, offset ))) } else { - Ok( - ((*page).messages.as_mut_ptr() as *mut u8).offset(offset as isize) - as *mut LlmpMsg, - ) + Ok(((*page).messages.as_mut_ptr() as *mut u8).add(offset) as *mut LlmpMsg) } } } @@ -1310,7 +1309,7 @@ pub struct LlmpBrokerSignalHandler { #[cfg(all(unix))] impl Handler for LlmpBrokerSignalHandler { - fn handle(&mut self, _signal: Signal, _info: siginfo_t, _void: c_void) { + fn handle(&mut self, _signal: Signal, _info: siginfo_t, _void: *const c_void) { unsafe { ptr::write_volatile(&mut self.shutting_down, true) }; } @@ -1400,6 +1399,7 @@ where /// Internal function, returns true when shuttdown is requested by a `SIGINT` signal #[inline] #[cfg(unix)] + #[allow(clippy::unused_self)] fn is_shutting_down(&self) -> bool { unsafe { ptr::read_volatile(&GLOBAL_SIGHANDLER_STATE.shutting_down) } } diff --git a/libafl/src/bolts/os/unix_signals.rs b/libafl/src/bolts/os/unix_signals.rs index 0c149afca8..b388b7bd1f 100644 --- a/libafl/src/bolts/os/unix_signals.rs +++ b/libafl/src/bolts/os/unix_signals.rs @@ -84,7 +84,7 @@ impl Display for Signal { pub trait Handler { /// Handle a signal - fn handle(&mut self, signal: Signal, info: siginfo_t, _void: c_void); + fn handle(&mut self, signal: Signal, info: siginfo_t, _void: *const c_void); /// Return a list of signals to handle fn signals(&self) -> Vec; } @@ -112,7 +112,7 @@ static mut SIGNAL_HANDLERS: [Option; 32] = [ /// # Safety /// This should be somewhat safe to call for signals previously registered, /// unless the signal handlers registered using [setup_signal_handler] are broken. -unsafe fn handle_signal(sig: c_int, info: siginfo_t, void: c_void) { +unsafe fn handle_signal(sig: c_int, info: siginfo_t, void: *const c_void) { let signal = &Signal::try_from(sig).unwrap(); let handler = { match &SIGNAL_HANDLERS[*signal as usize] { diff --git a/libafl/src/bolts/serdeany.rs b/libafl/src/bolts/serdeany.rs index 50e3d6034f..d1b52c7ed9 100644 --- a/libafl/src/bolts/serdeany.rs +++ b/libafl/src/bolts/serdeany.rs @@ -277,7 +277,7 @@ macro_rules! create_serde_registry_for_trait { None => None, Some(h) => h .get(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) - .map(|x| x.as_ref()), + .map(AsRef::as_ref), } } @@ -304,7 +304,7 @@ macro_rules! create_serde_registry_for_trait { None => None, Some(h) => h .get_mut(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) - .map(|x| x.as_mut()), + .map(AsMut::as_mut), } } diff --git a/libafl/src/bolts/shmem.rs b/libafl/src/bolts/shmem.rs index 94c5ed52f6..f87b7afbfc 100644 --- a/libafl/src/bolts/shmem.rs +++ b/libafl/src/bolts/shmem.rs @@ -356,7 +356,7 @@ pub mod unix_shmem { } /// Deinitialize this shmem instance - #[allow(clippy::clippy::clippy::unnecessary_cast)] // for c_ types + #[allow(clippy::clippy::unnecessary_cast)] // for c_ types unsafe fn unix_shmem_deinit(shm: *mut UnixShMem) { if shm.is_null() || (*shm).map.is_null() { /* Serialized map id */ @@ -370,7 +370,7 @@ pub mod unix_shmem { /// Functions to create Shared memory region, for observation channels and /// opening inputs and stuff. - #[allow(clippy::clippy::clippy::unnecessary_cast)] // for c_ types + #[allow(clippy::clippy::unnecessary_cast)] // for c_ types unsafe fn unix_shmem_init(shm: *mut UnixShMem, map_size: usize) -> *mut c_uchar { (*shm).map_size = map_size; (*shm).map = ptr::null_mut(); @@ -390,8 +390,7 @@ pub mod unix_shmem { (*shm).shm_id, ); (*shm).shm_str - [(size_of::<[c_char; 20]>() as c_ulong).wrapping_sub(1 as c_int as c_ulong) as usize] = - 0u8; + [(size_of::<[c_char; 20]>() as c_ulong).wrapping_sub(1 as c_ulong) as usize] = 0u8; (*shm).map = shmat((*shm).shm_id, ptr::null(), 0 as c_int) as *mut c_uchar; if (*shm).map == -(1 as c_int) as *mut c_void as *mut c_uchar || (*shm).map.is_null() { shmctl((*shm).shm_id, 0 as c_int, ptr::null_mut()); @@ -403,7 +402,7 @@ pub mod unix_shmem { } /// Uses a shmap id string to open a shared map - #[allow(clippy::clippy::unnecessary_cast)] // for c_int and c_long + #[allow(clippy::unnecessary_cast)] // for c_int and c_long unsafe fn unix_shmem_by_str( shm: *mut UnixShMem, shm_str: &CStr, diff --git a/libafl/src/corpus/minimizer.rs b/libafl/src/corpus/minimizer.rs index 475688909b..6b90d9dad3 100644 --- a/libafl/src/corpus/minimizer.rs +++ b/libafl/src/corpus/minimizer.rs @@ -150,6 +150,8 @@ where C: Corpus, R: Rand, { + /// Update the `Corpus` score using the `MinimizerCorpusScheduler` + #[allow(clippy::unused_self)] pub fn update_score(&self, state: &mut S, idx: usize) -> Result<(), Error> { // Create a new top rated meta if not existing if state.metadata().get::().is_none() { @@ -194,6 +196,8 @@ where Ok(()) } + /// Cull the `Corpus` using the `MinimizerCorpusScheduler` + #[allow(clippy::unused_self)] pub fn cull(&self, state: &mut S) -> Result<(), Error> { if state.metadata().get::().is_none() { return Ok(()); diff --git a/libafl/src/corpus/testcase.rs b/libafl/src/corpus/testcase.rs index eea9fd01ac..bae9860ed6 100644 --- a/libafl/src/corpus/testcase.rs +++ b/libafl/src/corpus/testcase.rs @@ -217,14 +217,15 @@ where self.cached_len = Some(l); l } - None => match self.cached_len { - Some(l) => l, - None => { + None => { + if let Some(l) = self.cached_len { + l + } else { let l = self.load_input()?.len(); self.cached_len = Some(l); l } - }, + } }) } } diff --git a/libafl/src/events/llmp.rs b/libafl/src/events/llmp.rs index 0cc25b6e10..1772aca5b8 100644 --- a/libafl/src/events/llmp.rs +++ b/libafl/src/events/llmp.rs @@ -206,7 +206,7 @@ where } /// Handle arriving events in the broker - #[allow(clippy::clippy::unnecessary_wraps)] + #[allow(clippy::unnecessary_wraps)] fn handle_in_broker( stats: &mut ST, sender_id: u32, @@ -258,6 +258,7 @@ where } // Handle arriving events in the client + #[allow(clippy::unused_self)] fn handle_in_client( &mut self, state: &mut S, @@ -507,7 +508,11 @@ where /// A restarting state is a combination of restarter and runner, that can be used on systems without `fork`. /// The restarter will start a new process each time the child crashes or timeouts. #[cfg(feature = "std")] -#[allow(clippy::clippy::unnecessary_operation, clippy::clippy::type_complexity)] // for { mgr = LlmpEventManager... } +#[allow( + clippy::unnecessary_operation, + clippy::type_complexity, + clippy::similar_names +)] // for { mgr = LlmpEventManager... } pub fn setup_restarting_mgr( //mgr: &mut LlmpEventManager, stats: ST, diff --git a/libafl/src/events/simple.rs b/libafl/src/events/simple.rs index 82b7c401bb..1ce7598155 100644 --- a/libafl/src/events/simple.rs +++ b/libafl/src/events/simple.rs @@ -118,6 +118,7 @@ where } // Handle arriving events in the client + #[allow(clippy::needless_pass_by_value, clippy::unused_self)] fn handle_in_client(&mut self, _state: &mut S, event: Event) -> Result<(), Error> { Err(Error::Unknown(format!( "Received illegal message that message should not have arrived: {:?}.", diff --git a/libafl/src/executors/inprocess.rs b/libafl/src/executors/inprocess.rs index 0071f194a0..b5230b9265 100644 --- a/libafl/src/executors/inprocess.rs +++ b/libafl/src/executors/inprocess.rs @@ -273,8 +273,8 @@ mod unix_signal_handler { pub event_mgr_ptr: *mut c_void, pub observers_ptr: *const c_void, pub current_input_ptr: *const c_void, - pub crash_handler: unsafe fn(Signal, siginfo_t, c_void, data: &mut Self), - pub timeout_handler: unsafe fn(Signal, siginfo_t, c_void, data: &mut Self), + pub crash_handler: unsafe fn(Signal, siginfo_t, *const c_void, data: &mut Self), + pub timeout_handler: unsafe fn(Signal, siginfo_t, *const c_void, data: &mut Self), } unsafe impl Send for InProcessExecutorHandlerData {} @@ -283,14 +283,14 @@ mod unix_signal_handler { unsafe fn nop_handler( _signal: Signal, _info: siginfo_t, - _void: c_void, + _void: *const c_void, _data: &mut InProcessExecutorHandlerData, ) { } #[cfg(unix)] impl Handler for InProcessExecutorHandlerData { - fn handle(&mut self, signal: Signal, info: siginfo_t, void: c_void) { + fn handle(&mut self, signal: Signal, info: siginfo_t, void: *const c_void) { unsafe { let data = &mut GLOBAL_STATE; match signal { @@ -320,7 +320,7 @@ mod unix_signal_handler { pub unsafe fn inproc_timeout_handler( _signal: Signal, _info: siginfo_t, - _void: c_void, + _void: *const c_void, data: &mut InProcessExecutorHandlerData, ) where EM: EventManager, @@ -382,7 +382,7 @@ mod unix_signal_handler { pub unsafe fn inproc_crash_handler( _signal: Signal, _info: siginfo_t, - _void: c_void, + _void: *const c_void, data: &mut InProcessExecutorHandlerData, ) where EM: EventManager, diff --git a/libafl/src/mutators/mutations.rs b/libafl/src/mutators/mutations.rs index 0ba6cc7ffb..7ee4b4963b 100644 --- a/libafl/src/mutators/mutations.rs +++ b/libafl/src/mutators/mutations.rs @@ -118,7 +118,7 @@ where let bit = state.rand_mut().below((input.bytes().len() << 3) as u64) as usize; unsafe { // Moar speed, no bound check - *input.bytes_mut().get_unchecked_mut(bit >> 3) ^= (128 >> (bit & 7)) as u8; + *input.bytes_mut().get_unchecked_mut(bit >> 3) ^= (128u8 >> (bit & 7)) as u8; } Ok(MutationResult::Mutated) } @@ -734,6 +734,7 @@ where S: HasRand, R: Rand, { + #[allow(clippy::cast_sign_loss)] fn mutate( &mut self, state: &mut S, @@ -796,6 +797,7 @@ where S: HasRand, R: Rand, { + #[allow(clippy::cast_sign_loss)] fn mutate( &mut self, state: &mut S, @@ -863,6 +865,7 @@ where S: HasRand, R: Rand, { + #[allow(clippy::cast_sign_loss)] fn mutate( &mut self, state: &mut S, @@ -1651,6 +1654,7 @@ where R: Rand, S: HasRand + HasCorpus, { + #[allow(clippy::cast_sign_loss)] fn mutate( &mut self, state: &mut S, @@ -1670,12 +1674,12 @@ where let mut other_testcase = state.corpus().get(idx)?.borrow_mut(); let other = other_testcase.load_input()?; - let mut counter = 0; + let mut counter: u32 = 0; loop { let (f, l) = locate_diffs(input.bytes(), other.bytes()); if f != l && f >= 0 && l >= 2 { - break (f, l); + break (f as u64, l as u64); } if counter == 3 { return Ok(MutationResult::Skipped); @@ -1684,9 +1688,7 @@ where } }; - let split_at = state - .rand_mut() - .between(first_diff as u64, last_diff as u64) as usize; + let split_at = state.rand_mut().between(first_diff, last_diff) as usize; let mut other_testcase = state.corpus().get(idx)?.borrow_mut(); let other = other_testcase.load_input()?; diff --git a/libafl/src/mutators/token_mutations.rs b/libafl/src/mutators/token_mutations.rs index 6f8a6bfe9a..e38a3d65d6 100644 --- a/libafl/src/mutators/token_mutations.rs +++ b/libafl/src/mutators/token_mutations.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ inputs::{HasBytesVec, Input}, - mutators::*, + mutators::{buffer_self_copy, mutations, str_decode, MutationResult, Mutator, Named}, state::{HasMaxSize, HasMetadata, HasRand}, utils::Rand, Error, @@ -49,7 +49,7 @@ impl Tokens { /// Adds a token to a dictionary, checking it is not a duplicate /// Returns `false` if the token was already present and did not get added. - #[allow(clippy::clippy::ptr_arg)] + #[allow(clippy::ptr_arg)] pub fn add_token(&mut self, token: &Vec) -> bool { if self.token_vec.contains(token) { return false; @@ -82,7 +82,7 @@ impl Tokens { } let pos_quote = match line.find('\"') { Some(x) => x, - _ => return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)), + None => return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)), }; if line.chars().nth(line.len() - 1) != Some('"') { return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)); @@ -91,7 +91,7 @@ impl Tokens { // extract item let item = match line.get(pos_quote + 1..line.len() - 1) { Some(x) => x, - _ => return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)), + None => return Err(Error::IllegalArgument("Illegal line: ".to_owned() + line)), }; if item.is_empty() { continue; diff --git a/libafl/src/observers/map.rs b/libafl/src/observers/map.rs index d1e8f13a80..e92559551f 100644 --- a/libafl/src/observers/map.rs +++ b/libafl/src/observers/map.rs @@ -56,6 +56,7 @@ where /// A well-known example is the AFL-Style coverage map. #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(bound = "T: serde::de::DeserializeOwned")] +#[allow(clippy::unsafe_derive_deserialize)] pub struct StdMapObserver where T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned, @@ -156,6 +157,7 @@ where /// Overlooking a variable bitmap #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(bound = "T: serde::de::DeserializeOwned")] +#[allow(clippy::unsafe_derive_deserialize)] pub struct VariableMapObserver where T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned, @@ -226,11 +228,11 @@ where T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned, { /// Creates a new MapObserver - pub fn new(name: &'static str, map: &'static mut [T], size: &usize) -> Self { + pub fn new(name: &'static str, map: &'static mut [T], size: *const usize) -> Self { let initial = if map.is_empty() { T::default() } else { map[0] }; Self { map: ArrayMut::Cptr((map.as_mut_ptr(), map.len())), - size: Cptr::Cptr(size as *const _), + size: Cptr::Cptr(size), name: name.into(), initial, } diff --git a/libafl/src/stats/mod.rs b/libafl/src/stats/mod.rs index d2a2386907..6b673922a5 100644 --- a/libafl/src/stats/mod.rs +++ b/libafl/src/stats/mod.rs @@ -50,6 +50,7 @@ impl ClientStats { } /// Get the calculated executions per second for this client + #[allow(clippy::cast_sign_loss, clippy::cast_precision_loss)] pub fn execs_per_sec(&mut self, cur_time: time::Duration) -> u64 { if self.executions == 0 { return 0; @@ -130,7 +131,7 @@ pub trait Stats { for _ in client_stat_count..(client_id + 1) as usize { self.client_stats_mut().push(ClientStats { last_window_time: current_time(), - ..Default::default() + ..ClientStats::default() }) } &mut self.client_stats_mut()[client_id as usize] diff --git a/libafl/src/utils.rs b/libafl/src/utils.rs index 72b581d2ec..edc3a580af 100644 --- a/libafl/src/utils.rs +++ b/libafl/src/utils.rs @@ -179,7 +179,7 @@ pub struct Xoshiro256StarRand { } impl Rand for Xoshiro256StarRand { - #[allow(clippy::clippy::unreadable_literal)] + #[allow(clippy::unreadable_literal)] fn set_seed(&mut self, seed: u64) { self.rand_seed[0] = xxh3_64_with_seed(&HASH_CONST.to_le_bytes(), seed); self.rand_seed[1] = self.rand_seed[0] ^ 0x1234567890abcdef; @@ -224,7 +224,7 @@ pub struct XorShift64Rand { } impl Rand for XorShift64Rand { - #[allow(clippy::clippy::unreadable_literal)] + #[allow(clippy::unreadable_literal)] fn set_seed(&mut self, seed: u64) { self.rand_seed = seed ^ 0x1234567890abcdef; } @@ -256,13 +256,13 @@ pub struct Lehmer64Rand { } impl Rand for Lehmer64Rand { - #[allow(clippy::clippy::unreadable_literal)] + #[allow(clippy::unreadable_literal)] fn set_seed(&mut self, seed: u64) { - self.rand_seed = (seed as u128) ^ 0x1234567890abcdef; + self.rand_seed = u128::from(seed) ^ 0x1234567890abcdef; } #[inline] - #[allow(clippy::clippy::unreadable_literal)] + #[allow(clippy::unreadable_literal)] fn next(&mut self) -> u64 { self.rand_seed *= 0xda942042e4dd58b5; (self.rand_seed >> 64) as u64 @@ -307,7 +307,7 @@ impl Rand for RomuTrioRand { } #[inline] - #[allow(clippy::clippy::unreadable_literal)] + #[allow(clippy::unreadable_literal)] fn next(&mut self) -> u64 { let xp = self.x_state; let yp = self.y_state; @@ -344,7 +344,7 @@ impl Rand for RomuDuoJrRand { } #[inline] - #[allow(clippy::clippy::unreadable_literal)] + #[allow(clippy::unreadable_literal)] fn next(&mut self) -> u64 { let xp = self.x_state; self.x_state = 15241094284759029579u64.wrapping_mul(self.y_state); diff --git a/libafl_cc/src/lib.rs b/libafl_cc/src/lib.rs index 6c41039300..c26675aceb 100644 --- a/libafl_cc/src/lib.rs +++ b/libafl_cc/src/lib.rs @@ -67,7 +67,7 @@ pub trait CompilerWrapper { } /// Wrap Clang -#[allow(clippy::clippy::struct_excessive_bools)] +#[allow(clippy::struct_excessive_bools)] pub struct ClangWrapper { optimize: bool, wrapped_cc: String,