1 //===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 // IO functions implementation using Posix API.
9 //===----------------------------------------------------------------------===//
10 #include "mozilla/Unused.h"
11 #include "FuzzerDefs.h"
12 #if LIBFUZZER_POSIX || LIBFUZZER_FUCHSIA
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 
IsFile(const std::string & Path)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 
IsDirectory(const std::string & Path)35 static bool IsDirectory(const std::string &Path) {
36   struct stat St;
37   if (stat(Path.c_str(), &St))
38     return false;
39   return S_ISDIR(St.st_mode);
40 }
41 
FileSize(const std::string & Path)42 size_t FileSize(const std::string &Path) {
43   struct stat St;
44   if (stat(Path.c_str(), &St))
45     return 0;
46   return St.st_size;
47 }
48 
Basename(const std::string & Path)49 std::string Basename(const std::string &Path) {
50   size_t Pos = Path.rfind(GetSeparator());
51   if (Pos == std::string::npos) return Path;
52   assert(Pos < Path.size());
53   return Path.substr(Pos + 1);
54 }
55 
ListFilesInDirRecursive(const std::string & Dir,long * Epoch,Vector<std::string> * V,bool TopDir)56 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
57                              Vector<std::string> *V, bool TopDir) {
58   auto E = GetEpoch(Dir);
59   if (Epoch)
60     if (E && *Epoch >= E) return;
61 
62   DIR *D = opendir(Dir.c_str());
63   if (!D) {
64     Printf("%s: %s; exiting\n", strerror(errno), Dir.c_str());
65     exit(1);
66   }
67   while (auto E = readdir(D)) {
68     std::string Path = DirPlusFile(Dir, E->d_name);
69     if (E->d_type == DT_REG || E->d_type == DT_LNK ||
70         (E->d_type == DT_UNKNOWN && IsFile(Path)))
71       V->push_back(Path);
72     else if ((E->d_type == DT_DIR ||
73              (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
74              *E->d_name != '.')
75       ListFilesInDirRecursive(Path, Epoch, V, false);
76   }
77   closedir(D);
78   if (Epoch && TopDir)
79     *Epoch = E;
80 }
81 
82 
IterateDirRecursive(const std::string & Dir,void (* DirPreCallback)(const std::string & Dir),void (* DirPostCallback)(const std::string & Dir),void (* FileCallback)(const std::string & Dir))83 void IterateDirRecursive(const std::string &Dir,
84                          void (*DirPreCallback)(const std::string &Dir),
85                          void (*DirPostCallback)(const std::string &Dir),
86                          void (*FileCallback)(const std::string &Dir)) {
87   DirPreCallback(Dir);
88   DIR *D = opendir(Dir.c_str());
89   if (!D) return;
90   while (auto E = readdir(D)) {
91     std::string Path = DirPlusFile(Dir, E->d_name);
92     if (E->d_type == DT_REG || E->d_type == DT_LNK ||
93         (E->d_type == DT_UNKNOWN && IsFile(Path)))
94       FileCallback(Path);
95     else if ((E->d_type == DT_DIR ||
96              (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
97              *E->d_name != '.')
98       IterateDirRecursive(Path, DirPreCallback, DirPostCallback, FileCallback);
99   }
100   closedir(D);
101   DirPostCallback(Dir);
102 }
103 
GetSeparator()104 char GetSeparator() {
105   return '/';
106 }
107 
OpenFile(int Fd,const char * Mode)108 FILE* OpenFile(int Fd, const char* Mode) {
109   return fdopen(Fd, Mode);
110 }
111 
CloseFile(int fd)112 int CloseFile(int fd) {
113   return close(fd);
114 }
115 
DuplicateFile(int Fd)116 int DuplicateFile(int Fd) {
117   return dup(Fd);
118 }
119 
RemoveFile(const std::string & Path)120 void RemoveFile(const std::string &Path) {
121   unlink(Path.c_str());
122 }
123 
DiscardOutput(int Fd)124 void DiscardOutput(int Fd) {
125   FILE* Temp = fopen("/dev/null", "w");
126   if (!Temp)
127     return;
128   dup2(fileno(Temp), Fd);
129   fclose(Temp);
130 }
131 
GetHandleFromFd(int fd)132 intptr_t GetHandleFromFd(int fd) {
133   return static_cast<intptr_t>(fd);
134 }
135 
DirName(const std::string & FileName)136 std::string DirName(const std::string &FileName) {
137   char *Tmp = new char[FileName.size() + 1];
138   memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
139   std::string Res = dirname(Tmp);
140   delete [] Tmp;
141   return Res;
142 }
143 
TmpDir()144 std::string TmpDir() {
145   if (auto Env = getenv("TMPDIR"))
146     return Env;
147   return "/tmp";
148 }
149 
IsInterestingCoverageFile(const std::string & FileName)150 bool IsInterestingCoverageFile(const std::string &FileName) {
151   if (FileName.find("compiler-rt/lib/") != std::string::npos)
152     return false; // sanitizer internal.
153   if (FileName.find("/usr/lib/") != std::string::npos)
154     return false;
155   if (FileName.find("/usr/include/") != std::string::npos)
156     return false;
157   if (FileName == "<null>")
158     return false;
159   return true;
160 }
161 
RawPrint(const char * Str)162 void RawPrint(const char *Str) {
163   mozilla::Unused << write(2, Str, strlen(Str));
164 }
165 
MkDir(const std::string & Path)166 void MkDir(const std::string &Path) {
167   mkdir(Path.c_str(), 0700);
168 }
169 
RmDir(const std::string & Path)170 void RmDir(const std::string &Path) {
171   rmdir(Path.c_str());
172 }
173 
getDevNull()174 const std::string &getDevNull() {
175   static const std::string devNull = "/dev/null";
176   return devNull;
177 }
178 
179 }  // namespace fuzzer
180 
181 #endif // LIBFUZZER_POSIX
182