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