blob: cfd69bbc8111a8019b5f40d4ed4590f5ff7f7bb3 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
george.karpenkov29efa6d2017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// IO functions implementation using Posix API.
9//===----------------------------------------------------------------------===//
10#include "FuzzerDefs.h"
morehouse400262a2017-12-08 22:54:44 +000011#if LIBFUZZER_POSIX || LIBFUZZER_FUCHSIA
george.karpenkov29efa6d2017-08-21 23:25:50 +000012
13#include "FuzzerExtFunctions.h"
14#include "FuzzerIO.h"
15#include <cstdarg>
16#include <cstdio>
17#include <dirent.h>
18#include <fstream>
19#include <iterator>
20#include <libgen.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25namespace fuzzer {
26
27bool IsFile(const std::string &Path) {
28 struct stat St;
29 if (stat(Path.c_str(), &St))
30 return false;
31 return S_ISREG(St.st_mode);
32}
33
kcce7761352017-11-15 16:45:17 +000034static bool IsDirectory(const std::string &Path) {
35 struct stat St;
36 if (stat(Path.c_str(), &St))
37 return false;
38 return S_ISDIR(St.st_mode);
39}
40
kccdc00cd32017-08-29 20:51:24 +000041size_t FileSize(const std::string &Path) {
42 struct stat St;
43 if (stat(Path.c_str(), &St))
44 return 0;
45 return St.st_size;
46}
47
morehouse68f46432018-08-30 15:54:44 +000048std::string Basename(const std::string &Path) {
49 size_t Pos = Path.rfind(GetSeparator());
50 if (Pos == std::string::npos) return Path;
51 assert(Pos < Path.size());
52 return Path.substr(Pos + 1);
53}
54
george.karpenkov29efa6d2017-08-21 23:25:50 +000055void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000056 Vector<std::string> *V, bool TopDir) {
george.karpenkov29efa6d2017-08-21 23:25:50 +000057 auto E = GetEpoch(Dir);
58 if (Epoch)
59 if (E && *Epoch >= E) return;
60
61 DIR *D = opendir(Dir.c_str());
62 if (!D) {
morehousec2b235e2018-04-06 18:15:24 +000063 Printf("%s: %s; exiting\n", strerror(errno), Dir.c_str());
george.karpenkov29efa6d2017-08-21 23:25:50 +000064 exit(1);
65 }
66 while (auto E = readdir(D)) {
67 std::string Path = DirPlusFile(Dir, E->d_name);
kcce7761352017-11-15 16:45:17 +000068 if (E->d_type == DT_REG || E->d_type == DT_LNK ||
69 (E->d_type == DT_UNKNOWN && IsFile(Path)))
george.karpenkov29efa6d2017-08-21 23:25:50 +000070 V->push_back(Path);
kcce7761352017-11-15 16:45:17 +000071 else if ((E->d_type == DT_DIR ||
72 (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
73 *E->d_name != '.')
george.karpenkov29efa6d2017-08-21 23:25:50 +000074 ListFilesInDirRecursive(Path, Epoch, V, false);
75 }
76 closedir(D);
77 if (Epoch && TopDir)
78 *Epoch = E;
79}
80
kcc64bcb922019-02-13 04:04:45 +000081
kcc55e54ed2019-02-15 21:51:15 +000082void IterateDirRecursive(const std::string &Dir,
kcc64bcb922019-02-13 04:04:45 +000083 void (*DirPreCallback)(const std::string &Dir),
84 void (*DirPostCallback)(const std::string &Dir),
85 void (*FileCallback)(const std::string &Dir)) {
86 DirPreCallback(Dir);
87 DIR *D = opendir(Dir.c_str());
88 if (!D) return;
89 while (auto E = readdir(D)) {
90 std::string Path = DirPlusFile(Dir, E->d_name);
91 if (E->d_type == DT_REG || E->d_type == DT_LNK ||
92 (E->d_type == DT_UNKNOWN && IsFile(Path)))
93 FileCallback(Path);
94 else if ((E->d_type == DT_DIR ||
95 (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
96 *E->d_name != '.')
kcc55e54ed2019-02-15 21:51:15 +000097 IterateDirRecursive(Path, DirPreCallback, DirPostCallback, FileCallback);
kcc64bcb922019-02-13 04:04:45 +000098 }
99 closedir(D);
100 DirPostCallback(Dir);
101}
102
george.karpenkov29efa6d2017-08-21 23:25:50 +0000103char GetSeparator() {
104 return '/';
105}
106
107FILE* OpenFile(int Fd, const char* Mode) {
108 return fdopen(Fd, Mode);
109}
110
111int CloseFile(int fd) {
112 return close(fd);
113}
114
115int DuplicateFile(int Fd) {
116 return dup(Fd);
117}
118
119void RemoveFile(const std::string &Path) {
120 unlink(Path.c_str());
121}
122
kcc6f1e9bc2019-04-13 00:20:31 +0000123void RenameFile(const std::string &OldPath, const std::string &NewPath) {
124 rename(OldPath.c_str(), NewPath.c_str());
125}
126
george.karpenkov29efa6d2017-08-21 23:25:50 +0000127void DiscardOutput(int Fd) {
128 FILE* Temp = fopen("/dev/null", "w");
129 if (!Temp)
130 return;
131 dup2(fileno(Temp), Fd);
132 fclose(Temp);
133}
134
135intptr_t GetHandleFromFd(int fd) {
136 return static_cast<intptr_t>(fd);
137}
138
139std::string DirName(const std::string &FileName) {
140 char *Tmp = new char[FileName.size() + 1];
141 memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
142 std::string Res = dirname(Tmp);
143 delete [] Tmp;
144 return Res;
145}
146
147std::string TmpDir() {
148 if (auto Env = getenv("TMPDIR"))
149 return Env;
150 return "/tmp";
151}
152
153bool IsInterestingCoverageFile(const std::string &FileName) {
154 if (FileName.find("compiler-rt/lib/") != std::string::npos)
155 return false; // sanitizer internal.
156 if (FileName.find("/usr/lib/") != std::string::npos)
157 return false;
158 if (FileName.find("/usr/include/") != std::string::npos)
159 return false;
160 if (FileName == "<null>")
161 return false;
162 return true;
163}
164
george.karpenkov29efa6d2017-08-21 23:25:50 +0000165void RawPrint(const char *Str) {
166 write(2, Str, strlen(Str));
167}
168
kcc243006d2019-02-12 00:12:33 +0000169void MkDir(const std::string &Path) {
170 mkdir(Path.c_str(), 0700);
171}
172
173void RmDir(const std::string &Path) {
174 rmdir(Path.c_str());
175}
176
metzmane847d8a2019-02-27 19:27:16 +0000177const std::string &getDevNull() {
178 static const std::string devNull = "/dev/null";
179 return devNull;
180}
181
george.karpenkov29efa6d2017-08-21 23:25:50 +0000182} // namespace fuzzer
183
184#endif // LIBFUZZER_POSIX