LLVM  6.0.0svn
FuzzerIOPosix.cpp
Go to the documentation of this file.
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 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
36  std::vector<std::string> *V, bool TopDir) {
37  auto E = GetEpoch(Dir);
38  if (Epoch)
39  if (E && *Epoch >= E) return;
40 
41  DIR *D = opendir(Dir.c_str());
42  if (!D) {
43  Printf("No such directory: %s; exiting\n", Dir.c_str());
44  exit(1);
45  }
46  while (auto E = readdir(D)) {
47  std::string Path = DirPlusFile(Dir, E->d_name);
48  if (E->d_type == DT_REG || E->d_type == DT_LNK)
49  V->push_back(Path);
50  else if (E->d_type == DT_DIR && *E->d_name != '.')
51  ListFilesInDirRecursive(Path, Epoch, V, false);
52  }
53  closedir(D);
54  if (Epoch && TopDir)
55  *Epoch = E;
56 }
57 
58 char GetSeparator() {
59  return '/';
60 }
61 
62 FILE* OpenFile(int Fd, const char* Mode) {
63  return fdopen(Fd, Mode);
64 }
65 
66 int CloseFile(int fd) {
67  return close(fd);
68 }
69 
70 int DuplicateFile(int Fd) {
71  return dup(Fd);
72 }
73 
74 void RemoveFile(const std::string &Path) {
75  unlink(Path.c_str());
76 }
77 
78 void DiscardOutput(int Fd) {
79  FILE* Temp = fopen("/dev/null", "w");
80  if (!Temp)
81  return;
82  dup2(fileno(Temp), Fd);
83  fclose(Temp);
84 }
85 
86 intptr_t GetHandleFromFd(int fd) {
87  return static_cast<intptr_t>(fd);
88 }
89 
90 std::string DirName(const std::string &FileName) {
91  char *Tmp = new char[FileName.size() + 1];
92  memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
93  std::string Res = dirname(Tmp);
94  delete [] Tmp;
95  return Res;
96 }
97 
98 std::string TmpDir() {
99  if (auto Env = getenv("TMPDIR"))
100  return Env;
101  return "/tmp";
102 }
103 
104 bool IsInterestingCoverageFile(const std::string &FileName) {
105  if (FileName.find("compiler-rt/lib/") != std::string::npos)
106  return false; // sanitizer internal.
107  if (FileName.find("/usr/lib/") != std::string::npos)
108  return false;
109  if (FileName.find("/usr/include/") != std::string::npos)
110  return false;
111  if (FileName == "<null>")
112  return false;
113  return true;
114 }
115 
116 
117 void RawPrint(const char *Str) {
118  write(2, Str, strlen(Str));
119 }
120 
121 } // namespace fuzzer
122 
123 #endif // LIBFUZZER_POSIX
int CloseFile(int Fd)
SI Whole Quad Mode
int DuplicateFile(int Fd)
void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, std::vector< std::string > *V, bool TopDir)
bool IsFile(const std::string &Path)
long GetEpoch(const std::string &Path)
Definition: FuzzerIO.cpp:26
void Printf(const char *Fmt,...)
Definition: FuzzerIO.cpp:112
FILE * OpenFile(int Fd, const char *Mode)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string DirPlusFile(const std::string &DirPath, const std::string &FileName)
Definition: FuzzerIO.cpp:89
intptr_t GetHandleFromFd(int fd)
static void write(bool isBE, void *P, T V)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void RemoveFile(const std::string &Path)
std::string TmpDir()
void DiscardOutput(int Fd)
std::string DirName(const std::string &FileName)
bool IsInterestingCoverageFile(const std::string &FileName)
char GetSeparator()
void RawPrint(const char *Str)