[sancov] blacklist support.
authorMike Aizatsky <aizatsky@chromium.org>
Wed, 16 Dec 2015 00:31:48 +0000 (00:31 +0000)
committerMike Aizatsky <aizatsky@chromium.org>
Wed, 16 Dec 2015 00:31:48 +0000 (00:31 +0000)
Summary:
Using the blacklist the user can filter own unwanted functions
from all outputs. By default blacklist contains "fun:__sancov*" line.

Differential Revision: http://reviews.llvm.org/D15364

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255732 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/sancov/Inputs/blacklist.txt [new file with mode: 0644]
test/tools/sancov/Inputs/test-linux_x86_64
test/tools/sancov/Inputs/test-linux_x86_64-1.sancov
test/tools/sancov/Inputs/test-linux_x86_64.sancov
test/tools/sancov/Inputs/test.cpp
test/tools/sancov/blacklist.test [new file with mode: 0644]
test/tools/sancov/not_covered_functions.test
test/tools/sancov/print.test
tools/sancov/sancov.cc

diff --git a/test/tools/sancov/Inputs/blacklist.txt b/test/tools/sancov/Inputs/blacklist.txt
new file mode 100644 (file)
index 0000000..b782960
--- /dev/null
@@ -0,0 +1 @@
+fun:bar*
index 50060166ac33c8abeba19505d11c904557dee7ae..2d141b693b2e8534288aa0e657df34ef409cabde 100755 (executable)
Binary files a/test/tools/sancov/Inputs/test-linux_x86_64 and b/test/tools/sancov/Inputs/test-linux_x86_64 differ
index 08a0521277ba136a962ec33363d52c09c345815e..a1c7f7b6cc38561aa1ca2c72908bcd4dd70ef101 100644 (file)
Binary files a/test/tools/sancov/Inputs/test-linux_x86_64-1.sancov and b/test/tools/sancov/Inputs/test-linux_x86_64-1.sancov differ
index f8030c33d6c8ced0f520b902797f61398d1f3ea8..e5ed81ed906e07fa1c4019b3ccfdac80155a9ced 100644 (file)
Binary files a/test/tools/sancov/Inputs/test-linux_x86_64.sancov and b/test/tools/sancov/Inputs/test-linux_x86_64.sancov differ
index 795468b4bcba27cdc98bdd0bbcc4d7e7b8484878..5690409a27815e4cab942b4b966c2c13c7afca37 100644 (file)
@@ -1,5 +1,5 @@
 // compile & generate coverage data using:
 // compile & generate coverage data using:
-// clang++ -g -o test-linux_x86_64 -fsanitize=address -fsanitize-coverage=edge *.cpp
+// clang++ -g -o test-linux_x86_64 -fsanitize=address -fsanitize-coverage=bb test.cpp foo.cpp
 // ASAN_OPTIONS="coverage=1" ./test-linux_x86_64 && mv test-linux_x86_64.*.sancov test-linux_x86_64.sancov
 // ASAN_OPTIONS="coverage=1" ./test-linux_x86_64 1 && mv test-linux_x86_64.*.sancov test-linux_x86_64-1.sancov
 
 // ASAN_OPTIONS="coverage=1" ./test-linux_x86_64 && mv test-linux_x86_64.*.sancov test-linux_x86_64.sancov
 // ASAN_OPTIONS="coverage=1" ./test-linux_x86_64 1 && mv test-linux_x86_64.*.sancov test-linux_x86_64-1.sancov
 
diff --git a/test/tools/sancov/blacklist.test b/test/tools/sancov/blacklist.test
new file mode 100644 (file)
index 0000000..c07f5cd
--- /dev/null
@@ -0,0 +1,5 @@
+REQUIRES: x86_64-linux
+RUN: sancov -obj %p/Inputs/test-linux_x86_64 -covered-functions -blacklist %p/Inputs/blacklist.txt %p/Inputs/test-linux_x86_64.sancov | FileCheck %s
+
+CHECK-NOT: Inputs{{[/\\]}}test.cpp:12 bar(std::string)
+CHECK: Inputs{{[/\\]}}test.cpp:14 main
index b82f9e22d5d820ccc913b69645bdf415c4664145..8bcbac7f7b1b103cc12807a3469686d56d86259b 100644 (file)
@@ -3,5 +3,6 @@ RUN: sancov -obj %p/Inputs/test-linux_x86_64 -not-covered-functions %p/Inputs/te
 RUN: sancov -obj %p/Inputs/test-linux_x86_64 -not-covered-functions %p/Inputs/test-linux_x86_64-1.sancov | FileCheck --check-prefix=CHECK1 --allow-empty %s
 
 CHECK: Inputs{{[/\\]}}foo.cpp:5 foo()
 RUN: sancov -obj %p/Inputs/test-linux_x86_64 -not-covered-functions %p/Inputs/test-linux_x86_64-1.sancov | FileCheck --check-prefix=CHECK1 --allow-empty %s
 
 CHECK: Inputs{{[/\\]}}foo.cpp:5 foo()
-CHECK1-NOT: {{.}}*
+CHECK-NOT: {{.*__sanitizer.*}}
+CHECK1-NOT: {{.+}}
 
 
index e6079ec33693a3ec43c9b013e951facfc0f68c47..c67bbaa842a8bd79d8cf3795ce91d6cc8084a35d 100644 (file)
@@ -1,11 +1,11 @@
 REQUIRES: x86_64-linux
 RUN: sancov -obj %p/Inputs/test-linux_x86_64 -print %p/Inputs/test-linux_x86_64.sancov | FileCheck %s
 
 REQUIRES: x86_64-linux
 RUN: sancov -obj %p/Inputs/test-linux_x86_64 -print %p/Inputs/test-linux_x86_64.sancov | FileCheck %s
 
-CHECK: 0x4db18b
-CHECK: 0x4db2d2
-CHECK: 0x4db322
-CHECK: 0x4db376
-CHECK: 0x4db3bd
-CHECK: 0x4db3f4
-CHECK: 0x4db427
-CHECK: 0x4db45a
+CHECK: 0x4dbe2b
+CHECK: 0x4dbf72
+CHECK: 0x4dbfec
+CHECK: 0x4dc033
+CHECK: 0x4dc06a
+CHECK: 0x4dc09d
+CHECK: 0x4dc0d0
+
index 94d8c4c7aabdc0c9d1ee0ec78f0f8b40678f6998..67865daa33655da222bd0de03e9fcf7bb5e016c4 100644 (file)
@@ -34,6 +34,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/SpecialCaseList.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/ToolOutputFile.h"
@@ -80,6 +81,16 @@ static cl::opt<std::string> ClStripPathPrefix(
     "strip_path_prefix", cl::init(""),
     cl::desc("Strip this prefix from file paths in reports."));
 
     "strip_path_prefix", cl::init(""),
     cl::desc("Strip this prefix from file paths in reports."));
 
+static cl::opt<std::string>
+    ClBlacklist("blacklist", cl::init(""),
+                cl::desc("Blacklist file (sanitizer blacklist format)."));
+
+static cl::opt<bool> ClUseDefaultBlacklist(
+    "use_default_blacklist", cl::init(true), cl::Hidden,
+    cl::desc("Controls if default blacklist should be used."));
+
+static const char *const DefaultBlacklist = "fun:__sanitizer_*";
+
 // --------- FORMAT SPECIFICATION ---------
 
 struct FileHeader {
 // --------- FORMAT SPECIFICATION ---------
 
 struct FileHeader {
@@ -169,8 +180,8 @@ computeFunctionsMap(const std::set<uint64_t> &Addrs) {
   for (auto Addr : Addrs) {
     auto InliningInfo = Symbolizer.symbolizeInlinedCode(ClBinaryName, Addr);
     FailIfError(InliningInfo);
   for (auto Addr : Addrs) {
     auto InliningInfo = Symbolizer.symbolizeInlinedCode(ClBinaryName, Addr);
     FailIfError(InliningInfo);
-    for (uint32_t i = 0; i < InliningInfo->getNumberOfFrames(); ++i) {
-      auto FrameInfo = InliningInfo->getFrame(i);
+    for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
+      auto FrameInfo = InliningInfo->getFrame(I);
       SmallString<256> FileName(FrameInfo.FileName);
       sys::path::remove_dots(FileName, /* remove_dot_dot */ true);
       FileLoc Loc = {FileName.str(), FrameInfo.Line};
       SmallString<256> FileName(FrameInfo.FileName);
       sys::path::remove_dots(FileName, /* remove_dot_dot */ true);
       FileLoc Loc = {FileName.str(), FrameInfo.Line};
@@ -186,7 +197,7 @@ computeFunctionsMap(const std::set<uint64_t> &Addrs) {
 std::set<FunctionLoc> computeFunctionLocs(const std::set<uint64_t> &Addrs) {
   std::map<FileLoc, std::string> Fns = computeFunctionsMap(Addrs);
 
 std::set<FunctionLoc> computeFunctionLocs(const std::set<uint64_t> &Addrs) {
   std::map<FileLoc, std::string> Fns = computeFunctionsMap(Addrs);
 
-  std::set<FunctionLoc> result;
+  std::set<FunctionLoc> Result;
   std::string LastFileName;
   std::set<std::string> ProcessedFunctions;
 
   std::string LastFileName;
   std::set<std::string> ProcessedFunctions;
 
@@ -201,10 +212,10 @@ std::set<FunctionLoc> computeFunctionLocs(const std::set<uint64_t> &Addrs) {
     if (!ProcessedFunctions.insert(FunctionName).second)
       continue;
 
     if (!ProcessedFunctions.insert(FunctionName).second)
       continue;
 
-    result.insert(FunctionLoc{P.first, P.second});
+    Result.insert(FunctionLoc{P.first, P.second});
   }
 
   }
 
-  return result;
+  return Result;
 }
 
 // Locate __sanitizer_cov* function addresses that are used for coverage
 }
 
 // Locate __sanitizer_cov* function addresses that are used for coverage
@@ -346,9 +357,41 @@ std::set<uint64_t> getCoveragePoints(std::string FileName) {
   return Result;
 }
 
   return Result;
 }
 
+static std::unique_ptr<SpecialCaseList> createDefaultBlacklist() {
+  if (!ClUseDefaultBlacklist) 
+    return std::unique_ptr<SpecialCaseList>();
+  std::unique_ptr<MemoryBuffer> MB =
+      MemoryBuffer::getMemBuffer(DefaultBlacklist);
+  std::string Error;
+  auto Blacklist = SpecialCaseList::create(MB.get(), Error);
+  FailIfNotEmpty(Error);
+  return Blacklist;
+}
+
+static std::unique_ptr<SpecialCaseList> createUserBlacklist() {
+  if (ClBlacklist.empty())
+    return std::unique_ptr<SpecialCaseList>();
+
+  return SpecialCaseList::createOrDie({{ClBlacklist}});
+}
+
 static void printFunctionLocs(const std::set<FunctionLoc> &FnLocs,
                               raw_ostream &OS) {
 static void printFunctionLocs(const std::set<FunctionLoc> &FnLocs,
                               raw_ostream &OS) {
+  std::unique_ptr<SpecialCaseList> DefaultBlacklist = createDefaultBlacklist();
+  std::unique_ptr<SpecialCaseList> UserBlacklist = createUserBlacklist();
+
   for (const FunctionLoc &FnLoc : FnLocs) {
   for (const FunctionLoc &FnLoc : FnLocs) {
+    if (DefaultBlacklist &&
+        DefaultBlacklist->inSection("fun", FnLoc.FunctionName))
+      continue;
+    if (DefaultBlacklist &&
+        DefaultBlacklist->inSection("src", FnLoc.Loc.FileName))
+      continue;
+    if (UserBlacklist && UserBlacklist->inSection("fun", FnLoc.FunctionName))
+      continue;
+    if (UserBlacklist && UserBlacklist->inSection("src", FnLoc.Loc.FileName))
+      continue;
+
     OS << stripPathPrefix(FnLoc.Loc.FileName) << ":" << FnLoc.Loc.Line << " "
        << FnLoc.FunctionName << "\n";
   }
     OS << stripPathPrefix(FnLoc.Loc.FileName) << ":" << FnLoc.Loc.Line << " "
        << FnLoc.FunctionName << "\n";
   }