Skip to content

Commit

Permalink
Merge pull request #29 from FuzzAnything/fix_2024_09
Browse files Browse the repository at this point in the history
Fix clippy errors
  • Loading branch information
spinpx authored Sep 7, 2024
2 parents 1befc2f + 80853d0 commit 6dd62e8
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ install/
core.*
*.data
*.log
build/
32 changes: 0 additions & 32 deletions hopper-core/src/execute/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,38 +235,6 @@ impl Executor {
}
}

/// Spawn and execute program
///
/// FIXME: It can't catch crash in FFI code
#[cfg(feature = "spawn_execute")]
fn spawn_execute<T, F>(&self, fun: F) -> eyre::Result<T>
where
F: FnOnce() -> crate::Result<T> + std::marker::Send,
T: Send,
{
let res = crossbeam_utils::thread::scope(|s| {
let (sender, receiver) = mpsc::channel();
let handle = s.spawn(|_| {
let res = Self::execute_fn(fun);
let _ = sender.send(res);
res
});
let res = receiver.recv_timeout(self.timeout).map_or(
Err(HopperError::SpawnTimeout),
|status| match status {
_ => Ok(()),
},
);
// kill thread if timeout
match handle.join() {
Ok(_) => res,
Err(err) => Err(HopperError::SpawnThreadPanic(err)),
}
});

res.unwrap()
}

/// Execute programs generated by hopper directly
///
/// It will ignore and print errors we defined in `eval`, and
Expand Down
12 changes: 6 additions & 6 deletions hopper-core/src/feedback/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ pub struct CmpBuf {

impl CmpOperation {
/// Calculate a state for the cmp
/// == : 0x04
/// > : 0x01
/// < : 0x02
/// `==` : 0x04
/// `>` : 0x01
/// `<` : 0x02
pub fn calculate_state(&self) -> u32 {
if self.is_instcmp() {
let operand1 = { self.operand1 };
Expand Down Expand Up @@ -109,9 +109,9 @@ impl CmpOperation {
}

/// Check if cmp is solved
/// == and != : > 0x04
/// > and < : 0x03
/// there is no >= or <= in asm level
/// `==` and `!=` : > 0x04
/// `>` and `<` : 0x03
/// there is no `>=` or `<=` in asm level
pub fn is_solved(&self) -> bool {
self.state > 0x04 || self.state == 0x03
}
Expand Down
8 changes: 6 additions & 2 deletions hopper-core/src/feedback/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ pub fn add_hooks() -> eyre::Result<()> {
}

// get_return_addrss
//#![feature(link_llvm_intrinsics)]
#[cfg(feature = "unstable")]
//#[cfg(feature = "unstable")]
// #![feature(link_llvm_intrinsics)]
/*
extern {
#[link_name = "llvm.returnaddress"]
fn return_address(a: i32) -> *const u8;
Expand All @@ -67,7 +68,10 @@ macro_rules! caller_address {
unsafe { return_address(0) }
};
}
#[cfg(not(feature = "unstable"))]
*/

macro_rules! caller_address {
() => {
0
Expand Down
2 changes: 1 addition & 1 deletion hopper-core/src/feedback/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub trait ShmIteratorItem {
impl<'a, T: ShmIteratorItem> Iterator for ShmBufIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.len {
if self.offset >= self.len || self.offset >= self.list.len() {
return None;
}
let ele = &self.list[self.offset];
Expand Down
48 changes: 18 additions & 30 deletions hopper-core/src/feedback/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ impl SHMable for Path {
}
#[cfg(feature = "llvm_mode")]
fn post_hander(ptr: *const u8) {
unsafe {
unsafe {
__hopper_area_ptr = ptr;
};
crate::log!(info, "update {} shm pointer in llvm runtime !", Self::name());
crate::log!(
info,
"update {} shm pointer in llvm runtime !",
Self::name()
);
}
}

Expand All @@ -44,30 +48,16 @@ impl Path {
let mut path = Vec::<(usize, BucketType)>::new();
let flat_buf: &BranchFlatBuf = unsafe { std::mem::transmute(&self.buf) };
for (i, &v) in flat_buf.iter().enumerate() {
macro_rules! run_loop {
() => {{
let base = i * ENTRY_SIZE;
for j in 0..ENTRY_SIZE {
let idx = base + j;
let new_val = self.buf[idx];
if new_val > 0 {
// crate::log!(trace, "id: {}, val: {}", idx, new_val);
path.push((idx, COUNT_LOOKUP[new_val as usize]));
}
if v > 0 {
cold();
let base = i * ENTRY_SIZE;
for j in 0..ENTRY_SIZE {
let idx = base + j;
let new_val = self.buf[idx];
if new_val > 0 {
// crate::log!(trace, "id: {}, val: {}", idx, new_val);
path.push((idx, COUNT_LOOKUP[new_val as usize]));
}
}};
}
#[cfg(feature = "unstable")]
{
if unsafe { unlikely(v > 0) } {
run_loop!()
}
}
#[cfg(not(feature = "unstable"))]
{
if v > 0 {
cold();
run_loop!()
}
}
}
Expand Down Expand Up @@ -98,10 +88,10 @@ fn is_sub_set(path: &[(usize, BucketType)], sub: &[(usize, BucketType)]) -> bool
let sub_len = sub.len();
while i < sub_len {
while j < path_len {
if sub[i].0 == path[j].0 {
if sub[i].0 == path[j].0 {
i += 1;
j += 1;
break;
break;
}
j += 1;
}
Expand All @@ -112,8 +102,6 @@ fn is_sub_set(path: &[(usize, BucketType)], sub: &[(usize, BucketType)]) -> bool
i == sub_len
}

#[cfg(feature = "unstable")]
use std::intrinsics::unlikely;
/// `cold` is used to mark sth is unlikely to be invoked
#[inline]
#[cold]
Expand Down Expand Up @@ -182,4 +170,4 @@ fn test_include() {
let a = [(1, 1), (2, 1), (4, 1)];
let b = [(1, 1), (2, 1), (3, 1)];
assert!(!is_sub_set(&b, &a));
}
}
19 changes: 8 additions & 11 deletions hopper-core/src/feedback/sanitize.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use eyre::ContextCompat;
use hopper_derive::Serde;
use std::fmt;
use std::fmt::Display;
use std::fmt::Write as _;

use crate::{
config,
Expand Down Expand Up @@ -178,21 +178,18 @@ impl SanitizeResult {
}
}

impl ToString for SanitizeResult {
fn to_string(&self) -> String {
let mut buf = String::new();
impl Display for SanitizeResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.cause.is_empty() {
return buf;
return Ok(());
}
let _ = writeln!(
buf,
writeln!(f,
"<SANITIZER> Program crashes or hangs may be due to the following reasons: "
);
)?;
for cause in &self.cause {
let _ = writeln!(buf, "\t* {cause}");
writeln!(f, "\t* {cause}")?;
}
buf.push('\n');
buf
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions hopper-core/src/fuzz/constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl Constraints {
/// Get function's mut constraint
#[inline]
pub fn get_func_constraint_mut(&mut self, f_name: &str) -> eyre::Result<&mut FuncConstraint> {
if self.func_constraints.get(f_name).is_none() {
if !self.func_constraints.contains_key(f_name) {
self.init_func_constraint(f_name)?;
}
self.func_constraints
Expand All @@ -263,7 +263,7 @@ impl Constraints {
}

pub fn init_type_constraint(&mut self, type_name: &str) -> eyre::Result<()> {
if self.type_constraints.get(type_name).is_some() {
if self.type_constraints.contains_key(type_name) {
return Ok(());
}
let tc = TypeConstraint::init(type_name);
Expand Down
2 changes: 1 addition & 1 deletion hopper-core/src/fuzz/object/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ fn parse_dictionary(buf: &[u8]) -> Vec<(Option<String>, Vec<u8>)> {
continue;
}
l = &l[1..];
if let Some(pos) = l.find('"') {
if let Some(pos) = l.rfind('"') {
l = &l[..pos];
} else {
continue;
Expand Down
Loading

0 comments on commit 6dd62e8

Please sign in to comment.