-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libFuzzer] refactoring: split the large header into many; NFC
llvm-svn: 282044
- Loading branch information
Showing
16 changed files
with
539 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===- FuzzerCorpus.h - Internal header for the Fuzzer ----------*- C++ -* ===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// fuzzer::InputCorpus | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_FUZZER_CORPUS | ||
#define LLVM_FUZZER_CORPUS | ||
|
||
#include "FuzzerDefs.h" | ||
|
||
namespace fuzzer { | ||
|
||
struct InputInfo { | ||
Unit U; // The actual input data. | ||
}; | ||
|
||
class InputCorpus { | ||
public: | ||
InputCorpus() { | ||
Corpus.reserve(1 << 14); // Avoid too many resizes. | ||
} | ||
size_t size() const { return Corpus.size(); } | ||
bool empty() const { return Corpus.empty(); } | ||
const Unit &operator[] (size_t Idx) const { return Corpus[Idx].U; } | ||
void Append(const std::vector<Unit> &V) { | ||
for (auto &U : V) | ||
push_back(U); | ||
} | ||
void push_back(const Unit &U) { | ||
auto H = Hash(U); | ||
if (!Hashes.insert(H).second) return; | ||
InputInfo II; | ||
II.U = U; | ||
Corpus.push_back(II); | ||
} | ||
|
||
typedef const std::vector<InputInfo>::const_iterator ConstIter; | ||
ConstIter begin() const { return Corpus.begin(); } | ||
ConstIter end() const { return Corpus.end(); } | ||
|
||
bool HasUnit(const Unit &U) { return Hashes.count(Hash(U)); } | ||
|
||
private: | ||
std::unordered_set<std::string> Hashes; | ||
std::vector<InputInfo> Corpus; | ||
}; | ||
|
||
} // namespace fuzzer | ||
|
||
#endif // LLVM_FUZZER_CORPUS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//===- FuzzerDefs.h - Internal header for the Fuzzer ------------*- C++ -* ===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// Basic definitions. | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_FUZZER_DEFS_H | ||
#define LLVM_FUZZER_DEFS_H | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
// 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 | ||
|
||
#ifdef __x86_64 | ||
#define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt"))) | ||
#else | ||
#define ATTRIBUTE_TARGET_POPCNT | ||
#endif | ||
|
||
namespace fuzzer { | ||
|
||
class Random; | ||
class Dictionary; | ||
class DictionaryEntry; | ||
class MutationDispatcher; | ||
|
||
typedef std::vector<uint8_t> Unit; | ||
typedef std::vector<Unit> 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); | ||
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, | ||
long *Epoch, size_t MaxSize); | ||
void WriteToFile(const Unit &U, const std::string &Path); | ||
void CopyFileToErr(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 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); | ||
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(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); | ||
|
||
} // namespace fuzzer | ||
#endif // LLVM_FUZZER_DEFS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// fuzzer::Dictionary | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_FUZZER_DICTIONARY_H | ||
#define LLVM_FUZZER_DICTIONARY_H | ||
|
||
#include "FuzzerDefs.h" | ||
|
||
namespace fuzzer { | ||
// A simple POD sized array of bytes. | ||
template <size_t kMaxSize> class FixedWord { | ||
public: | ||
FixedWord() {} | ||
FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); } | ||
|
||
void Set(const uint8_t *B, uint8_t S) { | ||
assert(S <= kMaxSize); | ||
memcpy(Data, B, S); | ||
Size = S; | ||
} | ||
|
||
bool operator==(const FixedWord<kMaxSize> &w) const { | ||
return Size == w.Size && 0 == memcmp(Data, w.Data, Size); | ||
} | ||
|
||
bool operator<(const FixedWord<kMaxSize> &w) const { | ||
if (Size != w.Size) | ||
return Size < w.Size; | ||
return memcmp(Data, w.Data, Size) < 0; | ||
} | ||
|
||
static size_t GetMaxSize() { return kMaxSize; } | ||
const uint8_t *data() const { return Data; } | ||
uint8_t size() const { return Size; } | ||
|
||
private: | ||
uint8_t Size = 0; | ||
uint8_t Data[kMaxSize]; | ||
}; | ||
|
||
typedef FixedWord<27> Word; // 28 bytes. | ||
|
||
class DictionaryEntry { | ||
public: | ||
DictionaryEntry() {} | ||
DictionaryEntry(Word W) : W(W) {} | ||
DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {} | ||
const Word &GetW() const { return W; } | ||
|
||
bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); } | ||
size_t GetPositionHint() const { | ||
assert(HasPositionHint()); | ||
return PositionHint; | ||
} | ||
void IncUseCount() { UseCount++; } | ||
void IncSuccessCount() { SuccessCount++; } | ||
size_t GetUseCount() const { return UseCount; } | ||
size_t GetSuccessCount() const {return SuccessCount; } | ||
|
||
private: | ||
Word W; | ||
size_t PositionHint = std::numeric_limits<size_t>::max(); | ||
size_t UseCount = 0; | ||
size_t SuccessCount = 0; | ||
}; | ||
|
||
class Dictionary { | ||
public: | ||
static const size_t kMaxDictSize = 1 << 14; | ||
|
||
bool ContainsWord(const Word &W) const { | ||
return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) { | ||
return DE.GetW() == W; | ||
}); | ||
} | ||
const DictionaryEntry *begin() const { return &DE[0]; } | ||
const DictionaryEntry *end() const { return begin() + Size; } | ||
DictionaryEntry & operator[] (size_t Idx) { | ||
assert(Idx < Size); | ||
return DE[Idx]; | ||
} | ||
void push_back(DictionaryEntry DE) { | ||
if (Size < kMaxDictSize) | ||
this->DE[Size++] = DE; | ||
} | ||
void clear() { Size = 0; } | ||
bool empty() const { return Size == 0; } | ||
size_t size() const { return Size; } | ||
|
||
private: | ||
DictionaryEntry DE[kMaxDictSize]; | ||
size_t Size = 0; | ||
}; | ||
|
||
// Parses one dictionary entry. | ||
// If successfull, write the enty to Unit and returns true, | ||
// otherwise returns false. | ||
bool ParseOneDictionaryEntry(const std::string &Str, Unit *U); | ||
// Parses the dictionary file, fills Units, returns true iff all lines | ||
// were parsed succesfully. | ||
bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units); | ||
|
||
} // namespace fuzzer | ||
|
||
#endif // LLVM_FUZZER_DICTIONARY_H | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.