george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +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 | |
| 12 | #include "FuzzerIO.h" |
| 13 | #include "FuzzerDefs.h" |
| 14 | #include "FuzzerExtFunctions.h" |
| 15 | #include <algorithm> |
| 16 | #include <cstdarg> |
| 17 | #include <fstream> |
| 18 | #include <iterator> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | namespace fuzzer { |
| 23 | |
| 24 | static FILE *OutputFile = stderr; |
| 25 | |
| 26 | long GetEpoch(const std::string &Path) { |
| 27 | struct stat St; |
| 28 | if (stat(Path.c_str(), &St)) |
| 29 | return 0; // Can't stat, be conservative. |
| 30 | return St.st_mtime; |
| 31 | } |
| 32 | |
| 33 | Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) { |
| 34 | std::ifstream T(Path); |
| 35 | if (ExitOnError && !T) { |
| 36 | Printf("No such directory: %s; exiting\n", Path.c_str()); |
| 37 | exit(1); |
| 38 | } |
| 39 | |
| 40 | T.seekg(0, T.end); |
| 41 | auto EndPos = T.tellg(); |
| 42 | if (EndPos < 0) return {}; |
| 43 | size_t FileLen = EndPos; |
| 44 | if (MaxSize) |
| 45 | FileLen = std::min(FileLen, MaxSize); |
| 46 | |
| 47 | T.seekg(0, T.beg); |
| 48 | Unit Res(FileLen); |
| 49 | T.read(reinterpret_cast<char *>(Res.data()), FileLen); |
| 50 | return Res; |
| 51 | } |
| 52 | |
| 53 | std::string FileToString(const std::string &Path) { |
| 54 | std::ifstream T(Path); |
| 55 | return std::string((std::istreambuf_iterator<char>(T)), |
| 56 | std::istreambuf_iterator<char>()); |
| 57 | } |
| 58 | |
| 59 | void CopyFileToErr(const std::string &Path) { |
| 60 | Printf("%s", FileToString(Path).c_str()); |
| 61 | } |
| 62 | |
| 63 | void WriteToFile(const Unit &U, const std::string &Path) { |
| 64 | // Use raw C interface because this function may be called from a sig handler. |
| 65 | FILE *Out = fopen(Path.c_str(), "w"); |
| 66 | if (!Out) return; |
| 67 | fwrite(U.data(), sizeof(U[0]), U.size(), Out); |
| 68 | fclose(Out); |
| 69 | } |
| 70 | |
george.karpenkov | 5b2e0ee | 2017-08-26 17:17:37 +0000 | [diff] [blame^] | 71 | void ReadDirToVectorOfUnits(const char *Path, fuzzer::vector<Unit> *V, |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 72 | long *Epoch, size_t MaxSize, bool ExitOnError) { |
| 73 | long E = Epoch ? *Epoch : 0; |
george.karpenkov | 5b2e0ee | 2017-08-26 17:17:37 +0000 | [diff] [blame^] | 74 | fuzzer::vector<std::string> Files; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 75 | ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true); |
| 76 | size_t NumLoaded = 0; |
| 77 | for (size_t i = 0; i < Files.size(); i++) { |
| 78 | auto &X = Files[i]; |
| 79 | if (Epoch && GetEpoch(X) < E) continue; |
| 80 | NumLoaded++; |
| 81 | if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024) |
| 82 | Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path); |
| 83 | auto S = FileToVector(X, MaxSize, ExitOnError); |
| 84 | if (!S.empty()) |
| 85 | V->push_back(S); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | std::string DirPlusFile(const std::string &DirPath, |
| 90 | const std::string &FileName) { |
| 91 | return DirPath + GetSeparator() + FileName; |
| 92 | } |
| 93 | |
| 94 | void DupAndCloseStderr() { |
| 95 | int OutputFd = DuplicateFile(2); |
| 96 | if (OutputFd > 0) { |
| 97 | FILE *NewOutputFile = OpenFile(OutputFd, "w"); |
| 98 | if (NewOutputFile) { |
| 99 | OutputFile = NewOutputFile; |
| 100 | if (EF->__sanitizer_set_report_fd) |
| 101 | EF->__sanitizer_set_report_fd( |
| 102 | reinterpret_cast<void *>(GetHandleFromFd(OutputFd))); |
| 103 | DiscardOutput(2); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void CloseStdout() { |
| 109 | DiscardOutput(1); |
| 110 | } |
| 111 | |
| 112 | void Printf(const char *Fmt, ...) { |
| 113 | va_list ap; |
| 114 | va_start(ap, Fmt); |
| 115 | vfprintf(OutputFile, Fmt, ap); |
| 116 | va_end(ap); |
| 117 | fflush(OutputFile); |
| 118 | } |
| 119 | |
| 120 | } // namespace fuzzer |