Home | History | Annotate | Download | only in Fuzzer
      1 //===- FuzzerDefs.h - Internal header for the Fuzzer ------------*- C++ -* ===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 // Basic definitions.
     10 //===----------------------------------------------------------------------===//
     11 #ifndef LLVM_FUZZER_DEFS_H
     12 #define LLVM_FUZZER_DEFS_H
     13 
     14 #include <cassert>
     15 #include <cstddef>
     16 #include <cstdint>
     17 #include <cstring>
     18 #include <string>
     19 #include <vector>
     20 
     21 // Platform detection.
     22 #ifdef __linux__
     23 #define LIBFUZZER_LINUX 1
     24 #define LIBFUZZER_APPLE 0
     25 #elif __APPLE__
     26 #define LIBFUZZER_LINUX 0
     27 #define LIBFUZZER_APPLE 1
     28 #else
     29 #error "Support for your platform has not been implemented"
     30 #endif
     31 
     32 #ifdef __x86_64
     33 #define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
     34 #else
     35 #define ATTRIBUTE_TARGET_POPCNT
     36 #endif
     37 
     38 namespace fuzzer {
     39 
     40 template <class T> T Min(T a, T b) { return a < b ? a : b; }
     41 template <class T> T Max(T a, T b) { return a > b ? a : b; }
     42 
     43 class Random;
     44 class Dictionary;
     45 class DictionaryEntry;
     46 class MutationDispatcher;
     47 struct FuzzingOptions;
     48 class InputCorpus;
     49 struct InputInfo;
     50 struct ExternalFunctions;
     51 
     52 // Global interface to functions that may or may not be available.
     53 extern ExternalFunctions *EF;
     54 
     55 typedef std::vector<uint8_t> Unit;
     56 typedef std::vector<Unit> UnitVector;
     57 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
     58 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
     59 
     60 bool IsFile(const std::string &Path);
     61 long GetEpoch(const std::string &Path);
     62 std::string FileToString(const std::string &Path);
     63 Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
     64                   bool ExitOnError = true);
     65 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
     66                             long *Epoch, size_t MaxSize, bool ExitOnError);
     67 void WriteToFile(const Unit &U, const std::string &Path);
     68 void CopyFileToErr(const std::string &Path);
     69 void DeleteFile(const std::string &Path);
     70 // Returns "Dir/FileName" or equivalent for the current OS.
     71 std::string DirPlusFile(const std::string &DirPath,
     72                         const std::string &FileName);
     73 
     74 void DupAndCloseStderr();
     75 void CloseStdout();
     76 void Printf(const char *Fmt, ...);
     77 void PrintHexArray(const Unit &U, const char *PrintAfter = "");
     78 void PrintHexArray(const uint8_t *Data, size_t Size,
     79                    const char *PrintAfter = "");
     80 void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
     81 void PrintASCII(const Unit &U, const char *PrintAfter = "");
     82 
     83 void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
     84 std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
     85 std::string Hash(const Unit &U);
     86 void SetTimer(int Seconds);
     87 void SetSigSegvHandler();
     88 void SetSigBusHandler();
     89 void SetSigAbrtHandler();
     90 void SetSigIllHandler();
     91 void SetSigFpeHandler();
     92 void SetSigIntHandler();
     93 void SetSigTermHandler();
     94 std::string Base64(const Unit &U);
     95 int ExecuteCommand(const std::string &Command);
     96 bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out);
     97 
     98 size_t GetPeakRSSMb();
     99 
    100 // Private copy of SHA1 implementation.
    101 static const int kSHA1NumBytes = 20;
    102 // Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
    103 void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
    104 std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
    105 
    106 // Changes U to contain only ASCII (isprint+isspace) characters.
    107 // Returns true iff U has been changed.
    108 bool ToASCII(uint8_t *Data, size_t Size);
    109 bool IsASCII(const Unit &U);
    110 bool IsASCII(const uint8_t *Data, size_t Size);
    111 
    112 int NumberOfCpuCores();
    113 int GetPid();
    114 void SleepSeconds(int Seconds);
    115 
    116 
    117 struct ScopedDoingMyOwnMemmem {
    118   ScopedDoingMyOwnMemmem();
    119   ~ScopedDoingMyOwnMemmem();
    120 };
    121 
    122 inline uint8_t  Bswap(uint8_t x)  { return x; }
    123 inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
    124 inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
    125 inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
    126 
    127 }  // namespace fuzzer
    128 #endif  // LLVM_FUZZER_DEFS_H
    129