| 1 | //===- FuzzerIO.cpp - IO utils. -------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // IO functions. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "FuzzerDefs.h" |
| 12 | #include "FuzzerExtFunctions.h" |
| 13 | #include "FuzzerIO.h" |
| 14 | #include "FuzzerUtil.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 | FILE *GetOutputFile() { |
| 27 | return OutputFile; |
| 28 | } |
| 29 | |
| 30 | void SetOutputFile(FILE *NewOutputFile) { |
| 31 | OutputFile = NewOutputFile; |
| 32 | } |
| 33 | |
| 34 | long GetEpoch(const std::string &Path) { |
| 35 | struct stat St; |
| 36 | if (stat(file: Path.c_str(), buf: &St)) |
| 37 | return 0; // Can't stat, be conservative. |
| 38 | return St.st_mtime; |
| 39 | } |
| 40 | |
| 41 | Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) { |
| 42 | std::ifstream T(Path, std::ios::binary); |
| 43 | if (ExitOnError && !T) { |
| 44 | Printf(Fmt: "No such directory: %s; exiting\n" , Path.c_str()); |
| 45 | exit(status: 1); |
| 46 | } |
| 47 | |
| 48 | T.seekg(off: 0, dir: T.end); |
| 49 | auto EndPos = T.tellg(); |
| 50 | if (EndPos < 0) return {}; |
| 51 | size_t FileLen = EndPos; |
| 52 | if (MaxSize) |
| 53 | FileLen = std::min(a: FileLen, b: MaxSize); |
| 54 | |
| 55 | T.seekg(off: 0, dir: T.beg); |
| 56 | Unit Res(FileLen); |
| 57 | T.read(s: reinterpret_cast<char *>(Res.data()), n: FileLen); |
| 58 | return Res; |
| 59 | } |
| 60 | |
| 61 | std::string FileToString(const std::string &Path) { |
| 62 | std::ifstream T(Path, std::ios::binary); |
| 63 | return std::string((std::istreambuf_iterator<char>(T)), |
| 64 | std::istreambuf_iterator<char>()); |
| 65 | } |
| 66 | |
| 67 | void CopyFileToErr(const std::string &Path) { |
| 68 | Puts(Str: FileToString(Path).c_str()); |
| 69 | } |
| 70 | |
| 71 | void WriteToFile(const Unit &U, const std::string &Path) { |
| 72 | WriteToFile(Data: U.data(), Size: U.size(), Path); |
| 73 | } |
| 74 | |
| 75 | void WriteToFile(const std::string &Data, const std::string &Path) { |
| 76 | WriteToFile(Data: reinterpret_cast<const uint8_t *>(Data.c_str()), Size: Data.size(), |
| 77 | Path); |
| 78 | } |
| 79 | |
| 80 | void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path) { |
| 81 | // Use raw C interface because this function may be called from a sig handler. |
| 82 | FILE *Out = fopen(filename: Path.c_str(), modes: "wb" ); |
| 83 | if (!Out) return; |
| 84 | fwrite(ptr: Data, size: sizeof(Data[0]), n: Size, s: Out); |
| 85 | fclose(stream: Out); |
| 86 | } |
| 87 | |
| 88 | void AppendToFile(const std::string &Data, const std::string &Path) { |
| 89 | AppendToFile(Data: reinterpret_cast<const uint8_t *>(Data.data()), Size: Data.size(), |
| 90 | Path); |
| 91 | } |
| 92 | |
| 93 | void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path) { |
| 94 | FILE *Out = fopen(filename: Path.c_str(), modes: "a" ); |
| 95 | if (!Out) |
| 96 | return; |
| 97 | fwrite(ptr: Data, size: sizeof(Data[0]), n: Size, s: Out); |
| 98 | fclose(stream: Out); |
| 99 | } |
| 100 | |
| 101 | void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, long *Epoch, |
| 102 | size_t MaxSize, bool ExitOnError, |
| 103 | std::vector<std::string> *VPaths) { |
| 104 | long E = Epoch ? *Epoch : 0; |
| 105 | std::vector<std::string> Files; |
| 106 | ListFilesInDirRecursive(Dir: Path, Epoch, V: &Files, /*TopDir*/true); |
| 107 | size_t NumLoaded = 0; |
| 108 | for (size_t i = 0; i < Files.size(); i++) { |
| 109 | auto &X = Files[i]; |
| 110 | if (Epoch && GetEpoch(Path: X) < E) continue; |
| 111 | NumLoaded++; |
| 112 | if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024) |
| 113 | Printf(Fmt: "Loaded %zd/%zd files from %s\n" , NumLoaded, Files.size(), Path); |
| 114 | auto S = FileToVector(Path: X, MaxSize, ExitOnError); |
| 115 | if (!S.empty()) { |
| 116 | V->push_back(x: S); |
| 117 | if (VPaths) |
| 118 | VPaths->push_back(x: X); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void GetSizedFilesFromDir(const std::string &Dir, std::vector<SizedFile> *V) { |
| 124 | std::vector<std::string> Files; |
| 125 | ListFilesInDirRecursive(Dir, Epoch: 0, V: &Files, /*TopDir*/true); |
| 126 | for (auto &File : Files) |
| 127 | if (size_t Size = FileSize(Path: File)) |
| 128 | V->push_back(x: {.File: File, .Size: Size}); |
| 129 | } |
| 130 | |
| 131 | std::string DirPlusFile(const std::string &DirPath, |
| 132 | const std::string &FileName) { |
| 133 | return DirPath + GetSeparator() + FileName; |
| 134 | } |
| 135 | |
| 136 | void DupAndCloseStderr() { |
| 137 | int OutputFd = DuplicateFile(Fd: 2); |
| 138 | if (OutputFd >= 0) { |
| 139 | FILE *NewOutputFile = OpenFile(Fd: OutputFd, Mode: "w" ); |
| 140 | if (NewOutputFile) { |
| 141 | OutputFile = NewOutputFile; |
| 142 | if (EF->__sanitizer_set_report_fd) |
| 143 | EF->__sanitizer_set_report_fd( |
| 144 | reinterpret_cast<void *>(GetHandleFromFd(fd: OutputFd))); |
| 145 | DiscardOutput(Fd: 2); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void CloseStdout() { |
| 151 | DiscardOutput(Fd: 1); |
| 152 | } |
| 153 | |
| 154 | void Puts(const char *Str) { |
| 155 | fputs(s: Str, stream: OutputFile); |
| 156 | fflush(stream: OutputFile); |
| 157 | } |
| 158 | |
| 159 | void Printf(const char *Fmt, ...) { |
| 160 | va_list ap; |
| 161 | va_start(ap, Fmt); |
| 162 | vfprintf(s: OutputFile, format: Fmt, arg: ap); |
| 163 | va_end(ap); |
| 164 | fflush(stream: OutputFile); |
| 165 | } |
| 166 | |
| 167 | void VPrintf(bool Verbose, const char *Fmt, ...) { |
| 168 | if (!Verbose) return; |
| 169 | va_list ap; |
| 170 | va_start(ap, Fmt); |
| 171 | vfprintf(s: OutputFile, format: Fmt, arg: ap); |
| 172 | va_end(ap); |
| 173 | fflush(stream: OutputFile); |
| 174 | } |
| 175 | |
| 176 | static bool MkDirRecursiveInner(const std::string &Leaf) { |
| 177 | // Prevent chance of potential infinite recursion |
| 178 | if (Leaf == "." ) |
| 179 | return true; |
| 180 | |
| 181 | const std::string &Dir = DirName(FileName: Leaf); |
| 182 | |
| 183 | if (IsDirectory(Path: Dir)) { |
| 184 | MkDir(Path: Leaf); |
| 185 | return IsDirectory(Path: Leaf); |
| 186 | } |
| 187 | |
| 188 | bool ret = MkDirRecursiveInner(Leaf: Dir); |
| 189 | if (!ret) { |
| 190 | // Give up early if a previous MkDir failed |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | MkDir(Path: Leaf); |
| 195 | return IsDirectory(Path: Leaf); |
| 196 | } |
| 197 | |
| 198 | bool MkDirRecursive(const std::string &Dir) { |
| 199 | if (Dir.empty()) |
| 200 | return false; |
| 201 | |
| 202 | if (IsDirectory(Path: Dir)) |
| 203 | return true; |
| 204 | |
| 205 | return MkDirRecursiveInner(Leaf: Dir); |
| 206 | } |
| 207 | |
| 208 | void RmDirRecursive(const std::string &Dir) { |
| 209 | IterateDirRecursive( |
| 210 | Dir, DirPreCallback: [](const std::string &Path) {}, |
| 211 | DirPostCallback: [](const std::string &Path) { RmDir(Path); }, |
| 212 | FileCallback: [](const std::string &Path) { RemoveFile(Path); }); |
| 213 | } |
| 214 | |
| 215 | std::string TempPath(const char *Prefix, const char *Extension) { |
| 216 | return DirPlusFile(DirPath: TmpDir(), FileName: std::string("libFuzzerTemp." ) + Prefix + |
| 217 | std::to_string(val: GetPid()) + Extension); |
| 218 | } |
| 219 | |
| 220 | } // namespace fuzzer |
| 221 | |