FuzzerUtil.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. void SetThreadName(std::thread &thread, const std::string &name);
  41. // Fuchsia does not have popen/pclose.
  42. FILE *OpenProcessPipe(const char *Command, const char *Mode);
  43. int CloseProcessPipe(FILE *F);
  44. const void *SearchMemory(const void *haystack, size_t haystacklen,
  45. const void *needle, size_t needlelen);
  46. std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
  47. const char *X1, const char *X2);
  48. inline std::string CloneArgsWithoutX(const std::vector<std::string> &Args,
  49. const char *X) {
  50. return CloneArgsWithoutX(Args, X, X);
  51. }
  52. inline std::pair<std::string, std::string> SplitBefore(std::string X,
  53. std::string S) {
  54. auto Pos = S.find(X);
  55. if (Pos == std::string::npos)
  56. return std::make_pair(S, "");
  57. return std::make_pair(S.substr(0, Pos), S.substr(Pos));
  58. }
  59. void DiscardOutput(int Fd);
  60. std::string DisassembleCmd(const std::string &FileName);
  61. std::string SearchRegexCmd(const std::string &Regex);
  62. uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);
  63. inline size_t Log(size_t X) {
  64. return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);
  65. }
  66. size_t PageSize();
  67. inline uint8_t *RoundUpByPage(uint8_t *P) {
  68. uintptr_t X = reinterpret_cast<uintptr_t>(P);
  69. size_t Mask = PageSize() - 1;
  70. X = (X + Mask) & ~Mask;
  71. return reinterpret_cast<uint8_t *>(X);
  72. }
  73. inline uint8_t *RoundDownByPage(uint8_t *P) {
  74. uintptr_t X = reinterpret_cast<uintptr_t>(P);
  75. size_t Mask = PageSize() - 1;
  76. X = X & ~Mask;
  77. return reinterpret_cast<uint8_t *>(X);
  78. }
  79. #if __BYTE_ORDER == __LITTLE_ENDIAN
  80. template <typename T> T HostToLE(T X) { return X; }
  81. #else
  82. template <typename T> T HostToLE(T X) { return Bswap(X); }
  83. #endif
  84. } // namespace fuzzer
  85. #endif // LLVM_FUZZER_UTIL_H