[compiler-rt] r236637 - [sanitizer] Extend sancov.py to show which PCs are missing from coverage.

Sergey Matveev earthdok at google.com
Wed May 6 13:48:29 PDT 2015


Author: smatveev
Date: Wed May  6 15:48:29 2015
New Revision: 236637

URL: http://llvm.org/viewvc/llvm-project?rev=236637&view=rev
Log:
[sanitizer] Extend sancov.py to show which PCs are missing from coverage.

Example usage:

sancov.py print a.out.1234.sancov | sancov.py missing a.out

Added:
    compiler-rt/trunk/test/asan/TestCases/Linux/coverage-missing.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/scripts/sancov.py

Modified: compiler-rt/trunk/lib/sanitizer_common/scripts/sancov.py
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/scripts/sancov.py?rev=236637&r1=236636&r2=236637&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/scripts/sancov.py (original)
+++ compiler-rt/trunk/lib/sanitizer_common/scripts/sancov.py Wed May  6 15:48:29 2015
@@ -8,16 +8,18 @@ import bisect
 import glob
 import os.path
 import struct
+import subprocess
 import sys
 
 prog_name = ""
 
 def Usage():
   print >> sys.stderr, "Usage: \n" + \
-      " " + prog_name + " [32|64] merge file1 [file2 ...]  > output\n" \
-      " " + prog_name + " [32|64] print file1 [file2 ...]\n" \
-      " " + prog_name + " [32|64] unpack file1 [file2 ...]\n" \
-      " " + prog_name + " [32|64] rawunpack file1 [file2 ...]\n"
+      " " + prog_name + " merge FILE [FILE...] > OUTPUT\n" \
+      " " + prog_name + " print FILE [FILE...]\n" \
+      " " + prog_name + " unpack FILE [FILE...]\n" \
+      " " + prog_name + " rawunpack FILE [FILE ...]\n" \
+      " " + prog_name + " missing BINARY < LIST_OF_PCS\n"
   exit(1)
 
 def CheckBits(bits):
@@ -177,11 +179,46 @@ def RawUnpack(files):
     f_map = f[:-3] + 'map'
     UnpackOneRawFile(f, f_map)
 
+def GetInstrumentedPCs(binary):
+  cmd = "objdump -d %s | " \
+        "grep '^\s\+[0-9a-f]\+:.*\scall\(q\|\)\s\+[0-9a-f]\+ <__sanitizer_cov\(@plt\|\)>' | " \
+        "grep '^\s\+[0-9a-f]\+' -o" % binary
+  proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+                          shell=True)
+  proc.stdin.close()
+  # The PCs we get from objdump are off by 4 bytes, as they point to the
+  # beginning of the callq instruction. Empirically this is true on x86 and
+  # x86_64.
+  return set(int(line.strip(), 16) + 4 for line in proc.stdout)
+
+def PrintMissing(binary):
+  if not os.path.isfile(binary):
+    raise Exception('File not found: %s' % binary)
+  instrumented = GetInstrumentedPCs(binary)
+  print >> sys.stderr, "%s: found %d instrumented PCs in %s" % (prog_name,
+                                                                len(instrumented),
+                                                                binary)
+  covered = set(int(line, 16) for line in sys.stdin)
+  print >> sys.stderr, "%s: read %d PCs from stdin" % (prog_name, len(covered))
+  missing = instrumented - covered
+  print >> sys.stderr, "%s: %d PCs missing from coverage" % (prog_name, len(missing))
+  if (len(missing) > len(instrumented) - len(covered)):
+    print >> sys.stderr, \
+        "%s: WARNING: stdin contains PCs not found in binary" % prog_name
+  for pc in sorted(missing):
+    print "0x%x" % pc
+
 if __name__ == '__main__':
   prog_name = sys.argv[0]
   if len(sys.argv) <= 2:
     Usage();
 
+  if sys.argv[1] == "missing":
+    if len(sys.argv) != 3:
+      Usage()
+    PrintMissing(sys.argv[2])
+    exit(0)
+
   file_list = []
   for f in sys.argv[2:]:
     file_list += glob.glob(f)

Added: compiler-rt/trunk/test/asan/TestCases/Linux/coverage-missing.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Linux/coverage-missing.cc?rev=236637&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Linux/coverage-missing.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Linux/coverage-missing.cc Wed May  6 15:48:29 2015
@@ -0,0 +1,82 @@
+// Test for "sancov.py missing ...".
+
+// RUN: ASAN_OPTIONS=coverage=1:coverage_dir=%T/coverage-missing
+
+// First case: coverage from executable. main() is called on every code path;
+// other than that, the foo and bar code paths are complementary in terms of
+// PCs covered.
+// RUN: %clangxx_asan -fsanitize-coverage=1 %s -o %t -DFOOBAR -DMAIN
+// RUN: rm -rf %T/coverage-missing
+// RUN: mkdir -p %T/coverage-missing
+// RUN: cd %T/coverage-missing
+// RUN: ASAN_OPTIONS=$ASAN_OPTIONS %t
+// RUN: %sancov print *.sancov > main.txt
+// RUN: rm *.sancov
+// RUN: [ $(cat main.txt | wc -l) == 1 ]
+// RUN: ASAN_OPTIONS=$ASAN_OPTIONS %t x
+// RUN: %sancov print *.sancov > foo.txt
+// RUN: rm *.sancov
+// RUN: [ $(cat foo.txt | wc -l) == 3 ]
+// RUN: ASAN_OPTIONS=$ASAN_OPTIONS %t x x
+// RUN: %sancov print *.sancov > bar.txt
+// RUN: rm *.sancov
+// RUN: [ $(cat bar.txt | wc -l) == 4 ]
+// RUN: %sancov missing %t < foo.txt > foo-missing.txt
+// RUN: sort main.txt foo-missing.txt -o foo-missing-with-main.txt
+// RUN: diff bar.txt foo-missing-with-main.txt
+
+// Second case: coverage from DSO. Strictly complementary code paths.
+// cd %T
+// RUN: %clangxx_asan -fsanitize-coverage=1 %s -o %dynamiclib -DFOOBAR -shared -fPIC
+// RUN: %clangxx_asan -fsanitize-coverage=1 %s %dynamiclib -o %t -DMAIN
+// RUN: LIBNAME=`basename %dynamiclib`
+// RUN: rm -rf %T/coverage-missing
+// RUN: mkdir -p %T/coverage-missing
+// RUN: cd %T/coverage-missing
+// RUN: ASAN_OPTIONS=$ASAN_OPTIONS %t x
+// RUN: %sancov print $LIBNAME.*.sancov > foo.txt
+// RUN: rm *.sancov
+// RUN: [ $(cat foo.txt | wc -l) == 2 ]
+// RUN: ASAN_OPTIONS=$ASAN_OPTIONS %t x x
+// RUN: %sancov print $LIBNAME.*.sancov > bar.txt
+// RUN: rm *.sancov
+// RUN: [ $(cat bar.txt | wc -l) == 3 ]
+// RUN: %sancov missing %dynamiclib < foo.txt > foo-missing.txt
+// RUN: diff bar.txt foo-missing.txt
+
+// XFAIL: android
+
+#include <stdio.h>
+
+void foo1();
+void foo2();
+void bar1();
+void bar2();
+void bar3();
+
+#if defined(FOOBAR)
+void foo1() { fprintf(stderr, "foo1\n"); }
+void foo2() { fprintf(stderr, "foo2\n"); }
+
+void bar1() { fprintf(stderr, "bar1\n"); }
+void bar2() { fprintf(stderr, "bar2\n"); }
+void bar3() { fprintf(stderr, "bar3\n"); }
+#endif
+
+#if defined(MAIN)
+int main(int argc, char **argv) {
+  switch (argc) {
+    case 1:
+      break;
+    case 2:
+      foo1();
+      foo2();
+      break;
+    case 3:
+      bar1();
+      bar2();
+      bar3();
+      break;
+  }
+}
+#endif





More information about the llvm-commits mailing list