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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "FuzzerInternal.h"
|
2015-02-04 19:10:20 +00:00
|
|
|
#include <iterator>
|
2015-01-29 16:58:29 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <dirent.h>
|
2015-05-08 21:30:55 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2015-11-13 01:54:40 +00:00
|
|
|
#include <cstdarg>
|
2015-05-23 01:07:46 +00:00
|
|
|
#include <cstdio>
|
2015-05-08 21:30:55 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-02-02 03:03:47 +00: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-08 21:30:55 +00:00
|
|
|
static long GetEpoch(const std::string &Path) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::string> ListFilesInDir(const std::string &Dir,
|
|
|
|
long *Epoch) {
|
2015-01-29 16:58:29 +00:00
|
|
|
std::vector<std::string> V;
|
2015-05-08 21:30:55 +00:00
|
|
|
if (Epoch) {
|
2015-08-26 21:55:19 +00:00
|
|
|
auto E = GetEpoch(Dir);
|
2016-02-02 02:07:26 +00:00
|
|
|
if (E && *Epoch >= E) return V;
|
2015-05-08 21:30:55 +00:00
|
|
|
*Epoch = E;
|
|
|
|
}
|
2015-01-29 16:58:29 +00:00
|
|
|
DIR *D = opendir(Dir.c_str());
|
2015-07-18 00:03:37 +00:00
|
|
|
if (!D) {
|
|
|
|
Printf("No such directory: %s; exiting\n", Dir.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-29 16:58:29 +00:00
|
|
|
while (auto E = readdir(D)) {
|
|
|
|
if (E->d_type == DT_REG || E->d_type == DT_LNK)
|
|
|
|
V.push_back(E->d_name);
|
|
|
|
}
|
|
|
|
closedir(D);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:49:10 +00:00
|
|
|
Unit FileToVector(const std::string &Path, size_t MaxSize) {
|
2015-01-29 16:58:29 +00:00
|
|
|
std::ifstream T(Path);
|
2015-10-16 22:41:47 +00:00
|
|
|
if (!T) {
|
|
|
|
Printf("No such directory: %s; exiting\n", Path.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-02-18 21:49:10 +00:00
|
|
|
if (MaxSize) {
|
|
|
|
Unit Res(MaxSize);
|
|
|
|
T.read(reinterpret_cast<char*>(Res.data()), MaxSize);
|
|
|
|
Res.resize(T.gcount());
|
|
|
|
return Res;
|
|
|
|
}
|
2015-01-29 16:58:29 +00:00
|
|
|
return Unit((std::istreambuf_iterator<char>(T)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
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-02-18 21:49:10 +00:00
|
|
|
long *Epoch, size_t MaxSize) {
|
2015-05-08 21:30:55 +00:00
|
|
|
long E = Epoch ? *Epoch : 0;
|
2016-02-17 19:42:34 +00:00
|
|
|
auto Files = ListFilesInDir(Path, Epoch);
|
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];
|
2015-05-08 21:30:55 +00:00
|
|
|
auto FilePath = DirPlusFile(Path, X);
|
|
|
|
if (Epoch && GetEpoch(FilePath) < 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-02-18 21:49:10 +00:00
|
|
|
V->push_back(FileToVector(FilePath, MaxSize));
|
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) {
|
|
|
|
return DirPath + "/" + FileName;
|
|
|
|
}
|
|
|
|
|
2015-05-23 01:07:46 +00:00
|
|
|
void Printf(const char *Fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Fmt);
|
|
|
|
vfprintf(stderr, Fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
} // namespace fuzzer
|