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 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, std::ios::binary); 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, std::ios::binary); 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 WriteToFile(U.data(), U.size(), Path); 65 } 66 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 72 void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path) { 73 // Use raw C interface because this function may be called from a sig handler. 74 FILE *Out = fopen(Path.c_str(), "wb"); 75 if (!Out) return; 76 fwrite(Data, sizeof(Data[0]), Size, Out); 77 fclose(Out); 78 } 79 80 void AppendToFile(const std::string &Data, const std::string &Path) { 81 AppendToFile(reinterpret_cast<const uint8_t *>(Data.data()), Data.size(), 82 Path); 83 } 84 85 void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path) { 86 FILE *Out = fopen(Path.c_str(), "a"); 87 if (!Out) 88 return; 89 fwrite(Data, sizeof(Data[0]), Size, Out); 90 fclose(Out); 91 } 92 93 void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V, long *Epoch, 94 size_t MaxSize, bool ExitOnError, 95 Vector<std::string> *VPaths) { 96 long E = Epoch ? *Epoch : 0; 97 Vector<std::string> Files; 98 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true); 99 size_t NumLoaded = 0; 100 for (size_t i = 0; i < Files.size(); i++) { 101 auto &X = Files[i]; 102 if (Epoch && GetEpoch(X) < E) continue; 103 NumLoaded++; 104 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024) 105 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path); 106 auto S = FileToVector(X, MaxSize, ExitOnError); 107 if (!S.empty()) { 108 V->push_back(S); 109 if (VPaths) 110 VPaths->push_back(X); 111 } 112 } 113 } 114 115 void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V) { 116 Vector<std::string> Files; 117 ListFilesInDirRecursive(Dir, 0, &Files, /*TopDir*/true); 118 for (auto &File : Files) 119 if (size_t Size = FileSize(File)) 120 V->push_back({File, Size}); 121 } 122 123 std::string DirPlusFile(const std::string &DirPath, 124 const std::string &FileName) { 125 return DirPath + GetSeparator() + FileName; 126 } 127 128 void DupAndCloseStderr() { 129 int OutputFd = DuplicateFile(2); 130 if (OutputFd >= 0) { 131 FILE *NewOutputFile = OpenFile(OutputFd, "w"); 132 if (NewOutputFile) { 133 OutputFile = NewOutputFile; 134 if (EF->__sanitizer_set_report_fd) 135 EF->__sanitizer_set_report_fd( 136 reinterpret_cast<void *>(GetHandleFromFd(OutputFd))); 137 DiscardOutput(2); 138 } 139 } 140 } 141 142 void CloseStdout() { 143 DiscardOutput(1); 144 } 145 146 void Printf(const char *Fmt, ...) { 147 va_list ap; 148 va_start(ap, Fmt); 149 vfprintf(OutputFile, Fmt, ap); 150 va_end(ap); 151 fflush(OutputFile); 152 } 153 154 void VPrintf(bool Verbose, const char *Fmt, ...) { 155 if (!Verbose) return; 156 va_list ap; 157 va_start(ap, Fmt); 158 vfprintf(OutputFile, Fmt, ap); 159 va_end(ap); 160 fflush(OutputFile); 161 } 162 163 static bool MkDirRecursiveInner(const std::string &Leaf) { 164 // Prevent chance of potential infinite recursion 165 if (Leaf == ".") 166 return true; 167 168 const std::string &Dir = DirName(Leaf); 169 170 if (IsDirectory(Dir)) { 171 MkDir(Leaf); 172 return IsDirectory(Leaf); 173 } 174 175 bool ret = MkDirRecursiveInner(Dir); 176 if (!ret) { 177 // Give up early if a previous MkDir failed 178 return ret; 179 } 180 181 MkDir(Leaf); 182 return IsDirectory(Leaf); 183 } 184 185 bool MkDirRecursive(const std::string &Dir) { 186 if (Dir.empty()) 187 return false; 188 189 if (IsDirectory(Dir)) 190 return true; 191 192 return MkDirRecursiveInner(Dir); 193 } 194 195 void RmDirRecursive(const std::string &Dir) { 196 IterateDirRecursive( 197 Dir, [](const std::string &Path) {}, 198 [](const std::string &Path) { RmDir(Path); }, 199 [](const std::string &Path) { RemoveFile(Path); }); 200 } 201 202 std::string TempPath(const char *Prefix, const char *Extension) { 203 return DirPlusFile(TmpDir(), std::string("libFuzzerTemp.") + Prefix + 204 std::to_string(GetPid()) + Extension); 205 } 206 207 } // namespace fuzzer 208