kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 1 | //===- FuzzerDataFlowTrace.h - Internal header for the Fuzzer ---*- C++ -* ===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 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 | |
| 28 | #ifndef LLVM_FUZZER_DATA_FLOW_TRACE |
| 29 | #define LLVM_FUZZER_DATA_FLOW_TRACE |
| 30 | |
| 31 | #include "FuzzerDefs.h" |
| 32 | |
kcc | adf188b | 2018-06-07 01:40:20 +0000 | [diff] [blame] | 33 | #include <unordered_map> |
| 34 | #include <vector> |
| 35 | #include <string> |
| 36 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 37 | namespace fuzzer { |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 38 | |
| 39 | class BlockCoverage { |
| 40 | public: |
| 41 | bool AppendCoverage(std::istream &IN); |
| 42 | bool AppendCoverage(const std::string &S); |
| 43 | |
| 44 | size_t NumCoveredFunctions() const { return Functions.size(); } |
| 45 | |
| 46 | uint32_t GetCounter(size_t FunctionId, size_t BasicBlockId) { |
| 47 | auto It = Functions.find(FunctionId); |
| 48 | if (It == Functions.end()) return 0; |
| 49 | const auto &Counters = It->second; |
| 50 | if (BasicBlockId < Counters.size()) |
| 51 | return Counters[BasicBlockId]; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | uint32_t GetNumberOfBlocks(size_t FunctionId) { |
| 56 | auto It = Functions.find(FunctionId); |
| 57 | if (It == Functions.end()) return 0; |
| 58 | const auto &Counters = It->second; |
| 59 | return Counters.size(); |
| 60 | } |
| 61 | |
| 62 | uint32_t GetNumberOfCoveredBlocks(size_t FunctionId) { |
| 63 | auto It = Functions.find(FunctionId); |
| 64 | if (It == Functions.end()) return 0; |
| 65 | const auto &Counters = It->second; |
| 66 | uint32_t Result = 0; |
| 67 | for (auto Cnt: Counters) |
| 68 | if (Cnt) |
| 69 | Result++; |
| 70 | return Result; |
| 71 | } |
| 72 | |
| 73 | Vector<double> FunctionWeights(size_t NumFunctions) const; |
| 74 | void clear() { Functions.clear(); } |
| 75 | |
| 76 | private: |
| 77 | |
| 78 | typedef Vector<uint32_t> CoverageVector; |
| 79 | |
| 80 | uint32_t NumberOfCoveredBlocks(const CoverageVector &Counters) const { |
| 81 | uint32_t Res = 0; |
| 82 | for (auto Cnt : Counters) |
| 83 | if (Cnt) |
| 84 | Res++; |
| 85 | return Res; |
| 86 | } |
| 87 | |
| 88 | uint32_t NumberOfUncoveredBlocks(const CoverageVector &Counters) const { |
| 89 | return Counters.size() - NumberOfCoveredBlocks(Counters); |
| 90 | } |
| 91 | |
| 92 | uint32_t SmallestNonZeroCounter(const CoverageVector &Counters) const { |
| 93 | assert(!Counters.empty()); |
| 94 | uint32_t Res = Counters[0]; |
| 95 | for (auto Cnt : Counters) |
| 96 | if (Cnt) |
| 97 | Res = Min(Res, Cnt); |
| 98 | assert(Res); |
| 99 | return Res; |
| 100 | } |
| 101 | |
| 102 | // Function ID => vector of counters. |
| 103 | // Each counter represents how many input files trigger the given basic block. |
| 104 | std::unordered_map<size_t, CoverageVector> Functions; |
| 105 | }; |
| 106 | |
kcc | adf188b | 2018-06-07 01:40:20 +0000 | [diff] [blame] | 107 | class DataFlowTrace { |
| 108 | public: |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 109 | void ReadCoverage(const std::string &DirPath); |
| 110 | void Init(const std::string &DirPath, std::string *FocusFunction, |
| 111 | Random &Rand); |
kcc | adf188b | 2018-06-07 01:40:20 +0000 | [diff] [blame] | 112 | void Clear() { Traces.clear(); } |
kcc | 0cab3f0 | 2018-07-19 01:23:32 +0000 | [diff] [blame] | 113 | const Vector<uint8_t> *Get(const std::string &InputSha1) const { |
kcc | adf188b | 2018-06-07 01:40:20 +0000 | [diff] [blame] | 114 | auto It = Traces.find(InputSha1); |
| 115 | if (It != Traces.end()) |
| 116 | return &It->second; |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | // Input's sha1 => DFT for the FocusFunction. |
kcc | 0cab3f0 | 2018-07-19 01:23:32 +0000 | [diff] [blame] | 122 | std::unordered_map<std::string, Vector<uint8_t> > Traces; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 123 | BlockCoverage Coverage; |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 124 | }; |
| 125 | } // namespace fuzzer |
| 126 | |
| 127 | #endif // LLVM_FUZZER_DATA_FLOW_TRACE |