Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 1 | //===- 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 Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 11 | #include "FuzzerExtFunctions.h" |
Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 12 | #include "FuzzerDefs.h" |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 13 | #include <iterator> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 14 | #include <fstream> |
| 15 | #include <dirent.h> |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <unistd.h> |
Kostya Serebryany | 2a48c24 | 2015-11-13 01:54:40 +0000 | [diff] [blame] | 19 | #include <cstdarg> |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 20 | #include <cstdio> |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 21 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 22 | namespace fuzzer { |
| 23 | |
Kostya Serebryany | 6278f93 | 2016-03-24 00:57:32 +0000 | [diff] [blame] | 24 | static FILE *OutputFile = stderr; |
Kostya Serebryany | 49e4090 | 2016-03-18 20:58:29 +0000 | [diff] [blame] | 25 | |
Kostya Serebryany | bfbe7fc | 2016-02-02 03:03:47 +0000 | [diff] [blame] | 26 | bool 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 Serebryany | 09aa01a | 2016-09-21 01:04:43 +0000 | [diff] [blame] | 33 | long GetEpoch(const std::string &Path) { |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 34 | struct stat St; |
Kostya Serebryany | 9cdea94 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 35 | if (stat(Path.c_str(), &St)) |
| 36 | return 0; // Can't stat, be conservative. |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 37 | return St.st_mtime; |
| 38 | } |
| 39 | |
Kostya Serebryany | c43b584 | 2016-03-18 01:36:00 +0000 | [diff] [blame] | 40 | static 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 Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 46 | DIR *D = opendir(Dir.c_str()); |
Kostya Serebryany | 86e4a3e | 2015-07-18 00:03:37 +0000 | [diff] [blame] | 47 | if (!D) { |
| 48 | Printf("No such directory: %s; exiting\n", Dir.c_str()); |
| 49 | exit(1); |
| 50 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 51 | while (auto E = readdir(D)) { |
Kostya Serebryany | c43b584 | 2016-03-18 01:36:00 +0000 | [diff] [blame] | 52 | std::string Path = DirPlusFile(Dir, E->d_name); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 53 | if (E->d_type == DT_REG || E->d_type == DT_LNK) |
Kostya Serebryany | c43b584 | 2016-03-18 01:36:00 +0000 | [diff] [blame] | 54 | V->push_back(Path); |
| 55 | else if (E->d_type == DT_DIR && *E->d_name != '.') |
| 56 | ListFilesInDirRecursive(Path, Epoch, V, false); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 57 | } |
| 58 | closedir(D); |
Kostya Serebryany | c43b584 | 2016-03-18 01:36:00 +0000 | [diff] [blame] | 59 | if (Epoch && TopDir) |
| 60 | *Epoch = E; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 63 | Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 64 | std::ifstream T(Path); |
Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 65 | if (ExitOnError && !T) { |
Kostya Serebryany | b91c62b | 2015-10-16 22:41:47 +0000 | [diff] [blame] | 66 | Printf("No such directory: %s; exiting\n", Path.c_str()); |
| 67 | exit(1); |
| 68 | } |
Mike Aizatsky | 298516f | 2016-03-15 21:47:21 +0000 | [diff] [blame] | 69 | |
| 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 Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 81 | void DeleteFile(const std::string &Path) { |
| 82 | unlink(Path.c_str()); |
| 83 | } |
| 84 | |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 85 | std::string FileToString(const std::string &Path) { |
| 86 | std::ifstream T(Path); |
| 87 | return std::string((std::istreambuf_iterator<char>(T)), |
| 88 | std::istreambuf_iterator<char>()); |
| 89 | } |
| 90 | |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 91 | void CopyFileToErr(const std::string &Path) { |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 92 | Printf("%s", FileToString(Path).c_str()); |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 95 | void WriteToFile(const Unit &U, const std::string &Path) { |
Kostya Serebryany | ac25eeb | 2015-08-12 00:55:09 +0000 | [diff] [blame] | 96 | // Use raw C interface because this function may be called from a sig handler. |
| 97 | FILE *Out = fopen(Path.c_str(), "w"); |
| 98 | if (!Out) return; |
| 99 | fwrite(U.data(), sizeof(U[0]), U.size(), Out); |
| 100 | fclose(Out); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 103 | void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, |
Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 104 | long *Epoch, size_t MaxSize, bool ExitOnError) { |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 105 | long E = Epoch ? *Epoch : 0; |
Kostya Serebryany | c43b584 | 2016-03-18 01:36:00 +0000 | [diff] [blame] | 106 | std::vector<std::string> Files; |
| 107 | ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true); |
Kostya Serebryany | 5c3701c | 2016-03-04 22:35:40 +0000 | [diff] [blame] | 108 | size_t NumLoaded = 0; |
Kostya Serebryany | cfbcf90 | 2016-02-17 19:42:34 +0000 | [diff] [blame] | 109 | for (size_t i = 0; i < Files.size(); i++) { |
| 110 | auto &X = Files[i]; |
Kostya Serebryany | c43b584 | 2016-03-18 01:36:00 +0000 | [diff] [blame] | 111 | if (Epoch && GetEpoch(X) < E) continue; |
Kostya Serebryany | 5c3701c | 2016-03-04 22:35:40 +0000 | [diff] [blame] | 112 | NumLoaded++; |
| 113 | if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024) |
| 114 | Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path); |
Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 115 | auto S = FileToVector(X, MaxSize, ExitOnError); |
| 116 | if (!S.empty()) |
| 117 | V->push_back(S); |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 118 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | std::string DirPlusFile(const std::string &DirPath, |
| 122 | const std::string &FileName) { |
| 123 | return DirPath + "/" + FileName; |
| 124 | } |
| 125 | |
Kostya Serebryany | 49e4090 | 2016-03-18 20:58:29 +0000 | [diff] [blame] | 126 | void DupAndCloseStderr() { |
Kostya Serebryany | 6278f93 | 2016-03-24 00:57:32 +0000 | [diff] [blame] | 127 | int OutputFd = dup(2); |
| 128 | if (OutputFd > 0) { |
| 129 | FILE *NewOutputFile = fdopen(OutputFd, "w"); |
| 130 | if (NewOutputFile) { |
| 131 | OutputFile = NewOutputFile; |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 132 | if (EF->__sanitizer_set_report_fd) |
| 133 | EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(OutputFd)); |
Kostya Serebryany | 6278f93 | 2016-03-24 00:57:32 +0000 | [diff] [blame] | 134 | close(2); |
| 135 | } |
| 136 | } |
Kostya Serebryany | 49e4090 | 2016-03-18 20:58:29 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void CloseStdout() { close(1); } |
| 140 | |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 141 | void Printf(const char *Fmt, ...) { |
| 142 | va_list ap; |
| 143 | va_start(ap, Fmt); |
Kostya Serebryany | 6278f93 | 2016-03-24 00:57:32 +0000 | [diff] [blame] | 144 | vfprintf(OutputFile, Fmt, ap); |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 145 | va_end(ap); |
Kostya Serebryany | f3ab6d9 | 2016-03-25 20:31:26 +0000 | [diff] [blame] | 146 | fflush(OutputFile); |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 149 | } // namespace fuzzer |