Index: lib/Fuzzer/CMakeLists.txt =================================================================== --- lib/Fuzzer/CMakeLists.txt +++ lib/Fuzzer/CMakeLists.txt @@ -8,6 +8,9 @@ "LLVM_USE_SANITIZE_COVERAGE=YES to be set." ) endif() + + add_subdirectory(Support) + add_library(LLVMFuzzerNoMainObjects OBJECT FuzzerCrossOver.cpp FuzzerDriver.cpp @@ -24,20 +27,19 @@ FuzzerTracePC.cpp FuzzerTraceState.cpp FuzzerUtil.cpp - FuzzerUtilDarwin.cpp - FuzzerUtilLinux.cpp - FuzzerUtilPosix.cpp - FuzzerUtilWindows.cpp ) add_library(LLVMFuzzerNoMain STATIC $ ) target_link_libraries(LLVMFuzzerNoMain ${PTHREAD_LIB}) + target_link_libraries(LLVMFuzzerNoMain LLVMFuzzerSupport) + add_library(LLVMFuzzer STATIC FuzzerMain.cpp $ ) target_link_libraries(LLVMFuzzer ${PTHREAD_LIB}) + target_link_libraries(LLVMFuzzer LLVMFuzzerSupport) if( LLVM_INCLUDE_TESTS ) add_subdirectory(test) Index: lib/Fuzzer/FuzzerDriver.cpp =================================================================== --- lib/Fuzzer/FuzzerDriver.cpp +++ lib/Fuzzer/FuzzerDriver.cpp @@ -16,6 +16,7 @@ #include "FuzzerMutate.h" #include "FuzzerRandom.h" #include "FuzzerTracePC.h" +#include "Support/Util.h" #include #include #include Index: lib/Fuzzer/FuzzerLoop.cpp =================================================================== --- lib/Fuzzer/FuzzerLoop.cpp +++ lib/Fuzzer/FuzzerLoop.cpp @@ -15,6 +15,7 @@ #include "FuzzerMutate.h" #include "FuzzerRandom.h" #include "FuzzerTracePC.h" +#include "Support/Util.h" #include #include #include Index: lib/Fuzzer/FuzzerMerge.cpp =================================================================== --- lib/Fuzzer/FuzzerMerge.cpp +++ lib/Fuzzer/FuzzerMerge.cpp @@ -13,7 +13,7 @@ #include "FuzzerIO.h" #include "FuzzerMerge.h" #include "FuzzerTracePC.h" -#include "FuzzerUtil.h" +#include "Support/Util.h" #include #include Index: lib/Fuzzer/FuzzerMutate.cpp =================================================================== --- lib/Fuzzer/FuzzerMutate.cpp +++ lib/Fuzzer/FuzzerMutate.cpp @@ -15,6 +15,7 @@ #include "FuzzerIO.h" #include "FuzzerMutate.h" #include "FuzzerOptions.h" +#include "Support/Util.h" namespace fuzzer { Index: lib/Fuzzer/FuzzerTracePC.cpp =================================================================== --- lib/Fuzzer/FuzzerTracePC.cpp +++ lib/Fuzzer/FuzzerTracePC.cpp @@ -19,6 +19,7 @@ #include "FuzzerIO.h" #include "FuzzerTracePC.h" #include "FuzzerValueBitMap.h" +#include "Support/Util.h" #include #include #include Index: lib/Fuzzer/FuzzerTraceState.cpp =================================================================== --- lib/Fuzzer/FuzzerTraceState.cpp +++ lib/Fuzzer/FuzzerTraceState.cpp @@ -15,6 +15,7 @@ #include "FuzzerMutate.h" #include "FuzzerRandom.h" #include "FuzzerTracePC.h" +#include "Support/Util.h" #include #include #include Index: lib/Fuzzer/FuzzerUtil.h =================================================================== --- lib/Fuzzer/FuzzerUtil.h +++ lib/Fuzzer/FuzzerUtil.h @@ -39,34 +39,6 @@ std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC); -unsigned NumberOfCpuCores(); - -bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out); - -// Platform specific functions. -void SetSignalHandler(const FuzzingOptions& Options); - -void SleepSeconds(int Seconds); - -unsigned long GetPid(); - -size_t GetPeakRSSMb(); - -int ExecuteCommand(const std::string &Command); - -FILE *OpenProcessPipe(const char *Command, const char *Mode); - -const void *SearchMemory(const void *haystack, size_t haystacklen, - const void *needle, size_t needlelen); - -std::string CloneArgsWithoutX(const std::vector &Args, - const char *X1, const char *X2); - -inline std::string CloneArgsWithoutX(const std::vector &Args, - const char *X) { - return CloneArgsWithoutX(Args, X, X); -} - } // namespace fuzzer #endif // LLVM_FUZZER_UTIL_H Index: lib/Fuzzer/FuzzerUtil.cpp =================================================================== --- lib/Fuzzer/FuzzerUtil.cpp +++ lib/Fuzzer/FuzzerUtil.cpp @@ -195,24 +195,4 @@ Printf(FallbackFMT, PC); } -unsigned NumberOfCpuCores() { - unsigned N = std::thread::hardware_concurrency(); - if (!N) { - Printf("WARNING: std::thread::hardware_concurrency not well defined for " - "your platform. Assuming CPU count of 1.\n"); - N = 1; - } - return N; -} - -bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out) { - FILE *Pipe = OpenProcessPipe(Command.c_str(), "r"); - if (!Pipe) return false; - char Buff[1024]; - size_t N; - while ((N = fread(Buff, 1, sizeof(Buff), Pipe)) > 0) - Out->append(Buff, N); - return true; -} - } // namespace fuzzer Index: lib/Fuzzer/Support/CMakeLists.txt =================================================================== --- /dev/null +++ lib/Fuzzer/Support/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library(LLVMFuzzerSupport STATIC + Util.cpp + UtilDarwin.cpp + UtilLinux.cpp + UtilPosix.cpp + UtilWindows.cpp + ) Index: lib/Fuzzer/Support/Util.h =================================================================== --- /dev/null +++ lib/Fuzzer/Support/Util.h @@ -0,0 +1,53 @@ +//===- Util.h - Internal header for the Fuzzer Utils ------------*- C++ -* ===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// Util portable functions. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_FUZZER_SUPPORT_UTIL_H +#define LLVM_FUZZER_SUPPORT_UTIL_H + +#include "../FuzzerDefs.h" +#include +#include + +namespace fuzzer { + +typedef std::vector Unit; + +unsigned NumberOfCpuCores(); + +int ExecuteCommand(const std::string &Command); + +bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out); + +FILE *OpenProcessPipe(const char *Command, const char *Mode); + + +std::string CloneArgsWithoutX(const std::vector &Args, + const char *X1, const char *X2); + +inline std::string CloneArgsWithoutX(const std::vector &Args, + const char *X) { + return CloneArgsWithoutX(Args, X, X); +} + +void SetSignalHandler(const FuzzingOptions& Options); + +void SleepSeconds(int Seconds); + +unsigned long GetPid(); + +size_t GetPeakRSSMb(); + +const void *SearchMemory(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); + +} // namespace fuzzer + +#endif // LLVM_FUZZER_SUPPORT_UTIL_H Index: lib/Fuzzer/Support/Util.cpp =================================================================== --- /dev/null +++ lib/Fuzzer/Support/Util.cpp @@ -0,0 +1,44 @@ +//===- Util.cpp - Misc utils ----------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// Util portable functions. +//===----------------------------------------------------------------------===// + +#include "Util.h" +#include "../FuzzerIO.h" +#include +#include +#include +#include +#include +#include +#include + +namespace fuzzer { + +unsigned NumberOfCpuCores() { + unsigned N = std::thread::hardware_concurrency(); + if (!N) { + Printf("WARNING: std::thread::hardware_concurrency not well defined for " + "your platform. Assuming CPU count of 1.\n"); + N = 1; + } + return N; +} + +bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out) { + FILE *Pipe = OpenProcessPipe(Command.c_str(), "r"); + if (!Pipe) return false; + char Buff[1024]; + size_t N; + while ((N = fread(Buff, 1, sizeof(Buff), Pipe)) > 0) + Out->append(Buff, N); + return true; +} + +} // namespace fuzzer Index: lib/Fuzzer/Support/UtilDarwin.cpp =================================================================== --- lib/Fuzzer/Support/UtilDarwin.cpp +++ lib/Fuzzer/Support/UtilDarwin.cpp @@ -1,4 +1,4 @@ -//===- FuzzerUtilDarwin.cpp - Misc utils ----------------------------------===// +//===- UtilDarwin.cpp - Misc utils ----------------------------------------===// // // The LLVM Compiler Infrastructure // @@ -8,10 +8,10 @@ //===----------------------------------------------------------------------===// // Misc utils for Darwin. //===----------------------------------------------------------------------===// -#include "Support/Platform.h" +#include "Platform.h" #if LIBFUZZER_APPLE -#include "FuzzerIO.h" +#include "../FuzzerIO.h" #include #include #include Index: lib/Fuzzer/Support/UtilLinux.cpp =================================================================== --- lib/Fuzzer/Support/UtilLinux.cpp +++ lib/Fuzzer/Support/UtilLinux.cpp @@ -1,4 +1,4 @@ -//===- FuzzerUtilLinux.cpp - Misc utils for Linux. ------------------------===// +//===- UtilLinux.cpp - Misc utils for Linux. ------------------------------===// // // The LLVM Compiler Infrastructure // @@ -8,10 +8,11 @@ //===----------------------------------------------------------------------===// // Misc utils for Linux. //===----------------------------------------------------------------------===// -#include "Support/Platform.h" +#include "Platform.h" #if LIBFUZZER_LINUX #include +#include namespace fuzzer { Index: lib/Fuzzer/Support/UtilPosix.cpp =================================================================== --- lib/Fuzzer/Support/UtilPosix.cpp +++ lib/Fuzzer/Support/UtilPosix.cpp @@ -1,4 +1,4 @@ -//===- FuzzerUtilPosix.cpp - Misc utils for Posix. ------------------------===// +//===- UtilPosix.cpp - Misc utils for Posix. ------------------------------===// // // The LLVM Compiler Infrastructure // @@ -8,10 +8,10 @@ //===----------------------------------------------------------------------===// // Misc utils implementation using Posix API. //===----------------------------------------------------------------------===// -#include "Support/Platform.h" +#include "Platform.h" #if LIBFUZZER_POSIX -#include "FuzzerIO.h" -#include "FuzzerInternal.h" +#include "../FuzzerIO.h" +#include "../FuzzerInternal.h" #include #include #include Index: lib/Fuzzer/Support/UtilWindows.cpp =================================================================== --- lib/Fuzzer/Support/UtilWindows.cpp +++ lib/Fuzzer/Support/UtilWindows.cpp @@ -1,4 +1,4 @@ -//===- FuzzerUtilWindows.cpp - Misc utils for Windows. --------------------===// +//===- UtilWindows.cpp - Misc utils for Windows. --------------------------===// // // The LLVM Compiler Infrastructure // @@ -8,10 +8,10 @@ //===----------------------------------------------------------------------===// // Misc utils implementation for Windows. //===----------------------------------------------------------------------===// -#include "Support/Platform.h" +#include "Platform.h" #if LIBFUZZER_WINDOWS -#include "FuzzerIO.h" -#include "FuzzerInternal.h" +#include "../FuzzerIO.h" +#include "../FuzzerInternal.h" #include #include #include