[libcxx] r263611 - Add clang thread safety annotations to mutex and lock_guard. Patch by jamesr at google.com.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 15 19:30:07 PDT 2016


Author: ericwf
Date: Tue Mar 15 21:30:06 2016
New Revision: 263611

URL: http://llvm.org/viewvc/llvm-project?rev=263611&view=rev
Log:
Add clang thread safety annotations to mutex and lock_guard. Patch by jamesr at google.com.

This adds clang thread safety annotations to std::mutex and
std::lock_guard so code using these types can use these types directly
instead of having to wrap the types to provide annotations. These checks
when enabled by -Wthread-safety provide simple but useful static
checking to detect potential race conditions.
See http://clang.llvm.org/docs/ThreadSafetyAnalysis.html for details.

This patch was reviewed in http://reviews.llvm.org/D14731.

Added:
    libcxx/trunk/test/libcxx/thread/thread.mutex/
    libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp
    libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp
    libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp
    libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp
    libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp
Modified:
    libcxx/trunk/include/__config
    libcxx/trunk/include/__mutex_base
    libcxx/trunk/test/libcxx/test/config.py

Modified: libcxx/trunk/include/__config
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__config?rev=263611&r1=263610&r2=263611&view=diff
==============================================================================
--- libcxx/trunk/include/__config (original)
+++ libcxx/trunk/include/__config Tue Mar 15 21:30:06 2016
@@ -859,6 +859,11 @@ extern "C" void __sanitizer_annotate_con
 #endif
 #endif
 
+#if (defined(_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS) && defined(__clang__) \
+      && __has_attribute(acquire_capability))
+#define _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
+#endif
+
 #endif // __cplusplus
 
 #endif // _LIBCPP_CONFIG

Modified: libcxx/trunk/include/__mutex_base
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__mutex_base?rev=263611&r1=263610&r2=263611&view=diff
==============================================================================
--- libcxx/trunk/include/__mutex_base (original)
+++ libcxx/trunk/include/__mutex_base Tue Mar 15 21:30:06 2016
@@ -26,7 +26,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #ifndef _LIBCPP_HAS_NO_THREADS
 
-class _LIBCPP_TYPE_VIS mutex
+#ifndef _LIBCPP_THREAD_SAFETY_ANNOTATION
+#  ifdef _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS
+#    define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) __attribute__((x))
+#  else
+#    define _LIBCPP_THREAD_SAFETY_ANNOTATION(x)
+#  endif
+#endif  // _LIBCPP_THREAD_SAFETY_ANNOTATION
+
+class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
 {
     pthread_mutex_t __m_;
 
@@ -44,9 +52,9 @@ private:
     mutex& operator=(const mutex&);// = delete;
 
 public:
-    void lock();
-    bool try_lock() _NOEXCEPT;
-    void unlock() _NOEXCEPT;
+    void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
+    bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
+    void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
 
     typedef pthread_mutex_t* native_handle_type;
     _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
@@ -71,7 +79,7 @@ constexpr adopt_lock_t  adopt_lock  = ad
 #endif
 
 template <class _Mutex>
-class _LIBCPP_TYPE_VIS_ONLY lock_guard
+class _LIBCPP_TYPE_VIS_ONLY _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) lock_guard
 {
 public:
     typedef _Mutex mutex_type;
@@ -81,13 +89,13 @@ private:
 public:
 
     _LIBCPP_INLINE_VISIBILITY
-    explicit lock_guard(mutex_type& __m)
+    explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
         : __m_(__m) {__m_.lock();}
     _LIBCPP_INLINE_VISIBILITY
-    lock_guard(mutex_type& __m, adopt_lock_t)
+    lock_guard(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
         : __m_(__m) {}
     _LIBCPP_INLINE_VISIBILITY
-    ~lock_guard() {__m_.unlock();}
+    ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
 
 private:
     lock_guard(lock_guard const&);// = delete;

Modified: libcxx/trunk/test/libcxx/test/config.py
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/test/config.py?rev=263611&r1=263610&r2=263611&view=diff
==============================================================================
--- libcxx/trunk/test/libcxx/test/config.py (original)
+++ libcxx/trunk/test/libcxx/test/config.py Tue Mar 15 21:30:06 2016
@@ -98,6 +98,7 @@ class Configuration(object):
         self.configure_cxx_library_root()
         self.configure_use_system_cxx_lib()
         self.configure_use_clang_verify()
+        self.configure_use_thread_safety()
         self.configure_execute_external()
         self.configure_ccache()
         self.configure_compile_flags()
@@ -218,6 +219,14 @@ class Configuration(object):
             self.lit_config.note(
                 "inferred use_clang_verify as: %r" % self.use_clang_verify)
 
+    def configure_use_thread_safety(self):
+        '''If set, run clang with -verify on failing tests.'''
+        has_thread_safety = self.cxx.hasCompileFlag('-Werror=thread-safety')
+        if has_thread_safety:
+            self.cxx.compile_flags += ['-Werror=thread-safety']
+            self.config.available_features.add('thread-safety')
+            self.lit_config.note("enabling thread-safety annotations")
+
     def configure_execute_external(self):
         # Choose between lit's internal shell pipeline runner and a real shell.
         # If LIT_USE_INTERNAL_SHELL is in the environment, we use that as the

Added: libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp?rev=263611&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_annotations_not_enabled.pass.cpp Tue Mar 15 21:30:06 2016
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <mutex>
+
+// This test does not define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS so it
+// should compile without any warnings or errors even though this pattern is not
+// understood by the thread safety annotations.
+
+#include <mutex>
+
+int main() {
+  std::mutex m;
+  m.lock();
+  {
+    std::unique_lock<std::mutex> g(m, std::adopt_lock);
+  }
+}

Added: libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp?rev=263611&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_guard.pass.cpp Tue Mar 15 21:30:06 2016
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: thread-safety
+
+// <mutex>
+
+#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
+
+#include <mutex>
+
+std::mutex m;
+int foo __attribute__((guarded_by(m)));
+
+int main() {
+  std::lock_guard<std::mutex> lock(m);
+  foo++;
+}

Added: libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp?rev=263611&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_lock_unlock.pass.cpp Tue Mar 15 21:30:06 2016
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: thread-safety
+
+// <mutex>
+
+#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
+
+#include <mutex>
+
+std::mutex m;
+int foo __attribute__((guarded_by(m)));
+
+int main() {
+  m.lock();
+  foo++;
+  m.unlock();
+}

Added: libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp?rev=263611&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp (added)
+++ libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_missing_unlock.fail.cpp Tue Mar 15 21:30:06 2016
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: thread-safety
+
+// <mutex>
+
+#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
+
+#include <mutex>
+
+std::mutex m;
+
+int main() {
+  m.lock();
+} // expected-error {{mutex 'm' is still held at the end of function}}

Added: libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp?rev=263611&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/thread/thread.mutex/thread_safety_requires_capability.pass.cpp Tue Mar 15 21:30:06 2016
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: thread-safety
+
+// <mutex>
+
+#define _LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
+
+#include <mutex>
+
+std::mutex m;
+int foo __attribute__((guarded_by(m)));
+
+void increment() __attribute__((requires_capability(m))) {
+  foo++;
+}
+
+int main() {
+  m.lock();
+  increment();
+  m.unlock();
+}




More information about the cfe-commits mailing list