blob: 40209a034e37dd77e38ba200129e25401330507d [file] [log] [blame]
Zachary Turner5abac172016-11-30 21:44:26 +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//===----------------------------------------------------------------------===//
Zachary Turner5abac172016-11-30 21:44:26 +000011#include "FuzzerDefs.h"
12#if LIBFUZZER_POSIX
Marcos Pividori178fe582016-12-13 17:46:11 +000013
Zachary Turner5abac172016-11-30 21:44:26 +000014#include "FuzzerExtFunctions.h"
15#include "FuzzerIO.h"
16#include <cstdarg>
17#include <cstdio>
18#include <dirent.h>
19#include <fstream>
20#include <iterator>
Kostya Serebryany1cba0a92016-11-30 21:53:32 +000021#include <libgen.h>
Zachary Turner5abac172016-11-30 21:44:26 +000022#include <sys/stat.h>
Kostya Serebryany1cba0a92016-11-30 21:53:32 +000023#include <sys/types.h>
Zachary Turner5abac172016-11-30 21:44:26 +000024#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
35void 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
58char GetSeparator() {
59 return '/';
60}
61
62FILE* OpenFile(int Fd, const char* Mode) {
63 return fdopen(Fd, Mode);
64}
65
66int CloseFile(int fd) {
67 return close(fd);
68}
69
70int DuplicateFile(int Fd) {
71 return dup(Fd);
72}
73
Marcos Pividori7c1defd2016-12-13 17:46:40 +000074void RemoveFile(const std::string &Path) {
Zachary Turner5abac172016-11-30 21:44:26 +000075 unlink(Path.c_str());
76}
77
Marcos Pividori61ecfc02017-01-22 01:58:45 +000078void 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
Kostya Serebryany1cba0a92016-11-30 21:53:32 +000086std::string DirName(const std::string &FileName) {
87 char *Tmp = new char[FileName.size() + 1];
88 memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
89 std::string Res = dirname(Tmp);
90 delete [] Tmp;
91 return Res;
92}
93
Kostya Serebryany26482432017-01-05 04:32:19 +000094std::string TmpDir() {
95 if (auto Env = getenv("TMPDIR"))
96 return Env;
97 return "/tmp";
98}
99
Marcos Pividori60cc2fb2017-01-22 01:27:47 +0000100bool IsInterestingCoverageFile(const std::string &FileName) {
101 if (FileName.find("compiler-rt/lib/") != std::string::npos)
102 return false; // sanitizer internal.
103 if (FileName.find("/usr/lib/") != std::string::npos)
104 return false;
105 if (FileName.find("/usr/include/") != std::string::npos)
106 return false;
107 if (FileName == "<null>")
108 return false;
109 return true;
110}
111
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000112
113void RawPrint(const char *Str) {
114 write(2, Str, strlen(Str));
115}
116
Zachary Turner5abac172016-11-30 21:44:26 +0000117} // namespace fuzzer
Marcos Pividori178fe582016-12-13 17:46:11 +0000118
Zachary Turner5abac172016-11-30 21:44:26 +0000119#endif // LIBFUZZER_POSIX