[compiler-rt] 11c533e - [sanitizer coverage] write the pc-table at the process exit

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 21 09:09:48 PDT 2021


Author: Kostya Serebryany
Date: 2021-09-21T09:09:25-07:00
New Revision: 11c533e1ea38381726ed04ab65937a03fbfb3e19

URL: https://github.com/llvm/llvm-project/commit/11c533e1ea38381726ed04ab65937a03fbfb3e19
DIFF: https://github.com/llvm/llvm-project/commit/11c533e1ea38381726ed04ab65937a03fbfb3e19.diff

LOG: [sanitizer coverage] write the pc-table at the process exit

The current code writes the pc-table at the process startup,
which may happen before the common_flags() are initialized.
Move writing to the process end.
This is consistent with how we write the counters and avoids the problem with the uninitalized flags.
Add prints if verbosity>=1.

Reviewed By: kostik

Differential Revision: https://reviews.llvm.org/D110119

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp
    compiler-rt/test/sanitizer_common/TestCases/sanitizer_coverage_inline8bit_counter_default_impl.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp
index 65f2ef9553ef0..56220df2ac18b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp
@@ -161,30 +161,41 @@ static TracePcGuardController pc_guard_controller;
 // flexibility.
 namespace SingletonCounterCoverage {
 
-static char *counters_start, *counters_end;
+static char *counters_beg, *counters_end;
+static const uptr *pcs_beg, *pcs_end;
 
 static void DumpCoverage() {
   const char* file_path = common_flags()->cov_8bit_counters_out;
-  if (!file_path || !internal_strlen(file_path))
-    return;
-  fd_t fd = OpenFile(file_path);
-  FileCloser file_closer(fd);
-  WriteToFile(fd, counters_start, counters_end - counters_start);
+  if (file_path && internal_strlen(file_path)) {
+    fd_t fd = OpenFile(file_path);
+    FileCloser file_closer(fd);
+    uptr size = counters_end - counters_beg;
+    WriteToFile(fd, counters_beg, size);
+    if (common_flags()->verbosity)
+      __sanitizer::Printf("cov_8bit_counters_out: written %zd bytes to %s\n",
+                          size, file_path);
+  }
+  file_path = common_flags()->cov_pcs_out;
+  if (file_path && internal_strlen(file_path)) {
+    fd_t fd = OpenFile(file_path);
+    FileCloser file_closer(fd);
+    uptr size = (pcs_end - pcs_beg) * sizeof(uptr);
+    WriteToFile(fd, pcs_beg, size);
+    if (common_flags()->verbosity)
+      __sanitizer::Printf("cov_pcs_out: written %zd bytes to %s\n", size,
+                          file_path);
+  }
 }
 
 static void Cov8bitCountersInit(char* beg, char* end) {
-  counters_start = beg;
+  counters_beg = beg;
   counters_end = end;
   Atexit(DumpCoverage);
 }
 
-static void CovPcsInit(const uptr* pcs_beg, const uptr* pcs_end) {
-  const char* file_path = common_flags()->cov_pcs_out;
-  if (!file_path || !internal_strlen(file_path))
-    return;
-  fd_t fd = OpenFile(file_path);
-  FileCloser file_closer(fd);
-  WriteToFile(fd, pcs_beg, (pcs_end - pcs_beg) * sizeof(uptr));
+static void CovPcsInit(const uptr* beg, const uptr* end) {
+  pcs_beg = beg;
+  pcs_end = end;
 }
 
 }  // namespace SingletonCounterCoverage

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/sanitizer_coverage_inline8bit_counter_default_impl.cpp b/compiler-rt/test/sanitizer_common/TestCases/sanitizer_coverage_inline8bit_counter_default_impl.cpp
index 7981d8f487e19..1ac04b53491e1 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/sanitizer_coverage_inline8bit_counter_default_impl.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/sanitizer_coverage_inline8bit_counter_default_impl.cpp
@@ -5,7 +5,7 @@
 
 // RUN: %clangxx -O0 %s -fsanitize-coverage=inline-8bit-counters,pc-table -o %t
 // RUN: rm -f %t-counters %t-pcs
-// RUN: env %tool_options="cov_8bit_counters_out=%t-counters cov_pcs_out=%t-pcs" %run %t 2>&1 | FileCheck %s
+// RUN: env %tool_options="cov_8bit_counters_out=%t-counters cov_pcs_out=%t-pcs verbosity=1" %run %t 2>&1 | FileCheck %s
 
 // Check the file sizes
 // RUN: wc -c %t-counters | grep "^2 "
@@ -19,4 +19,6 @@ int main() {
   foo();
   fprintf(stderr, "PASS\n");
   // CHECK: PASS
+  // CHECK: cov_8bit_counters_out: written {{.*}} bytes to {{.*}}-counter
+  // CHECK: cov_pcs_out: written {{.*}} bytes to {{.*}}-pcs
 }


        


More information about the llvm-commits mailing list