Skip to content

Commit

Permalink
Add some code to try to catch segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
louismerlin committed Nov 12, 2024
1 parent 368f950 commit 3c72eb0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"examples/arbitrary",
"examples/asan",
"examples/url",
"examples/segfault",
]

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions examples/segfault/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
output
Cargo.lock
8 changes: 8 additions & 0 deletions examples/segfault/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "segfault-fuzz"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
ziggy = { path = "../../", default-features = false }
5 changes: 5 additions & 0 deletions examples/segfault/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
ziggy::fuzz!(|data: &[u8]| {
unsafe { std::ptr::null_mut::<i32>().write(data[0] as i32) };
});
}
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ pub use honggfuzz::fuzz as honggfuzz_fuzz;
// We open the input file and feed the data to the harness closure.
#[doc(hidden)]
#[cfg(not(any(feature = "afl", feature = "honggfuzz", feature = "coverage")))]
pub fn read_file_and_fuzz<F>(mut closure: F, file: String)
pub fn read_file_and_fuzz<F>(closure: F, file: String)
where
F: FnMut(&[u8]),
F: FnOnce(&[u8]) + std::panic::UnwindSafe,
{
use std::{fs::File, io::Read};
use std::{fs::File, io::{Write, Read}, panic::catch_unwind, process::exit};
println!("Now running file {file}");
let mut buffer: Vec<u8> = Vec::new();
match File::open(file) {
Ok(mut f) => {
match f.read_to_end(&mut buffer) {
Ok(_) => {
closure(buffer.as_slice());
println!("Running");
match catch_unwind(|| closure(&buffer.clone())) {
Err(error) => {
println!("ERROR {error:?}\n");
let _ = std::io::stdout().lock().flush();
exit(321);
},
Ok(_) => {
println!("OK");
let _ = std::io::stdout().lock().flush();
exit(123);
},
};
}
Err(e) => {
println!("Could not get data from file: {e}");
Expand Down

0 comments on commit 3c72eb0

Please sign in to comment.