xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerIO.cpp (revision ecaeac805b044f715c98960a8fbf19fe2b76ae6b)
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(Path.c_str(), &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("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 
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   Printf("%s", FileToString(Path).c_str());
69 }
70 
71 void WriteToFile(const Unit &U, const std::string &Path) {
72   WriteToFile(U.data(), U.size(), Path);
73 }
74 
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 
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 
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 
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 
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 
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 
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(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 
150 void CloseStdout() {
151   DiscardOutput(1);
152 }
153 
154 void Printf(const char *Fmt, ...) {
155   va_list ap;
156   va_start(ap, Fmt);
157   vfprintf(OutputFile, Fmt, ap);
158   va_end(ap);
159   fflush(OutputFile);
160 }
161 
162 void VPrintf(bool Verbose, const char *Fmt, ...) {
163   if (!Verbose) return;
164   va_list ap;
165   va_start(ap, Fmt);
166   vfprintf(OutputFile, Fmt, ap);
167   va_end(ap);
168   fflush(OutputFile);
169 }
170 
171 static bool MkDirRecursiveInner(const std::string &Leaf) {
172   // Prevent chance of potential infinite recursion
173   if (Leaf == ".")
174     return true;
175 
176   const std::string &Dir = DirName(Leaf);
177 
178   if (IsDirectory(Dir)) {
179     MkDir(Leaf);
180     return IsDirectory(Leaf);
181   }
182 
183   bool ret = MkDirRecursiveInner(Dir);
184   if (!ret) {
185     // Give up early if a previous MkDir failed
186     return ret;
187   }
188 
189   MkDir(Leaf);
190   return IsDirectory(Leaf);
191 }
192 
193 bool MkDirRecursive(const std::string &Dir) {
194   if (Dir.empty())
195     return false;
196 
197   if (IsDirectory(Dir))
198     return true;
199 
200   return MkDirRecursiveInner(Dir);
201 }
202 
203 void RmDirRecursive(const std::string &Dir) {
204   IterateDirRecursive(
205       Dir, [](const std::string &Path) {},
206       [](const std::string &Path) { RmDir(Path); },
207       [](const std::string &Path) { RemoveFile(Path); });
208 }
209 
210 std::string TempPath(const char *Prefix, const char *Extension) {
211   return DirPlusFile(TmpDir(), std::string("libFuzzerTemp.") + Prefix +
212                                    std::to_string(GetPid()) + Extension);
213 }
214 
215 }  // namespace fuzzer
216