blob: 2c452a7dd8d24dfb896da07178c0705c050a8213 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- 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
26namespace fuzzer {
27
28bool 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
kccdc00cd32017-08-29 20:51:24 +000035size_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
george.karpenkov29efa6d2017-08-21 23:25:50 +000042void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000043 Vector<std::string> *V, bool TopDir) {
george.karpenkov29efa6d2017-08-21 23:25:50 +000044 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
65char GetSeparator() {
66 return '/';
67}
68
69FILE* OpenFile(int Fd, const char* Mode) {
70 return fdopen(Fd, Mode);
71}
72
73int CloseFile(int fd) {
74 return close(fd);
75}
76
77int DuplicateFile(int Fd) {
78 return dup(Fd);
79}
80
81void RemoveFile(const std::string &Path) {
82 unlink(Path.c_str());
83}
84
85void 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
93intptr_t GetHandleFromFd(int fd) {
94 return static_cast<intptr_t>(fd);
95}
96
97std::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
105std::string TmpDir() {
106 if (auto Env = getenv("TMPDIR"))
107 return Env;
108 return "/tmp";
109}
110
111bool 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
124void RawPrint(const char *Str) {
125 write(2, Str, strlen(Str));
126}
127
128} // namespace fuzzer
129
130#endif // LIBFUZZER_POSIX