blob: 1e14cfcb2e6b360c995c923cb8240979acfe35b0 [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
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000037static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
38 std::vector<std::string> *V, bool TopDir) {
39 auto E = GetEpoch(Dir);
40 if (Epoch)
41 if (E && *Epoch >= E) return;
42
Aaron Ballmanef116982015-01-29 16:58:29 +000043 DIR *D = opendir(Dir.c_str());
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000044 if (!D) {
45 Printf("No such directory: %s; exiting\n", Dir.c_str());
46 exit(1);
47 }
Aaron Ballmanef116982015-01-29 16:58:29 +000048 while (auto E = readdir(D)) {
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000049 std::string Path = DirPlusFile(Dir, E->d_name);
Aaron Ballmanef116982015-01-29 16:58:29 +000050 if (E->d_type == DT_REG || E->d_type == DT_LNK)
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000051 V->push_back(Path);
52 else if (E->d_type == DT_DIR && *E->d_name != '.')
53 ListFilesInDirRecursive(Path, Epoch, V, false);
Aaron Ballmanef116982015-01-29 16:58:29 +000054 }
55 closedir(D);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000056 if (Epoch && TopDir)
57 *Epoch = E;
Aaron Ballmanef116982015-01-29 16:58:29 +000058}
59
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000060Unit FileToVector(const std::string &Path, size_t MaxSize) {
Aaron Ballmanef116982015-01-29 16:58:29 +000061 std::ifstream T(Path);
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000062 if (!T) {
63 Printf("No such directory: %s; exiting\n", Path.c_str());
64 exit(1);
65 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000066
67 T.seekg(0, T.end);
68 size_t FileLen = T.tellg();
69 if (MaxSize)
70 FileLen = std::min(FileLen, MaxSize);
71
72 T.seekg(0, T.beg);
73 Unit Res(FileLen);
74 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
75 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000076}
77
Kostya Serebryany52a788e2015-03-31 20:13:20 +000078std::string FileToString(const std::string &Path) {
79 std::ifstream T(Path);
80 return std::string((std::istreambuf_iterator<char>(T)),
81 std::istreambuf_iterator<char>());
82}
83
Kostya Serebryany5b266a82015-02-04 19:10:20 +000084void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000085 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000086}
87
Aaron Ballmanef116982015-01-29 16:58:29 +000088void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000089 // Use raw C interface because this function may be called from a sig handler.
90 FILE *Out = fopen(Path.c_str(), "w");
91 if (!Out) return;
92 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
93 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000094}
95
Kostya Serebryany1ac80552015-05-08 21:30:55 +000096void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanya35f7d32016-02-18 21:49:10 +000097 long *Epoch, size_t MaxSize) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000098 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000099 std::vector<std::string> Files;
100 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000101 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +0000102 for (size_t i = 0; i < Files.size(); i++) {
103 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000104 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +0000105 NumLoaded++;
106 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
107 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc43b5842016-03-18 01:36:00 +0000108 V->push_back(FileToVector(X, MaxSize));
Kostya Serebryany1ac80552015-05-08 21:30:55 +0000109 }
Aaron Ballmanef116982015-01-29 16:58:29 +0000110}
111
112std::string DirPlusFile(const std::string &DirPath,
113 const std::string &FileName) {
114 return DirPath + "/" + FileName;
115}
116
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000117void Printf(const char *Fmt, ...) {
118 va_list ap;
119 va_start(ap, Fmt);
120 vfprintf(stderr, Fmt, ap);
121 va_end(ap);
122}
123
Aaron Ballmanef116982015-01-29 16:58:29 +0000124} // namespace fuzzer