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 | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 18 | #include <cstdio> |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 19 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 20 | namespace fuzzer { |
| 21 | |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 22 | static long GetEpoch(const std::string &Path) { |
| 23 | struct stat St; |
Kostya Serebryany | 9cdea94 | 2015-09-08 20:36:33 +0000 | [diff] [blame] | 24 | if (stat(Path.c_str(), &St)) |
| 25 | return 0; // Can't stat, be conservative. |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 26 | return St.st_mtime; |
| 27 | } |
| 28 | |
| 29 | static std::vector<std::string> ListFilesInDir(const std::string &Dir, |
| 30 | long *Epoch) { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 31 | std::vector<std::string> V; |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 32 | if (Epoch) { |
Kostya Serebryany | 06c199a | 2015-08-26 21:55:19 +0000 | [diff] [blame] | 33 | auto E = GetEpoch(Dir); |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 34 | if (*Epoch >= E) return V; |
| 35 | *Epoch = E; |
| 36 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 37 | DIR *D = opendir(Dir.c_str()); |
Kostya Serebryany | 86e4a3e | 2015-07-18 00:03:37 +0000 | [diff] [blame] | 38 | if (!D) { |
| 39 | Printf("No such directory: %s; exiting\n", Dir.c_str()); |
| 40 | exit(1); |
| 41 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 42 | while (auto E = readdir(D)) { |
| 43 | if (E->d_type == DT_REG || E->d_type == DT_LNK) |
| 44 | V.push_back(E->d_name); |
| 45 | } |
| 46 | closedir(D); |
| 47 | return V; |
| 48 | } |
| 49 | |
| 50 | Unit FileToVector(const std::string &Path) { |
| 51 | std::ifstream T(Path); |
Kostya Serebryany | b91c62b | 2015-10-16 22:41:47 +0000 | [diff] [blame^] | 52 | if (!T) { |
| 53 | Printf("No such directory: %s; exiting\n", Path.c_str()); |
| 54 | exit(1); |
| 55 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 56 | return Unit((std::istreambuf_iterator<char>(T)), |
| 57 | std::istreambuf_iterator<char>()); |
| 58 | } |
| 59 | |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 60 | std::string FileToString(const std::string &Path) { |
| 61 | std::ifstream T(Path); |
| 62 | return std::string((std::istreambuf_iterator<char>(T)), |
| 63 | std::istreambuf_iterator<char>()); |
| 64 | } |
| 65 | |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 66 | void CopyFileToErr(const std::string &Path) { |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 67 | Printf("%s", FileToString(Path).c_str()); |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 70 | void WriteToFile(const Unit &U, const std::string &Path) { |
Kostya Serebryany | ac25eeb | 2015-08-12 00:55:09 +0000 | [diff] [blame] | 71 | // Use raw C interface because this function may be called from a sig handler. |
| 72 | FILE *Out = fopen(Path.c_str(), "w"); |
| 73 | if (!Out) return; |
| 74 | fwrite(U.data(), sizeof(U[0]), U.size(), Out); |
| 75 | fclose(Out); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 78 | void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, |
| 79 | long *Epoch) { |
| 80 | long E = Epoch ? *Epoch : 0; |
| 81 | for (auto &X : ListFilesInDir(Path, Epoch)) { |
| 82 | auto FilePath = DirPlusFile(Path, X); |
| 83 | if (Epoch && GetEpoch(FilePath) < E) continue; |
| 84 | V->push_back(FileToVector(FilePath)); |
| 85 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::string DirPlusFile(const std::string &DirPath, |
| 89 | const std::string &FileName) { |
| 90 | return DirPath + "/" + FileName; |
| 91 | } |
| 92 | |
Kostya Serebryany | ca6a2a2 | 2015-05-05 21:59:51 +0000 | [diff] [blame] | 93 | void PrintFileAsBase64(const std::string &Path) { |
| 94 | std::string Cmd = "base64 -w 0 < " + Path + "; echo"; |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 95 | ExecuteCommand(Cmd); |
Kostya Serebryany | ca6a2a2 | 2015-05-05 21:59:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 98 | void Printf(const char *Fmt, ...) { |
| 99 | va_list ap; |
| 100 | va_start(ap, Fmt); |
| 101 | vfprintf(stderr, Fmt, ap); |
| 102 | va_end(ap); |
| 103 | } |
| 104 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 105 | } // namespace fuzzer |