diff --git a/llvm/lib/Fuzzer/FuzzerInternal.h b/llvm/lib/Fuzzer/FuzzerInternal.h index f43f99f838a5..c3d3f9e1183a 100644 --- a/llvm/lib/Fuzzer/FuzzerInternal.h +++ b/llvm/lib/Fuzzer/FuzzerInternal.h @@ -27,6 +27,17 @@ #include "FuzzerInterface.h" #include "FuzzerTracePC.h" +// Platform detection. +#ifdef __linux__ +#define LIBFUZZER_LINUX 1 +#define LIBFUZZER_APPLE 0 +#elif __APPLE__ +#define LIBFUZZER_LINUX 0 +#define LIBFUZZER_APPLE 1 +#else +#error "Support for your platform has not been implemented" +#endif + namespace fuzzer { typedef int (*UserCallback)(const uint8_t *Data, size_t Size); diff --git a/llvm/lib/Fuzzer/FuzzerLoop.cpp b/llvm/lib/Fuzzer/FuzzerLoop.cpp index 6b19a157e2b4..0adbc891bd0a 100644 --- a/llvm/lib/Fuzzer/FuzzerLoop.cpp +++ b/llvm/lib/Fuzzer/FuzzerLoop.cpp @@ -437,9 +437,19 @@ struct MallocFreeTracer { static thread_local MallocFreeTracer AllocTracer; +// FIXME: The hooks only count on Linux because +// on Mac OSX calls to malloc are intercepted before +// thread local storage is initialised leading to +// crashes when accessing ``AllocTracer``. extern "C" { -void __sanitizer_malloc_hook(void *ptr, size_t size) { AllocTracer.Mallocs++; } -void __sanitizer_free_hook(void *ptr) { AllocTracer.Frees++; } +void __sanitizer_malloc_hook(void *ptr, size_t size) { + if (!LIBFUZZER_APPLE) + AllocTracer.Mallocs++; +} +void __sanitizer_free_hook(void *ptr) { + if (!LIBFUZZER_APPLE) + AllocTracer.Frees++; +} } // extern "C" void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {