[llvm-commits] [llvm] r144567 - in /llvm/trunk: include/llvm/Support/ManagedStatic.h include/llvm/Support/Valgrind.h lib/Support/ManagedStatic.cpp lib/Support/Valgrind.cpp unittests/Support/ManagedStatic.cpp

Nick Lewycky nicholas at mxc.ca
Mon Nov 14 12:50:16 PST 2011


Author: nicholas
Date: Mon Nov 14 14:50:16 2011
New Revision: 144567

URL: http://llvm.org/viewvc/llvm-project?rev=144567&view=rev
Log:
Add support for tsan annotations (thread sanitizer, a valgrind-based tool).
These annotations are disabled entirely when either ENABLE_THREADS is off, or
building a release build. When enabled, they add calls to functions with no
statements to ManagedStatic's getters.

Use these annotations to inform tsan that the race used inside ManagedStatic
initialization is actually benign. Thanks to Kostya Serebryany for helping
write this patch!

Added:
    llvm/trunk/unittests/Support/ManagedStatic.cpp
Modified:
    llvm/trunk/include/llvm/Support/ManagedStatic.h
    llvm/trunk/include/llvm/Support/Valgrind.h
    llvm/trunk/lib/Support/ManagedStatic.cpp
    llvm/trunk/lib/Support/Valgrind.cpp

Modified: llvm/trunk/include/llvm/Support/ManagedStatic.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/ManagedStatic.h?rev=144567&r1=144566&r2=144567&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/ManagedStatic.h (original)
+++ llvm/trunk/include/llvm/Support/ManagedStatic.h Mon Nov 14 14:50:16 2011
@@ -16,6 +16,7 @@
 
 #include "llvm/Support/Atomic.h"
 #include "llvm/Support/Threading.h"
+#include "llvm/Support/Valgrind.h"
 
 namespace llvm {
 
@@ -65,6 +66,7 @@
     void* tmp = Ptr;
     if (llvm_is_multithreaded()) sys::MemoryFence();
     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
+    TsanHappensAfter(this);
 
     return *static_cast<C*>(Ptr);
   }
@@ -72,6 +74,7 @@
     void* tmp = Ptr;
     if (llvm_is_multithreaded()) sys::MemoryFence();
     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
+    TsanHappensAfter(this);
 
     return static_cast<C*>(Ptr);
   }
@@ -79,6 +82,7 @@
     void* tmp = Ptr;
     if (llvm_is_multithreaded()) sys::MemoryFence();
     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
+    TsanHappensAfter(this);
 
     return *static_cast<C*>(Ptr);
   }
@@ -86,6 +90,7 @@
     void* tmp = Ptr;
     if (llvm_is_multithreaded()) sys::MemoryFence();
     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
+    TsanHappensAfter(this);
 
     return static_cast<C*>(Ptr);
   }

Modified: llvm/trunk/include/llvm/Support/Valgrind.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Valgrind.h?rev=144567&r1=144566&r2=144567&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Valgrind.h (original)
+++ llvm/trunk/include/llvm/Support/Valgrind.h Mon Nov 14 14:50:16 2011
@@ -16,8 +16,25 @@
 #ifndef LLVM_SYSTEM_VALGRIND_H
 #define LLVM_SYSTEM_VALGRIND_H
 
+#include "llvm/Support/Compiler.h"
+#include "llvm/Config/config.h"
 #include <stddef.h>
 
+#if ENABLE_THREADS != 0 && !defined(NDEBUG)
+// tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact
+// functions by name.
+extern "C" {
+LLVM_ATTRIBUTE_NOINLINE void AnnotateHappensAfter(const char *file, int line,
+                                                  const volatile void *cv);
+LLVM_ATTRIBUTE_NOINLINE void AnnotateHappensBefore(const char *file, int line,
+                                                   const volatile void *cv);
+LLVM_ATTRIBUTE_NOINLINE void AnnotateIgnoreWritesBegin(const char *file,
+                                                       int line);
+LLVM_ATTRIBUTE_NOINLINE void AnnotateIgnoreWritesEnd(const char *file,
+                                                     int line);
+}
+#endif
+
 namespace llvm {
 namespace sys {
   // True if Valgrind is controlling this process.
@@ -26,6 +43,34 @@
   // Discard valgrind's translation of code in the range [Addr .. Addr + Len).
   // Otherwise valgrind may continue to execute the old version of the code.
   void ValgrindDiscardTranslations(const void *Addr, size_t Len);
+
+#if ENABLE_THREADS != 0 && !defined(NDEBUG)
+  // Thread Sanitizer is a valgrind tool that finds races in code.
+  // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
+
+  // This marker is used to define a happens-before arc. The race detector will
+  // infer an arc from the begin to the end when they share the same pointer
+  // argument.
+  #define TsanHappensBefore(cv) \
+    AnnotateHappensBefore(__FILE__, __LINE__, cv)
+
+  // This marker defines the destination of a happens-before arc.
+  #define TsanHappensAfter(cv) \
+    AnnotateHappensAfter(__FILE__, __LINE__, cv)
+
+  // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
+  #define TsanIgnoreWritesBegin() \
+    AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
+
+  // Resume checking for racy writes.
+  #define TsanIgnoreWritesEnd() \
+    AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
+#else
+  #define TsanHappensBefore(cv)
+  #define TsanHappensAfter(cv)
+  #define TsanIgnoreWritesBegin()
+  #define TsanIgnoreWritesEnd()
+#endif
 }
 }
 

Modified: llvm/trunk/lib/Support/ManagedStatic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ManagedStatic.cpp?rev=144567&r1=144566&r2=144567&view=diff
==============================================================================
--- llvm/trunk/lib/Support/ManagedStatic.cpp (original)
+++ llvm/trunk/lib/Support/ManagedStatic.cpp Mon Nov 14 14:50:16 2011
@@ -27,8 +27,15 @@
     if (Ptr == 0) {
       void* tmp = Creator ? Creator() : 0;
 
+      TsanHappensBefore(this);
       sys::MemoryFence();
+
+      // This write is racy against the first read in the ManagedStatic
+      // accessors. The race is benign because it does a second read after a
+      // memory fence, at which point it isn't possible to get a partial value.
+      TsanIgnoreWritesBegin();
       Ptr = tmp;
+      TsanIgnoreWritesEnd();
       DeleterFn = Deleter;
       
       // Add to list of managed statics.
@@ -72,4 +79,3 @@
 
   if (llvm_is_multithreaded()) llvm_stop_multithreaded();
 }
-

Modified: llvm/trunk/lib/Support/Valgrind.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Valgrind.cpp?rev=144567&r1=144566&r2=144567&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Valgrind.cpp (original)
+++ llvm/trunk/lib/Support/Valgrind.cpp Mon Nov 14 14:50:16 2011
@@ -52,3 +52,14 @@
 }
 
 #endif  // !HAVE_VALGRIND_VALGRIND_H
+
+// These functions require no implementation, tsan just looks at the arguments
+// they're called with.
+extern "C" {
+void AnnotateHappensBefore(const char *file, int line,
+                           const volatile void *cv) {}
+void AnnotateHappensAfter(const char *file, int line,
+                          const volatile void *cv) {}
+void AnnotateIgnoreWritesBegin(const char *file, int line) {}
+void AnnotateIgnoreWritesEnd(const char *file, int line) {}
+}

Added: llvm/trunk/unittests/Support/ManagedStatic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ManagedStatic.cpp?rev=144567&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/ManagedStatic.cpp (added)
+++ llvm/trunk/unittests/Support/ManagedStatic.cpp Mon Nov 14 14:50:16 2011
@@ -0,0 +1,39 @@
+//===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/Threading.h"
+#include <pthread.h>
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+namespace test1 {
+  llvm::ManagedStatic<int> ms;
+  void *helper(void*) {
+    *ms;
+    return NULL;
+  }
+}
+
+TEST(Initialize, MultipleThreads) {
+  // Run this test under tsan: http://code.google.com/p/data-race-test/
+
+  llvm_start_multithreaded();
+  pthread_t t1, t2;
+  pthread_create(&t1, NULL, test1::helper, NULL);
+  pthread_create(&t2, NULL, test1::helper, NULL);
+  pthread_join(t1, NULL);
+  pthread_join(t2, NULL);
+  llvm_stop_multithreaded();
+}
+
+} // anonymous namespace





More information about the llvm-commits mailing list