[libFuzzer] be more robust when dealing with files on disk (e.g. don't crash if a...
[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   return Unit((std::istreambuf_iterator<char>(T)),
53               std::istreambuf_iterator<char>());
54 }
55
56 std::string FileToString(const std::string &Path) {
57   std::ifstream T(Path);
58   return std::string((std::istreambuf_iterator<char>(T)),
59                      std::istreambuf_iterator<char>());
60 }
61
62 void CopyFileToErr(const std::string &Path) {
63   Printf("%s", FileToString(Path).c_str());
64 }
65
66 void WriteToFile(const Unit &U, const std::string &Path) {
67   // Use raw C interface because this function may be called from a sig handler.
68   FILE *Out = fopen(Path.c_str(), "w");
69   if (!Out) return;
70   fwrite(U.data(), sizeof(U[0]), U.size(), Out);
71   fclose(Out);
72 }
73
74 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
75                             long *Epoch) {
76   long E = Epoch ? *Epoch : 0;
77   for (auto &X : ListFilesInDir(Path, Epoch)) {
78     auto FilePath = DirPlusFile(Path, X);
79     if (Epoch && GetEpoch(FilePath) < E) continue;
80     V->push_back(FileToVector(FilePath));
81   }
82 }
83
84 std::string DirPlusFile(const std::string &DirPath,
85                         const std::string &FileName) {
86   return DirPath + "/" + FileName;
87 }
88
89 void PrintFileAsBase64(const std::string &Path) {
90   std::string Cmd = "base64 -w 0 < " + Path + "; echo";
91   ExecuteCommand(Cmd);
92 }
93
94 void Printf(const char *Fmt, ...) {
95   va_list ap;
96   va_start(ap, Fmt);
97   vfprintf(stderr, Fmt, ap);
98   va_end(ap);
99 }
100
101 }  // namespace fuzzer