blob: eda8e8772930001dc5ccdd6f221e5b8f2839263a [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//===----------------------------------------------------------------------===//
Marcos Pividori178fe582016-12-13 17:46:11 +000011
Zachary Turner24a148b2016-11-30 19:06:14 +000012#include "FuzzerIO.h"
Kostya Serebryany556894f2016-09-21 02:05:39 +000013#include "FuzzerDefs.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000014#include "FuzzerExtFunctions.h"
Zachary Turner5abac172016-11-30 21:44:26 +000015#include <algorithm>
16#include <cstdarg>
Aaron Ballmanef116982015-01-29 16:58:29 +000017#include <fstream>
Zachary Turner5abac172016-11-30 21:44:26 +000018#include <iterator>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000019#include <sys/stat.h>
Marcos Pividori178fe582016-12-13 17:46:11 +000020#include <sys/types.h>
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 Serebryany09aa01a2016-09-21 01:04:43 +000026long GetEpoch(const std::string &Path) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000027 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000028 if (stat(Path.c_str(), &St))
29 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000030 return St.st_mtime;
31}
32
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000033Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
Aaron Ballmanef116982015-01-29 16:58:29 +000034 std::ifstream T(Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000035 if (ExitOnError && !T) {
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000036 Printf("No such directory: %s; exiting\n", Path.c_str());
37 exit(1);
38 }
Mike Aizatsky298516f2016-03-15 21:47:21 +000039
40 T.seekg(0, T.end);
41 size_t FileLen = T.tellg();
42 if (MaxSize)
43 FileLen = std::min(FileLen, MaxSize);
44
45 T.seekg(0, T.beg);
46 Unit Res(FileLen);
47 T.read(reinterpret_cast<char *>(Res.data()), FileLen);
48 return Res;
Aaron Ballmanef116982015-01-29 16:58:29 +000049}
50
Kostya Serebryany52a788e2015-03-31 20:13:20 +000051std::string FileToString(const std::string &Path) {
52 std::ifstream T(Path);
53 return std::string((std::istreambuf_iterator<char>(T)),
54 std::istreambuf_iterator<char>());
55}
56
Kostya Serebryany5b266a82015-02-04 19:10:20 +000057void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000058 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000059}
60
Aaron Ballmanef116982015-01-29 16:58:29 +000061void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000062 // Use raw C interface because this function may be called from a sig handler.
63 FILE *Out = fopen(Path.c_str(), "w");
64 if (!Out) return;
65 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
66 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000067}
68
Kostya Serebryany1ac80552015-05-08 21:30:55 +000069void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000070 long *Epoch, size_t MaxSize, bool ExitOnError) {
Kostya Serebryany1ac80552015-05-08 21:30:55 +000071 long E = Epoch ? *Epoch : 0;
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000072 std::vector<std::string> Files;
73 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
Kostya Serebryany5c3701c2016-03-04 22:35:40 +000074 size_t NumLoaded = 0;
Kostya Serebryanycfbcf902016-02-17 19:42:34 +000075 for (size_t i = 0; i < Files.size(); i++) {
76 auto &X = Files[i];
Kostya Serebryanyc43b5842016-03-18 01:36:00 +000077 if (Epoch && GetEpoch(X) < E) continue;
Kostya Serebryany5c3701c2016-03-04 22:35:40 +000078 NumLoaded++;
79 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
80 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000081 auto S = FileToVector(X, MaxSize, ExitOnError);
82 if (!S.empty())
83 V->push_back(S);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000084 }
Aaron Ballmanef116982015-01-29 16:58:29 +000085}
86
87std::string DirPlusFile(const std::string &DirPath,
88 const std::string &FileName) {
Zachary Turner5abac172016-11-30 21:44:26 +000089 return DirPath + GetSeparator() + FileName;
Aaron Ballmanef116982015-01-29 16:58:29 +000090}
91
Kostya Serebryany49e40902016-03-18 20:58:29 +000092void DupAndCloseStderr() {
Zachary Turner5abac172016-11-30 21:44:26 +000093 int OutputFd = DuplicateFile(2);
Kostya Serebryany6278f932016-03-24 00:57:32 +000094 if (OutputFd > 0) {
Zachary Turner5abac172016-11-30 21:44:26 +000095 FILE *NewOutputFile = OpenFile(OutputFd, "w");
Kostya Serebryany6278f932016-03-24 00:57:32 +000096 if (NewOutputFile) {
97 OutputFile = NewOutputFile;
Dan Liew1873a492016-06-07 23:32:50 +000098 if (EF->__sanitizer_set_report_fd)
99 EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd));
Zachary Turner5abac172016-11-30 21:44:26 +0000100 CloseFile(2);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000101 }
102 }
Kostya Serebryany49e40902016-03-18 20:58:29 +0000103}
104
Zachary Turner5abac172016-11-30 21:44:26 +0000105void CloseStdout() {
106 CloseFile(1);
107}
Kostya Serebryany49e40902016-03-18 20:58:29 +0000108
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000109void Printf(const char *Fmt, ...) {
110 va_list ap;
111 va_start(ap, Fmt);
Kostya Serebryany6278f932016-03-24 00:57:32 +0000112 vfprintf(OutputFile, Fmt, ap);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000113 va_end(ap);
Kostya Serebryanyf3ab6d92016-03-25 20:31:26 +0000114 fflush(OutputFile);
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000115}
116
Aaron Ballmanef116982015-01-29 16:58:29 +0000117} // namespace fuzzer