FuzzerUtil.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===- FuzzerUtil.h - Internal header for the Fuzzer Utils ------*- C++ -* ===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Util functions.
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_UTIL_H
  11. #define LLVM_FUZZER_UTIL_H
  12. #include "FuzzerBuiltins.h"
  13. #include "FuzzerBuiltinsMsvc.h"
  14. #include "FuzzerCommand.h"
  15. #include "FuzzerDefs.h"
  16. namespace fuzzer {
  17. void PrintHexArray(const Unit &U, const char *PrintAfter = "");
  18. void PrintHexArray(const uint8_t *Data, size_t Size,
  19. const char *PrintAfter = "");
  20. void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
  21. void PrintASCII(const Unit &U, const char *PrintAfter = "");
  22. // Changes U to contain only ASCII (isprint+isspace) characters.
  23. // Returns true iff U has been changed.
  24. bool ToASCII(uint8_t *Data, size_t Size);
  25. bool IsASCII(const Unit &U);
  26. bool IsASCII(const uint8_t *Data, size_t Size);
  27. std::string Base64(const Unit &U);
  28. void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
  29. std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
  30. void PrintStackTrace();
  31. void PrintMemoryProfile();
  32. unsigned NumberOfCpuCores();
  33. // Platform specific functions.
  34. void SetSignalHandler(const FuzzingOptions& Options);
  35. void SleepSeconds(int Seconds);
  36. unsigned long GetPid();
  37. size_t GetPeakRSSMb();
  38. int ExecuteCommand(const Command &Cmd);
  39. bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);
  40. // Fuchsia does not have popen/pclose.
  41. FILE *OpenProcessPipe(const char *Command, const char *Mode);
  42. int CloseProcessPipe(FILE *F);
  43. const void *SearchMemory(const void *haystack, size_t haystacklen,
  44. const void *needle, size_t needlelen);
  45. std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
  46. const char *X1, const char *X2);
  47. inline std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
  48. const char *X) {
  49. return CloneArgsWithoutX(Args, X, X);
  50. }
  51. inline std::pair<std::string, std::string> SplitBefore(std::string X,
  52. std::string S) {
  53. auto Pos = S.find(X);
  54. if (Pos == std::string::npos)
  55. return std::make_pair(S, "");
  56. return std::make_pair(S.substr(0, Pos), S.substr(Pos));
  57. }
  58. void DiscardOutput(int Fd);
  59. std::string DisassembleCmd(const std::string &FileName);
  60. std::string SearchRegexCmd(const std::string &Regex);
  61. uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);
  62. inline size_t Log(size_t X) {
  63. return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);
  64. }
  65. inline size_t PageSize() { return 4096; }
  66. inline uint8_t *RoundUpByPage(uint8_t *P) {
  67. uintptr_t X = reinterpret_cast<uintptr_t>(P);
  68. size_t Mask = PageSize() - 1;
  69. X = (X + Mask) & ~Mask;
  70. return reinterpret_cast<uint8_t *>(X);
  71. }
  72. inline uint8_t *RoundDownByPage(uint8_t *P) {
  73. uintptr_t X = reinterpret_cast<uintptr_t>(P);
  74. size_t Mask = PageSize() - 1;
  75. X = X & ~Mask;
  76. return reinterpret_cast<uint8_t *>(X);
  77. }
  78. #if __BYTE_ORDER == __LITTLE_ENDIAN
  79. template <typename T> T HostToLE(T X) { return X; }
  80. #else
  81. template <typename T> T HostToLE(T X) { return Bswap(X); }
  82. #endif
  83. } // namespace fuzzer
  84. #endif // LLVM_FUZZER_UTIL_H