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