blob: a8140b601678eed8220da6497c11796eefed6cd3 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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
george.karpenkov29efa6d2017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// IO functions.
9//===----------------------------------------------------------------------===//
10
george.karpenkov29efa6d2017-08-21 23:25:50 +000011#include "FuzzerDefs.h"
12#include "FuzzerExtFunctions.h"
kcc2e6ca5c2019-02-12 22:48:55 +000013#include "FuzzerIO.h"
14#include "FuzzerUtil.h"
george.karpenkov29efa6d2017-08-21 23:25:50 +000015#include <algorithm>
16#include <cstdarg>
17#include <fstream>
18#include <iterator>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22namespace fuzzer {
23
24static FILE *OutputFile = stderr;
25
26long 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
33Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
metzman2a530982018-11-06 23:25:25 +000034 std::ifstream T(Path, std::ios::binary);
george.karpenkov29efa6d2017-08-21 23:25:50 +000035 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
53std::string FileToString(const std::string &Path) {
metzman2a530982018-11-06 23:25:25 +000054 std::ifstream T(Path, std::ios::binary);
george.karpenkov29efa6d2017-08-21 23:25:50 +000055 return std::string((std::istreambuf_iterator<char>(T)),
56 std::istreambuf_iterator<char>());
57}
58
59void CopyFileToErr(const std::string &Path) {
60 Printf("%s", FileToString(Path).c_str());
61}
62
63void WriteToFile(const Unit &U, const std::string &Path) {
kcc6f1e9bc2019-04-13 00:20:31 +000064 WriteToFile(U.data(), U.size(), Path);
65}
66
67void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path) {
george.karpenkov29efa6d2017-08-21 23:25:50 +000068 // Use raw C interface because this function may be called from a sig handler.
vitalybuka07f76062019-04-05 20:17:03 +000069 FILE *Out = fopen(Path.c_str(), "wb");
george.karpenkov29efa6d2017-08-21 23:25:50 +000070 if (!Out) return;
kcc6f1e9bc2019-04-13 00:20:31 +000071 fwrite(Data, sizeof(Data[0]), Size, Out);
george.karpenkov29efa6d2017-08-21 23:25:50 +000072 fclose(Out);
73}
74
george.karpenkovfbfa45c2017-08-27 23:20:09 +000075void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
george.karpenkov29efa6d2017-08-21 23:25:50 +000076 long *Epoch, size_t MaxSize, bool ExitOnError) {
77 long E = Epoch ? *Epoch : 0;
george.karpenkovfbfa45c2017-08-27 23:20:09 +000078 Vector<std::string> Files;
george.karpenkov29efa6d2017-08-21 23:25:50 +000079 ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
80 size_t NumLoaded = 0;
81 for (size_t i = 0; i < Files.size(); i++) {
82 auto &X = Files[i];
83 if (Epoch && GetEpoch(X) < E) continue;
84 NumLoaded++;
85 if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
86 Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
87 auto S = FileToVector(X, MaxSize, ExitOnError);
88 if (!S.empty())
89 V->push_back(S);
90 }
91}
92
kcc7f5f2222017-09-12 21:58:07 +000093
94void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V) {
95 Vector<std::string> Files;
96 ListFilesInDirRecursive(Dir, 0, &Files, /*TopDir*/true);
97 for (auto &File : Files)
98 if (size_t Size = FileSize(File))
99 V->push_back({File, Size});
100}
101
george.karpenkov29efa6d2017-08-21 23:25:50 +0000102std::string DirPlusFile(const std::string &DirPath,
103 const std::string &FileName) {
104 return DirPath + GetSeparator() + FileName;
105}
106
george.karpenkov29efa6d2017-08-21 23:25:50 +0000107void DupAndCloseStderr() {
108 int OutputFd = DuplicateFile(2);
109 if (OutputFd > 0) {
110 FILE *NewOutputFile = OpenFile(OutputFd, "w");
111 if (NewOutputFile) {
112 OutputFile = NewOutputFile;
113 if (EF->__sanitizer_set_report_fd)
114 EF->__sanitizer_set_report_fd(
115 reinterpret_cast<void *>(GetHandleFromFd(OutputFd)));
116 DiscardOutput(2);
117 }
118 }
119}
120
121void CloseStdout() {
122 DiscardOutput(1);
123}
124
125void Printf(const char *Fmt, ...) {
126 va_list ap;
127 va_start(ap, Fmt);
128 vfprintf(OutputFile, Fmt, ap);
129 va_end(ap);
130 fflush(OutputFile);
131}
132
kccbfb59752019-02-12 03:12:40 +0000133void VPrintf(bool Verbose, const char *Fmt, ...) {
134 if (!Verbose) return;
135 va_list ap;
136 va_start(ap, Fmt);
137 vfprintf(OutputFile, Fmt, ap);
138 va_end(ap);
139 fflush(OutputFile);
140}
141
kcc64bcb922019-02-13 04:04:45 +0000142void RmDirRecursive(const std::string &Dir) {
kcc55e54ed2019-02-15 21:51:15 +0000143 IterateDirRecursive(
kcc64bcb922019-02-13 04:04:45 +0000144 Dir, [](const std::string &Path) {},
145 [](const std::string &Path) { RmDir(Path); },
146 [](const std::string &Path) { RemoveFile(Path); });
kcc243006d2019-02-12 00:12:33 +0000147}
148
kcc2e6ca5c2019-02-12 22:48:55 +0000149std::string TempPath(const char *Extension) {
150 return DirPlusFile(TmpDir(),
151 "libFuzzerTemp." + std::to_string(GetPid()) + Extension);
152}
153
george.karpenkov29efa6d2017-08-21 23:25:50 +0000154} // namespace fuzzer