Index: lib/Fuzzer/CMakeLists.txt =================================================================== --- lib/Fuzzer/CMakeLists.txt +++ lib/Fuzzer/CMakeLists.txt @@ -1,3 +1,17 @@ +include(CheckCXXSourceCompiles) + +CHECK_CXX_SOURCE_COMPILES(" + static thread_local int blah; + int main() { + return 0; + } + " HAS_THREAD_LOCAL) + +configure_file( + "${PROJECT_SOURCE_DIR}/lib/Fuzzer/FuzzerConfig.h.in" + "${PROJECT_BINARY_DIR}/lib/Fuzzer/FuzzerConfig.h" + ) + set(LIBFUZZER_FLAGS_BASE "${CMAKE_CXX_FLAGS}") # Disable the coverage and sanitizer instrumentation for the fuzzer itself. set(CMAKE_CXX_FLAGS "${LIBFUZZER_FLAGS_BASE} -fno-sanitize-coverage=trace-pc-guard,edge,trace-cmp,indirect-calls,8bit-counters -Werror") Index: lib/Fuzzer/FuzzerInternal.h =================================================================== --- lib/Fuzzer/FuzzerInternal.h +++ lib/Fuzzer/FuzzerInternal.h @@ -12,6 +12,7 @@ #ifndef LLVM_FUZZER_INTERNAL_H #define LLVM_FUZZER_INTERNAL_H +#include "FuzzerConfig.h" #include "FuzzerDefs.h" #include "FuzzerExtFunctions.h" #include "FuzzerInterface.h" @@ -142,7 +143,11 @@ size_t MaxMutationLen = 0; // Need to know our own thread. +#if (HAS_THREAD_LOCAL == 1) static thread_local bool IsMyThread; +#else + static __thread bool IsMyThread; +#endif }; }; // namespace fuzzer Index: lib/Fuzzer/FuzzerLoop.cpp =================================================================== --- lib/Fuzzer/FuzzerLoop.cpp +++ lib/Fuzzer/FuzzerLoop.cpp @@ -9,6 +9,7 @@ // Fuzzer's main loop. //===----------------------------------------------------------------------===// +#include "FuzzerConfig.h" #include "FuzzerCorpus.h" #include "FuzzerInternal.h" #include "FuzzerIO.h" @@ -41,7 +42,11 @@ namespace fuzzer { static const size_t kMaxUnitSizeToPrint = 256; +#if (HAS_THREAD_LOCAL == 1) thread_local bool Fuzzer::IsMyThread; +#else +__thread bool Fuzzer::IsMyThread +#endif SharedMemoryRegion SMR; Index: lib/Fuzzer/build.sh =================================================================== --- lib/Fuzzer/build.sh +++ lib/Fuzzer/build.sh @@ -1,6 +1,9 @@ #!/bin/bash LIBFUZZER_SRC_DIR=$(dirname $0) CXX="${CXX:-clang}" +cat < $LIBFUZZER_SRC_DIR/FuzzerConfig.h +#define HAS_THREAD_LOCAL 1 +EOF for f in $LIBFUZZER_SRC_DIR/*.cpp; do $CXX -g -O2 -fno-omit-frame-pointer -std=c++11 $f -c & done @@ -8,4 +11,4 @@ rm -f libFuzzer.a ar ru libFuzzer.a Fuzzer*.o rm -f Fuzzer*.o - +rm -f $LIBFUZZER_SRC_DIR/FuzzerConfig.h