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