blob: 5d4d415d49e3e03aab97b1eb4162e272e59bf9e2 [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//===----------------------------------------------------------------------===//
Zachary Turner24a148b2016-11-30 19:06:14 +000011#include "FuzzerIO.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000012#include "FuzzerDefs.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000013#include "FuzzerExtFunctions.h"
Zachary Turner5abac172016-11-30 21:44:26 +000014#include <algorithm>
15#include <cstdarg>
Aaron Ballmanef116982015-01-29 16:58:29 +000016#include <fstream>
Zachary Turner5abac172016-11-30 21:44:26 +000017#include <iterator>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000018#include <sys/types.h>
19#include <sys/stat.h>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000020
Aaron Ballmanef116982015-01-29 16:58:29 +000021namespace fuzzer {
22
Kostya Serebryany6278f932016-03-24 00:57:32 +000023static FILE *OutputFile = stderr;
Kostya Serebryany49e40902016-03-18 20:58:29 +000024
Kostya Serebryany09aa01a2016-09-21 01:04:43 +000025long GetEpoch(const std::string &Path) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000026 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000027 if (stat(Path.c_str(), &St))
28 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000029 return St.st_mtime;
30}
31
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000032Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
Aaron Ballmanef116982015-01-29 16:58:29 +000033 std::ifstream T(Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000034 if (ExitOnError && !T) {
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000035 Printf("No such directory: %s; exiting\n", Path.c_str());
36 exit(1);
37 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000038
39 T.seekg(0, T.end);
40 size_t FileLen = T.tellg();
41 if (MaxSize)
42 FileLen = std::min(FileLen, MaxSize);
43
44 T.seekg(0, T.beg);
45 Unit Res(FileLen);
46 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
47 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000048}
49
Kostya Serebryany52a788e2015-03-31 20:13:20 +000050std::string FileToString(const std::string &Path) {
51 std::ifstream T(Path);
52 return std::string((std::istreambuf_iterator<char>(T)),
53 std::istreambuf_iterator<char>());
54}
55
Kostya Serebryany5b266a82015-02-04 19:10:20 +000056void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000057 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000058}
59
Aaron Ballmanef116982015-01-29 16:58:29 +000060void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000061 // Use raw C interface because this function may be called from a sig handler.
62 FILE *Out = fopen(Path.c_str(), "w");
63 if (!Out) return;
64 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
65 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000066}
67
Kostya Serebryany1ac80552015-05-08 21:30:55 +000068void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000069 long *Epoch, size_t MaxSize, bool ExitOnError) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000070 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000071 std::vector<std::string> Files;
72 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +000073 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +000074 for (size_t i = 0; i < Files.size(); i++) {
75 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000076 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +000077 NumLoaded++;
78 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
79 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000080 auto S = FileToVector(X, MaxSize, ExitOnError);
81 if (!S.empty())
82 V->push_back(S);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000083 }
Aaron Ballmanef116982015-01-29 16:58:29 +000084}
85
86std::string DirPlusFile(const std::string &DirPath,
87 const std::string &FileName) {
Zachary Turner5abac172016-11-30 21:44:26 +000088 return DirPath + GetSeparator() + FileName;
Aaron Ballmanef116982015-01-29 16:58:29 +000089}
90
Kostya Serebryany49e40902016-03-18 20:58:29 +000091void DupAndCloseStderr() {
Zachary Turner5abac172016-11-30 21:44:26 +000092 int OutputFd = DuplicateFile(2);
Kostya Serebryany6278f932016-03-24 00:57:32 +000093 if (OutputFd > 0) {
Zachary Turner5abac172016-11-30 21:44:26 +000094 FILE *NewOutputFile = OpenFile(OutputFd, "w");
Kostya Serebryany6278f932016-03-24 00:57:32 +000095 if (NewOutputFile) {
96 OutputFile = NewOutputFile;
Dan Liew1873a492016-06-07 23:32:50 +000097 if (EF->__sanitizer_set_report_fd)
98 EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
Zachary Turner5abac172016-11-30 21:44:26 +000099 CloseFile(2);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000100 }
101 }
Kostya Serebryany49e40902016-03-18 20:58:29 +0000102}
103
Zachary Turner5abac172016-11-30 21:44:26 +0000104void CloseStdout() {
105 CloseFile(1);
106}
Kostya Serebryany49e40902016-03-18 20:58:29 +0000107
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000108void Printf(const char *Fmt, ...) {
109 va_list ap;
110 va_start(ap, Fmt);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000111 vfprintf(OutputFile, Fmt, ap);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000112 va_end(ap);
Kostya Serebryanyf3ab6d92016-03-25 20:31:26 +0000113 fflush(OutputFile);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000114}
115
Aaron Ballmanef116982015-01-29 16:58:29 +0000116} // namespace fuzzer