[libFuzzer] read corpus dirs recursively

llvm-svn: 263773
This commit is contained in:
Kostya Serebryany 2016-03-18 01:36:00 +00:00
parent 709e3046ee
commit c43b584c1c
2 changed files with 25 additions and 14 deletions

View File

@ -34,25 +34,27 @@ static long GetEpoch(const std::string &Path) {
return St.st_mtime;
}
static std::vector<std::string> ListFilesInDir(const std::string &Dir,
long *Epoch) {
std::vector<std::string> V;
if (Epoch) {
auto E = GetEpoch(Dir);
if (E && *Epoch >= E) return V;
*Epoch = E;
}
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;
DIR *D = opendir(Dir.c_str());
if (!D) {
Printf("No such directory: %s; exiting\n", Dir.c_str());
exit(1);
}
while (auto E = readdir(D)) {
std::string Path = DirPlusFile(Dir, E->d_name);
if (E->d_type == DT_REG || E->d_type == DT_LNK)
V.push_back(E->d_name);
V->push_back(Path);
else if (E->d_type == DT_DIR && *E->d_name != '.')
ListFilesInDirRecursive(Path, Epoch, V, false);
}
closedir(D);
return V;
if (Epoch && TopDir)
*Epoch = E;
}
Unit FileToVector(const std::string &Path, size_t MaxSize) {
@ -94,16 +96,16 @@ void WriteToFile(const Unit &U, const std::string &Path) {
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
long *Epoch, size_t MaxSize) {
long E = Epoch ? *Epoch : 0;
auto Files = ListFilesInDir(Path, Epoch);
std::vector<std::string> Files;
ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
size_t NumLoaded = 0;
for (size_t i = 0; i < Files.size(); i++) {
auto &X = Files[i];
auto FilePath = DirPlusFile(Path, X);
if (Epoch && GetEpoch(FilePath) < E) continue;
if (Epoch && GetEpoch(X) < E) continue;
NumLoaded++;
if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
V->push_back(FileToVector(FilePath, MaxSize));
V->push_back(FileToVector(X, MaxSize));
}
}

View File

@ -60,6 +60,15 @@ SINGLE_INPUTS: LLVMFuzzer-SimpleTest: Running 2 inputs 1 time(s) each.
SINGLE_INPUTS: aaa:
SINGLE_INPUTS: bbb:
RUN: rm -rf %t/SUB1
RUN: mkdir -p %t/SUB1/SUB2/SUB3
RUN: echo a > %t/SUB1/a
RUN: echo b > %t/SUB1/SUB2/b
RUN: echo c > %t/SUB1/SUB2/SUB3/c
RUN: LLVMFuzzer-SimpleTest %t/SUB1 -runs=0 2>&1 | FileCheck %s --check-prefix=SUBDIRS
SUBDIRS: READ units: 3
RUN: rm -rf %t/SUB1
RUN: not LLVMFuzzer-LeakTest -runs=10 2>&1 | FileCheck %s --check-prefix=LEAK
LEAK: ERROR: LeakSanitizer: detected memory leaks
LEAK-NOT: DEATH: