From c7f1d83b6efc2d484e38012541723ab4f5778156 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Tue, 2 Mar 2021 19:57:43 +0100 Subject: [PATCH] more clippy fixes --- libafl/src/bolts/llmp.rs | 47 +++++++++++++++----------- libafl/src/bolts/serdeany.rs | 2 +- libafl/src/bolts/shmem.rs | 10 +++--- libafl/src/bolts/tuples.rs | 10 ++---- libafl/src/corpus/minimizer.rs | 6 ++-- libafl/src/corpus/ondisk.rs | 2 +- libafl/src/corpus/testcase.rs | 2 +- libafl/src/events/llmp.rs | 11 +++--- libafl/src/events/simple.rs | 2 +- libafl/src/feedbacks/map.rs | 2 +- libafl/src/fuzzer.rs | 4 +-- libafl/src/generators/mod.rs | 4 +-- libafl/src/inputs/bytes.rs | 2 +- libafl/src/mutators/mutations.rs | 4 +-- libafl/src/mutators/scheduled.rs | 6 ++-- libafl/src/mutators/token_mutations.rs | 4 +-- libafl/src/observers/map.rs | 2 +- libafl/src/stages/mutational.rs | 2 +- libafl/src/state/mod.rs | 2 +- libafl/src/stats/mod.rs | 6 ++-- libafl/src/utils.rs | 6 ++-- 21 files changed, 70 insertions(+), 66 deletions(-) diff --git a/libafl/src/bolts/llmp.rs b/libafl/src/bolts/llmp.rs index 0055354f10..95afb6f9f4 100644 --- a/libafl/src/bolts/llmp.rs +++ b/libafl/src/bolts/llmp.rs @@ -120,8 +120,8 @@ unsafe fn shmem2page(afl_shmem: &SH) -> *const LlmpPage { #[inline] unsafe fn llmp_msg_in_page(page: *const LlmpPage, msg: *const LlmpMsg) -> bool { /* DBG("llmp_msg_in_page %p within %p-%p\n", msg, page, page + page->size_total); */ - return (page as *const u8) < msg as *const u8 - && (page as *const u8).offset((*page).size_total as isize) > msg as *const u8; + (page as *const u8) < msg as *const u8 + && (page as *const u8).add((*page).size_total) > msg as *const u8 } /// allign to LLMP_PREF_ALIGNNMENT=64 bytes @@ -198,9 +198,9 @@ unsafe fn llmp_next_msg_ptr_checked( ) -> Result<*mut LlmpMsg, Error> { let page = map.page_mut(); let map_size = map.shmem.map().len(); - let msg_begin_min = (page as *const u8).offset(size_of::() as isize); + let msg_begin_min = (page as *const u8).add(size_of::()); // We still need space for this msg (alloc_size). - let msg_begin_max = (page as *const u8).offset((map_size - alloc_size) as isize); + let msg_begin_max = (page as *const u8).add(map_size - alloc_size); let next = _llmp_next_msg_ptr(last_msg); let next_ptr = next as *const u8; if next_ptr >= msg_begin_min && next_ptr <= msg_begin_max { @@ -217,9 +217,9 @@ unsafe fn llmp_next_msg_ptr_checked( #[inline] unsafe fn _llmp_next_msg_ptr(last_msg: *const LlmpMsg) -> *mut LlmpMsg { /* DBG("_llmp_next_msg_ptr %p %lu + %lu\n", last_msg, last_msg->buf_len_padded, sizeof(llmp_message)); */ - return (last_msg as *mut u8) - .offset(size_of::() as isize) - .offset((*last_msg).buf_len_padded as isize) as *mut LlmpMsg; + (last_msg as *mut u8) + .add(size_of::()) + .add((*last_msg).buf_len_padded as usize) as *mut LlmpMsg } /// Description of a shared map. @@ -262,6 +262,7 @@ pub struct LlmpMsg { /// The message we receive impl LlmpMsg { /// Gets the buffer from this message as slice, with the corrent length. + /// # Safety /// This is unsafe if somebody has access to shared mem pages on the system. pub unsafe fn as_slice_unsafe(&self) -> &[u8] { slice::from_raw_parts(self.buf.as_ptr(), self.buf_len as usize) @@ -285,14 +286,13 @@ impl LlmpMsg { unsafe { let map_size = map.shmem.map().len(); let buf_ptr = self.buf.as_ptr(); - if buf_ptr > (map.page_mut() as *const u8).offset(size_of::() as isize) + if buf_ptr > (map.page_mut() as *const u8).add(size_of::()) && buf_ptr - <= (map.page_mut() as *const u8) - .offset((map_size - size_of::() as usize) as isize) + <= (map.page_mut() as *const u8).add(map_size - size_of::() as usize) { // The message header is in the page. Continue with checking the body. let len = self.buf_len_padded as usize + size_of::(); - buf_ptr <= (map.page_mut() as *const u8).offset((map_size - len) as isize) + buf_ptr <= (map.page_mut() as *const u8).add(map_size - len) } else { false } @@ -448,6 +448,8 @@ where /// Completely reset the current sender map. /// Afterwards, no receiver should read from it at a different location. /// This is only useful if all connected llmp parties start over, for example after a crash. + /// # Safety + /// Only safe if you really really restart the page on everything connected pub unsafe fn reset(&mut self) { _llmp_page_init(&mut self.out_maps.last_mut().unwrap().shmem, self.id, true); self.last_msg_sent = ptr::null_mut(); @@ -640,7 +642,7 @@ where panic!("Allocated new message without calling send() inbetween. ret: {:?}, page: {:?}, complete_msg_size: {:?}, size_used: {:?}, last_msg: {:?}", ret, page, buf_len_padded, (*page).size_used, last_msg); } - (*page).size_used = (*page).size_used + complete_msg_size; + (*page).size_used += complete_msg_size; (*ret).buf_len_padded = buf_len_padded as u64; (*ret).buf_len = buf_len as u64; /* DBG("Returning new message at %p with len %ld, TAG was %x", ret, ret->buf_len_padded, ret->tag); */ @@ -714,16 +716,17 @@ where } /// Allocates the next space on this sender page - pub unsafe fn alloc_next(&mut self, buf_len: usize) -> Result<*mut LlmpMsg, Error> { - match self.alloc_next_if_space(buf_len) { - Some(msg) => return Ok(msg), - _ => (), + pub fn alloc_next(&mut self, buf_len: usize) -> Result<*mut LlmpMsg, Error> { + if let Some(msg) = unsafe { self.alloc_next_if_space(buf_len) } { + return Ok(msg); }; /* no more space left! We'll have to start a new page */ - self.handle_out_eop()?; + unsafe { + self.handle_out_eop()?; + } - match self.alloc_next_if_space(buf_len) { + match unsafe { self.alloc_next_if_space(buf_len) } { Some(msg) => Ok(msg), None => Err(Error::Unknown(format!( "Error allocating {} bytes in shmap", @@ -733,6 +736,8 @@ where } /// Cancel send of the next message, this allows us to allocate a new message without sending this one. + /// # Safety + /// They msg pointer may no longer be used after `cancel_send` pub unsafe fn cancel_send(&mut self, msg: *mut LlmpMsg) { /* DBG("Client %d cancels send of msg at %p with tag 0x%X and size %ld", client->id, msg, msg->tag, * msg->buf_len_padded); */ @@ -1047,11 +1052,15 @@ where } /// Get the unsafe ptr to this page, situated on the shared map + /// # Safety + /// The unsafe page pointer is obviously unsafe. pub unsafe fn page_mut(&mut self) -> *mut LlmpPage { shmem2page_mut(&mut self.shmem) } /// Get the unsafe ptr to this page, situated on the shared map + /// # Safety + /// The unsafe page pointer is obviously unsafe. pub unsafe fn page(&self) -> *const LlmpPage { shmem2page(&self.shmem) } @@ -1250,7 +1259,7 @@ where let listener = TcpListener::bind(format!("127.0.0.1:{}", port))?; // accept connections and process them, spawning a new thread for each one println!("Server listening on port {}", port); - return self.launch_tcp_listener(listener); + self.launch_tcp_listener(listener) } #[cfg(feature = "std")] diff --git a/libafl/src/bolts/serdeany.rs b/libafl/src/bolts/serdeany.rs index 2ed0f77359..4f6b292ec1 100644 --- a/libafl/src/bolts/serdeany.rs +++ b/libafl/src/bolts/serdeany.rs @@ -105,7 +105,7 @@ macro_rules! create_serde_registry_for_trait { .get(&id) .expect("Cannot deserialize an unregistered type") }; - let seed = DeserializeCallbackSeed:: { cb: cb }; + let seed = DeserializeCallbackSeed:: { cb }; let obj: Self::Value = visitor.next_element_seed(seed)?.unwrap(); Ok(obj) } diff --git a/libafl/src/bolts/shmem.rs b/libafl/src/bolts/shmem.rs index 6ddb7cbfed..6313a3dbaa 100644 --- a/libafl/src/bolts/shmem.rs +++ b/libafl/src/bolts/shmem.rs @@ -3,11 +3,11 @@ #[cfg(feature = "std")] #[cfg(unix)] -pub use shmem::UnixShMem; +pub use unix_shmem::UnixShMem; #[cfg(feature = "std")] #[cfg(windows)] -pub use shmem::Win32ShMem; +pub use unix_shmem::Win32ShMem; use alloc::string::{String, ToString}; use core::fmt::Debug; @@ -101,7 +101,7 @@ pub trait ShMem: Sized + Debug { #[cfg(unix)] #[cfg(feature = "std")] -pub mod shmem { +pub mod unix_shmem { use core::{mem::size_of, ptr, slice}; use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, c_void}; @@ -288,7 +288,7 @@ pub mod shmem { (*shm).shm_str[0 as c_int as usize] = 0u8; return ptr::null_mut(); } - return (*shm).map; + (*shm).map } /// Uses a shmap id string to open a shared map @@ -319,7 +319,7 @@ pub mod shmem { (*shm).shm_str[0] = 0u8; return ptr::null_mut(); } - return (*shm).map; + (*shm).map } } diff --git a/libafl/src/bolts/tuples.rs b/libafl/src/bolts/tuples.rs index 21d93442c7..c44f74102d 100644 --- a/libafl/src/bolts/tuples.rs +++ b/libafl/src/bolts/tuples.rs @@ -68,12 +68,8 @@ pub trait MatchType { } impl MatchType for () { - fn match_type(&self, _f: fn(t: &T)) { - () - } - fn match_type_mut(&mut self, _f: fn(t: &mut T)) { - () - } + fn match_type(&self, _f: fn(t: &T)) {} + fn match_type_mut(&mut self, _f: fn(t: &mut T)) {} } impl MatchType for (Head, Tail) @@ -182,7 +178,7 @@ where fn append(self, value: T) -> Self::AppendResult { let (head, tail) = self; - return (head, tail.append(value)); + (head, tail.append(value)) } } diff --git a/libafl/src/corpus/minimizer.rs b/libafl/src/corpus/minimizer.rs index a57dc0d4cd..1055a867e9 100644 --- a/libafl/src/corpus/minimizer.rs +++ b/libafl/src/corpus/minimizer.rs @@ -214,7 +214,7 @@ where pub fn new(base: CS) -> Self { Self { - base: base, + base, skip_not_fav_prob: DEFAULT_SKIP_NOT_FAV_PROB, phantom: PhantomData, } @@ -222,8 +222,8 @@ where pub fn with_skip_prob(base: CS, skip_not_fav_prob: u64) -> Self { Self { - base: base, - skip_not_fav_prob: skip_not_fav_prob, + base, + skip_not_fav_prob, phantom: PhantomData, } } diff --git a/libafl/src/corpus/ondisk.rs b/libafl/src/corpus/ondisk.rs index e0787d6816..dafe3ecc5a 100644 --- a/libafl/src/corpus/ondisk.rs +++ b/libafl/src/corpus/ondisk.rs @@ -98,7 +98,7 @@ where Ok(Self { entries: vec![], current: None, - dir_path: dir_path, + dir_path, }) } } diff --git a/libafl/src/corpus/testcase.rs b/libafl/src/corpus/testcase.rs index d84f9835d9..eea9fd01ac 100644 --- a/libafl/src/corpus/testcase.rs +++ b/libafl/src/corpus/testcase.rs @@ -183,7 +183,7 @@ where Testcase { input: Some(input), filename: None, - fitness: fitness, + fitness, metadata: SerdeAnyMap::new(), exec_time: None, cached_len: None, diff --git a/libafl/src/events/llmp.rs b/libafl/src/events/llmp.rs index b47f70cb8b..543a460aa1 100644 --- a/libafl/src/events/llmp.rs +++ b/libafl/src/events/llmp.rs @@ -280,14 +280,13 @@ where let observers: OT = postcard::from_bytes(&observers_buf)?; // TODO include ExitKind in NewTestcase let fitness = state.is_interesting(&input, &observers, ExitKind::Ok)?; - if fitness > 0 { - if state + if fitness > 0 + && state .add_if_interesting(&input, fitness, scheduler)? .is_some() - { - #[cfg(feature = "std")] - println!("Added received Testcase"); - } + { + #[cfg(feature = "std")] + println!("Added received Testcase"); } Ok(()) } diff --git a/libafl/src/events/simple.rs b/libafl/src/events/simple.rs index 2420a68eff..ab0d88cab3 100644 --- a/libafl/src/events/simple.rs +++ b/libafl/src/events/simple.rs @@ -66,7 +66,7 @@ where { pub fn new(stats: ST) -> Self { Self { - stats: stats, + stats, events: vec![], phantom: PhantomData, } diff --git a/libafl/src/feedbacks/map.rs b/libafl/src/feedbacks/map.rs index fa4628a0e7..2f7e71fb3c 100644 --- a/libafl/src/feedbacks/map.rs +++ b/libafl/src/feedbacks/map.rs @@ -323,7 +323,7 @@ where /// The map can be shared. pub fn with_history_map(name: &'static str, history_map: Vec) -> Self { Self { - history_map: history_map, + history_map, name: name.to_string(), indexes: None, novelties: None, diff --git a/libafl/src/fuzzer.rs b/libafl/src/fuzzer.rs index d4507ed9b6..b65587d205 100644 --- a/libafl/src/fuzzer.rs +++ b/libafl/src/fuzzer.rs @@ -142,8 +142,8 @@ where { pub fn new(scheduler: CS, stages: ST) -> Self { Self { - scheduler: scheduler, - stages: stages, + scheduler, + stages, phantom: PhantomData, } } diff --git a/libafl/src/generators/mod.rs b/libafl/src/generators/mod.rs index 262381df96..f42a6c175a 100644 --- a/libafl/src/generators/mod.rs +++ b/libafl/src/generators/mod.rs @@ -61,7 +61,7 @@ where { pub fn new(max_size: usize) -> Self { Self { - max_size: max_size, + max_size, phantom: PhantomData, } } @@ -103,7 +103,7 @@ where { pub fn new(max_size: usize) -> Self { Self { - max_size: max_size, + max_size, phantom: PhantomData, } } diff --git a/libafl/src/inputs/bytes.rs b/libafl/src/inputs/bytes.rs index 2747cb3643..5840e783be 100644 --- a/libafl/src/inputs/bytes.rs +++ b/libafl/src/inputs/bytes.rs @@ -64,7 +64,7 @@ impl From<&[u8]> for BytesInput { impl BytesInput { /// Creates a new bytes input using the given bytes pub fn new(bytes: Vec) -> Self { - Self { bytes: bytes } + Self { bytes } } } diff --git a/libafl/src/mutators/mutations.rs b/libafl/src/mutators/mutations.rs index 0c405c8a51..3a0dfebbe8 100644 --- a/libafl/src/mutators/mutations.rs +++ b/libafl/src/mutators/mutations.rs @@ -767,7 +767,7 @@ fn from_hex(hex: u8) -> Result { if hex >= 97 && hex <= 102 { return Ok(hex - 87); } - return Err(Error::IllegalArgument("".to_owned())); + Err(Error::IllegalArgument("".to_owned())) } /// Decodes a dictionary token: 'foo\x41\\and\"bar' -> 'fooA\and"bar' @@ -801,7 +801,7 @@ pub fn str_decode(item: &str) -> Result, Error> { } } - return Ok(token); + Ok(token) } #[cfg(test)] diff --git a/libafl/src/mutators/scheduled.rs b/libafl/src/mutators/scheduled.rs index 961e266b8c..8fad11a207 100644 --- a/libafl/src/mutators/scheduled.rs +++ b/libafl/src/mutators/scheduled.rs @@ -133,7 +133,7 @@ where /// Create a new StdScheduledMutator instance specifying mutations pub fn with_mutations(mutations: Vec>) -> Self { StdScheduledMutator { - mutations: mutations, + mutations, phantom: PhantomData, } } @@ -202,7 +202,7 @@ where scheduled.add_mutation(mutation_bitflip); scheduled.add_mutation(mutation_splice); Self { - scheduled: scheduled, + scheduled, phantom: PhantomData, } } @@ -253,7 +253,7 @@ where //scheduled.add_mutation(mutation_splice); HavocBytesMutator { - scheduled: scheduled, + scheduled, phantom: PhantomData, } } diff --git a/libafl/src/mutators/token_mutations.rs b/libafl/src/mutators/token_mutations.rs index 27ce1715a7..c722e25e18 100644 --- a/libafl/src/mutators/token_mutations.rs +++ b/libafl/src/mutators/token_mutations.rs @@ -54,7 +54,7 @@ impl Tokens { return false; } self.token_vec.push(token.to_vec()); - return true; + true } /// Reads a tokens file, returning the count of new entries read @@ -117,7 +117,7 @@ impl Tokens { /// Gets the tokens stored in this db pub fn tokens(&self) -> &[Vec] { - return &self.token_vec; + &self.token_vec } } diff --git a/libafl/src/observers/map.rs b/libafl/src/observers/map.rs index 8c6fa0ef00..2a82ade8c8 100644 --- a/libafl/src/observers/map.rs +++ b/libafl/src/observers/map.rs @@ -339,6 +339,6 @@ where { /// Creates a new MapObserver pub fn new(base: M) -> Self { - Self { base: base } + Self { base } } } diff --git a/libafl/src/stages/mutational.rs b/libafl/src/stages/mutational.rs index bf05d9c803..cd872b3462 100644 --- a/libafl/src/stages/mutational.rs +++ b/libafl/src/stages/mutational.rs @@ -157,7 +157,7 @@ where /// Creates a new default mutational stage pub fn new(mutator: M) -> Self { Self { - mutator: mutator, + mutator, phantom: PhantomData, } } diff --git a/libafl/src/state/mod.rs b/libafl/src/state/mod.rs index ae771d703d..d201143ad7 100644 --- a/libafl/src/state/mod.rs +++ b/libafl/src/state/mod.rs @@ -523,7 +523,7 @@ where manager.fire( self, Event::NewTestcase { - input: input, + input, observers_buf, corpus_size: self.corpus().count() + 1, client_config: "TODO".into(), diff --git a/libafl/src/stats/mod.rs b/libafl/src/stats/mod.rs index efd56de220..6a0883c707 100644 --- a/libafl/src/stats/mod.rs +++ b/libafl/src/stats/mod.rs @@ -187,7 +187,7 @@ where { pub fn new(print_fn: F) -> Self { Self { - print_fn: print_fn, + print_fn, start_time: current_time(), corpus_size: 0, client_stats: vec![], @@ -196,8 +196,8 @@ where pub fn with_time(print_fn: F, start_time: time::Duration) -> Self { Self { - print_fn: print_fn, - start_time: start_time, + print_fn, + start_time, corpus_size: 0, client_stats: vec![], } diff --git a/libafl/src/utils.rs b/libafl/src/utils.rs index 072c86e3c0..405baca7e2 100644 --- a/libafl/src/utils.rs +++ b/libafl/src/utils.rs @@ -204,7 +204,7 @@ impl Rand for Xoshiro256StarRand { self.rand_seed[3] = self.rand_seed[3].rotate_left(45); - return ret; + ret } } @@ -235,7 +235,7 @@ impl Rand for XorShift64Rand { x ^= x >> 7; x ^= x << 17; self.rand_seed = x; - return x; + x } } @@ -262,7 +262,7 @@ impl Rand for Lehmer64Rand { #[inline] fn next(&mut self) -> u64 { self.rand_seed *= 0xda942042e4dd58b5; - return (self.rand_seed >> 64) as u64; + (self.rand_seed >> 64) as u64 } }