blob: 358e645d728bbc361bacb2abad3e34008417ae81 [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//===----------------------------------------------------------------------===//
11#include "FuzzerInternal.h"
Kostya Serebryany5b266a82015-02-04 19:10:20 +000012#include <iterator>
Aaron Ballmanef116982015-01-29 16:58:29 +000013#include <fstream>
14#include <dirent.h>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000015#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
Kostya Serebryany2a48c242015-11-13 01:54:40 +000018#include <cstdarg>
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000019#include <cstdio>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000020
Aaron Ballmanef116982015-01-29 16:58:29 +000021namespace fuzzer {
22
Kostya Serebryany49e40902016-03-18 20:58:29 +000023static int OutputFd = 2;
24
Kostya Serebryanybfbe7fc2016-02-02 03:03:47 +000025bool IsFile(const std::string &Path) {
26 struct stat St;
27 if (stat(Path.c_str(), &St))
28 return false;
29 return S_ISREG(St.st_mode);
30}
31
Kostya Serebryany1ac80552015-05-08 21:30:55 +000032static long GetEpoch(const std::string &Path) {
33 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000034 if (stat(Path.c_str(), &St))
35 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000036 return St.st_mtime;
37}
38
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000039static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
40 std::vector<std::string> *V, bool TopDir) {
41 auto E = GetEpoch(Dir);
42 if (Epoch)
43 if (E && *Epoch >= E) return;
44
Aaron Ballmanef116982015-01-29 16:58:29 +000045 DIR *D = opendir(Dir.c_str());
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000046 if (!D) {
47 Printf("No such directory: %s; exiting\n", Dir.c_str());
48 exit(1);
49 }
Aaron Ballmanef116982015-01-29 16:58:29 +000050 while (auto E = readdir(D)) {
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000051 std::string Path = DirPlusFile(Dir, E->d_name);
Aaron Ballmanef116982015-01-29 16:58:29 +000052 if (E->d_type == DT_REG || E->d_type == DT_LNK)
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000053 V->push_back(Path);
54 else if (E->d_type == DT_DIR && *E->d_name != '.')
55 ListFilesInDirRecursive(Path, Epoch, V, false);
Aaron Ballmanef116982015-01-29 16:58:29 +000056 }
57 closedir(D);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000058 if (Epoch && TopDir)
59 *Epoch = E;
Aaron Ballmanef116982015-01-29 16:58:29 +000060}
61
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000062Unit FileToVector(const std::string &Path, size_t MaxSize) {
Aaron Ballmanef116982015-01-29 16:58:29 +000063 std::ifstream T(Path);
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000064 if (!T) {
65 Printf("No such directory: %s; exiting\n", Path.c_str());
66 exit(1);
67 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000068
69 T.seekg(0, T.end);
70 size_t FileLen = T.tellg();
71 if (MaxSize)
72 FileLen = std::min(FileLen, MaxSize);
73
74 T.seekg(0, T.beg);
75 Unit Res(FileLen);
76 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
77 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000078}
79
Kostya Serebryany52a788e2015-03-31 20:13:20 +000080std::string FileToString(const std::string &Path) {
81 std::ifstream T(Path);
82 return std::string((std::istreambuf_iterator<char>(T)),
83 std::istreambuf_iterator<char>());
84}
85
Kostya Serebryany5b266a82015-02-04 19:10:20 +000086void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000087 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000088}
89
Aaron Ballmanef116982015-01-29 16:58:29 +000090void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000091 // Use raw C interface because this function may be called from a sig handler.
92 FILE *Out = fopen(Path.c_str(), "w");
93 if (!Out) return;
94 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
95 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000096}
97
Kostya Serebryany1ac80552015-05-08 21:30:55 +000098void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000099 long *Epoch, size_t MaxSize) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000100 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000101 std::vector<std::string> Files;
102 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000103 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000104 for (size_t i = 0; i < Files.size(); i++) {
105 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000106 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000107 NumLoaded++;
108 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
109 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000110 V->push_back(FileToVector(X, MaxSize));
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000111 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000112}
113
114std::string DirPlusFile(const std::string &DirPath,
115 const std::string &FileName) {
116 return DirPath + "/" + FileName;
117}
118
Kostya Serebryany49e40902016-03-18 20:58:29 +0000119void DupAndCloseStderr() {
120 assert(OutputFd == 2);
121 OutputFd = dup(OutputFd);
122 if (OutputFd < 0)
123 OutputFd = 2;
124 else
125 close(2);
126}
127
128void CloseStdout() { close(1); }
129
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000130void Printf(const char *Fmt, ...) {
Kostya Serebryany49e40902016-03-18 20:58:29 +0000131 char Buf[1024];
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000132 va_list ap;
133 va_start(ap, Fmt);
Kostya Serebryany49e40902016-03-18 20:58:29 +0000134 int Formatted = vsnprintf(Buf, sizeof(Buf), Fmt, ap);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000135 va_end(ap);
Kostya Serebryany49e40902016-03-18 20:58:29 +0000136 if (Formatted)
137 write(OutputFd, Buf, Formatted);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000138}
139
Aaron Ballmanef116982015-01-29 16:58:29 +0000140} // namespace fuzzer