1 //===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===//
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 implementation using Posix API.
10 //===----------------------------------------------------------------------===//
11 #include "FuzzerDefs.h"
12 #if LIBFUZZER_POSIX
13 
14 #include "FuzzerExtFunctions.h"
15 #include "FuzzerIO.h"
16 #include <cstdarg>
17 #include <cstdio>
18 #include <dirent.h>
19 #include <fstream>
20 #include <iterator>
21 #include <libgen.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 namespace fuzzer {
27 
28 bool IsFile(const std::string &Path) {
29   struct stat St;
30   if (stat(Path.c_str(), &St))
31     return false;
32   return S_ISREG(St.st_mode);
33 }
34 
35 size_t FileSize(const std::string &Path) {
36   struct stat St;
37   if (stat(Path.c_str(), &St))
38     return 0;
39   return St.st_size;
40 }
41 
42 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
43                              Vector<std::string> *V, bool TopDir) {
44   auto E = GetEpoch(Dir);
45   if (Epoch)
46     if (E && *Epoch >= E) return;
47 
48   DIR *D = opendir(Dir.c_str());
49   if (!D) {
50     Printf("No such directory: %s; exiting\n", Dir.c_str());
51     exit(1);
52   }
53   while (auto E = readdir(D)) {
54     std::string Path = DirPlusFile(Dir, E->d_name);
55     if (E->d_type == DT_REG || E->d_type == DT_LNK)
56       V->push_back(Path);
57     else if (E->d_type == DT_DIR && *E->d_name != '.')
58       ListFilesInDirRecursive(Path, Epoch, V, false);
59   }
60   closedir(D);
61   if (Epoch && TopDir)
62     *Epoch = E;
63 }
64 
65 char GetSeparator() {
66   return '/';
67 }
68 
69 FILE* OpenFile(int Fd, const char* Mode) {
70   return fdopen(Fd, Mode);
71 }
72 
73 int CloseFile(int fd) {
74   return close(fd);
75 }
76 
77 int DuplicateFile(int Fd) {
78   return dup(Fd);
79 }
80 
81 void RemoveFile(const std::string &Path) {
82   unlink(Path.c_str());
83 }
84 
85 void DiscardOutput(int Fd) {
86   FILE* Temp = fopen("/dev/null", "w");
87   if (!Temp)
88     return;
89   dup2(fileno(Temp), Fd);
90   fclose(Temp);
91 }
92 
93 intptr_t GetHandleFromFd(int fd) {
94   return static_cast<intptr_t>(fd);
95 }
96 
97 std::string DirName(const std::string &FileName) {
98   char *Tmp = new char[FileName.size() + 1];
99   memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
100   std::string Res = dirname(Tmp);
101   delete [] Tmp;
102   return Res;
103 }
104 
105 std::string TmpDir() {
106   if (auto Env = getenv("TMPDIR"))
107     return Env;
108   return "/tmp";
109 }
110 
111 bool IsInterestingCoverageFile(const std::string &FileName) {
112   if (FileName.find("compiler-rt/lib/") != std::string::npos)
113     return false; // sanitizer internal.
114   if (FileName.find("/usr/lib/") != std::string::npos)
115     return false;
116   if (FileName.find("/usr/include/") != std::string::npos)
117     return false;
118   if (FileName == "<null>")
119     return false;
120   return true;
121 }
122 
123 
124 void RawPrint(const char *Str) {
125   write(2, Str, strlen(Str));
126 }
127 
128 }  // namespace fuzzer
129 
130 #endif // LIBFUZZER_POSIX
131