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