blob: a70af886c2b7352d3c35e59705f1fba61403f959 [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//===----------------------------------------------------------------------===//
Dan Liew1873a492016-06-07 23:32:50 +000011#include "FuzzerExtFunctions.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000012#include "FuzzerDefs.h"
Kostya Serebryany5b266a82015-02-04 19:10:20 +000013#include <iterator>
Aaron Ballmanef116982015-01-29 16:58:29 +000014#include <fstream>
15#include <dirent.h>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000016#include <sys/types.h>
17#include <sys/stat.h>
18#include <unistd.h>
Kostya Serebryany2a48c242015-11-13 01:54:40 +000019#include <cstdarg>
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000020#include <cstdio>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000021
Aaron Ballmanef116982015-01-29 16:58:29 +000022namespace fuzzer {
23
Kostya Serebryany6278f932016-03-24 00:57:32 +000024static FILE *OutputFile = stderr;
Kostya Serebryany49e40902016-03-18 20:58:29 +000025
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000026bool IsFile(const std::string &Path) {
27 struct stat St;
28 if (stat(Path.c_str(), &St))
29 return false;
30 return S_ISREG(St.st_mode);
31}
32
Kostya Serebryany09aa01a2016-09-21 01:04:43 +000033long GetEpoch(const std::string &Path) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000034 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000035 if (stat(Path.c_str(), &St))
36 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000037 return St.st_mtime;
38}
39
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000040static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
41 std::vector<std::string> *V, bool TopDir) {
42 auto E = GetEpoch(Dir);
43 if (Epoch)
44 if (E && *Epoch >= E) return;
45
Aaron Ballmanef116982015-01-29 16:58:29 +000046 DIR *D = opendir(Dir.c_str());
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000047 if (!D) {
48 Printf("No such directory: %s; exiting\n", Dir.c_str());
49 exit(1);
50 }
Aaron Ballmanef116982015-01-29 16:58:29 +000051 while (auto E = readdir(D)) {
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000052 std::string Path = DirPlusFile(Dir, E->d_name);
Aaron Ballmanef116982015-01-29 16:58:29 +000053 if (E->d_type == DT_REG || E->d_type == DT_LNK)
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000054 V->push_back(Path);
55 else if (E->d_type == DT_DIR && *E->d_name != '.')
56 ListFilesInDirRecursive(Path, Epoch, V, false);
Aaron Ballmanef116982015-01-29 16:58:29 +000057 }
58 closedir(D);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000059 if (Epoch && TopDir)
60 *Epoch = E;
Aaron Ballmanef116982015-01-29 16:58:29 +000061}
62
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000063Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
Aaron Ballmanef116982015-01-29 16:58:29 +000064 std::ifstream T(Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000065 if (ExitOnError && !T) {
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000066 Printf("No such directory: %s; exiting\n", Path.c_str());
67 exit(1);
68 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000069
70 T.seekg(0, T.end);
71 size_t FileLen = T.tellg();
72 if (MaxSize)
73 FileLen = std::min(FileLen, MaxSize);
74
75 T.seekg(0, T.beg);
76 Unit Res(FileLen);
77 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
78 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000079}
80
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000081void DeleteFile(const std::string &Path) {
82 unlink(Path.c_str());
83}
84
Kostya Serebryany52a788e2015-03-31 20:13:20 +000085std::string FileToString(const std::string &Path) {
86 std::ifstream T(Path);
87 return std::string((std::istreambuf_iterator<char>(T)),
88 std::istreambuf_iterator<char>());
89}
90
Kostya Serebryany5b266a82015-02-04 19:10:20 +000091void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000092 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000093}
94
Aaron Ballmanef116982015-01-29 16:58:29 +000095void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000096 // Use raw C interface because this function may be called from a sig handler.
97 FILE *Out = fopen(Path.c_str(), "w");
98 if (!Out) return;
99 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
100 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +0000101}
102
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000103void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000104 long *Epoch, size_t MaxSize, bool ExitOnError) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000105 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000106 std::vector<std::string> Files;
107 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000108 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000109 for (size_t i = 0; i < Files.size(); i++) {
110 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000111 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000112 NumLoaded++;
113 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
114 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +0000115 auto S = FileToVector(X, MaxSize, ExitOnError);
116 if (!S.empty())
117 V->push_back(S);
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000118 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000119}
120
121std::string DirPlusFile(const std::string &DirPath,
122 const std::string &FileName) {
123 return DirPath + "/" + FileName;
124}
125
Kostya Serebryany49e40902016-03-18 20:58:29 +0000126void DupAndCloseStderr() {
Kostya Serebryany6278f932016-03-24 00:57:32 +0000127 int OutputFd = dup(2);
128 if (OutputFd > 0) {
129 FILE *NewOutputFile = fdopen(OutputFd, "w");
130 if (NewOutputFile) {
131 OutputFile = NewOutputFile;
Dan Liew1873a492016-06-07 23:32:50 +0000132 if (EF->__sanitizer_set_report_fd)
133 EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
Kostya Serebryany6278f932016-03-24 00:57:32 +0000134 close(2);
135 }
136 }
Kostya Serebryany49e40902016-03-18 20:58:29 +0000137}
138
139void CloseStdout() { close(1); }
140
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000141void Printf(const char *Fmt, ...) {
142 va_list ap;
143 va_start(ap, Fmt);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000144 vfprintf(OutputFile, Fmt, ap);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000145 va_end(ap);
Kostya Serebryanyf3ab6d92016-03-25 20:31:26 +0000146 fflush(OutputFile);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000147}
148
Aaron Ballmanef116982015-01-29 16:58:29 +0000149} // namespace fuzzer