Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleaned-up code, published on crates.io #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[package]
name = "libfuzzer-sys"
version = "0.1.0"
version = "0.1.1"
authors = ["The cargo-fuzz Project Developers"]
repository = "https://github.com/rust-fuzz/libfuzzer-sys"
license = "MIT/Apache-2.0/NCSA"
description = "LibFuzzer wrapper for Rust"

# cargo-fuzz puts this in a crate subdirectory,
# which causes problems if the crate uses workspaces.
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
let custom_lib_dir = custom_lib_path.parent().unwrap().to_string_lossy();

let custom_lib_name = custom_lib_path.file_stem().unwrap().to_string_lossy();
let custom_lib_name = custom_lib_name.trim_left_matches("lib");
let custom_lib_name = custom_lib_name.trim_start_matches("lib");

println!("cargo:rustc-link-search=native={}", custom_lib_dir);
println!("cargo:rustc-link-lib=static={}", custom_lib_name);
Expand Down
34 changes: 18 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
#![allow(improper_ctypes)] // we do not actually cross the FFI bound here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This… feels wrong.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah you're right... that's cargofmt's fault. I moved things around so it's prettier.

extern "C" {
#![allow(improper_ctypes)] // we do not actually cross the FFI bound here

fn rust_fuzzer_test_input(input: &[u8]);
}

#[export_name="LLVMFuzzerTestOneInput"]
#[export_name = "LLVMFuzzerTestOneInput"]
#[allow(clippy::block_in_if_condition_stmt)]
pub fn test_input_wrap(data: *const u8, size: usize) -> i32 {
::std::panic::catch_unwind(|| unsafe {
if ::std::panic::catch_unwind(|| unsafe {
let data_slice = ::std::slice::from_raw_parts(data, size);
rust_fuzzer_test_input(data_slice);
})
.err().map(|_|
.err()
.is_some()
{
// hopefully the custom panic hook will be called before and abort the
// process before the stack frames are unwinded.
::std::process::abort()
);
::std::process::abort();
}
0
}

#[export_name="LLVMFuzzerInitialize"]
#[export_name = "LLVMFuzzerInitialize"]
pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize {
// Registers a panic hook that aborts the process before unwinding.
// It is useful to abort before unwinding so that the fuzzer will then be
// able to analyse the process stack frames to tell different bugs appart.
//
//
// HACK / FIXME: it would be better to use `-C panic=abort` but it's currently
// impossible to build code using compiler plugins with this flag.
// We will be able to remove this code when
// https://github.com/rust-lang/cargo/issues/5423 is fixed.
::std::panic::set_hook(Box::new(|_| {
::std::process::abort();
::std::process::abort();
}));
0
}
Expand All @@ -38,7 +40,7 @@ pub fn initialize(_argc: *const isize, _argv: *const *const *const u8) -> isize
macro_rules! fuzz_target {
(|$bytes:ident| $body:block) => {
#[no_mangle]
pub extern fn rust_fuzzer_test_input($bytes: &[u8]) {
pub extern "C" fn rust_fuzzer_test_input($bytes: &[u8]) {
$body
}
};
Expand All @@ -49,15 +51,15 @@ macro_rules! fuzz_target {
extern crate arbitrary;

#[no_mangle]
pub extern fn rust_fuzzer_test_input(bytes: &[u8]) {
pub extern "C" fn rust_fuzzer_test_input(bytes: &[u8]) {
use arbitrary::{Arbitrary, RingBuffer};

let $data: $dty = if let Ok(d) = RingBuffer::new(bytes, bytes.len()).and_then(|mut b|{
Arbitrary::arbitrary(&mut b).map_err(|_| "")
}) {
let $data: $dty = if let Ok(d) = RingBuffer::new(bytes, bytes.len())
.and_then(|mut b| Arbitrary::arbitrary(&mut b).map_err(|_| ""))
{
d
} else {
return
return;
};
$body
}
Expand Down