1 //===- FuzzerDataFlowTrace.h - Internal header for the Fuzzer ---*- C++ -* ===//
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
7 //===----------------------------------------------------------------------===//
8 // fuzzer::DataFlowTrace; reads and handles a data-flow trace.
10 // A data flow trace is generated by e.g. dataflow/DataFlow.cpp
11 // and is stored on disk in a separate directory.
13 // The trace dir contains a file 'functions.txt' which lists function names,
14 // oner per line, e.g.
15 // ==> functions.txt <==
17 // LLVMFuzzerTestOneInput
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.
24 // the data is parsed and the summary is printed, but the data is not yet
25 // used in any other way.
26 //===----------------------------------------------------------------------===//
28 #ifndef LLVM_FUZZER_DATA_FLOW_TRACE
29 #define LLVM_FUZZER_DATA_FLOW_TRACE
31 #include "FuzzerDefs.h"
34 #include <unordered_map>
35 #include <unordered_set>
41 int CollectDataFlow(const std::string
&DFTBinary
, const std::string
&DirPath
,
42 const std::vector
<SizedFile
> &CorporaFiles
);
46 // These functions guarantee no CoverageVector is longer than UINT32_MAX.
47 bool AppendCoverage(std::istream
&IN
);
48 bool AppendCoverage(const std::string
&S
);
50 size_t NumCoveredFunctions() const { return Functions
.size(); }
52 uint32_t GetCounter(size_t FunctionId
, size_t BasicBlockId
) {
53 auto It
= Functions
.find(FunctionId
);
54 if (It
== Functions
.end())
56 const auto &Counters
= It
->second
;
57 if (BasicBlockId
< Counters
.size())
58 return Counters
[BasicBlockId
];
62 uint32_t GetNumberOfBlocks(size_t FunctionId
) {
63 auto It
= Functions
.find(FunctionId
);
64 if (It
== Functions
.end()) return 0;
65 const auto &Counters
= It
->second
;
66 return static_cast<uint32_t>(Counters
.size());
69 uint32_t GetNumberOfCoveredBlocks(size_t FunctionId
) {
70 auto It
= Functions
.find(FunctionId
);
71 if (It
== Functions
.end()) return 0;
72 const auto &Counters
= It
->second
;
74 for (auto Cnt
: Counters
)
80 std::vector
<double> FunctionWeights(size_t NumFunctions
) const;
81 void clear() { Functions
.clear(); }
84 typedef std::vector
<uint32_t> CoverageVector
;
86 uint32_t NumberOfCoveredBlocks(const CoverageVector
&Counters
) const {
88 for (auto Cnt
: Counters
)
94 uint32_t NumberOfUncoveredBlocks(const CoverageVector
&Counters
) const {
95 return static_cast<uint32_t>(Counters
.size()) -
96 NumberOfCoveredBlocks(Counters
);
99 uint32_t SmallestNonZeroCounter(const CoverageVector
&Counters
) const {
100 assert(!Counters
.empty());
101 uint32_t Res
= Counters
[0];
102 for (auto Cnt
: Counters
)
109 // Function ID => vector of counters.
110 // Each counter represents how many input files trigger the given basic block.
111 std::unordered_map
<size_t, CoverageVector
> Functions
;
112 // Functions that have DFT entry.
113 std::unordered_set
<size_t> FunctionsWithDFT
;
116 class DataFlowTrace
{
118 void ReadCoverage(const std::string
&DirPath
);
119 bool Init(const std::string
&DirPath
, std::string
*FocusFunction
,
120 std::vector
<SizedFile
> &CorporaFiles
, Random
&Rand
);
121 void Clear() { Traces
.clear(); }
122 const std::vector
<uint8_t> *Get(const std::string
&InputSha1
) const {
123 auto It
= Traces
.find(InputSha1
);
124 if (It
!= Traces
.end())
130 // Input's sha1 => DFT for the FocusFunction.
131 std::unordered_map
<std::string
, std::vector
<uint8_t>> Traces
;
132 BlockCoverage Coverage
;
133 std::unordered_set
<std::string
> CorporaHashes
;
135 } // namespace fuzzer
137 #endif // LLVM_FUZZER_DATA_FLOW_TRACE