From 1cba0a96e7def030a89bd1bf10c355e515b0957b Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Wed, 30 Nov 2016 21:53:32 +0000 Subject: [PATCH] [libFuzzer] extend -print_coverage to print the comma-separated list of covered dirs. Note: the Windows stub for DirName is left unimplemented llvm-svn: 288276 --- llvm/lib/Fuzzer/FuzzerIO.h | 3 +++ llvm/lib/Fuzzer/FuzzerIOPosix.cpp | 11 ++++++++++- llvm/lib/Fuzzer/FuzzerIOWindows.cpp | 4 ++++ llvm/lib/Fuzzer/FuzzerTracePC.cpp | 12 +++++++++++- llvm/lib/Fuzzer/test/coverage.test | 1 + 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Fuzzer/FuzzerIO.h b/llvm/lib/Fuzzer/FuzzerIO.h index b1ac69c..39634f9 100644 --- a/llvm/lib/Fuzzer/FuzzerIO.h +++ b/llvm/lib/Fuzzer/FuzzerIO.h @@ -33,6 +33,9 @@ void ReadDirToVectorOfUnits(const char *Path, std::vector *V, std::string DirPlusFile(const std::string &DirPath, const std::string &FileName); +// Returns the name of the dir, similar to the 'dirname' utility. +std::string DirName(const std::string &FileName); + void DupAndCloseStderr(); void CloseStdout(); diff --git a/llvm/lib/Fuzzer/FuzzerIOPosix.cpp b/llvm/lib/Fuzzer/FuzzerIOPosix.cpp index 740d52d..a4290fa 100644 --- a/llvm/lib/Fuzzer/FuzzerIOPosix.cpp +++ b/llvm/lib/Fuzzer/FuzzerIOPosix.cpp @@ -18,8 +18,9 @@ #include #include #include -#include +#include #include +#include #include namespace fuzzer { @@ -74,5 +75,13 @@ void DeleteFile(const std::string &Path) { unlink(Path.c_str()); } +std::string DirName(const std::string &FileName) { + char *Tmp = new char[FileName.size() + 1]; + memcpy(Tmp, FileName.c_str(), FileName.size() + 1); + std::string Res = dirname(Tmp); + delete [] Tmp; + return Res; +} + } // namespace fuzzer #endif // LIBFUZZER_POSIX diff --git a/llvm/lib/Fuzzer/FuzzerIOWindows.cpp b/llvm/lib/Fuzzer/FuzzerIOWindows.cpp index f19353c..aaa2e34 100644 --- a/llvm/lib/Fuzzer/FuzzerIOWindows.cpp +++ b/llvm/lib/Fuzzer/FuzzerIOWindows.cpp @@ -139,5 +139,9 @@ void DeleteFile(const std::string &Path) { _unlink(Path.c_str()); } +std::string DirName(const std::string &FileName) { + assert(0 && "Unimplemented"); +} + } // namespace fuzzer #endif // LIBFUZZER_WINDOWS diff --git a/llvm/lib/Fuzzer/FuzzerTracePC.cpp b/llvm/lib/Fuzzer/FuzzerTracePC.cpp index eabf0d0..8d58a6d 100644 --- a/llvm/lib/Fuzzer/FuzzerTracePC.cpp +++ b/llvm/lib/Fuzzer/FuzzerTracePC.cpp @@ -131,7 +131,8 @@ void TracePC::PrintCoverage() { } std::map> CoveredPCsPerModule; std::map ModuleOffsets; - std::set CoveredFiles, CoveredFunctions, CoveredLines; + std::set CoveredDirs, CoveredFiles, CoveredFunctions, + CoveredLines; Printf("COVERAGE:\n"); for (size_t i = 1; i < GetNumPCs(); i++) { if (!PCs[i]) continue; @@ -150,12 +151,21 @@ void TracePC::PrintCoverage() { CoveredPCsPerModule[Module].push_back(PcOffset); CoveredFunctions.insert(FunctionStr); CoveredFiles.insert(FileStr); + CoveredDirs.insert(DirName(FileStr)); if (!CoveredLines.insert(FileStr + ":" + LineStr).second) continue; Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(), FileStr.c_str(), LineStr.c_str()); } + std::string CoveredDirsStr; + for (auto &Dir : CoveredDirs) { + if (!CoveredDirsStr.empty()) + CoveredDirsStr += ","; + CoveredDirsStr += Dir; + } + Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str()); + for (auto &M : CoveredPCsPerModule) { std::set UncoveredFiles, UncoveredFunctions; std::map > UncoveredLines; // Func+File => lines diff --git a/llvm/lib/Fuzzer/test/coverage.test b/llvm/lib/Fuzzer/test/coverage.test index b41a262..a5420bb 100644 --- a/llvm/lib/Fuzzer/test/coverage.test +++ b/llvm/lib/Fuzzer/test/coverage.test @@ -3,6 +3,7 @@ CHECK-DAG: COVERED: {{.*}}in LLVMFuzzerTestOneInput {{.*}}NullDerefTest.cpp:13 CHECK-DAG: COVERED: {{.*}}in LLVMFuzzerTestOneInput {{.*}}NullDerefTest.cpp:14 CHECK-DAG: COVERED: {{.*}}in LLVMFuzzerTestOneInput {{.*}}NullDerefTest.cpp:16 CHECK-DAG: COVERED: {{.*}}in LLVMFuzzerTestOneInput {{.*}}NullDerefTest.cpp:19 +CHECK: COVERED_DIRS: {{.*}}lib/Fuzzer/test RUN: not LLVMFuzzer-NullDerefTest-TracePC -print_coverage=1 2>&1 | FileCheck %s RUN: LLVMFuzzer-DSOTest -print_coverage=1 -runs=0 2>&1 | FileCheck %s --check-prefix=DSO -- 2.7.4