clippy fixes

This commit is contained in:
Dominik Maier 2021-04-07 11:30:03 +02:00
parent 0d11a41038
commit 8577d5c6a0
19 changed files with 119 additions and 64 deletions

15
clippy.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
# Clippy checks
cargo clean
RUST_BACKTRACE=full cargo clippy --all -- \
-D clippy::pedantic \
-W missing-docs \
-W clippy::missing-errors-doc \
-W clippy::similar-names \
-A clippy::missing-docs-in-private-items \
-A clippy::unseparated-literal-suffix \
-A clippy::module-name-repetitions \
-A clippy::unreadable-literal \
-A clippy::if-not-else \
#--allow clippy::print-with-newline \
#--allow clippy::write-with-newline \

View File

@ -1,3 +1,5 @@
//! special handling to build and link libafl
fn main() {
#[cfg(target_os = "windows")]
windows::build!(

View File

@ -1064,6 +1064,7 @@ where
}
/// Returns the next message, tag, buf, if avaliable, else None
#[allow(clippy::type_complexity)]
#[inline]
pub fn recv_buf(&mut self) -> Result<Option<(u32, u32, &[u8])>, Error> {
unsafe {
@ -1824,6 +1825,7 @@ where
}
/// Returns the next message, tag, buf, if avaliable, else None
#[allow(clippy::type_complexity)]
#[inline]
pub fn recv_buf(&mut self) -> Result<Option<(u32, u32, &[u8])>, Error> {
self.receiver.recv_buf()

View File

@ -24,6 +24,7 @@ pub use libc::{c_void, siginfo_t};
#[derive(IntoPrimitive, TryFromPrimitive, Clone, Copy)]
#[repr(i32)]
#[allow(clippy::clippy::pub_enum_variant_names)]
pub enum Signal {
SigAbort = SIGABRT,
SigBus = SIGBUS,

View File

@ -356,6 +356,7 @@ pub mod unix_shmem {
}
/// Deinitialize this shmem instance
#[allow(clippy::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 */
@ -369,6 +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
unsafe fn unix_shmem_init(shm: *mut UnixShMem, map_size: usize) -> *mut c_uchar {
(*shm).map_size = map_size;
(*shm).map = ptr::null_mut();
@ -394,13 +396,14 @@ pub mod unix_shmem {
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());
(*shm).shm_id = -(1 as c_int);
(*shm).shm_str[0 as c_int as usize] = 0u8;
(*shm).shm_str[0] = 0u8;
return ptr::null_mut();
}
(*shm).map
}
/// Uses a shmap id string to open a shared map
#[allow(clippy::clippy::unnecessary_cast)] // for c_int and c_long
unsafe fn unix_shmem_by_str(
shm: *mut UnixShMem,
shm_str: &CStr,
@ -414,7 +417,7 @@ pub mod unix_shmem {
strncpy(
(*shm).shm_str.as_mut_ptr() as *mut c_char,
shm_str.as_ptr() as *const c_char,
(size_of::<[c_char; 20]>() as c_ulong).wrapping_sub(1 as c_int as c_ulong),
(size_of::<[c_char; 20]>() as c_ulong).wrapping_sub(1 as c_ulong),
);
(*shm).shm_id = shm_str
.to_str()

View File

@ -206,6 +206,7 @@ where
}
/// Handle arriving events in the broker
#[allow(clippy::clippy::unnecessary_wraps)]
fn handle_in_broker(
stats: &mut ST,
sender_id: u32,
@ -398,6 +399,7 @@ where
}
/// Deserialize the state and corpus tuple, previously serialized with `serialize_state_corpus(...)`
#[allow(clippy::type_complexity)]
pub fn deserialize_state_mgr<I, S, SH, ST>(
state_corpus_serialized: &[u8],
) -> Result<(S, LlmpEventManager<I, S, SH, ST>), Error>
@ -505,6 +507,7 @@ 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)] // for { mgr = LlmpEventManager... }
pub fn setup_restarting_mgr<I, S, SH, ST>(
//mgr: &mut LlmpEventManager<I, S, SH, ST>,
stats: ST,
@ -534,7 +537,8 @@ where
println!("Doing broker things. Run this tool again to start fuzzing in a client.");
mgr.broker_loop()?;
return Err(Error::ShuttingDown);
} else {
}
// We are the fuzzer respawner in a llmp client
mgr.to_env(_ENV_FUZZER_BROKER_CLIENT_INITIAL);
@ -570,7 +574,7 @@ where
}
ctr = ctr.wrapping_add(1);
}
}
} else {
// We are the newly started fuzzing instance, first, connect to our own restore map.

View File

@ -73,6 +73,7 @@ where
}
// Handle arriving events in the broker
#[allow(clippy::unnecessary_wraps)]
fn handle_in_broker(stats: &mut ST, event: &Event<I>) -> Result<BrokerEventResult, Error> {
match event {
Event::NewTestcase {

View File

@ -21,7 +21,7 @@ use crate::{
pub enum ExitKind {
Ok,
Crash,
OOM,
Oom,
Timeout,
}

View File

@ -93,7 +93,7 @@ where
/// Generates up to DUMMY_BYTES_MAX non-random dummy bytes (0)
fn generate_dummy(&self) -> BytesInput {
let size = min(self.max_size, DUMMY_BYTES_MAX);
BytesInput::new(vec![0u8; size])
BytesInput::new(vec![0_u8; size])
}
}

View File

@ -16,10 +16,11 @@ pub struct BytesInput {
impl Input for BytesInput {}
/// Rc Ref-cell from Input
impl Into<Rc<RefCell<Self>>> for BytesInput {
fn into(self) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(self))
impl From<BytesInput> for Rc<RefCell<BytesInput>> {
fn from(input: BytesInput) -> Self {
Rc::new(RefCell::new(input))
}
}

View File

@ -179,7 +179,7 @@ where
/// Create a new StdScheduledMutator instance specifying mutations
pub fn new(mutations: MT) -> Self {
StdScheduledMutator {
mutations: mutations,
mutations,
phantom: PhantomData,
}
}
@ -377,7 +377,7 @@ where
/// Create a new StdScheduledMutator instance without mutations and corpus
pub fn new(scheduled: SM) -> Self {
Self {
scheduled: scheduled,
scheduled,
mutation_log: vec![],
phantom: PhantomData,
}

View File

@ -49,6 +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)]
pub fn add_token(&mut self, token: &Vec<u8>) -> bool {
if self.token_vec.contains(token) {
return false;

View File

@ -81,6 +81,7 @@ where
R: Rand,
{
mutator: M,
#[allow(clippy::type_complexity)]
phantom: PhantomData<(C, CS, E, EM, I, OT, R, S)>,
}

View File

@ -25,7 +25,7 @@ use crate::{
use crate::inputs::bytes::BytesInput;
/// The maximum size of a testcase
pub const DEFAULT_MAX_SIZE: usize = 1048576;
pub const DEFAULT_MAX_SIZE: usize = 1_048_576;
/// Trait for elements offering a corpus
pub trait HasCorpus<C, I>
@ -453,9 +453,9 @@ where
where
OT: ObserversTuple,
{
Ok(self
self
.feedbacks_mut()
.is_interesting_all(input, observers, exit_kind)?)
.is_interesting_all(input, observers, exit_kind)
}
/// Adds this input to the corpus, if it's intersting, and return the index

View File

@ -97,14 +97,14 @@ pub trait Stats {
fn corpus_size(&self) -> u64 {
self.client_stats()
.iter()
.fold(0u64, |acc, x| acc + x.corpus_size)
.fold(0_u64, |acc, x| acc + x.corpus_size)
}
/// Amount of elements in the objectives (combined for all children)
fn objective_size(&self) -> u64 {
self.client_stats()
.iter()
.fold(0u64, |acc, x| acc + x.objective_size)
.fold(0_u64, |acc, x| acc + x.objective_size)
}
/// Total executions
@ -112,7 +112,7 @@ pub trait Stats {
fn total_execs(&mut self) -> u64 {
self.client_stats()
.iter()
.fold(0u64, |acc, x| acc + x.executions)
.fold(0_u64, |acc, x| acc + x.executions)
}
/// Executions per second
@ -121,7 +121,7 @@ pub trait Stats {
let cur_time = current_time();
self.client_stats_mut()
.iter_mut()
.fold(0u64, |acc, x| acc + x.execs_per_sec(cur_time))
.fold(0_u64, |acc, x| acc + x.execs_per_sec(cur_time))
}
/// The client stats for a specific id, creating new if it doesn't exist

View File

@ -179,6 +179,7 @@ pub struct Xoshiro256StarRand {
}
impl Rand for Xoshiro256StarRand {
#[allow(clippy::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;
@ -223,6 +224,7 @@ pub struct XorShift64Rand {
}
impl Rand for XorShift64Rand {
#[allow(clippy::clippy::unreadable_literal)]
fn set_seed(&mut self, seed: u64) {
self.rand_seed = seed ^ 0x1234567890abcdef;
}
@ -254,11 +256,13 @@ pub struct Lehmer64Rand {
}
impl Rand for Lehmer64Rand {
#[allow(clippy::clippy::unreadable_literal)]
fn set_seed(&mut self, seed: u64) {
self.rand_seed = (seed as u128) ^ 0x1234567890abcdef;
}
#[inline]
#[allow(clippy::clippy::unreadable_literal)]
fn next(&mut self) -> u64 {
self.rand_seed *= 0xda942042e4dd58b5;
(self.rand_seed >> 64) as u64
@ -303,6 +307,7 @@ impl Rand for RomuTrioRand {
}
#[inline]
#[allow(clippy::clippy::unreadable_literal)]
fn next(&mut self) -> u64 {
let xp = self.x_state;
let yp = self.y_state;
@ -339,6 +344,7 @@ impl Rand for RomuDuoJrRand {
}
#[inline]
#[allow(clippy::clippy::unreadable_literal)]
fn next(&mut self) -> u64 {
let xp = self.x_state;
self.x_state = 15241094284759029579u64.wrapping_mul(self.y_state);

View File

@ -1,36 +1,46 @@
//! Compiler Wrapper from `LibAFL`
use std::{process::Command, string::String, vec::Vec};
/// `LibAFL` CC Error Type
#[derive(Debug)]
pub enum Error {
/// CC Wrapper called with invalid arguments
InvalidArguments(String),
IOError(std::io::Error),
/// Io error occurred
Io(std::io::Error),
/// Something else happened
Unknown(String),
}
// TODO macOS
/// extension for static libraries
#[cfg(windows)]
pub const LIB_EXT: &'static str = "lib";
pub const LIB_EXT: &str = "lib";
/// extension for static libraries
#[cfg(not(windows))]
pub const LIB_EXT: &'static str = "a";
pub const LIB_EXT: &str = "a";
/// prefix for static libraries
#[cfg(windows)]
pub const LIB_PREFIX: &'static str = "";
pub const LIB_PREFIX: &str = "";
/// prefix for static libraries
#[cfg(not(windows))]
pub const LIB_PREFIX: &'static str = "lib";
pub const LIB_PREFIX: &str = "lib";
/// Wrap a compiler hijacking its arguments
pub trait CompilerWrapper {
/// Set the wrapper arguments parsing a command line set of arguments
fn from_args<'a>(&'a mut self, args: &[String]) -> Result<&'a mut Self, Error>;
fn from_args(&mut self, args: &[String]) -> Result<&'_ mut Self, Error>;
/// Add a compiler argument
fn add_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error>;
fn add_arg(&mut self, arg: String) -> Result<&'_ mut Self, Error>;
/// Add a compiler argument only when compiling
fn add_cc_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error>;
fn add_cc_arg(&mut self, arg: String) -> Result<&'_ mut Self, Error>;
/// Add a compiler argument only when linking
fn add_link_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error>;
fn add_link_arg(&mut self, arg: String) -> Result<&'_ mut Self, Error>;
/// Command to run the compiler
fn command(&mut self) -> Result<Vec<String>, Error>;
@ -42,14 +52,14 @@ pub trait CompilerWrapper {
fn run(&mut self) -> Result<(), Error> {
let args = self.command()?;
dbg!(&args);
if args.len() < 1 {
if args.is_empty() {
return Err(Error::InvalidArguments(
"The number of arguments cannot be 0".into(),
));
}
let status = match Command::new(&args[0]).args(&args[1..]).status() {
Ok(s) => s,
Err(e) => return Err(Error::IOError(e)),
Err(e) => return Err(Error::Io(e)),
};
dbg!(status);
Ok(())
@ -57,6 +67,7 @@ pub trait CompilerWrapper {
}
/// Wrap Clang
#[allow(clippy::clippy::struct_excessive_bools)]
pub struct ClangWrapper {
optimize: bool,
wrapped_cc: String,
@ -73,10 +84,11 @@ pub struct ClangWrapper {
link_args: Vec<String>,
}
#[allow(clippy::match_same_arms)] // for the linking = false wip for "shared"
impl CompilerWrapper for ClangWrapper {
fn from_args<'a>(&'a mut self, args: &[String]) -> Result<&'a mut Self, Error> {
let mut new_args = vec![];
if args.len() < 1 {
if args.is_empty() {
return Err(Error::InvalidArguments(
"The number of arguments cannot be 0".into(),
));
@ -122,17 +134,17 @@ impl CompilerWrapper for ClangWrapper {
Ok(self)
}
fn add_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error> {
fn add_arg(&mut self, arg: String) -> Result<&'_ mut Self, Error> {
self.base_args.push(arg);
Ok(self)
}
fn add_cc_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error> {
fn add_cc_arg(&mut self, arg: String) -> Result<&'_ mut Self, Error> {
self.cc_args.push(arg);
Ok(self)
}
fn add_link_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error> {
fn add_link_arg(&mut self, arg: String) -> Result<&'_ mut Self, Error> {
self.link_args.push(arg);
Ok(self)
}
@ -165,6 +177,8 @@ impl CompilerWrapper for ClangWrapper {
}
impl ClangWrapper {
/// Create a new Clang Wrapper
#[must_use]
pub fn new(wrapped_cc: &str, wrapped_cxx: &str) -> Self {
Self {
optimize: true,
@ -181,12 +195,14 @@ impl ClangWrapper {
}
}
pub fn dont_optimize<'a>(&'a mut self) -> &'a mut Self {
/// Disable optimizations
pub fn dont_optimize(&mut self) -> &'_ mut Self {
self.optimize = false;
self
}
pub fn is_cpp<'a>(&'a mut self) -> &'a mut Self {
/// set cpp mode
pub fn is_cpp(&mut self) -> &'_ mut Self {
self.is_cpp = true;
self
}

View File

@ -1,4 +1,4 @@
// build.rs
//! build.rs for `libafl_targets`
use std::env;
use std::path::Path;

View File

@ -1,3 +1,5 @@
//! `libafl_targets` contains runtime code, injected in the target itself during compilation.
#[cfg(any(feature = "pcguard_edges", feature = "pcguard_hitcounts"))]
pub mod pcguard;
#[cfg(any(feature = "pcguard_edges", feature = "pcguard_hitcounts"))]