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 Vector
<SizedFile
> &CorporaFiles
);
46 bool AppendCoverage(std::istream
&IN
);
47 bool AppendCoverage(const std::string
&S
);
49 size_t NumCoveredFunctions() const { return Functions
.size(); }
51 uint32_t GetCounter(size_t FunctionId
, size_t BasicBlockId
) {
52 auto It
= Functions
.find(FunctionId
);
53 if (It
== Functions
.end()) return 0;
54 const auto &Counters
= It
->second
;
55 if (BasicBlockId
< Counters
.size())
56 return Counters
[BasicBlockId
];
60 uint32_t GetNumberOfBlocks(size_t FunctionId
) {
61 auto It
= Functions
.find(FunctionId
);
62 if (It
== Functions
.end()) return 0;
63 const auto &Counters
= It
->second
;
64 return Counters
.size();
67 uint32_t GetNumberOfCoveredBlocks(size_t FunctionId
) {
68 auto It
= Functions
.find(FunctionId
);
69 if (It
== Functions
.end()) return 0;
70 const auto &Counters
= It
->second
;
72 for (auto Cnt
: Counters
)
78 Vector
<double> FunctionWeights(size_t NumFunctions
) const;
79 void clear() { Functions
.clear(); }
83 typedef Vector
<uint32_t> CoverageVector
;
85 uint32_t NumberOfCoveredBlocks(const CoverageVector
&Counters
) const {
87 for (auto Cnt
: Counters
)
93 uint32_t NumberOfUncoveredBlocks(const CoverageVector
&Counters
) const {
94 return Counters
.size() - NumberOfCoveredBlocks(Counters
);
97 uint32_t SmallestNonZeroCounter(const CoverageVector
&Counters
) const {
98 assert(!Counters
.empty());
99 uint32_t Res
= Counters
[0];
100 for (auto Cnt
: Counters
)
107 // Function ID => vector of counters.
108 // Each counter represents how many input files trigger the given basic block.
109 std::unordered_map
<size_t, CoverageVector
> Functions
;
110 // Functions that have DFT entry.
111 std::unordered_set
<size_t> FunctionsWithDFT
;
114 class DataFlowTrace
{
116 int ReadCoverage(const std::string
&DirPath
);
117 int Init(const std::string
&DirPath
, std::string
*FocusFunction
,
118 Vector
<SizedFile
> &CorporaFiles
, Random
&Rand
);
119 void Clear() { Traces
.clear(); }
120 const Vector
<uint8_t> *Get(const std::string
&InputSha1
) const {
121 auto It
= Traces
.find(InputSha1
);
122 if (It
!= Traces
.end())
128 // Input's sha1 => DFT for the FocusFunction.
129 std::unordered_map
<std::string
, Vector
<uint8_t> > Traces
;
130 BlockCoverage Coverage
;
131 std::unordered_set
<std::string
> CorporaHashes
;
133 } // namespace fuzzer
135 #endif // LLVM_FUZZER_DATA_FLOW_TRACE