1 //===- FuzzerDataFlowTrace.cpp - DataFlowTrace ---*- 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 9 //===----------------------------------------------------------------------===// 10 11 #include "FuzzerDataFlowTrace.h" 12 #include "FuzzerIO.h" 13 14 #include <cstdlib> 15 #include <fstream> 16 #include <string> 17 #include <vector> 18 19 namespace fuzzer { 20 21 void DataFlowTrace::Init(const std::string &DirPath, 22 const std::string &FocusFunction) { 23 if (DirPath.empty()) return; 24 const char *kFunctionsTxt = "functions.txt"; 25 Printf("INFO: DataFlowTrace: reading from '%s'\n", DirPath.c_str()); 26 Vector<SizedFile> Files; 27 GetSizedFilesFromDir(DirPath, &Files); 28 std::string L; 29 30 // Read functions.txt 31 std::ifstream IF(DirPlusFile(DirPath, kFunctionsTxt)); 32 size_t FocusFuncIdx = SIZE_MAX; 33 size_t NumFunctions = 0; 34 while (std::getline(IF, L, '\n')) { 35 NumFunctions++; 36 if (FocusFunction == L) 37 FocusFuncIdx = NumFunctions - 1; 38 } 39 if (!NumFunctions || FocusFuncIdx == SIZE_MAX || Files.size() <= 1) 40 return; 41 // Read traces. 42 size_t NumTraceFiles = 0; 43 size_t NumTracesWithFocusFunction = 0; 44 for (auto &SF : Files) { 45 auto Name = Basename(SF.File); 46 if (Name == kFunctionsTxt) continue; 47 auto ParseError = [&](const char *Err) { 48 Printf("DataFlowTrace: parse error: %s\n File: %s\n Line: %s\n", Err, 49 Name.c_str(), L.c_str()); 50 }; 51 NumTraceFiles++; 52 // Printf("=== %s\n", Name.c_str()); 53 std::ifstream IF(SF.File); 54 while (std::getline(IF, L, '\n')) { 55 if (!L.empty() && L[0] == 'C') 56 continue; // Ignore coverage. 57 size_t SpacePos = L.find(' '); 58 if (SpacePos == std::string::npos) 59 return ParseError("no space in the trace line"); 60 if (L.empty() || L[0] != 'F') 61 return ParseError("the trace line doesn't start with 'F'"); 62 size_t N = std::atol(L.c_str() + 1); 63 if (N >= NumFunctions) 64 return ParseError("N is greater than the number of functions"); 65 if (N == FocusFuncIdx) { 66 NumTracesWithFocusFunction++; 67 const char *Beg = L.c_str() + SpacePos + 1; 68 const char *End = L.c_str() + L.size(); 69 assert(Beg < End); 70 size_t Len = End - Beg; 71 Vector<uint8_t> V(Len); 72 for (size_t I = 0; I < Len; I++) { 73 if (Beg[I] != '0' && Beg[I] != '1') 74 ParseError("the trace should contain only 0 or 1"); 75 V[I] = Beg[I] == '1'; 76 } 77 Traces[Name] = V; 78 // Print just a few small traces. 79 if (NumTracesWithFocusFunction <= 3 && Len <= 16) 80 Printf("%s => |%s|\n", Name.c_str(), L.c_str() + SpacePos + 1); 81 break; // No need to parse the following lines. 82 } 83 } 84 } 85 assert(NumTraceFiles == Files.size() - 1); 86 Printf("INFO: DataFlowTrace: %zd trace files, %zd functions, " 87 "%zd traces with focus function\n", 88 NumTraceFiles, NumFunctions, NumTracesWithFocusFunction); 89 } 90 91 } // namespace fuzzer 92 93