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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "FuzzerInternal.h"
|
2015-02-05 03:10:20 +08:00
|
|
|
#include <iterator>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <dirent.h>
|
2015-05-09 05:30:55 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2015-11-13 09:54:40 +08:00
|
|
|
#include <cstdarg>
|
2015-05-23 09:07:46 +08:00
|
|
|
#include <cstdio>
|
2015-05-09 05:30:55 +08:00
|
|
|
|
2016-05-28 05:46:22 +08:00
|
|
|
extern "C" {
|
|
|
|
__attribute__((weak)) void __sanitizer_set_report_fd(void *);
|
|
|
|
}
|
|
|
|
|
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-02-02 11:03:47 +08:00
|
|
|
bool IsFile(const std::string &Path) {
|
|
|
|
struct stat St;
|
|
|
|
if (stat(Path.c_str(), &St))
|
|
|
|
return false;
|
|
|
|
return S_ISREG(St.st_mode);
|
|
|
|
}
|
|
|
|
|
2015-05-09 05:30:55 +08:00
|
|
|
static long GetEpoch(const std::string &Path) {
|
|
|
|
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-03-18 09:36:00 +08:00
|
|
|
static void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
|
|
|
|
std::vector<std::string> *V, bool TopDir) {
|
|
|
|
auto E = GetEpoch(Dir);
|
|
|
|
if (Epoch)
|
|
|
|
if (E && *Epoch >= E) return;
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
DIR *D = opendir(Dir.c_str());
|
2015-07-18 08:03:37 +08:00
|
|
|
if (!D) {
|
|
|
|
Printf("No such directory: %s; exiting\n", Dir.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
while (auto E = readdir(D)) {
|
2016-03-18 09:36:00 +08:00
|
|
|
std::string Path = DirPlusFile(Dir, E->d_name);
|
2015-01-30 00:58:29 +08:00
|
|
|
if (E->d_type == DT_REG || E->d_type == DT_LNK)
|
2016-03-18 09:36:00 +08:00
|
|
|
V->push_back(Path);
|
|
|
|
else if (E->d_type == DT_DIR && *E->d_name != '.')
|
|
|
|
ListFilesInDirRecursive(Path, Epoch, V, false);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
closedir(D);
|
2016-03-18 09:36:00 +08:00
|
|
|
if (Epoch && TopDir)
|
|
|
|
*Epoch = E;
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 05:49:10 +08:00
|
|
|
Unit FileToVector(const std::string &Path, size_t MaxSize) {
|
2015-01-30 00:58:29 +08:00
|
|
|
std::ifstream T(Path);
|
2015-10-17 06:41:47 +08:00
|
|
|
if (!T) {
|
|
|
|
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-02-19 05:49:10 +08:00
|
|
|
long *Epoch, size_t MaxSize) {
|
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-03-18 09:36:00 +08:00
|
|
|
V->push_back(FileToVector(X, MaxSize));
|
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) {
|
|
|
|
return DirPath + "/" + FileName;
|
|
|
|
}
|
|
|
|
|
2016-03-19 04:58:29 +08:00
|
|
|
void DupAndCloseStderr() {
|
2016-03-24 08:57:32 +08:00
|
|
|
int OutputFd = dup(2);
|
|
|
|
if (OutputFd > 0) {
|
|
|
|
FILE *NewOutputFile = fdopen(OutputFd, "w");
|
|
|
|
if (NewOutputFile) {
|
|
|
|
OutputFile = NewOutputFile;
|
2016-05-28 05:46:22 +08:00
|
|
|
if (__sanitizer_set_report_fd)
|
|
|
|
__sanitizer_set_report_fd(reinterpret_cast<void*>(OutputFd));
|
2016-03-24 08:57:32 +08:00
|
|
|
close(2);
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 04:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseStdout() { close(1); }
|
|
|
|
|
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
|