[libFuzzer] When -test_single_input crashes the test it is not necessary to write...
[oota-llvm.git] / lib / Fuzzer / FuzzerIO.cpp
1 //===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
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 // IO functions.
10 //===----------------------------------------------------------------------===//
11 #include "FuzzerInternal.h"
12 #include <iterator>
13 #include <fstream>
14 #include <dirent.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <cstdio>
19
20 namespace fuzzer {
21
22 static long GetEpoch(const std::string &Path) {
23   struct stat St;
24   if (stat(Path.c_str(), &St))
25     return 0;  // Can't stat, be conservative.
26   return St.st_mtime;
27 }
28
29 static std::vector<std::string> ListFilesInDir(const std::string &Dir,
30                                                long *Epoch) {
31   std::vector<std::string> V;
32   if (Epoch) {
33     auto E = GetEpoch(Dir);
34     if (*Epoch >= E) return V;
35     *Epoch = E;
36   }
37   DIR *D = opendir(Dir.c_str());
38   if (!D) {
39     Printf("No such directory: %s; exiting\n", Dir.c_str());
40     exit(1);
41   }
42   while (auto E = readdir(D)) {
43     if (E->d_type == DT_REG || E->d_type == DT_LNK)
44       V.push_back(E->d_name);
45   }
46   closedir(D);
47   return V;
48 }
49
50 Unit FileToVector(const std::string &Path) {
51   std::ifstream T(Path);
52   if (!T) {
53     Printf("No such directory: %s; exiting\n", Path.c_str());
54     exit(1);
55   }
56   return Unit((std::istreambuf_iterator<char>(T)),
57               std::istreambuf_iterator<char>());
58 }
59
60 std::string FileToString(const std::string &Path) {
61   std::ifstream T(Path);
62   return std::string((std::istreambuf_iterator<char>(T)),
63                      std::istreambuf_iterator<char>());
64 }
65
66 void CopyFileToErr(const std::string &Path) {
67   Printf("%s", FileToString(Path).c_str());
68 }
69
70 void WriteToFile(const Unit &U, const std::string &Path) {
71   // Use raw C interface because this function may be called from a sig handler.
72   FILE *Out = fopen(Path.c_str(), "w");
73   if (!Out) return;
74   fwrite(U.data(), sizeof(U[0]), U.size(), Out);
75   fclose(Out);
76 }
77
78 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
79                             long *Epoch) {
80   long E = Epoch ? *Epoch : 0;
81   for (auto &X : ListFilesInDir(Path, Epoch)) {
82     auto FilePath = DirPlusFile(Path, X);
83     if (Epoch && GetEpoch(FilePath) < E) continue;
84     V->push_back(FileToVector(FilePath));
85   }
86 }
87
88 std::string DirPlusFile(const std::string &DirPath,
89                         const std::string &FileName) {
90   return DirPath + "/" + FileName;
91 }
92
93 void PrintFileAsBase64(const std::string &Path) {
94   std::string Cmd = "base64 -w 0 < " + Path + "; echo";
95   ExecuteCommand(Cmd);
96 }
97
98 void Printf(const char *Fmt, ...) {
99   va_list ap;
100   va_start(ap, Fmt);
101   vfprintf(stderr, Fmt, ap);
102   va_end(ap);
103 }
104
105 }  // namespace fuzzer