george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerIO.cpp - IO utils. -------------------------------------------===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // IO functions. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 11 | #include "FuzzerDefs.h" |
| 12 | #include "FuzzerExtFunctions.h" |
kcc | 2e6ca5c | 2019-02-12 22:48:55 +0000 | [diff] [blame] | 13 | #include "FuzzerIO.h" |
| 14 | #include "FuzzerUtil.h" |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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) { |
metzman | 2a53098 | 2018-11-06 23:25:25 +0000 | [diff] [blame] | 34 | std::ifstream T(Path, std::ios::binary); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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) { |
metzman | 2a53098 | 2018-11-06 23:25:25 +0000 | [diff] [blame] | 54 | std::ifstream T(Path, std::ios::binary); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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) { |
kcc | 6f1e9bc | 2019-04-13 00:20:31 +0000 | [diff] [blame] | 64 | WriteToFile(U.data(), U.size(), Path); |
| 65 | } |
| 66 | |
metzman | e9b95bc | 2019-04-30 20:56:18 +0000 | [diff] [blame] | 67 | void WriteToFile(const std::string &Data, const std::string &Path) { |
| 68 | WriteToFile(reinterpret_cast<const uint8_t *>(Data.c_str()), Data.size(), |
| 69 | Path); |
| 70 | } |
| 71 | |
kcc | 6f1e9bc | 2019-04-13 00:20:31 +0000 | [diff] [blame] | 72 | void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 73 | // Use raw C interface because this function may be called from a sig handler. |
vitalybuka | 07f7606 | 2019-04-05 20:17:03 +0000 | [diff] [blame] | 74 | FILE *Out = fopen(Path.c_str(), "wb"); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 75 | if (!Out) return; |
kcc | 6f1e9bc | 2019-04-13 00:20:31 +0000 | [diff] [blame] | 76 | fwrite(Data, sizeof(Data[0]), Size, Out); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 77 | fclose(Out); |
| 78 | } |
| 79 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 80 | void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V, |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 81 | long *Epoch, size_t MaxSize, bool ExitOnError) { |
| 82 | long E = Epoch ? *Epoch : 0; |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 83 | Vector<std::string> Files; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 84 | ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true); |
| 85 | size_t NumLoaded = 0; |
| 86 | for (size_t i = 0; i < Files.size(); i++) { |
| 87 | auto &X = Files[i]; |
| 88 | if (Epoch && GetEpoch(X) < E) continue; |
| 89 | NumLoaded++; |
| 90 | if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024) |
| 91 | Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path); |
| 92 | auto S = FileToVector(X, MaxSize, ExitOnError); |
| 93 | if (!S.empty()) |
| 94 | V->push_back(S); |
| 95 | } |
| 96 | } |
| 97 | |
kcc | 7f5f222 | 2017-09-12 21:58:07 +0000 | [diff] [blame] | 98 | |
| 99 | void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V) { |
| 100 | Vector<std::string> Files; |
| 101 | ListFilesInDirRecursive(Dir, 0, &Files, /*TopDir*/true); |
| 102 | for (auto &File : Files) |
| 103 | if (size_t Size = FileSize(File)) |
| 104 | V->push_back({File, Size}); |
| 105 | } |
| 106 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 107 | std::string DirPlusFile(const std::string &DirPath, |
| 108 | const std::string &FileName) { |
| 109 | return DirPath + GetSeparator() + FileName; |
| 110 | } |
| 111 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 112 | void DupAndCloseStderr() { |
| 113 | int OutputFd = DuplicateFile(2); |
| 114 | if (OutputFd > 0) { |
| 115 | FILE *NewOutputFile = OpenFile(OutputFd, "w"); |
| 116 | if (NewOutputFile) { |
| 117 | OutputFile = NewOutputFile; |
| 118 | if (EF->__sanitizer_set_report_fd) |
| 119 | EF->__sanitizer_set_report_fd( |
| 120 | reinterpret_cast<void *>(GetHandleFromFd(OutputFd))); |
| 121 | DiscardOutput(2); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void CloseStdout() { |
| 127 | DiscardOutput(1); |
| 128 | } |
| 129 | |
| 130 | void Printf(const char *Fmt, ...) { |
| 131 | va_list ap; |
| 132 | va_start(ap, Fmt); |
| 133 | vfprintf(OutputFile, Fmt, ap); |
| 134 | va_end(ap); |
| 135 | fflush(OutputFile); |
| 136 | } |
| 137 | |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 138 | void VPrintf(bool Verbose, const char *Fmt, ...) { |
| 139 | if (!Verbose) return; |
| 140 | va_list ap; |
| 141 | va_start(ap, Fmt); |
| 142 | vfprintf(OutputFile, Fmt, ap); |
| 143 | va_end(ap); |
| 144 | fflush(OutputFile); |
| 145 | } |
| 146 | |
kcc | 64bcb92 | 2019-02-13 04:04:45 +0000 | [diff] [blame] | 147 | void RmDirRecursive(const std::string &Dir) { |
kcc | 55e54ed | 2019-02-15 21:51:15 +0000 | [diff] [blame] | 148 | IterateDirRecursive( |
kcc | 64bcb92 | 2019-02-13 04:04:45 +0000 | [diff] [blame] | 149 | Dir, [](const std::string &Path) {}, |
| 150 | [](const std::string &Path) { RmDir(Path); }, |
| 151 | [](const std::string &Path) { RemoveFile(Path); }); |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 152 | } |
| 153 | |
kcc | 2e6ca5c | 2019-02-12 22:48:55 +0000 | [diff] [blame] | 154 | std::string TempPath(const char *Extension) { |
| 155 | return DirPlusFile(TmpDir(), |
| 156 | "libFuzzerTemp." + std::to_string(GetPid()) + Extension); |
| 157 | } |
| 158 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 159 | } // namespace fuzzer |