FuzzerDataFlowTrace.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //===- FuzzerDataFlowTrace.h - Internal header for the Fuzzer ---*- 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. // fuzzer::DataFlowTrace; reads and handles a data-flow trace.
  9. //
  10. // A data flow trace is generated by e.g. dataflow/DataFlow.cpp
  11. // and is stored on disk in a separate directory.
  12. //
  13. // The trace dir contains a file 'functions.txt' which lists function names,
  14. // oner per line, e.g.
  15. // ==> functions.txt <==
  16. // Func2
  17. // LLVMFuzzerTestOneInput
  18. // Func1
  19. //
  20. // All other files in the dir are the traces, see dataflow/DataFlow.cpp.
  21. // The name of the file is sha1 of the input used to generate the trace.
  22. //
  23. // Current status:
  24. // the data is parsed and the summary is printed, but the data is not yet
  25. // used in any other way.
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LLVM_FUZZER_DATA_FLOW_TRACE
  28. #define LLVM_FUZZER_DATA_FLOW_TRACE
  29. #include "FuzzerDefs.h"
  30. #include "FuzzerIO.h"
  31. #include <unordered_map>
  32. #include <unordered_set>
  33. #include <vector>
  34. #include <string>
  35. namespace fuzzer {
  36. int CollectDataFlow(const std::string &DFTBinary, const std::string &DirPath,
  37. const std::vector<SizedFile> &CorporaFiles);
  38. class BlockCoverage {
  39. public:
  40. // These functions guarantee no CoverageVector is longer than UINT32_MAX.
  41. bool AppendCoverage(std::istream &IN);
  42. bool AppendCoverage(const std::string &S);
  43. size_t NumCoveredFunctions() const { return Functions.size(); }
  44. uint32_t GetCounter(size_t FunctionId, size_t BasicBlockId) {
  45. auto It = Functions.find(FunctionId);
  46. if (It == Functions.end())
  47. return 0;
  48. const auto &Counters = It->second;
  49. if (BasicBlockId < Counters.size())
  50. return Counters[BasicBlockId];
  51. return 0;
  52. }
  53. uint32_t GetNumberOfBlocks(size_t FunctionId) {
  54. auto It = Functions.find(FunctionId);
  55. if (It == Functions.end()) return 0;
  56. const auto &Counters = It->second;
  57. return static_cast<uint32_t>(Counters.size());
  58. }
  59. uint32_t GetNumberOfCoveredBlocks(size_t FunctionId) {
  60. auto It = Functions.find(FunctionId);
  61. if (It == Functions.end()) return 0;
  62. const auto &Counters = It->second;
  63. uint32_t Result = 0;
  64. for (auto Cnt: Counters)
  65. if (Cnt)
  66. Result++;
  67. return Result;
  68. }
  69. std::vector<double> FunctionWeights(size_t NumFunctions) const;
  70. void clear() { Functions.clear(); }
  71. private:
  72. typedef std::vector<uint32_t> CoverageVector;
  73. uint32_t NumberOfCoveredBlocks(const CoverageVector &Counters) const {
  74. uint32_t Res = 0;
  75. for (auto Cnt : Counters)
  76. if (Cnt)
  77. Res++;
  78. return Res;
  79. }
  80. uint32_t NumberOfUncoveredBlocks(const CoverageVector &Counters) const {
  81. return static_cast<uint32_t>(Counters.size()) -
  82. NumberOfCoveredBlocks(Counters);
  83. }
  84. uint32_t SmallestNonZeroCounter(const CoverageVector &Counters) const {
  85. assert(!Counters.empty());
  86. uint32_t Res = Counters[0];
  87. for (auto Cnt : Counters)
  88. if (Cnt)
  89. Res = Min(Res, Cnt);
  90. assert(Res);
  91. return Res;
  92. }
  93. // Function ID => vector of counters.
  94. // Each counter represents how many input files trigger the given basic block.
  95. std::unordered_map<size_t, CoverageVector> Functions;
  96. // Functions that have DFT entry.
  97. std::unordered_set<size_t> FunctionsWithDFT;
  98. };
  99. class DataFlowTrace {
  100. public:
  101. void ReadCoverage(const std::string &DirPath);
  102. bool Init(const std::string &DirPath, std::string *FocusFunction,
  103. std::vector<SizedFile> &CorporaFiles, Random &Rand);
  104. void Clear() { Traces.clear(); }
  105. const std::vector<uint8_t> *Get(const std::string &InputSha1) const {
  106. auto It = Traces.find(InputSha1);
  107. if (It != Traces.end())
  108. return &It->second;
  109. return nullptr;
  110. }
  111. private:
  112. // Input's sha1 => DFT for the FocusFunction.
  113. std::unordered_map<std::string, std::vector<uint8_t>> Traces;
  114. BlockCoverage Coverage;
  115. std::unordered_set<std::string> CorporaHashes;
  116. };
  117. } // namespace fuzzer
  118. #endif // LLVM_FUZZER_DATA_FLOW_TRACE