• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FuzzerExtFunctions.h"
12 #include "FuzzerInternal.h"
13 #include <iterator>
14 #include <fstream>
15 #include <dirent.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <cstdarg>
20 #include <cstdio>
21 
22 namespace fuzzer {
23 
24 static FILE *OutputFile = stderr;
25 
IsFile(const std::string & Path)26 bool IsFile(const std::string &Path) {
27   struct stat St;
28   if (stat(Path.c_str(), &St))
29     return false;
30   return S_ISREG(St.st_mode);
31 }
32 
GetEpoch(const std::string & Path)33 static long GetEpoch(const std::string &Path) {
34   struct stat St;
35   if (stat(Path.c_str(), &St))
36     return 0;  // Can't stat, be conservative.
37   return St.st_mtime;
38 }
39 
ListFilesInDirRecursive(const std::string & Dir,long * Epoch,std::vector<std::string> * V,bool TopDir)40 static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
41                                     std::vector<std::string> *V, bool TopDir) {
42   auto E = GetEpoch(Dir);
43   if (Epoch)
44     if (E && *Epoch >= E) return;
45 
46   DIR *D = opendir(Dir.c_str());
47   if (!D) {
48     Printf("No such directory: %s; exiting\n", Dir.c_str());
49     exit(1);
50   }
51   while (auto E = readdir(D)) {
52     std::string Path = DirPlusFile(Dir, E->d_name);
53     if (E->d_type == DT_REG || E->d_type == DT_LNK)
54       V->push_back(Path);
55     else if (E->d_type == DT_DIR && *E->d_name != '.')
56       ListFilesInDirRecursive(Path, Epoch, V, false);
57   }
58   closedir(D);
59   if (Epoch && TopDir)
60     *Epoch = E;
61 }
62 
FileToVector(const std::string & Path,size_t MaxSize)63 Unit FileToVector(const std::string &Path, size_t MaxSize) {
64   std::ifstream T(Path);
65   if (!T) {
66     Printf("No such directory: %s; exiting\n", Path.c_str());
67     exit(1);
68   }
69 
70   T.seekg(0, T.end);
71   size_t FileLen = T.tellg();
72   if (MaxSize)
73     FileLen = std::min(FileLen, MaxSize);
74 
75   T.seekg(0, T.beg);
76   Unit Res(FileLen);
77   T.read(reinterpret_cast<char *>(Res.data()), FileLen);
78   return Res;
79 }
80 
FileToString(const std::string & Path)81 std::string FileToString(const std::string &Path) {
82   std::ifstream T(Path);
83   return std::string((std::istreambuf_iterator<char>(T)),
84                      std::istreambuf_iterator<char>());
85 }
86 
CopyFileToErr(const std::string & Path)87 void CopyFileToErr(const std::string &Path) {
88   Printf("%s", FileToString(Path).c_str());
89 }
90 
WriteToFile(const Unit & U,const std::string & Path)91 void WriteToFile(const Unit &U, const std::string &Path) {
92   // Use raw C interface because this function may be called from a sig handler.
93   FILE *Out = fopen(Path.c_str(), "w");
94   if (!Out) return;
95   fwrite(U.data(), sizeof(U[0]), U.size(), Out);
96   fclose(Out);
97 }
98 
ReadDirToVectorOfUnits(const char * Path,std::vector<Unit> * V,long * Epoch,size_t MaxSize)99 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
100                             long *Epoch, size_t MaxSize) {
101   long E = Epoch ? *Epoch : 0;
102   std::vector<std::string> Files;
103   ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
104   size_t NumLoaded = 0;
105   for (size_t i = 0; i < Files.size(); i++) {
106     auto &X = Files[i];
107     if (Epoch && GetEpoch(X) < E) continue;
108     NumLoaded++;
109     if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
110       Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
111     V->push_back(FileToVector(X, MaxSize));
112   }
113 }
114 
DirPlusFile(const std::string & DirPath,const std::string & FileName)115 std::string DirPlusFile(const std::string &DirPath,
116                         const std::string &FileName) {
117   return DirPath + "/" + FileName;
118 }
119 
DupAndCloseStderr()120 void DupAndCloseStderr() {
121   int OutputFd = dup(2);
122   if (OutputFd > 0) {
123     FILE *NewOutputFile = fdopen(OutputFd, "w");
124     if (NewOutputFile) {
125       OutputFile = NewOutputFile;
126       if (EF->__sanitizer_set_report_fd)
127         EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
128       close(2);
129     }
130   }
131 }
132 
CloseStdout()133 void CloseStdout() { close(1); }
134 
Printf(const char * Fmt,...)135 void Printf(const char *Fmt, ...) {
136   va_list ap;
137   va_start(ap, Fmt);
138   vfprintf(OutputFile, Fmt, ap);
139   va_end(ap);
140   fflush(OutputFile);
141 }
142 
143 }  // namespace fuzzer
144