blob: 5ae7510e3922502d8d5f12769fc56f44c6475d5b [file] [log] [blame]
kcc86e43882018-06-06 01:23:29 +00001//===- FuzzerDataFlowTrace.cpp - DataFlowTrace ---*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
kcc86e43882018-06-06 01:23:29 +00006//
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
19namespace fuzzer {
20
21void 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 size_t SpacePos = L.find(' ');
56 if (SpacePos == std::string::npos)
57 return ParseError("no space in the trace line");
58 if (L.empty() || L[0] != 'F')
59 return ParseError("the trace line doesn't start with 'F'");
60 size_t N = std::atol(L.c_str() + 1);
61 if (N >= NumFunctions)
62 return ParseError("N is greater than the number of functions");
63 if (N == FocusFuncIdx) {
64 NumTracesWithFocusFunction++;
65 const char *Beg = L.c_str() + SpacePos + 1;
66 const char *End = L.c_str() + L.size();
67 assert(Beg < End);
68 size_t Len = End - Beg;
kcc0cab3f02018-07-19 01:23:32 +000069 Vector<uint8_t> V(Len);
kcc86e43882018-06-06 01:23:29 +000070 for (size_t I = 0; I < Len; I++) {
71 if (Beg[I] != '0' && Beg[I] != '1')
72 ParseError("the trace should contain only 0 or 1");
73 V[I] = Beg[I] == '1';
74 }
kccadf188b2018-06-07 01:40:20 +000075 Traces[Name] = V;
kcc86e43882018-06-06 01:23:29 +000076 // Print just a few small traces.
77 if (NumTracesWithFocusFunction <= 3 && Len <= 16)
78 Printf("%s => |%s|\n", Name.c_str(), L.c_str() + SpacePos + 1);
79 break; // No need to parse the following lines.
80 }
81 }
82 }
83 assert(NumTraceFiles == Files.size() - 1);
84 Printf("INFO: DataFlowTrace: %zd trace files, %zd functions, "
85 "%zd traces with focus function\n",
86 NumTraceFiles, NumFunctions, NumTracesWithFocusFunction);
87}
88
89} // namespace fuzzer
90