2015-01-30 00:58:29 +08: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-14 01:46:11 +08:00
|
|
|
|
2016-12-01 03:06:14 +08:00
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 10:05:39 +08:00
|
|
|
#include "FuzzerDefs.h"
|
2016-12-01 03:06:14 +08:00
|
|
|
#include "FuzzerExtFunctions.h"
|
2016-12-01 05:44:26 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdarg>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <fstream>
|
2016-12-01 05:44:26 +08:00
|
|
|
#include <iterator>
|
2015-05-09 05:30:55 +08:00
|
|
|
#include <sys/stat.h>
|
2016-12-14 01:46:11 +08:00
|
|
|
#include <sys/types.h>
|
2015-05-09 05:30:55 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-03-24 08:57:32 +08:00
|
|
|
static FILE *OutputFile = stderr;
|
2016-03-19 04:58:29 +08:00
|
|
|
|
2016-09-21 09:04:43 +08:00
|
|
|
long GetEpoch(const std::string &Path) {
|
2015-05-09 05:30:55 +08:00
|
|
|
struct stat St;
|
2015-09-09 04:36:33 +08:00
|
|
|
if (stat(Path.c_str(), &St))
|
|
|
|
return 0; // Can't stat, be conservative.
|
2015-05-09 05:30:55 +08:00
|
|
|
return St.st_mtime;
|
|
|
|
}
|
|
|
|
|
2016-10-09 07:24:45 +08:00
|
|
|
Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) {
|
2015-01-30 00:58:29 +08:00
|
|
|
std::ifstream T(Path);
|
2016-10-09 07:24:45 +08:00
|
|
|
if (ExitOnError && !T) {
|
2015-10-17 06:41:47 +08:00
|
|
|
Printf("No such directory: %s; exiting\n", Path.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-03-16 05:47:21 +08: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-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-01 04:13:20 +08: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-05 03:10:20 +08:00
|
|
|
void CopyFileToErr(const std::string &Path) {
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("%s", FileToString(Path).c_str());
|
2015-02-05 03:10:20 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void WriteToFile(const Unit &U, const std::string &Path) {
|
2015-08-12 08:55:09 +08: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-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 05:30:55 +08:00
|
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
|
2016-10-09 07:24:45 +08:00
|
|
|
long *Epoch, size_t MaxSize, bool ExitOnError) {
|
2015-05-09 05:30:55 +08:00
|
|
|
long E = Epoch ? *Epoch : 0;
|
2016-03-18 09:36:00 +08:00
|
|
|
std::vector<std::string> Files;
|
|
|
|
ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
|
2016-03-05 06:35:40 +08:00
|
|
|
size_t NumLoaded = 0;
|
2016-02-18 03:42:34 +08:00
|
|
|
for (size_t i = 0; i < Files.size(); i++) {
|
|
|
|
auto &X = Files[i];
|
2016-03-18 09:36:00 +08:00
|
|
|
if (Epoch && GetEpoch(X) < E) continue;
|
2016-03-05 06:35:40 +08:00
|
|
|
NumLoaded++;
|
|
|
|
if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
|
|
|
|
Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
|
2016-10-09 07:24:45 +08:00
|
|
|
auto S = FileToVector(X, MaxSize, ExitOnError);
|
|
|
|
if (!S.empty())
|
|
|
|
V->push_back(S);
|
2015-05-09 05:30:55 +08:00
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DirPlusFile(const std::string &DirPath,
|
|
|
|
const std::string &FileName) {
|
2016-12-01 05:44:26 +08:00
|
|
|
return DirPath + GetSeparator() + FileName;
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-03-19 04:58:29 +08:00
|
|
|
void DupAndCloseStderr() {
|
2016-12-01 05:44:26 +08:00
|
|
|
int OutputFd = DuplicateFile(2);
|
2016-03-24 08:57:32 +08:00
|
|
|
if (OutputFd > 0) {
|
2016-12-01 05:44:26 +08:00
|
|
|
FILE *NewOutputFile = OpenFile(OutputFd, "w");
|
2016-03-24 08:57:32 +08:00
|
|
|
if (NewOutputFile) {
|
|
|
|
OutputFile = NewOutputFile;
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->__sanitizer_set_report_fd)
|
|
|
|
EF->__sanitizer_set_report_fd(reinterpret_cast<void *>(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
llvm-svn: 292743
2017-01-22 09:58:45 +08:00
|
|
|
DiscardOutput(2);
|
2016-03-24 08:57:32 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-19 04:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 05:44:26 +08: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
llvm-svn: 292743
2017-01-22 09:58:45 +08:00
|
|
|
DiscardOutput(1);
|
2016-12-01 05:44:26 +08:00
|
|
|
}
|
2016-03-19 04:58:29 +08:00
|
|
|
|
2015-05-23 09:07:46 +08:00
|
|
|
void Printf(const char *Fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Fmt);
|
2016-03-24 08:57:32 +08:00
|
|
|
vfprintf(OutputFile, Fmt, ap);
|
2015-05-23 09:07:46 +08:00
|
|
|
va_end(ap);
|
2016-03-26 04:31:26 +08:00
|
|
|
fflush(OutputFile);
|
2015-05-23 09:07:46 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
} // namespace fuzzer
|