[PATCH] Introduce a common mutex to prevent mixing reports from different sanitizers.

Alexey Samsonov samsonov at google.com
Wed Apr 3 08:12:32 PDT 2013


Hi rsmith, dvyukov, kcc,

This patch addresses http://llvm.org/bugs/show_bug.cgi?id=15516

http://llvm-reviews.chandlerc.com/D618

Files:
  lib/msan/msan_report.cc
  lib/ubsan/ubsan_diag.cc
  lib/asan/asan_report.cc
  lib/tsan/rtl/tsan_rtl_report.cc
  lib/tsan/rtl/tsan_rtl.cc
  lib/tsan/rtl/tsan_rtl.h
  lib/sanitizer_common/sanitizer_common.h
  lib/sanitizer_common/sanitizer_printf.cc

Index: lib/msan/msan_report.cc
===================================================================
--- lib/msan/msan_report.cc
+++ lib/msan/msan_report.cc
@@ -21,8 +21,6 @@
 
 using namespace __sanitizer;
 
-static StaticSpinMutex report_mu;
-
 namespace __msan {
 
 static bool PrintsToTtyCached() {
@@ -89,7 +87,7 @@
 void ReportUMR(StackTrace *stack, u32 origin) {
   if (!__msan::flags()->report_umrs) return;
 
-  GenericScopedLock<StaticSpinMutex> lock(&report_mu);
+  SpinMutexLock l(&CommonSanitizerReportMutex);
 
   Decorator d;
   Printf("%s", d.Warning());
@@ -103,13 +101,15 @@
 }
 
 void ReportExpectedUMRNotFound(StackTrace *stack) {
-  GenericScopedLock<StaticSpinMutex> lock(&report_mu);
+  SpinMutexLock l(&CommonSanitizerReportMutex);
 
   Printf(" WARNING: Expected use of uninitialized value not found\n");
   PrintStack(stack->trace, stack->size);
 }
 
 void ReportAtExitStatistics() {
+  SpinMutexLock l(&CommonSanitizerReportMutex);
+
   Decorator d;
   Printf("%s", d.Warning());
   Printf("MemorySanitizer: %d warnings reported.\n", msan_report_count);
Index: lib/ubsan/ubsan_diag.cc
===================================================================
--- lib/ubsan/ubsan_diag.cc
+++ lib/ubsan/ubsan_diag.cc
@@ -31,7 +31,7 @@
   if (!SymbolizeCode(Loc, &Info, 1) || !Info.module || !*Info.module)
     return Location(Loc);
 
-  if (!Info.function)
+  if (!Info.file)
     return ModuleLocation(Info.module, Info.module_offset);
 
   return SourceLocation(Info.file, Info.line, Info.column);
@@ -237,6 +237,7 @@
 
 Diag::~Diag() {
   __sanitizer::AnsiColorDecorator Decor(PrintsToTty());
+  SpinMutexLock l(&CommonSanitizerReportMutex);
   Printf(Decor.Bold());
 
   renderLocation(Loc);
Index: lib/asan/asan_report.cc
===================================================================
--- lib/asan/asan_report.cc
+++ lib/asan/asan_report.cc
@@ -458,10 +458,12 @@
       internal__exit(flags()->exitcode);
     }
     ASAN_ON_ERROR();
-    // Make sure the registry is locked while we're printing an error report.
-    // We can lock the registry only here to avoid self-deadlock in case of
+    // Make sure the registry and sanitizer report mutexes are locked while
+    // we're printing an error report.
+    // We can lock them only here to avoid self-deadlock in case of
     // recursive reports.
     asanThreadRegistry().Lock();
+    CommonSanitizerReportMutex.Lock();
     reporting_thread_tid = GetCurrentTidOrInvalid();
     Printf("===================================================="
            "=============\n");
Index: lib/tsan/rtl/tsan_rtl_report.cc
===================================================================
--- lib/tsan/rtl/tsan_rtl_report.cc
+++ lib/tsan/rtl/tsan_rtl_report.cc
@@ -129,11 +129,11 @@
   void *mem = internal_alloc(MBlockReport, sizeof(ReportDesc));
   rep_ = new(mem) ReportDesc;
   rep_->typ = typ;
-  ctx_->report_mtx.Lock();
+  CommonSanitizerReportMutex.Lock();
 }
 
 ScopedReport::~ScopedReport() {
-  ctx_->report_mtx.Unlock();
+  CommonSanitizerReportMutex.Unlock();
   DestroyAndFree(rep_);
 }
 
Index: lib/tsan/rtl/tsan_rtl.cc
===================================================================
--- lib/tsan/rtl/tsan_rtl.cc
+++ lib/tsan/rtl/tsan_rtl.cc
@@ -67,7 +67,6 @@
 
 Context::Context()
   : initialized()
-  , report_mtx(MutexTypeReport, StatMtxReport)
   , nreported()
   , nmissed_expected()
   , thread_registry(new(thread_registry_placeholder) ThreadRegistry(
@@ -151,7 +150,7 @@
       u64 last = atomic_load(&ctx->last_symbolize_time_ns,
                              memory_order_relaxed);
       if (last != 0 && last + flags()->flush_symbolizer_ms * kMs2Ns < now) {
-        Lock l(&ctx->report_mtx);
+        SpinMutexLock l(&CommonSanitizerReportMutex);
         SymbolizeFlush();
         atomic_store(&ctx->last_symbolize_time_ns, 0, memory_order_relaxed);
       }
@@ -252,8 +251,8 @@
     SleepForMillis(flags()->atexit_sleep_ms);
 
   // Wait for pending reports.
-  ctx->report_mtx.Lock();
-  ctx->report_mtx.Unlock();
+  CommonSanitizerReportMutex.Lock();
+  CommonSanitizerReportMutex.Unlock();
 
 #ifndef TSAN_GO
   if (ctx->flags.verbosity)
Index: lib/tsan/rtl/tsan_rtl.h
===================================================================
--- lib/tsan/rtl/tsan_rtl.h
+++ lib/tsan/rtl/tsan_rtl.h
@@ -522,7 +522,6 @@
 
   SyncTab synctab;
 
-  Mutex report_mtx;
   int nreported;
   int nmissed_expected;
   atomic_uint64_t last_symbolize_time_ns;
Index: lib/sanitizer_common/sanitizer_common.h
===================================================================
--- lib/sanitizer_common/sanitizer_common.h
+++ lib/sanitizer_common/sanitizer_common.h
@@ -18,6 +18,7 @@
 
 #include "sanitizer_internal_defs.h"
 #include "sanitizer_libc.h"
+#include "sanitizer_mutex.h"
 
 namespace __sanitizer {
 struct StackTrace;
@@ -109,6 +110,8 @@
 void Printf(const char *format, ...);
 void Report(const char *format, ...);
 void SetPrintfAndReportCallback(void (*callback)(const char *));
+// Can be used to prevent mixing error reports from different sanitizers.
+extern StaticSpinMutex CommonSanitizerReportMutex;
 
 fd_t OpenFile(const char *filename, bool write);
 // Opens the file 'file_name" and reads up to 'max_len' bytes.
Index: lib/sanitizer_common/sanitizer_printf.cc
===================================================================
--- lib/sanitizer_common/sanitizer_printf.cc
+++ lib/sanitizer_common/sanitizer_printf.cc
@@ -23,6 +23,8 @@
 
 namespace __sanitizer {
 
+StaticSpinMutex CommonSanitizerReportMutex;
+
 static int AppendChar(char **buff, const char *buff_end, char c) {
   if (*buff < buff_end) {
     **buff = c;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D618.1.patch
Type: text/x-patch
Size: 5645 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130403/859d2147/attachment.bin>


More information about the llvm-commits mailing list