From 40fe286cf9e20e5ddf921967c44b0fd4e667d60a Mon Sep 17 00:00:00 2001 From: andreafioraldi Date: Thu, 25 Mar 2021 20:04:52 +0100 Subject: [PATCH] run on win32 using the clang wrapper --- fuzzers/libfuzzer_libpng/src/bin/cc.rs | 8 ++++++-- fuzzers/libfuzzer_libpng/src/bin/cxx.rs | 8 ++++++-- fuzzers/libfuzzer_libpng/src/lib.rs | 2 +- libafl_cc/src/lib.rs | 11 +++++++++++ libafl_targets/libfuzzer_compatibility.c | 17 +++++++++-------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/fuzzers/libfuzzer_libpng/src/bin/cc.rs b/fuzzers/libfuzzer_libpng/src/bin/cc.rs index 8a34130ba4..f8f4ca1d2d 100644 --- a/fuzzers/libfuzzer_libpng/src/bin/cc.rs +++ b/fuzzers/libfuzzer_libpng/src/bin/cc.rs @@ -1,4 +1,4 @@ -use libafl_cc::{ClangWrapper, CompilerWrapper}; +use libafl_cc::{ClangWrapper, CompilerWrapper, LIB_EXT, LIB_PREFIX}; use std::env; fn main() { @@ -11,7 +11,11 @@ fn main() { .unwrap() .add_arg("-fsanitize-coverage=trace-pc-guard".into()) .unwrap() - .add_link_arg(dir.join("liblibfuzzer_libpng.a").display().to_string()) + .add_link_arg( + dir.join(format!("{}libfuzzer_libpng.{}", LIB_PREFIX, LIB_EXT)) + .display() + .to_string(), + ) .unwrap() .run() .unwrap(); diff --git a/fuzzers/libfuzzer_libpng/src/bin/cxx.rs b/fuzzers/libfuzzer_libpng/src/bin/cxx.rs index aaf901835e..bad4aa18d4 100644 --- a/fuzzers/libfuzzer_libpng/src/bin/cxx.rs +++ b/fuzzers/libfuzzer_libpng/src/bin/cxx.rs @@ -1,4 +1,4 @@ -use libafl_cc::{ClangWrapper, CompilerWrapper}; +use libafl_cc::{ClangWrapper, CompilerWrapper, LIB_EXT, LIB_PREFIX}; use std::env; fn main() { @@ -12,7 +12,11 @@ fn main() { .unwrap() .add_arg("-fsanitize-coverage=trace-pc-guard".into()) .unwrap() - .add_link_arg(dir.join("liblibfuzzer_libpng.a").display().to_string()) + .add_link_arg( + dir.join(format!("{}libfuzzer_libpng.{}", LIB_PREFIX, LIB_EXT)) + .display() + .to_string(), + ) .unwrap() .run() .unwrap(); diff --git a/fuzzers/libfuzzer_libpng/src/lib.rs b/fuzzers/libfuzzer_libpng/src/lib.rs index 15d55b30a3..f64e92e385 100644 --- a/fuzzers/libfuzzer_libpng/src/lib.rs +++ b/fuzzers/libfuzzer_libpng/src/lib.rs @@ -131,7 +131,7 @@ fn fuzz(corpus_dirs: Vec, objective_dir: PathBuf, broker_port: u16) -> // The actual target run starts here. // Call LLVMFUzzerInitialize() if present. - let args: Vec = env::args().collect(); + let args: Vec = env::args().collect(); if libfuzzer_initialize(&args) == -1 { println!("Warning: LLVMFuzzerInitialize failed with -1") } diff --git a/libafl_cc/src/lib.rs b/libafl_cc/src/lib.rs index 9b27de117b..4898150c66 100644 --- a/libafl_cc/src/lib.rs +++ b/libafl_cc/src/lib.rs @@ -7,6 +7,17 @@ pub enum Error { Unknown(String), } +// TODO macOS +#[cfg(windows)] +pub const LIB_EXT: &'static str = "lib"; +#[cfg(not(windows))] +pub const LIB_EXT: &'static str = "a"; + +#[cfg(windows)] +pub const LIB_PREFIX: &'static str = ""; +#[cfg(not(windows))] +pub const LIB_PREFIX: &'static str = "lib"; + /// Wrap a compiler hijacking its arguments pub trait CompilerWrapper { /// Set the wrapper arguments parsing a command line set of arguments diff --git a/libafl_targets/libfuzzer_compatibility.c b/libafl_targets/libfuzzer_compatibility.c index 79d2391b4a..8136c7237f 100644 --- a/libafl_targets/libfuzzer_compatibility.c +++ b/libafl_targets/libfuzzer_compatibility.c @@ -31,6 +31,8 @@ #define EXTERNAL_FUNC(Name, Default) \ __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \ Name) "=" WIN_SYM_PREFIX STRINGIFY(Default))) + +#define CHECK_WEAK_FN(Name) (Name != &Name##Def) #else // Declare external functions as weak to allow them to default to a specified // function if not defined explicitly. We must use weak symbols because clang's @@ -38,21 +40,20 @@ // https://bugs.llvm.org/show_bug.cgi?id=40218 for more details. #define EXTERNAL_FUNC(Name, Default) \ __attribute__((weak, alias(STRINGIFY(Default)))) + +#define CHECK_WEAK_FN(Name) (Name != NULL) #endif // LIBFUZZER_MSVC #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \ - RETURN_TYPE NAME##Def FUNC_SIG { \ - printf("ERROR: Function \"%s\" not defined.\n", #NAME); \ - exit(1); \ - } \ + RETURN_TYPE (*NAME##Def) FUNC_SIG = NULL; \ EXTERNAL_FUNC(NAME, NAME##Def) RETURN_TYPE NAME FUNC_SIG - #else // Declare these symbols as weak to allow them to be optionally defined. #define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \ __attribute__((weak, visibility("default"))) RETURN_TYPE NAME FUNC_SIG +#define CHECK_WEAK_FN(Name) (Name != NULL) #endif EXT_FUNC(LLVMFuzzerInitialize, int, (int *argc, char ***argv), false); @@ -68,13 +69,13 @@ EXT_FUNC(LLVMFuzzerCustomCrossOver, size_t, #undef EXT_FUNC int libafl_targets_has_libfuzzer_init() { - return LLVMFuzzerInitialize != NULL; + return CHECK_WEAK_FN(LLVMFuzzerInitialize); } int libafl_targets_libfuzzer_init(int *argc, char ***argv) { - if (LLVMFuzzerInitialize) { + if (libafl_targets_has_libfuzzer_init()) { return LLVMFuzzerInitialize(argc, argv); } else { return 0; } -} +} \ No newline at end of file