FuzzerDataFlowTrace.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Vector<SizedFile> &CorporaFiles);
  38. class BlockCoverage {
  39. public:
  40. bool AppendCoverage(std::istream &IN);
  41. bool AppendCoverage(const std::string &S);
  42. size_t NumCoveredFunctions() const { return Functions.size(); }
  43. uint32_t GetCounter(size_t FunctionId, size_t BasicBlockId) {
  44. auto It = Functions.find(FunctionId);
  45. if (It == Functions.end()) return 0;
  46. const auto &Counters = It->second;
  47. if (BasicBlockId < Counters.size())
  48. return Counters[BasicBlockId];
  49. return 0;
  50. }
  51. uint32_t GetNumberOfBlocks(size_t FunctionId) {
  52. auto It = Functions.find(FunctionId);
  53. if (It == Functions.end()) return 0;
  54. const auto &Counters = It->second;
  55. return Counters.size();
  56. }
  57. uint32_t GetNumberOfCoveredBlocks(size_t FunctionId) {
  58. auto It = Functions.find(FunctionId);
  59. if (It == Functions.end()) return 0;
  60. const auto &Counters = It->second;
  61. uint32_t Result = 0;
  62. for (auto Cnt: Counters)
  63. if (Cnt)
  64. Result++;
  65. return Result;
  66. }
  67. Vector<double> FunctionWeights(size_t NumFunctions) const;
  68. void clear() { Functions.clear(); }
  69. private:
  70. typedef Vector<uint32_t> CoverageVector;
  71. uint32_t NumberOfCoveredBlocks(const CoverageVector &Counters) const {
  72. uint32_t Res = 0;
  73. for (auto Cnt : Counters)
  74. if (Cnt)
  75. Res++;
  76. return Res;
  77. }
  78. uint32_t NumberOfUncoveredBlocks(const CoverageVector &Counters) const {
  79. return Counters.size() - NumberOfCoveredBlocks(Counters);
  80. }
  81. uint32_t SmallestNonZeroCounter(const CoverageVector &Counters) const {
  82. assert(!Counters.empty());
  83. uint32_t Res = Counters[0];
  84. for (auto Cnt : Counters)
  85. if (Cnt)
  86. Res = Min(Res, Cnt);
  87. assert(Res);
  88. return Res;
  89. }
  90. // Function ID => vector of counters.
  91. // Each counter represents how many input files trigger the given basic block.
  92. std::unordered_map<size_t, CoverageVector> Functions;
  93. // Functions that have DFT entry.
  94. std::unordered_set<size_t> FunctionsWithDFT;
  95. };
  96. class DataFlowTrace {
  97. public:
  98. void ReadCoverage(const std::string &DirPath);
  99. bool Init(const std::string &DirPath, std::string *FocusFunction,
  100. Vector<SizedFile> &CorporaFiles, Random &Rand);
  101. void Clear() { Traces.clear(); }
  102. const Vector<uint8_t> *Get(const std::string &InputSha1) const {
  103. auto It = Traces.find(InputSha1);
  104. if (It != Traces.end())
  105. return &It->second;
  106. return nullptr;
  107. }
  108. private:
  109. // Input's sha1 => DFT for the FocusFunction.
  110. std::unordered_map<std::string, Vector<uint8_t> > Traces;
  111. BlockCoverage Coverage;
  112. std::unordered_set<std::string> CorporaHashes;
  113. };
  114. } // namespace fuzzer
  115. #endif // LLVM_FUZZER_DATA_FLOW_TRACE