kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 1 | //===- FuzzerDataFlowTrace.cpp - DataFlowTrace ---*- 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 |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "FuzzerDataFlowTrace.h" |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 12 | |
| 13 | #include "FuzzerCommand.h" |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 14 | #include "FuzzerIO.h" |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 15 | #include "FuzzerRandom.h" |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 16 | #include "FuzzerSHA1.h" |
| 17 | #include "FuzzerUtil.h" |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 18 | |
| 19 | #include <cstdlib> |
| 20 | #include <fstream> |
kcc | 908220a | 2019-05-10 00:59:32 +0000 | [diff] [blame] | 21 | #include <numeric> |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 22 | #include <queue> |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 23 | #include <sstream> |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 24 | #include <string> |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 25 | #include <unordered_map> |
| 26 | #include <unordered_set> |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | namespace fuzzer { |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 30 | static const char *kFunctionsTxt = "functions.txt"; |
| 31 | |
| 32 | bool BlockCoverage::AppendCoverage(const std::string &S) { |
| 33 | std::stringstream SS(S); |
| 34 | return AppendCoverage(SS); |
| 35 | } |
| 36 | |
| 37 | // Coverage lines have this form: |
| 38 | // CN X Y Z T |
| 39 | // where N is the number of the function, T is the total number of instrumented |
| 40 | // BBs, and X,Y,Z, if present, are the indecies of covered BB. |
| 41 | // BB #0, which is the entry block, is not explicitly listed. |
| 42 | bool BlockCoverage::AppendCoverage(std::istream &IN) { |
| 43 | std::string L; |
| 44 | while (std::getline(IN, L, '\n')) { |
kcc | b80b89c | 2019-06-14 23:29:56 +0000 | [diff] [blame] | 45 | if (L.empty()) |
| 46 | continue; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 47 | std::stringstream SS(L.c_str() + 1); |
| 48 | size_t FunctionId = 0; |
| 49 | SS >> FunctionId; |
kcc | b80b89c | 2019-06-14 23:29:56 +0000 | [diff] [blame] | 50 | if (L[0] == 'F') { |
| 51 | FunctionsWithDFT.insert(FunctionId); |
| 52 | continue; |
| 53 | } |
| 54 | if (L[0] != 'C') continue; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 55 | Vector<uint32_t> CoveredBlocks; |
| 56 | while (true) { |
| 57 | uint32_t BB = 0; |
| 58 | SS >> BB; |
| 59 | if (!SS) break; |
| 60 | CoveredBlocks.push_back(BB); |
| 61 | } |
| 62 | if (CoveredBlocks.empty()) return false; |
| 63 | uint32_t NumBlocks = CoveredBlocks.back(); |
| 64 | CoveredBlocks.pop_back(); |
| 65 | for (auto BB : CoveredBlocks) |
| 66 | if (BB >= NumBlocks) return false; |
| 67 | auto It = Functions.find(FunctionId); |
| 68 | auto &Counters = |
| 69 | It == Functions.end() |
| 70 | ? Functions.insert({FunctionId, Vector<uint32_t>(NumBlocks)}) |
| 71 | .first->second |
| 72 | : It->second; |
| 73 | |
| 74 | if (Counters.size() != NumBlocks) return false; // wrong number of blocks. |
| 75 | |
| 76 | Counters[0]++; |
| 77 | for (auto BB : CoveredBlocks) |
| 78 | Counters[BB]++; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | // Assign weights to each function. |
| 84 | // General principles: |
| 85 | // * any uncovered function gets weight 0. |
| 86 | // * a function with lots of uncovered blocks gets bigger weight. |
| 87 | // * a function with a less frequently executed code gets bigger weight. |
| 88 | Vector<double> BlockCoverage::FunctionWeights(size_t NumFunctions) const { |
| 89 | Vector<double> Res(NumFunctions); |
| 90 | for (auto It : Functions) { |
| 91 | auto FunctionID = It.first; |
| 92 | auto Counters = It.second; |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 93 | assert(FunctionID < NumFunctions); |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 94 | auto &Weight = Res[FunctionID]; |
kcc | b80b89c | 2019-06-14 23:29:56 +0000 | [diff] [blame] | 95 | // Give higher weight if the function has a DFT. |
| 96 | Weight = FunctionsWithDFT.count(FunctionID) ? 1000. : 1; |
| 97 | // Give higher weight to functions with less frequently seen basic blocks. |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 98 | Weight /= SmallestNonZeroCounter(Counters); |
kcc | b80b89c | 2019-06-14 23:29:56 +0000 | [diff] [blame] | 99 | // Give higher weight to functions with the most uncovered basic blocks. |
| 100 | Weight *= NumberOfUncoveredBlocks(Counters) + 1; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 101 | } |
| 102 | return Res; |
| 103 | } |
| 104 | |
| 105 | void DataFlowTrace::ReadCoverage(const std::string &DirPath) { |
| 106 | Vector<SizedFile> Files; |
| 107 | GetSizedFilesFromDir(DirPath, &Files); |
| 108 | for (auto &SF : Files) { |
| 109 | auto Name = Basename(SF.File); |
| 110 | if (Name == kFunctionsTxt) continue; |
kcc | 81cba77 | 2019-05-24 00:43:52 +0000 | [diff] [blame] | 111 | if (!CorporaHashes.count(Name)) continue; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 112 | std::ifstream IF(SF.File); |
| 113 | Coverage.AppendCoverage(IF); |
| 114 | } |
| 115 | } |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 116 | |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 117 | static void DFTStringAppendToVector(Vector<uint8_t> *DFT, |
kcc | 0cd1e56 | 2019-05-14 22:16:04 +0000 | [diff] [blame] | 118 | const std::string &DFTString) { |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 119 | assert(DFT->size() == DFTString.size()); |
| 120 | for (size_t I = 0, Len = DFT->size(); I < Len; I++) |
| 121 | (*DFT)[I] = DFTString[I] == '1'; |
| 122 | } |
| 123 | |
| 124 | // converts a string of '0' and '1' into a Vector<uint8_t> |
kcc | 0cd1e56 | 2019-05-14 22:16:04 +0000 | [diff] [blame] | 125 | static Vector<uint8_t> DFTStringToVector(const std::string &DFTString) { |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 126 | Vector<uint8_t> DFT(DFTString.size()); |
| 127 | DFTStringAppendToVector(&DFT, DFTString); |
| 128 | return DFT; |
| 129 | } |
| 130 | |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 131 | static bool ParseError(const char *Err, const std::string &Line) { |
| 132 | Printf("DataFlowTrace: parse error: %s: Line: %s\n", Err, Line.c_str()); |
| 133 | return false; |
kcc | 69e0205 | 2019-06-14 22:34:30 +0000 | [diff] [blame] | 134 | } |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 135 | |
kcc | 0cd1e56 | 2019-05-14 22:16:04 +0000 | [diff] [blame] | 136 | // TODO(metzman): replace std::string with std::string_view for |
| 137 | // better performance. Need to figure our how to use string_view on Windows. |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 138 | static bool ParseDFTLine(const std::string &Line, size_t *FunctionNum, |
kcc | 0cd1e56 | 2019-05-14 22:16:04 +0000 | [diff] [blame] | 139 | std::string *DFTString) { |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 140 | if (!Line.empty() && Line[0] != 'F') |
| 141 | return false; // Ignore coverage. |
| 142 | size_t SpacePos = Line.find(' '); |
| 143 | if (SpacePos == std::string::npos) |
| 144 | return ParseError("no space in the trace line", Line); |
| 145 | if (Line.empty() || Line[0] != 'F') |
| 146 | return ParseError("the trace line doesn't start with 'F'", Line); |
| 147 | *FunctionNum = std::atol(Line.c_str() + 1); |
| 148 | const char *Beg = Line.c_str() + SpacePos + 1; |
| 149 | const char *End = Line.c_str() + Line.size(); |
| 150 | assert(Beg < End); |
| 151 | size_t Len = End - Beg; |
| 152 | for (size_t I = 0; I < Len; I++) { |
| 153 | if (Beg[I] != '0' && Beg[I] != '1') |
| 154 | return ParseError("the trace should contain only 0 or 1", Line); |
| 155 | } |
| 156 | *DFTString = Beg; |
| 157 | return true; |
| 158 | } |
| 159 | |
kcc | 81cba77 | 2019-05-24 00:43:52 +0000 | [diff] [blame] | 160 | bool DataFlowTrace::Init(const std::string &DirPath, std::string *FocusFunction, |
| 161 | Vector<SizedFile> &CorporaFiles, Random &Rand) { |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 162 | if (DirPath.empty()) return false; |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 163 | Printf("INFO: DataFlowTrace: reading from '%s'\n", DirPath.c_str()); |
| 164 | Vector<SizedFile> Files; |
| 165 | GetSizedFilesFromDir(DirPath, &Files); |
| 166 | std::string L; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 167 | size_t FocusFuncIdx = SIZE_MAX; |
| 168 | Vector<std::string> FunctionNames; |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 169 | |
kcc | 81cba77 | 2019-05-24 00:43:52 +0000 | [diff] [blame] | 170 | // Collect the hashes of the corpus files. |
| 171 | for (auto &SF : CorporaFiles) |
| 172 | CorporaHashes.insert(Hash(FileToVector(SF.File))); |
| 173 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 174 | // Read functions.txt |
| 175 | std::ifstream IF(DirPlusFile(DirPath, kFunctionsTxt)); |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 176 | size_t NumFunctions = 0; |
| 177 | while (std::getline(IF, L, '\n')) { |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 178 | FunctionNames.push_back(L); |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 179 | NumFunctions++; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 180 | if (*FocusFunction == L) |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 181 | FocusFuncIdx = NumFunctions - 1; |
| 182 | } |
kcc | d701d9e | 2019-05-23 00:22:46 +0000 | [diff] [blame] | 183 | if (!NumFunctions) |
| 184 | return false; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 185 | |
| 186 | if (*FocusFunction == "auto") { |
| 187 | // AUTOFOCUS works like this: |
| 188 | // * reads the coverage data from the DFT files. |
| 189 | // * assigns weights to functions based on coverage. |
| 190 | // * chooses a random function according to the weights. |
| 191 | ReadCoverage(DirPath); |
| 192 | auto Weights = Coverage.FunctionWeights(NumFunctions); |
| 193 | Vector<double> Intervals(NumFunctions + 1); |
| 194 | std::iota(Intervals.begin(), Intervals.end(), 0); |
| 195 | auto Distribution = std::piecewise_constant_distribution<double>( |
| 196 | Intervals.begin(), Intervals.end(), Weights.begin()); |
| 197 | FocusFuncIdx = static_cast<size_t>(Distribution(Rand)); |
| 198 | *FocusFunction = FunctionNames[FocusFuncIdx]; |
| 199 | assert(FocusFuncIdx < NumFunctions); |
| 200 | Printf("INFO: AUTOFOCUS: %zd %s\n", FocusFuncIdx, |
| 201 | FunctionNames[FocusFuncIdx].c_str()); |
| 202 | for (size_t i = 0; i < NumFunctions; i++) { |
| 203 | if (!Weights[i]) continue; |
| 204 | Printf(" [%zd] W %g\tBB-tot %u\tBB-cov %u\tEntryFreq %u:\t%s\n", i, |
| 205 | Weights[i], Coverage.GetNumberOfBlocks(i), |
| 206 | Coverage.GetNumberOfCoveredBlocks(i), Coverage.GetCounter(i, 0), |
| 207 | FunctionNames[i].c_str()); |
| 208 | } |
| 209 | } |
| 210 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 211 | if (!NumFunctions || FocusFuncIdx == SIZE_MAX || Files.size() <= 1) |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 212 | return false; |
kcc | f7d6ba3 | 2019-05-09 21:29:45 +0000 | [diff] [blame] | 213 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 214 | // Read traces. |
| 215 | size_t NumTraceFiles = 0; |
| 216 | size_t NumTracesWithFocusFunction = 0; |
| 217 | for (auto &SF : Files) { |
| 218 | auto Name = Basename(SF.File); |
| 219 | if (Name == kFunctionsTxt) continue; |
kcc | 81cba77 | 2019-05-24 00:43:52 +0000 | [diff] [blame] | 220 | if (!CorporaHashes.count(Name)) continue; // not in the corpus. |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 221 | NumTraceFiles++; |
| 222 | // Printf("=== %s\n", Name.c_str()); |
| 223 | std::ifstream IF(SF.File); |
| 224 | while (std::getline(IF, L, '\n')) { |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 225 | size_t FunctionNum = 0; |
kcc | 0cd1e56 | 2019-05-14 22:16:04 +0000 | [diff] [blame] | 226 | std::string DFTString; |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 227 | if (ParseDFTLine(L, &FunctionNum, &DFTString) && |
| 228 | FunctionNum == FocusFuncIdx) { |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 229 | NumTracesWithFocusFunction++; |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 230 | |
| 231 | if (FunctionNum >= NumFunctions) |
| 232 | return ParseError("N is greater than the number of functions", L); |
| 233 | Traces[Name] = DFTStringToVector(DFTString); |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 234 | // Print just a few small traces. |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 235 | if (NumTracesWithFocusFunction <= 3 && DFTString.size() <= 16) |
| 236 | Printf("%s => |%s|\n", Name.c_str(), std::string(DFTString).c_str()); |
| 237 | break; // No need to parse the following lines. |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 241 | Printf("INFO: DataFlowTrace: %zd trace files, %zd functions, " |
| 242 | "%zd traces with focus function\n", |
| 243 | NumTraceFiles, NumFunctions, NumTracesWithFocusFunction); |
kcc | 81cba77 | 2019-05-24 00:43:52 +0000 | [diff] [blame] | 244 | return NumTraceFiles > 0; |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 245 | } |
| 246 | |
kcc | 908220a | 2019-05-10 00:59:32 +0000 | [diff] [blame] | 247 | int CollectDataFlow(const std::string &DFTBinary, const std::string &DirPath, |
kcc | 11883b2 | 2019-05-10 01:34:26 +0000 | [diff] [blame] | 248 | const Vector<SizedFile> &CorporaFiles) { |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 249 | Printf("INFO: collecting data flow: bin: %s dir: %s files: %zd\n", |
| 250 | DFTBinary.c_str(), DirPath.c_str(), CorporaFiles.size()); |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 251 | static char DFSanEnv[] = "DFSAN_OPTIONS=fast16labels=1:warn_unimplemented=0"; |
| 252 | putenv(DFSanEnv); |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 253 | MkDir(DirPath); |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 254 | for (auto &F : CorporaFiles) { |
| 255 | // For every input F we need to collect the data flow and the coverage. |
| 256 | // Data flow collection may fail if we request too many DFSan tags at once. |
| 257 | // So, we start from requesting all tags in range [0,Size) and if that fails |
| 258 | // we then request tags in [0,Size/2) and [Size/2, Size), and so on. |
| 259 | // Function number => DFT. |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 260 | auto OutPath = DirPlusFile(DirPath, Hash(FileToVector(F.File))); |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 261 | std::unordered_map<size_t, Vector<uint8_t>> DFTMap; |
| 262 | std::unordered_set<std::string> Cov; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 263 | Command Cmd; |
| 264 | Cmd.addArgument(DFTBinary); |
| 265 | Cmd.addArgument(F.File); |
| 266 | Cmd.addArgument(OutPath); |
| 267 | Printf("CMD: %s\n", Cmd.toString().c_str()); |
| 268 | ExecuteCommand(Cmd); |
kcc | 81236df | 2019-05-14 21:47:35 +0000 | [diff] [blame] | 269 | } |
kcc | ecf5e56 | 2019-05-23 01:03:42 +0000 | [diff] [blame] | 270 | // Write functions.txt if it's currently empty or doesn't exist. |
kcc | 81cba77 | 2019-05-24 00:43:52 +0000 | [diff] [blame] | 271 | auto FunctionsTxtPath = DirPlusFile(DirPath, kFunctionsTxt); |
kcc | ecf5e56 | 2019-05-23 01:03:42 +0000 | [diff] [blame] | 272 | if (FileToString(FunctionsTxtPath).empty()) { |
| 273 | Command Cmd; |
| 274 | Cmd.addArgument(DFTBinary); |
| 275 | Cmd.setOutputFile(FunctionsTxtPath); |
| 276 | ExecuteCommand(Cmd); |
| 277 | } |
kcc | 908220a | 2019-05-10 00:59:32 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 281 | } // namespace fuzzer |