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
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::string> ListFilesInDir(const std::string &Dir,
|
|
|
|
long *Epoch) {
|
2015-01-30 00:58:29 +08:00
|
|
|
std::vector<std::string> V;
|
2015-05-09 05:30:55 +08:00
|
|
|
if (Epoch) {
|
2015-08-27 05:55:19 +08:00
|
|
|
auto E = GetEpoch(Dir);
|
2016-02-02 10:07:26 +08:00
|
|
|
if (E && *Epoch >= E) return V;
|
2015-05-09 05:30:55 +08:00
|
|
|
*Epoch = E;
|
|
|
|
}
|
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)) {
|
|
|
|
if (E->d_type == DT_REG || E->d_type == DT_LNK)
|
|
|
|
V.push_back(E->d_name);
|
|
|
|
}
|
|
|
|
closedir(D);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
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-02-19 05:49:10 +08:00
|
|
|
if (MaxSize) {
|
|
|
|
Unit Res(MaxSize);
|
|
|
|
T.read(reinterpret_cast<char*>(Res.data()), MaxSize);
|
|
|
|
Res.resize(T.gcount());
|
|
|
|
return Res;
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
return Unit((std::istreambuf_iterator<char>(T)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
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-02-18 03:42:34 +08:00
|
|
|
auto Files = ListFilesInDir(Path, Epoch);
|
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];
|
2015-05-09 05:30:55 +08:00
|
|
|
auto FilePath = DirPlusFile(Path, X);
|
|
|
|
if (Epoch && GetEpoch(FilePath) < 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-02-19 05:49:10 +08:00
|
|
|
V->push_back(FileToVector(FilePath, 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;
|
|
|
|
}
|
|
|
|
|
2015-05-23 09:07:46 +08:00
|
|
|
void Printf(const char *Fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Fmt);
|
|
|
|
vfprintf(stderr, Fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
} // namespace fuzzer
|