Fix Clippy on MacOS for qemu, accept some clippy::missing_transmute_annotations suggestions (#2009)

* Fix Clippy/CI

* Ignore transmute annotations in pybind

* fix

* more clippy

* more clippy

* fix fixes
This commit is contained in:
Dominik Maier 2024-04-08 16:24:10 +02:00 committed by GitHub
parent fa58dff4b7
commit ff938261df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 75 additions and 64 deletions

View File

@ -29,8 +29,7 @@ Welcome to `LibAFL`
clippy::ptr_cast_constness,
clippy::unsafe_derive_deserialize,
clippy::similar_names,
clippy::too_many_lines,
clippy::missing_transmute_annotations
clippy::too_many_lines
)]
#![cfg_attr(not(test), warn(
missing_debug_implementations,

View File

@ -30,8 +30,7 @@
clippy::module_name_repetitions,
clippy::ptr_cast_constness,
clippy::negative_feature_names,
clippy::too_many_lines,
clippy::missing_transmute_annotations
clippy::too_many_lines
)]
#![cfg_attr(not(test), warn(
missing_debug_implementations,

View File

@ -2492,6 +2492,7 @@ where
}
/// Tell the broker to disconnect this client from it.
#[cfg(feature = "std")]
fn announce_client_exit(sender: &mut LlmpSender<SP>, client_id: u32) -> Result<(), Error> {
unsafe {
let msg = sender

View File

@ -6,6 +6,7 @@ use alloc::vec::Vec;
use core::any::type_name;
use core::{
any::TypeId,
mem::transmute,
ptr::{addr_of, addr_of_mut},
};
@ -272,10 +273,7 @@ where
fn take<'a, T: 'static>(mut self) -> (Option<&'a T>, Self) {
if TypeId::of::<T>() == TypeId::of::<Head>() {
let r = self.0.take();
(
unsafe { core::mem::transmute::<Option<&Head>, Option<&T>>(r) },
self,
)
(unsafe { transmute::<Option<&Head>, Option<&T>>(r) }, self)
} else {
let (r, tail) = self.1.take::<T>();
(r, (self.0, tail))
@ -292,7 +290,7 @@ where
if TypeId::of::<T>() == TypeId::of::<Head>() {
let r = self.0.take();
(
unsafe { core::mem::transmute::<Option<&mut Head>, Option<&T>>(r) },
unsafe { transmute::<Option<&mut Head>, Option<&T>>(r) },
self,
)
} else {
@ -323,7 +321,7 @@ where
if TypeId::of::<T>() == TypeId::of::<Head>() {
let r = self.0.take();
(
unsafe { core::mem::transmute::<Option<&mut Head>, Option<&mut T>>(r) },
unsafe { transmute::<Option<&mut Head>, Option<&mut T>>(r) },
self,
)
} else {

View File

@ -13,8 +13,7 @@
clippy::missing_panics_doc,
clippy::missing_docs_in_private_items,
clippy::module_name_repetitions,
clippy::unreadable_literal,
clippy::missing_transmute_annotations
clippy::unreadable_literal
)]
#![cfg_attr(not(test), warn(
missing_debug_implementations,

View File

@ -1533,7 +1533,10 @@ impl AsanRuntime {
.unwrap();
blob.as_ptr()
.copy_to_nonoverlapping(mapping as *mut u8, blob.len());
self.shadow_check_func = Some(std::mem::transmute(mapping as *mut u8));
self.shadow_check_func = Some(std::mem::transmute::<
*mut u8,
extern "C" fn(*const c_void, usize) -> bool,
>(mapping as *mut u8));
}
}
@ -1680,7 +1683,10 @@ impl AsanRuntime {
{
libc::pthread_jit_write_protect_np(1);
}
self.shadow_check_func = Some(std::mem::transmute(mapping as *mut u8));
self.shadow_check_func = Some(std::mem::transmute::<
*mut u8,
extern "C" fn(*const c_void, usize) -> bool,
>(mapping as *mut u8));
}
}

View File

@ -746,7 +746,7 @@ impl CmpLogRuntime {
}
#[cfg(all(feature = "cmplog", target_arch = "aarch64"))]
#[allow(clippy::similar_names)]
#[allow(clippy::similar_names, clippy::type_complexity)]
#[inline]
/// Check if the current instruction is cmplog relevant one(any opcode which sets the flags)
#[must_use]
@ -765,7 +765,7 @@ impl CmpLogRuntime {
.operands
.iter()
.position(|item| *item == Operand::Nothing)
.unwrap_or_else(|| 4);
.unwrap_or(4);
// "cmp" | "ands" | "subs" | "adds" | "negs" | "ngcs" | "sbcs" | "bics" | "cbz"
// | "cbnz" | "tbz" | "tbnz" | "adcs" - yaxpeax aliases insns (i.e., cmp -> subs)
// We only care for compare instructions - aka instructions which set the flags
@ -846,14 +846,14 @@ impl CmpLogRuntime {
None,
)),
Operand::ImmShift(imm, shift) => {
Some((CmplogOperandType::Imm((imm as u64) << shift), None))
Some((CmplogOperandType::Imm(u64::from(imm) << shift), None))
} //precalculate the shift
Operand::RegShift(shiftstyle, amount, regsize, reg) => {
let reg = CmplogOperandType::Regid(writer_register(reg, regsize, true));
let shift = (shiftstyle, amount);
Some((reg, Some(shift)))
}
Operand::Immediate(imm) => Some((CmplogOperandType::Imm(imm as u64), None)),
Operand::Immediate(imm) => Some((CmplogOperandType::Imm(u64::from(imm)), None)),
_ => panic!("Second argument could not be decoded"),
}
};

View File

@ -20,8 +20,7 @@ Additional documentation is available in [the `LibAFL` book](https://aflplus.plu
clippy::module_name_repetitions,
clippy::unreadable_literal,
clippy::ptr_cast_constness,
clippy::must_use_candidate,
clippy::missing_transmute_annotations
clippy::must_use_candidate
)]
#![cfg_attr(not(test), warn(
missing_debug_implementations,

View File

@ -10,6 +10,7 @@ const QEMU_URL: &str = "https://github.com/AFLplusplus/qemu-libafl-bridge";
const QEMU_DIRNAME: &str = "qemu-libafl-bridge";
const QEMU_REVISION: &str = "821ad471430360c4eed644d07d59f0d603ef23f6";
#[allow(clippy::module_name_repetitions)]
pub struct BuildResult {
pub qemu_path: PathBuf,
pub build_dir: PathBuf,

View File

@ -14,9 +14,11 @@ __Warning__: The documentation is built by default for `x86_64` in `usermode`. T
#![allow(clippy::pedantic)]
#[cfg(all(not(feature = "clippy"), target_os = "linux"))]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[cfg(all(feature = "clippy", target_os = "linux"))]
#[cfg(any(feature = "clippy", not(target_os = "linux")))]
mod x86_64_stub_bindings;
#[cfg(emulation_mode = "usermode")]
@ -98,7 +100,11 @@ macro_rules! extern_c_checked {
use core::ops::BitAnd;
use std::{ffi::c_void, slice::from_raw_parts, str::from_utf8_unchecked};
#[cfg(all(feature = "clippy", target_os = "linux"))]
#[cfg(all(not(feature = "clippy"), target_os = "linux"))]
pub use bindings::*;
#[cfg(feature = "python")]
use pyo3::{pyclass, pymethods, IntoPy, PyObject, Python};
#[cfg(any(feature = "clippy", not(target_os = "linux")))]
pub use x86_64_stub_bindings::*;
pub type CPUStatePtr = *mut crate::CPUState;

View File

@ -625,25 +625,25 @@ pub struct HookData(u64);
impl<T> From<Pin<&mut T>> for HookData {
fn from(value: Pin<&mut T>) -> Self {
unsafe { HookData(core::mem::transmute(value)) }
unsafe { HookData(transmute::<Pin<&mut T>, u64>(value)) }
}
}
impl<T> From<Pin<&T>> for HookData {
fn from(value: Pin<&T>) -> Self {
unsafe { HookData(core::mem::transmute(value)) }
unsafe { HookData(transmute::<Pin<&T>, u64>(value)) }
}
}
impl<T> From<&'static mut T> for HookData {
fn from(value: &'static mut T) -> Self {
unsafe { HookData(core::mem::transmute(value)) }
unsafe { HookData(transmute::<&mut T, u64>(value)) }
}
}
impl<T> From<&'static T> for HookData {
fn from(value: &'static T) -> Self {
unsafe { HookData(core::mem::transmute(value)) }
unsafe { HookData(transmute::<&T, u64>(value)) }
}
}
@ -903,7 +903,7 @@ impl Qemu {
}
}
}
#[allow(clippy::missing_transmute_annotations)]
fn post_run(&self) -> Result<QemuExitReason, QemuExitReasonError> {
let exit_reason = unsafe { libafl_get_exit_reason() };
if exit_reason.is_null() {
@ -1068,6 +1068,7 @@ impl Qemu {
}
// TODO set T lifetime to be like Emulator
#[allow(clippy::missing_transmute_annotations)]
pub fn set_hook<T: Into<HookData>>(
&self,
data: T,
@ -1077,7 +1078,7 @@ impl Qemu {
) -> InstructionHookId {
unsafe {
let data: u64 = data.into().0;
let callback: extern "C" fn(u64, GuestAddr) = core::mem::transmute(callback);
let callback: extern "C" fn(u64, GuestAddr) = transmute(callback);
let num = libafl_qemu_sys::libafl_qemu_set_hook(
addr.into(),
Some(callback),
@ -1100,6 +1101,7 @@ impl Qemu {
}
}
#[allow(clippy::missing_transmute_annotations)]
pub fn add_edge_hooks<T: Into<HookData>>(
&self,
data: T,
@ -1108,14 +1110,14 @@ impl Qemu {
) -> EdgeHookId {
unsafe {
let data: u64 = data.into().0;
let gen: Option<extern "C" fn(u64, GuestAddr, GuestAddr) -> u64> =
core::mem::transmute(gen);
let exec: Option<extern "C" fn(u64, u64)> = core::mem::transmute(exec);
let gen: Option<extern "C" fn(u64, GuestAddr, GuestAddr) -> u64> = transmute(gen);
let exec: Option<extern "C" fn(u64, u64)> = transmute(exec);
let num = libafl_qemu_sys::libafl_add_edge_hook(gen, exec, data);
EdgeHookId(num)
}
}
#[allow(clippy::missing_transmute_annotations)]
pub fn add_block_hooks<T: Into<HookData>>(
&self,
data: T,
@ -1125,15 +1127,15 @@ impl Qemu {
) -> BlockHookId {
unsafe {
let data: u64 = data.into().0;
let gen: Option<extern "C" fn(u64, GuestAddr) -> u64> = core::mem::transmute(gen);
let post_gen: Option<extern "C" fn(u64, GuestAddr, GuestUsize)> =
core::mem::transmute(post_gen);
let exec: Option<extern "C" fn(u64, u64)> = core::mem::transmute(exec);
let gen: Option<extern "C" fn(u64, GuestAddr) -> u64> = transmute(gen);
let post_gen: Option<extern "C" fn(u64, GuestAddr, GuestUsize)> = transmute(post_gen);
let exec: Option<extern "C" fn(u64, u64)> = transmute(exec);
let num = libafl_qemu_sys::libafl_add_block_hook(gen, post_gen, exec, data);
BlockHookId(num)
}
}
#[allow(clippy::missing_transmute_annotations)]
pub fn add_read_hooks<T: Into<HookData>>(
&self,
data: T,
@ -1147,13 +1149,12 @@ impl Qemu {
unsafe {
let data: u64 = data.into().0;
let gen: Option<extern "C" fn(u64, GuestAddr, libafl_qemu_sys::MemOpIdx) -> u64> =
core::mem::transmute(gen);
let exec1: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec1);
let exec2: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec2);
let exec4: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec4);
let exec8: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec8);
let exec_n: Option<extern "C" fn(u64, u64, GuestAddr, usize)> =
core::mem::transmute(exec_n);
transmute(gen);
let exec1: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec1);
let exec2: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec2);
let exec4: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec4);
let exec8: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec8);
let exec_n: Option<extern "C" fn(u64, u64, GuestAddr, usize)> = transmute(exec_n);
let num = libafl_qemu_sys::libafl_add_read_hook(
gen, exec1, exec2, exec4, exec8, exec_n, data,
);
@ -1162,6 +1163,7 @@ impl Qemu {
}
// TODO add MemOp info
#[allow(clippy::missing_transmute_annotations)]
pub fn add_write_hooks<T: Into<HookData>>(
&self,
data: T,
@ -1175,13 +1177,12 @@ impl Qemu {
unsafe {
let data: u64 = data.into().0;
let gen: Option<extern "C" fn(u64, GuestAddr, libafl_qemu_sys::MemOpIdx) -> u64> =
core::mem::transmute(gen);
let exec1: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec1);
let exec2: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec2);
let exec4: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec4);
let exec8: Option<extern "C" fn(u64, u64, GuestAddr)> = core::mem::transmute(exec8);
let exec_n: Option<extern "C" fn(u64, u64, GuestAddr, usize)> =
core::mem::transmute(exec_n);
transmute(gen);
let exec1: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec1);
let exec2: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec2);
let exec4: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec4);
let exec8: Option<extern "C" fn(u64, u64, GuestAddr)> = transmute(exec8);
let exec_n: Option<extern "C" fn(u64, u64, GuestAddr, usize)> = transmute(exec_n);
let num = libafl_qemu_sys::libafl_add_write_hook(
gen, exec1, exec2, exec4, exec8, exec_n, data,
);
@ -1189,6 +1190,7 @@ impl Qemu {
}
}
#[allow(clippy::missing_transmute_annotations)]
pub fn add_cmp_hooks<T: Into<HookData>>(
&self,
data: T,
@ -1200,17 +1202,17 @@ impl Qemu {
) -> CmpHookId {
unsafe {
let data: u64 = data.into().0;
let gen: Option<extern "C" fn(u64, GuestAddr, usize) -> u64> =
core::mem::transmute(gen);
let exec1: Option<extern "C" fn(u64, u64, u8, u8)> = core::mem::transmute(exec1);
let exec2: Option<extern "C" fn(u64, u64, u16, u16)> = core::mem::transmute(exec2);
let exec4: Option<extern "C" fn(u64, u64, u32, u32)> = core::mem::transmute(exec4);
let exec8: Option<extern "C" fn(u64, u64, u64, u64)> = core::mem::transmute(exec8);
let gen: Option<extern "C" fn(u64, GuestAddr, usize) -> u64> = transmute(gen);
let exec1: Option<extern "C" fn(u64, u64, u8, u8)> = transmute(exec1);
let exec2: Option<extern "C" fn(u64, u64, u16, u16)> = transmute(exec2);
let exec4: Option<extern "C" fn(u64, u64, u32, u32)> = transmute(exec4);
let exec8: Option<extern "C" fn(u64, u64, u64, u64)> = transmute(exec8);
let num = libafl_qemu_sys::libafl_add_cmp_hook(gen, exec1, exec2, exec4, exec8, data);
CmpHookId(num)
}
}
#[allow(clippy::missing_transmute_annotations)]
pub fn add_backdoor_hook<T: Into<HookData>>(
&self,
data: T,
@ -1218,7 +1220,7 @@ impl Qemu {
) -> BackdoorHookId {
unsafe {
let data: u64 = data.into().0;
let callback: extern "C" fn(u64, GuestAddr) = core::mem::transmute(callback);
let callback: extern "C" fn(u64, GuestAddr) = transmute(callback);
let num = libafl_qemu_sys::libafl_add_backdoor_hook(Some(callback), data);
BackdoorHookId(num)
}
@ -1227,7 +1229,10 @@ impl Qemu {
#[allow(clippy::type_complexity)]
pub fn add_gdb_cmd(&self, callback: Box<dyn FnMut(&Self, &str) -> bool>) {
unsafe {
let fat: Box<FatPtr> = Box::new(transmute(callback));
let fat: Box<FatPtr> = Box::new(transmute::<
Box<dyn for<'a, 'b> FnMut(&'a Qemu, &'b str) -> bool>,
FatPtr,
>(callback));
libafl_qemu_add_gdb_cmd(gdb_cmd, core::ptr::from_ref(&*fat) as *const ());
GDB_COMMANDS.push(fat);
}

View File

@ -1,5 +1,5 @@
//! The high-level hooks
#![allow(clippy::type_complexity)]
#![allow(clippy::type_complexity, clippy::missing_transmute_annotations)]
#[cfg(emulation_mode = "usermode")]
use core::ptr::addr_of_mut;

View File

@ -18,7 +18,6 @@
#![allow(clippy::transmute_ptr_to_ptr)]
#![allow(clippy::ptr_cast_constness)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::missing_transmute_annotations)]
// Till they fix this buggy lint in clippy
#![allow(clippy::borrow_as_ptr)]
#![allow(clippy::borrow_deref_ref)]

View File

@ -14,8 +14,7 @@
clippy::missing_panics_doc,
clippy::missing_docs_in_private_items,
clippy::module_name_repetitions,
clippy::pub_underscore_fields,
clippy::missing_transmute_annotations
clippy::pub_underscore_fields
)]
#![cfg_attr(not(test), warn(
missing_debug_implementations,

View File

@ -11,13 +11,13 @@ fi
echo
echo "[+] Fixing build"
cargo +nightly fix --release --workspace --all-features
cargo +nightly fix --release --workspace --all-features --allow-dirty --allow-staged
echo "[+] Done fixing build"
echo
echo 'Fixing clippy (might need a "git commit" and a rerun, if "cargo fix" changed the source)'
RUST_BACKTRACE=full cargo +nightly clippy --fix --release --all --all-features --tests --examples --benches -- -Z macro-backtrace \
RUST_BACKTRACE=full cargo +nightly clippy --fix --release --all --all-features --tests --examples --benches --allow-dirty --allow-staged -- -Z macro-backtrace \
-D clippy::all \
-D clippy::pedantic \
-W clippy::similar_names \