xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerIO.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 
GetOutputFile()26 FILE *GetOutputFile() {
27   return OutputFile;
28 }
29 
SetOutputFile(FILE * NewOutputFile)30 void SetOutputFile(FILE *NewOutputFile) {
31   OutputFile = NewOutputFile;
32 }
33 
GetEpoch(const std::string & Path)34 long GetEpoch(const std::string &Path) {
35   struct stat St;
36   if (stat(Path.c_str(), &St))
37     return 0;  // Can't stat, be conservative.
38   return St.st_mtime;
39 }
40 
FileToVector(const std::string & Path,size_t MaxSize,bool ExitOnError)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("No such directory: %s; exiting\n", Path.c_str());
45     exit(1);
46   }
47 
48   T.seekg(0, T.end);
49   auto EndPos = T.tellg();
50   if (EndPos < 0) return {};
51   size_t FileLen = EndPos;
52   if (MaxSize)
53     FileLen = std::min(FileLen, MaxSize);
54 
55   T.seekg(0, T.beg);
56   Unit Res(FileLen);
57   T.read(reinterpret_cast<char *>(Res.data()), FileLen);
58   return Res;
59 }
60 
FileToString(const std::string & Path)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 
CopyFileToErr(const std::string & Path)67 void CopyFileToErr(const std::string &Path) {
68   Puts(FileToString(Path).c_str());
69 }
70 
WriteToFile(const Unit & U,const std::string & Path)71 void WriteToFile(const Unit &U, const std::string &Path) {
72   WriteToFile(U.data(), U.size(), Path);
73 }
74 
WriteToFile(const std::string & Data,const std::string & Path)75 void WriteToFile(const std::string &Data, const std::string &Path) {
76   WriteToFile(reinterpret_cast<const uint8_t *>(Data.c_str()), Data.size(),
77               Path);
78 }
79 
WriteToFile(const uint8_t * Data,size_t Size,const std::string & Path)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(Path.c_str(), "wb");
83   if (!Out) return;
84   fwrite(Data, sizeof(Data[0]), Size, Out);
85   fclose(Out);
86 }
87 
AppendToFile(const std::string & Data,const std::string & Path)88 void AppendToFile(const std::string &Data, const std::string &Path) {
89   AppendToFile(reinterpret_cast<const uint8_t *>(Data.data()), Data.size(),
90                Path);
91 }
92 
AppendToFile(const uint8_t * Data,size_t Size,const std::string & Path)93 void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path) {
94   FILE *Out = fopen(Path.c_str(), "a");
95   if (!Out)
96     return;
97   fwrite(Data, sizeof(Data[0]), Size, Out);
98   fclose(Out);
99 }
100 
ReadDirToVectorOfUnits(const char * Path,std::vector<Unit> * V,long * Epoch,size_t MaxSize,bool ExitOnError,std::vector<std::string> * VPaths)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(Path, Epoch, &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(X) < E) continue;
111     NumLoaded++;
112     if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
113       Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
114     auto S = FileToVector(X, MaxSize, ExitOnError);
115     if (!S.empty()) {
116       V->push_back(S);
117       if (VPaths)
118         VPaths->push_back(X);
119     }
120   }
121 }
122 
GetSizedFilesFromDir(const std::string & Dir,std::vector<SizedFile> * V)123 void GetSizedFilesFromDir(const std::string &Dir, std::vector<SizedFile> *V) {
124   std::vector<std::string> Files;
125   ListFilesInDirRecursive(Dir, 0, &Files, /*TopDir*/true);
126   for (auto &File : Files)
127     if (size_t Size = FileSize(File))
128       V->push_back({File, Size});
129 }
130 
DirPlusFile(const std::string & DirPath,const std::string & FileName)131 std::string DirPlusFile(const std::string &DirPath,
132                         const std::string &FileName) {
133   return DirPath + GetSeparator() + FileName;
134 }
135 
DupAndCloseStderr()136 void DupAndCloseStderr() {
137   int OutputFd = DuplicateFile(2);
138   if (OutputFd >= 0) {
139     FILE *NewOutputFile = OpenFile(OutputFd, "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(OutputFd)));
145       DiscardOutput(2);
146     }
147   }
148 }
149 
CloseStdout()150 void CloseStdout() {
151   DiscardOutput(1);
152 }
153 
Puts(const char * Str)154 void Puts(const char *Str) {
155   fputs(Str, OutputFile);
156   fflush(OutputFile);
157 }
158 
Printf(const char * Fmt,...)159 void Printf(const char *Fmt, ...) {
160   va_list ap;
161   va_start(ap, Fmt);
162   vfprintf(OutputFile, Fmt, ap);
163   va_end(ap);
164   fflush(OutputFile);
165 }
166 
VPrintf(bool Verbose,const char * Fmt,...)167 void VPrintf(bool Verbose, const char *Fmt, ...) {
168   if (!Verbose) return;
169   va_list ap;
170   va_start(ap, Fmt);
171   vfprintf(OutputFile, Fmt, ap);
172   va_end(ap);
173   fflush(OutputFile);
174 }
175 
MkDirRecursiveInner(const std::string & Leaf)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(Leaf);
182 
183   if (IsDirectory(Dir)) {
184     MkDir(Leaf);
185     return IsDirectory(Leaf);
186   }
187 
188   bool ret = MkDirRecursiveInner(Dir);
189   if (!ret) {
190     // Give up early if a previous MkDir failed
191     return ret;
192   }
193 
194   MkDir(Leaf);
195   return IsDirectory(Leaf);
196 }
197 
MkDirRecursive(const std::string & Dir)198 bool MkDirRecursive(const std::string &Dir) {
199   if (Dir.empty())
200     return false;
201 
202   if (IsDirectory(Dir))
203     return true;
204 
205   return MkDirRecursiveInner(Dir);
206 }
207 
RmDirRecursive(const std::string & Dir)208 void RmDirRecursive(const std::string &Dir) {
209   IterateDirRecursive(
210       Dir, [](const std::string &Path) {},
211       [](const std::string &Path) { RmDir(Path); },
212       [](const std::string &Path) { RemoveFile(Path); });
213 }
214 
TempPath(const char * Prefix,const char * Extension)215 std::string TempPath(const char *Prefix, const char *Extension) {
216   return DirPlusFile(TmpDir(), std::string("libFuzzerTemp.") + Prefix +
217                                    std::to_string(GetPid()) + Extension);
218 }
219 
220 }  // namespace fuzzer
221