2015-01-29 16:58:29 +00:00
|
|
|
//===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IO functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-12-13 17:46:11 +00:00
|
|
|
|
2016-11-30 19:06:14 +00:00
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 02:05:39 +00:00
|
|
|
#include "FuzzerDefs.h"
|
2016-11-30 19:06:14 +00:00
|
|
|
#include "FuzzerExtFunctions.h"
|
2016-11-30 21:44:26 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdarg>
|
2015-01-29 16:58:29 +00:00
|
|
|
#include <fstream>
|
2016-11-30 21:44:26 +00:00
|
|
|
#include <iterator>
|
2015-05-08 21:30:55 +00:00
|
|
|
#include <sys/stat.h>
|
2016-12-13 17:46:11 +00:00
|
|
|
#include <sys/types.h>
|
2015-05-08 21:30:55 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-03-24 00:57:32 +00:00
|
|
|
static FILE *OutputFile = stderr;
|
2016-03-18 20:58:29 +00:00
|
|
|
|
2016-09-21 01:04:43 +00:00
|
|
|
long GetEpoch(const std::string &Path) {
|
2015-05-08 21:30:55 +00:00
|
|
|
struct stat St;
|
2015-09-08 20:36:33 +00:00
|
|
|
if (stat(Path.c_str(), &St))
|
|
|
|
return 0; // Can't stat, be conservative.
|
2015-05-08 21:30:55 +00:00
|
|
|
return St.st_mtime;
|
|
|
|
}
|
|
|
|
|
2016-10-08 23:24:45 +00:00
|
|
|
Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
|
2015-01-29 16:58:29 +00:00
|
|
|
std::ifstream T(Path);
|
2016-10-08 23:24:45 +00:00
|
|
|
if (ExitOnError && !T) {
|
2015-10-16 22:41:47 +00:00
|
|
|
Printf("No such directory: %s; exiting\n", Path.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-03-15 21:47:21 +00:00
|
|
|
|
|
|
|
T.seekg(0, T.end);
|
|
|
|
size_t FileLen = T.tellg();
|
|
|
|
if (MaxSize)
|
|
|
|
FileLen = std::min(FileLen, MaxSize);
|
|
|
|
|
|
|
|
T.seekg(0, T.beg);
|
|
|
|
Unit Res(FileLen);
|
|
|
|
T.read(reinterpret_cast<char *>(Res.data()), FileLen);
|
|
|
|
return Res;
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 20:13:20 +00:00
|
|
|
std::string FileToString(const std::string &Path) {
|
|
|
|
std::ifstream T(Path);
|
|
|
|
return std::string((std::istreambuf_iterator<char>(T)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
2015-02-04 19:10:20 +00:00
|
|
|
void CopyFileToErr(const std::string &Path) {
|
2015-05-23 01:07:46 +00:00
|
|
|
Printf("%s", FileToString(Path).c_str());
|
2015-02-04 19:10:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
void WriteToFile(const Unit &U, const std::string &Path) {
|
2015-08-12 00:55:09 +00:00
|
|
|
// Use raw C interface because this function may be called from a sig handler.
|
|
|
|
FILE *Out = fopen(Path.c_str(), "w");
|
|
|
|
if (!Out) return;
|
|
|
|
fwrite(U.data(), sizeof(U[0]), U.size(), Out);
|
|
|
|
fclose(Out);
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 21:30:55 +00:00
|
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
|
2016-10-08 23:24:45 +00:00
|
|
|
long *Epoch, size_t MaxSize, bool ExitOnError) {
|
2015-05-08 21:30:55 +00:00
|
|
|
long E = Epoch ? *Epoch : 0;
|
2016-03-18 01:36:00 +00:00
|
|
|
std::vector<std::string> Files;
|
|
|
|
ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
|
2016-03-04 22:35:40 +00:00
|
|
|
size_t NumLoaded = 0;
|
2016-02-17 19:42:34 +00:00
|
|
|
for (size_t i = 0; i < Files.size(); i++) {
|
|
|
|
auto &X = Files[i];
|
2016-03-18 01:36:00 +00:00
|
|
|
if (Epoch && GetEpoch(X) < E) continue;
|
2016-03-04 22:35:40 +00:00
|
|
|
NumLoaded++;
|
|
|
|
if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
|
|
|
|
Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
|
2016-10-08 23:24:45 +00:00
|
|
|
auto S = FileToVector(X, MaxSize, ExitOnError);
|
|
|
|
if (!S.empty())
|
|
|
|
V->push_back(S);
|
2015-05-08 21:30:55 +00:00
|
|
|
}
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DirPlusFile(const std::string &DirPath,
|
|
|
|
const std::string &FileName) {
|
2016-11-30 21:44:26 +00:00
|
|
|
return DirPath + GetSeparator() + FileName;
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 20:58:29 +00:00
|
|
|
void DupAndCloseStderr() {
|
2016-11-30 21:44:26 +00:00
|
|
|
int OutputFd = DuplicateFile(2);
|
2016-03-24 00:57:32 +00:00
|
|
|
if (OutputFd > 0) {
|
2016-11-30 21:44:26 +00:00
|
|
|
FILE *NewOutputFile = OpenFile(OutputFd, "w");
|
2016-03-24 00:57:32 +00:00
|
|
|
if (NewOutputFile) {
|
|
|
|
OutputFile = NewOutputFile;
|
2016-06-07 23:32:50 +00:00
|
|
|
if (EF->__sanitizer_set_report_fd)
|
2017-02-08 00:03:18 +00:00
|
|
|
EF->__sanitizer_set_report_fd(
|
|
|
|
reinterpret_cast<void *>(GetHandleFromFd(OutputFd)));
|
[libFuzzer] Avoid undefined behavior, properly discard output to stdout/stderr.
Fix libFuzzer when setting -close_fd_mask to a non-zero value.
In previous implementation, libFuzzer closes the file descriptors for
stdout/stderr. This has some disavantages:
For `fuzzer-fdmask.test`, we write directly to stdout and stderr using the
file streams stdout and stderr, after the file descriptors are closed, which is
undefined behavior. In Windows, in particular, this was making the test fail.
Also, if we close stdout and we open a new file in libFuzzer, we get the file
descriptor 1, which could generate problem if some code assumes file descriptors
refers to stdout and works directly writing to the file descriptor 1, but it
will be writing to the opened file (for example using std::cout).
Instead of closing the file descriptors, I redirect the output to /dev/null on
linux and nul on Windows.
Differential Revision: https://reviews.llvm.org/D28718
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292743 91177308-0d34-0410-b5e6-96231b3b80d8
2017-01-22 01:58:45 +00:00
|
|
|
DiscardOutput(2);
|
2016-03-24 00:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-18 20:58:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 21:44:26 +00:00
|
|
|
void CloseStdout() {
|
[libFuzzer] Avoid undefined behavior, properly discard output to stdout/stderr.
Fix libFuzzer when setting -close_fd_mask to a non-zero value.
In previous implementation, libFuzzer closes the file descriptors for
stdout/stderr. This has some disavantages:
For `fuzzer-fdmask.test`, we write directly to stdout and stderr using the
file streams stdout and stderr, after the file descriptors are closed, which is
undefined behavior. In Windows, in particular, this was making the test fail.
Also, if we close stdout and we open a new file in libFuzzer, we get the file
descriptor 1, which could generate problem if some code assumes file descriptors
refers to stdout and works directly writing to the file descriptor 1, but it
will be writing to the opened file (for example using std::cout).
Instead of closing the file descriptors, I redirect the output to /dev/null on
linux and nul on Windows.
Differential Revision: https://reviews.llvm.org/D28718
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292743 91177308-0d34-0410-b5e6-96231b3b80d8
2017-01-22 01:58:45 +00:00
|
|
|
DiscardOutput(1);
|
2016-11-30 21:44:26 +00:00
|
|
|
}
|
2016-03-18 20:58:29 +00:00
|
|
|
|
2015-05-23 01:07:46 +00:00
|
|
|
void Printf(const char *Fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Fmt);
|
2016-03-24 00:57:32 +00:00
|
|
|
vfprintf(OutputFile, Fmt, ap);
|
2015-05-23 01:07:46 +00:00
|
|
|
va_end(ap);
|
2016-03-25 20:31:26 +00:00
|
|
|
fflush(OutputFile);
|
2015-05-23 01:07:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
} // namespace fuzzer
|