blob: be7759fcc911497fd8ad1df1747a2f011191757f [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 Serebryanybfbe7fc2016-02-02 03:03:47 +000023bool IsFile(const std::string &Path) {
24 struct stat St;
25 if (stat(Path.c_str(), &St))
26 return false;
27 return S_ISREG(St.st_mode);
28}
29
Kostya Serebryany1ac80552015-05-08 21:30:55 +000030static long GetEpoch(const std::string &Path) {
31 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000032 if (stat(Path.c_str(), &St))
33 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000034 return St.st_mtime;
35}
36
37static std::vector<std::string> ListFilesInDir(const std::string &Dir,
38 long *Epoch) {
Aaron Ballmanef116982015-01-29 16:58:29 +000039 std::vector<std::string> V;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000040 if (Epoch) {
Kostya Serebryany06c199a2015-08-26 21:55:19 +000041 auto E = GetEpoch(Dir);
Kostya Serebryany078e9842016-02-02 02:07:26 +000042 if (E && *Epoch >= E) return V;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000043 *Epoch = E;
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)) {
51 if (E->d_type == DT_REG || E->d_type == DT_LNK)
52 V.push_back(E->d_name);
53 }
54 closedir(D);
55 return V;
56}
57
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000058Unit FileToVector(const std::string &Path, size_t MaxSize) {
Aaron Ballmanef116982015-01-29 16:58:29 +000059 std::ifstream T(Path);
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000060 if (!T) {
61 Printf("No such directory: %s; exiting\n", Path.c_str());
62 exit(1);
63 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000064
65 T.seekg(0, T.end);
66 size_t FileLen = T.tellg();
67 if (MaxSize)
68 FileLen = std::min(FileLen, MaxSize);
69
70 T.seekg(0, T.beg);
71 Unit Res(FileLen);
72 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
73 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000074}
75
Kostya Serebryany52a788e2015-03-31 20:13:20 +000076std::string FileToString(const std::string &Path) {
77 std::ifstream T(Path);
78 return std::string((std::istreambuf_iterator<char>(T)),
79 std::istreambuf_iterator<char>());
80}
81
Kostya Serebryany5b266a82015-02-04 19:10:20 +000082void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000083 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000084}
85
Aaron Ballmanef116982015-01-29 16:58:29 +000086void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000087 // Use raw C interface because this function may be called from a sig handler.
88 FILE *Out = fopen(Path.c_str(), "w");
89 if (!Out) return;
90 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
91 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000092}
93
Kostya Serebryany1ac80552015-05-08 21:30:55 +000094void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000095 long *Epoch, size_t MaxSize) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000096 long E = Epoch ? *Epoch : 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +000097 auto Files = ListFilesInDir(Path, Epoch);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +000098 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +000099 for (size_t i = 0; i < Files.size(); i++) {
100 auto &X = Files[i];
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000101 auto FilePath = DirPlusFile(Path, X);
102 if (Epoch && GetEpoch(FilePath) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000103 NumLoaded++;
104 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
105 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanya35f7d32016-02-18 21:49:10 +0000106 V->push_back(FileToVector(FilePath, MaxSize));
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000107 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000108}
109
110std::string DirPlusFile(const std::string &DirPath,
111 const std::string &FileName) {
112 return DirPath + "/" + FileName;
113}
114
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000115void Printf(const char *Fmt, ...) {
116 va_list ap;
117 va_start(ap, Fmt);
118 vfprintf(stderr, Fmt, ap);
119 va_end(ap);
120}
121
Aaron Ballmanef116982015-01-29 16:58:29 +0000122} // namespace fuzzer