From 2b341f70ca7bd4ac84c54cfc3009f02fa2d24496 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Fri, 18 Mar 2016 01:36:00 +0000 Subject: [PATCH] [libFuzzer] read corpus dirs recursively git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263773 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Fuzzer/FuzzerIO.cpp | 30 ++++++++++++++++-------------- lib/Fuzzer/test/fuzzer.test | 9 +++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/Fuzzer/FuzzerIO.cpp b/lib/Fuzzer/FuzzerIO.cpp index be7759fcc91..1e14cfcb2e6 100644 --- a/lib/Fuzzer/FuzzerIO.cpp +++ b/lib/Fuzzer/FuzzerIO.cpp @@ -34,25 +34,27 @@ static long GetEpoch(const std::string &Path) { return St.st_mtime; } -static std::vector ListFilesInDir(const std::string &Dir, - long *Epoch) { - std::vector 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 *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 *V, long *Epoch, size_t MaxSize) { long E = Epoch ? *Epoch : 0; - auto Files = ListFilesInDir(Path, Epoch); + std::vector 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)); } } diff --git a/lib/Fuzzer/test/fuzzer.test b/lib/Fuzzer/test/fuzzer.test index 95d0ecaf9ab..3ab767353ea 100644 --- a/lib/Fuzzer/test/fuzzer.test +++ b/lib/Fuzzer/test/fuzzer.test @@ -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: -- 2.11.0