blob: 6cc8a8e0fd53ce8252ba5693dab6352178c41031 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
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.
10//===----------------------------------------------------------------------===//
Zachary Turner24a148b2016-11-30 19:06:14 +000011#include "FuzzerIO.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000012#include "FuzzerDefs.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000013#include "FuzzerExtFunctions.h"
Kostya Serebryany5b266a82015-02-04 19:10:20 +000014#include <iterator>
Aaron Ballmanef116982015-01-29 16:58:29 +000015#include <fstream>
16#include <dirent.h>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000017#include <sys/types.h>
18#include <sys/stat.h>
19#include <unistd.h>
Kostya Serebryany2a48c242015-11-13 01:54:40 +000020#include <cstdarg>
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000021#include <cstdio>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000022
Aaron Ballmanef116982015-01-29 16:58:29 +000023namespace fuzzer {
24
Kostya Serebryany6278f932016-03-24 00:57:32 +000025static FILE *OutputFile = stderr;
Kostya Serebryany49e40902016-03-18 20:58:29 +000026
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000027bool 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
Kostya Serebryany09aa01a2016-09-21 01:04:43 +000034long GetEpoch(const std::string &Path) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000035 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000036 if (stat(Path.c_str(), &St))
37 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000038 return St.st_mtime;
39}
40
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000041static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
42 std::vector<std::string> *V, bool TopDir) {
43 auto E = GetEpoch(Dir);
44 if (Epoch)
45 if (E && *Epoch >= E) return;
46
Aaron Ballmanef116982015-01-29 16:58:29 +000047 DIR *D = opendir(Dir.c_str());
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000048 if (!D) {
49 Printf("No such directory: %s; exiting\n", Dir.c_str());
50 exit(1);
51 }
Aaron Ballmanef116982015-01-29 16:58:29 +000052 while (auto E = readdir(D)) {
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000053 std::string Path = DirPlusFile(Dir, E->d_name);
Aaron Ballmanef116982015-01-29 16:58:29 +000054 if (E->d_type == DT_REG || E->d_type == DT_LNK)
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000055 V->push_back(Path);
56 else if (E->d_type == DT_DIR && *E->d_name != '.')
57 ListFilesInDirRecursive(Path, Epoch, V, false);
Aaron Ballmanef116982015-01-29 16:58:29 +000058 }
59 closedir(D);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000060 if (Epoch && TopDir)
61 *Epoch = E;
Aaron Ballmanef116982015-01-29 16:58:29 +000062}
63
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000064Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
Aaron Ballmanef116982015-01-29 16:58:29 +000065 std::ifstream T(Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000066 if (ExitOnError && !T) {
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000067 Printf("No such directory: %s; exiting\n", Path.c_str());
68 exit(1);
69 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000070
71 T.seekg(0, T.end);
72 size_t FileLen = T.tellg();
73 if (MaxSize)
74 FileLen = std::min(FileLen, MaxSize);
75
76 T.seekg(0, T.beg);
77 Unit Res(FileLen);
78 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
79 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000080}
81
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000082void DeleteFile(const std::string &Path) {
83 unlink(Path.c_str());
84}
85
Kostya Serebryany52a788e2015-03-31 20:13:20 +000086std::string FileToString(const std::string &Path) {
87 std::ifstream T(Path);
88 return std::string((std::istreambuf_iterator<char>(T)),
89 std::istreambuf_iterator<char>());
90}
91
Kostya Serebryany5b266a82015-02-04 19:10:20 +000092void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000093 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000094}
95
Aaron Ballmanef116982015-01-29 16:58:29 +000096void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000097 // Use raw C interface because this function may be called from a sig handler.
98 FILE *Out = fopen(Path.c_str(), "w");
99 if (!Out) return;
100 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
101 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +0000102}
103
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000104void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000105 long *Epoch, size_t MaxSize, bool ExitOnError) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000106 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000107 std::vector<std::string> Files;
108 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000109 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000110 for (size_t i = 0; i < Files.size(); i++) {
111 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000112 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000113 NumLoaded++;
114 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
115 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000116 auto S = FileToVector(X, MaxSize, ExitOnError);
117 if (!S.empty())
118 V->push_back(S);
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000119 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000120}
121
122std::string DirPlusFile(const std::string &DirPath,
123 const std::string &FileName) {
124 return DirPath + "/" + FileName;
125}
126
Kostya Serebryany49e40902016-03-18 20:58:29 +0000127void DupAndCloseStderr() {
Kostya Serebryany6278f932016-03-24 00:57:32 +0000128 int OutputFd = dup(2);
129 if (OutputFd > 0) {
130 FILE *NewOutputFile = fdopen(OutputFd, "w");
131 if (NewOutputFile) {
132 OutputFile = NewOutputFile;
Dan Liew1873a492016-06-07 23:32:50 +0000133 if (EF->__sanitizer_set_report_fd)
134 EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
Kostya Serebryany6278f932016-03-24 00:57:32 +0000135 close(2);
136 }
137 }
Kostya Serebryany49e40902016-03-18 20:58:29 +0000138}
139
140void CloseStdout() { close(1); }
141
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000142void Printf(const char *Fmt, ...) {
143 va_list ap;
144 va_start(ap, Fmt);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000145 vfprintf(OutputFile, Fmt, ap);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000146 va_end(ap);
Kostya Serebryanyf3ab6d92016-03-25 20:31:26 +0000147 fflush(OutputFile);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000148}
149
Aaron Ballmanef116982015-01-29 16:58:29 +0000150} // namespace fuzzer