Index: lib/Fuzzer/FuzzerCorpus.h =================================================================== --- lib/Fuzzer/FuzzerCorpus.h +++ lib/Fuzzer/FuzzerCorpus.h @@ -16,6 +16,8 @@ #include #include "FuzzerDefs.h" +#include "FuzzerIO.h" +#include "FuzzerSHA1.h" #include "FuzzerRandom.h" #include "FuzzerTracePC.h" Index: lib/Fuzzer/FuzzerDefs.h =================================================================== --- lib/Fuzzer/FuzzerDefs.h +++ lib/Fuzzer/FuzzerDefs.h @@ -63,64 +63,8 @@ typedef std::vector Unit; typedef std::vector UnitVector; typedef int (*UserCallback)(const uint8_t *Data, size_t Size); -int FuzzerDriver(int *argc, char ***argv, UserCallback Callback); - -bool IsFile(const std::string &Path); -long GetEpoch(const std::string &Path); -std::string FileToString(const std::string &Path); -Unit FileToVector(const std::string &Path, size_t MaxSize = 0, - bool ExitOnError = true); -void ReadDirToVectorOfUnits(const char *Path, std::vector *V, - long *Epoch, size_t MaxSize, bool ExitOnError); -void WriteToFile(const Unit &U, const std::string &Path); -void CopyFileToErr(const std::string &Path); -void DeleteFile(const std::string &Path); -// Returns "Dir/FileName" or equivalent for the current OS. -std::string DirPlusFile(const std::string &DirPath, - const std::string &FileName); - -void DupAndCloseStderr(); -void CloseStdout(); -void Printf(const char *Fmt, ...); -void PrintHexArray(const Unit &U, const char *PrintAfter = ""); -void PrintHexArray(const uint8_t *Data, size_t Size, - const char *PrintAfter = ""); -void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = ""); -void PrintASCII(const Unit &U, const char *PrintAfter = ""); - -void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC); -std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC); -std::string Hash(const Unit &U); -void SetTimer(int Seconds); -void SetSigSegvHandler(); -void SetSigBusHandler(); -void SetSigAbrtHandler(); -void SetSigIllHandler(); -void SetSigFpeHandler(); -void SetSigIntHandler(); -void SetSigTermHandler(); -std::string Base64(const Unit &U); -int ExecuteCommand(const std::string &Command); -bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out); - -size_t GetPeakRSSMb(); - -// Private copy of SHA1 implementation. -static const int kSHA1NumBytes = 20; -// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'. -void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out); -std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]); - -// Changes U to contain only ASCII (isprint+isspace) characters. -// Returns true iff U has been changed. -bool ToASCII(uint8_t *Data, size_t Size); -bool IsASCII(const Unit &U); -bool IsASCII(const uint8_t *Data, size_t Size); - -int NumberOfCpuCores(); -int GetPid(); -void SleepSeconds(int Seconds); +int FuzzerDriver(int *argc, char ***argv, UserCallback Callback); struct ScopedDoingMyOwnMemmem { ScopedDoingMyOwnMemmem(); Index: lib/Fuzzer/FuzzerDictionary.h =================================================================== --- lib/Fuzzer/FuzzerDictionary.h +++ lib/Fuzzer/FuzzerDictionary.h @@ -12,11 +12,12 @@ #ifndef LLVM_FUZZER_DICTIONARY_H #define LLVM_FUZZER_DICTIONARY_H +#include "FuzzerDefs.h" +#include "FuzzerIO.h" +#include "FuzzerUtil.h" #include #include -#include "FuzzerDefs.h" - namespace fuzzer { // A simple POD sized array of bytes. template class FixedWord { Index: lib/Fuzzer/FuzzerDriver.cpp =================================================================== --- lib/Fuzzer/FuzzerDriver.cpp +++ lib/Fuzzer/FuzzerDriver.cpp @@ -12,6 +12,7 @@ #include "FuzzerCorpus.h" #include "FuzzerInterface.h" #include "FuzzerInternal.h" +#include "FuzzerIO.h" #include "FuzzerMutate.h" #include "FuzzerRandom.h" Index: lib/Fuzzer/FuzzerExtFunctionsWeak.cpp =================================================================== --- lib/Fuzzer/FuzzerExtFunctionsWeak.cpp +++ lib/Fuzzer/FuzzerExtFunctionsWeak.cpp @@ -15,6 +15,7 @@ #include "FuzzerDefs.h" #if LIBFUZZER_LINUX +#include "FuzzerIO.h" #include "FuzzerExtFunctions.h" extern "C" { Index: lib/Fuzzer/FuzzerIO.h =================================================================== --- /dev/null +++ lib/Fuzzer/FuzzerIO.h @@ -0,0 +1,58 @@ +//===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// IO interface. +//===----------------------------------------------------------------------===// +#ifndef LLVM_FUZZER_IO_H +#define LLVM_FUZZER_IO_H + +#include "FuzzerDefs.h" + +namespace fuzzer { + +long GetEpoch(const std::string &Path); + +Unit FileToVector(const std::string &Path, size_t MaxSize = 0, + bool ExitOnError = true); + +void DeleteFile(const std::string &Path); + +std::string FileToString(const std::string &Path); + +void CopyFileToErr(const std::string &Path); + +void WriteToFile(const Unit &U, const std::string &Path); + +void ReadDirToVectorOfUnits(const char *Path, std::vector *V, + long *Epoch, size_t MaxSize, bool ExitOnError); + +// Returns "Dir/FileName" or equivalent for the current OS. +std::string DirPlusFile(const std::string &DirPath, + const std::string &FileName); + +void DupAndCloseStderr(); + +void CloseStdout(); + +void Printf(const char *Fmt, ...); + +bool IsFile(const std::string &Path); + +void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, + std::vector *V, bool TopDir); + +char GetSeparator(); + +FILE* OpenFile(int Fd, const char *Mode); + +int CloseFile(int Fd); + +int DuplicateFile(int Fd); + +} // namespace fuzzer +#endif // LLVM_FUZZER_IO_H Index: lib/Fuzzer/FuzzerIO.cpp =================================================================== --- lib/Fuzzer/FuzzerIO.cpp +++ lib/Fuzzer/FuzzerIO.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// // IO functions. //===----------------------------------------------------------------------===// +#include "FuzzerIO.h" #include "FuzzerExtFunctions.h" #include "FuzzerDefs.h" #include @@ -37,8 +38,8 @@ return St.st_mtime; } -static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, - std::vector *V, bool TopDir) { +void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, + std::vector *V, bool TopDir) { auto E = GetEpoch(Dir); if (Epoch) if (E && *Epoch >= E) return; Index: lib/Fuzzer/FuzzerInternal.h =================================================================== --- lib/Fuzzer/FuzzerInternal.h +++ lib/Fuzzer/FuzzerInternal.h @@ -23,6 +23,7 @@ #include "FuzzerExtFunctions.h" #include "FuzzerInterface.h" #include "FuzzerOptions.h" +#include "FuzzerSHA1.h" #include "FuzzerValueBitMap.h" namespace fuzzer { Index: lib/Fuzzer/FuzzerLoop.cpp =================================================================== --- lib/Fuzzer/FuzzerLoop.cpp +++ lib/Fuzzer/FuzzerLoop.cpp @@ -11,14 +11,15 @@ #include "FuzzerInternal.h" #include "FuzzerCorpus.h" +#include "FuzzerIO.h" #include "FuzzerMutate.h" #include "FuzzerTracePC.h" #include "FuzzerRandom.h" #include #include -#include #include +#include #if defined(__has_include) #if __has_include() Index: lib/Fuzzer/FuzzerMutate.cpp =================================================================== --- lib/Fuzzer/FuzzerMutate.cpp +++ lib/Fuzzer/FuzzerMutate.cpp @@ -9,11 +9,10 @@ // Mutate a test input. //===----------------------------------------------------------------------===// -#include - #include "FuzzerCorpus.h" #include "FuzzerDefs.h" #include "FuzzerExtFunctions.h" +#include "FuzzerIO.h" #include "FuzzerMutate.h" #include "FuzzerOptions.h" Index: lib/Fuzzer/FuzzerSHA1.h =================================================================== --- /dev/null +++ lib/Fuzzer/FuzzerSHA1.h @@ -0,0 +1,31 @@ +//===- FuzzerSHA1.h - Internal header for the SHA1 utils --------*- C++ -* ===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// SHA1 utils. +//===----------------------------------------------------------------------===// +#ifndef LLVM_FUZZER_SHA1_H +#define LLVM_FUZZER_SHA1_H + +#include "FuzzerDefs.h" +#include +#include + +namespace fuzzer { + +// Private copy of SHA1 implementation. +static const int kSHA1NumBytes = 20; + +// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'. +void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out); + +std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]); + +std::string Hash(const Unit &U); + +} // namespace fuzzer +#endif // LLVM_FUZZER_SHA1_H Index: lib/Fuzzer/FuzzerSHA1.cpp =================================================================== --- lib/Fuzzer/FuzzerSHA1.cpp +++ lib/Fuzzer/FuzzerSHA1.cpp @@ -16,12 +16,15 @@ // For the same reason we do not want to depend on SHA1 from LLVM tree. //===----------------------------------------------------------------------===// +#include "FuzzerSHA1.h" #include "FuzzerDefs.h" /* This code is public-domain - it is based on libcrypt * placed in the public domain by Wei Dai and other contributors. */ +#include +#include #include #include @@ -193,10 +196,27 @@ } // namespace; Added for LibFuzzer +namespace fuzzer { + // The rest is added for LibFuzzer -void fuzzer::ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) { +void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out) { sha1nfo s; sha1_init(&s); sha1_write(&s, (const char*)Data, Len); memcpy(Out, sha1_result(&s), HASH_LENGTH); } + +std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) { + std::stringstream SS; + for (int i = 0; i < kSHA1NumBytes; i++) + SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i]; + return SS.str(); +} + +std::string Hash(const Unit &U) { + uint8_t Hash[kSHA1NumBytes]; + ComputeSHA1(U.data(), U.size(), Hash); + return Sha1ToString(Hash); +} + +} Index: lib/Fuzzer/FuzzerTracePC.cpp =================================================================== --- lib/Fuzzer/FuzzerTracePC.cpp +++ lib/Fuzzer/FuzzerTracePC.cpp @@ -12,16 +12,16 @@ // //===----------------------------------------------------------------------===// -#include -#include -#include - #include "FuzzerCorpus.h" #include "FuzzerDefs.h" #include "FuzzerDictionary.h" #include "FuzzerExtFunctions.h" +#include "FuzzerIO.h" #include "FuzzerTracePC.h" #include "FuzzerValueBitMap.h" +#include +#include +#include namespace fuzzer { Index: lib/Fuzzer/FuzzerTraceState.cpp =================================================================== --- lib/Fuzzer/FuzzerTraceState.cpp +++ lib/Fuzzer/FuzzerTraceState.cpp @@ -11,15 +11,15 @@ #include "FuzzerInternal.h" #include "FuzzerDictionary.h" +#include "FuzzerIO.h" #include "FuzzerMutate.h" #include "FuzzerRandom.h" #include "FuzzerTracePC.h" - #include #include -#include #include #include +#include namespace fuzzer { Index: lib/Fuzzer/FuzzerUtil.h =================================================================== --- /dev/null +++ lib/Fuzzer/FuzzerUtil.h @@ -0,0 +1,70 @@ +//===- FuzzerUtil.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 functions. +//===----------------------------------------------------------------------===// +#ifndef LLVM_FUZZER_UTIL_H +#define LLVM_FUZZER_UTIL_H + +#include "FuzzerDefs.h" + +namespace fuzzer { + +void PrintHexArray(const Unit &U, const char *PrintAfter = ""); + +void PrintHexArray(const uint8_t *Data, size_t Size, + const char *PrintAfter = ""); + +void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = ""); + +void PrintASCII(const Unit &U, const char *PrintAfter = ""); + +// Changes U to contain only ASCII (isprint+isspace) characters. +// Returns true iff U has been changed. +bool ToASCII(uint8_t *Data, size_t Size); + +bool IsASCII(const Unit &U); + +bool IsASCII(const uint8_t *Data, size_t Size); + +std::string Base64(const Unit &U); + +void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC); + +std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC); + +int NumberOfCpuCores(); + +// Platform specific functions. +void SetTimer(int Seconds); + +void SetSigSegvHandler(); +void SetSigBusHandler(); +void SetSigAbrtHandler(); +void SetSigIllHandler(); +void SetSigFpeHandler(); +void SetSigIntHandler(); +void SetSigTermHandler(); + +void SleepSeconds(int Seconds); + +int GetPid(); + +size_t GetPeakRSSMb(); + +bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out); + +int ExecuteCommand(const std::string &Command); + +#if LIBFUZZER_WINDOWS +const void *memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); +#endif + +} // namespace fuzzer +#endif // LLVM_FUZZER_UTIL_H Index: lib/Fuzzer/FuzzerUtil.cpp =================================================================== --- lib/Fuzzer/FuzzerUtil.cpp +++ lib/Fuzzer/FuzzerUtil.cpp @@ -9,7 +9,9 @@ // Misc utils. //===----------------------------------------------------------------------===// +#include "FuzzerUtil.h" #include "FuzzerInternal.h" +#include "FuzzerIO.h" #include #include #include @@ -60,19 +62,6 @@ PrintASCII(U.data(), U.size(), PrintAfter); } -std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]) { - std::stringstream SS; - for (int i = 0; i < kSHA1NumBytes; i++) - SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i]; - return SS.str(); -} - -std::string Hash(const Unit &U) { - uint8_t Hash[kSHA1NumBytes]; - ComputeSHA1(U.data(), U.size(), Hash); - return Sha1ToString(Hash); -} - static void AlarmHandler(int, siginfo_t *, void *) { Fuzzer::StaticAlarmCallback(); } Index: lib/Fuzzer/FuzzerUtilDarwin.cpp =================================================================== --- lib/Fuzzer/FuzzerUtilDarwin.cpp +++ lib/Fuzzer/FuzzerUtilDarwin.cpp @@ -10,6 +10,7 @@ //===----------------------------------------------------------------------===// #include "FuzzerDefs.h" #if LIBFUZZER_APPLE +#include "FuzzerIO.h" #include #include #include Index: lib/Fuzzer/FuzzerUtilLinux.cpp =================================================================== --- lib/Fuzzer/FuzzerUtilLinux.cpp +++ lib/Fuzzer/FuzzerUtilLinux.cpp @@ -1,4 +1,4 @@ -//===- FuzzerUtilLinux.cpp - Misc utils -----------------------------------===// +//===- FuzzerUtilLinux.cpp - Misc utils for Linux. ------------------------===// // // The LLVM Compiler Infrastructure //