blob: 5b394b641a3acac4e078a99e81ec19ee92351f4f [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"
Aaron Ballmanef116982015-01-29 16:58:29 +000012#include "FuzzerInternal.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 Serebryanya35f7d32016-02-18 21:49:10 +000063Unit FileToVector(const std::string &Path, size_t MaxSize) {
Aaron Ballmanef116982015-01-29 16:58:29 +000064 std::ifstream T(Path);
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000065 if (!T) {
66 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 Serebryany52a788e2015-03-31 20:13:20 +000081std::string FileToString(const std::string &Path) {
82 std::ifstream T(Path);
83 return std::string((std::istreambuf_iterator<char>(T)),
84 std::istreambuf_iterator<char>());
85}
86
Kostya Serebryany5b266a82015-02-04 19:10:20 +000087void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000088 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000089}
90
Aaron Ballmanef116982015-01-29 16:58:29 +000091void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000092 // Use raw C interface because this function may be called from a sig handler.
93 FILE *Out = fopen(Path.c_str(), "w");
94 if (!Out) return;
95 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
96 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000097}
98
Kostya Serebryany1ac80552015-05-08 21:30:55 +000099void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000100 long *Epoch, size_t MaxSize) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000101 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000102 std::vector<std::string> Files;
103 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000104 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000105 for (size_t i = 0; i < Files.size(); i++) {
106 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000107 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000108 NumLoaded++;
109 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
110 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000111 V->push_back(FileToVector(X, MaxSize));
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000112 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000113}
114
115std::string DirPlusFile(const std::string &DirPath,
116 const std::string &FileName) {
117 return DirPath + "/" + FileName;
118}
119
Kostya Serebryany49e40902016-03-18 20:58:29 +0000120void DupAndCloseStderr() {
Kostya Serebryany6278f932016-03-24 00:57:32 +0000121 int OutputFd = dup(2);
122 if (OutputFd > 0) {
123 FILE *NewOutputFile = fdopen(OutputFd, "w");
124 if (NewOutputFile) {
125 OutputFile = NewOutputFile;
Dan Liew1873a492016-06-07 23:32:50 +0000126 if (EF->__sanitizer_set_report_fd)
127 EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
Kostya Serebryany6278f932016-03-24 00:57:32 +0000128 close(2);
129 }
130 }
Kostya Serebryany49e40902016-03-18 20:58:29 +0000131}
132
133void CloseStdout() { close(1); }
134
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000135void Printf(const char *Fmt, ...) {
136 va_list ap;
137 va_start(ap, Fmt);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000138 vfprintf(OutputFile, Fmt, ap);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000139 va_end(ap);
Kostya Serebryanyf3ab6d92016-03-25 20:31:26 +0000140 fflush(OutputFile);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000141}
142
Aaron Ballmanef116982015-01-29 16:58:29 +0000143} // namespace fuzzer