[libcxx] r282640 - Mark libc++ internal globals with _LIBCPP_SAFE_STATIC.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 28 15:08:14 PDT 2016


Author: ericwf
Date: Wed Sep 28 17:08:13 2016
New Revision: 282640

URL: http://llvm.org/viewvc/llvm-project?rev=282640&view=rev
Log:
Mark libc++ internal globals with _LIBCPP_SAFE_STATIC.

This patch applies the _LIBCPP_SAFE_STATIC attribute to internal globals,
most of which are locking primitives, in order to ensure that they can
safely be used during program startup.

This patch also fixes an unsafe static init issue with the global locks
used to implement atomic operations on shared pointers. Previously the
locks were initialized using a dynamically initialized pointer, so it was
possible that the pointer was uninitialized.

Modified:
    libcxx/trunk/src/algorithm.cpp
    libcxx/trunk/src/exception.cpp
    libcxx/trunk/src/memory.cpp
    libcxx/trunk/src/mutex.cpp

Modified: libcxx/trunk/src/algorithm.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/algorithm.cpp?rev=282640&r1=282639&r2=282640&view=diff
==============================================================================
--- libcxx/trunk/src/algorithm.cpp (original)
+++ libcxx/trunk/src/algorithm.cpp Wed Sep 28 17:08:13 2016
@@ -48,7 +48,7 @@ template bool __insertion_sort_incomplet
 template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
 
 #ifndef _LIBCPP_HAS_NO_THREADS
-static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
+_LIBCPP_SAFE_STATIC static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
 #endif
 unsigned __rs_default::__c_ = 0;
 

Modified: libcxx/trunk/src/exception.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/exception.cpp?rev=282640&r1=282639&r2=282640&view=diff
==============================================================================
--- libcxx/trunk/src/exception.cpp (original)
+++ libcxx/trunk/src/exception.cpp Wed Sep 28 17:08:13 2016
@@ -32,8 +32,8 @@
     #define HAVE_DEPENDENT_EH_ABI 1
   #endif
 #elif !defined(__GLIBCXX__) // defined(LIBCXX_BUILDING_LIBCXXABI)
-  static std::terminate_handler  __terminate_handler;
-  static std::unexpected_handler __unexpected_handler;
+  _LIBCPP_SAFE_STATIC static std::terminate_handler  __terminate_handler;
+  _LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
 
 namespace std

Modified: libcxx/trunk/src/memory.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/memory.cpp?rev=282640&r1=282639&r2=282640&view=diff
==============================================================================
--- libcxx/trunk/src/memory.cpp (original)
+++ libcxx/trunk/src/memory.cpp Wed Sep 28 17:08:13 2016
@@ -154,8 +154,8 @@ __shared_weak_count::__get_deleter(const
 
 #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
 
-static const std::size_t __sp_mut_count = 16;
-static __libcpp_mutex_t mut_back_imp[__sp_mut_count] =
+_LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16;
+_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] =
 {
     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
@@ -163,8 +163,6 @@ static __libcpp_mutex_t mut_back_imp[__s
     _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER
 };
 
-static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp);
-
 _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
    : __lx(p)
 {
@@ -173,13 +171,13 @@ _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(voi
 void
 __sp_mut::lock() _NOEXCEPT
 {
-    mutex& m = *static_cast<mutex*>(__lx);
+    auto m = static_cast<__libcpp_mutex_t*>(__lx);
     unsigned count = 0;
-    while (!m.try_lock())
+    while (__libcpp_mutex_trylock(m) != 0)
     {
         if (++count > 16)
         {
-            m.lock();
+            __libcpp_mutex_lock(m);
             break;
         }
         this_thread::yield();
@@ -189,13 +187,13 @@ __sp_mut::lock() _NOEXCEPT
 void
 __sp_mut::unlock() _NOEXCEPT
 {
-    static_cast<mutex*>(__lx)->unlock();
+    __libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx));
 }
 
 __sp_mut&
 __get_sp_mut(const void* p)
 {
-    static __sp_mut muts[__sp_mut_count] 
+    static __sp_mut muts[__sp_mut_count]
     {
         &mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
         &mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],

Modified: libcxx/trunk/src/mutex.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/mutex.cpp?rev=282640&r1=282639&r2=282640&view=diff
==============================================================================
--- libcxx/trunk/src/mutex.cpp (original)
+++ libcxx/trunk/src/mutex.cpp Wed Sep 28 17:08:13 2016
@@ -195,8 +195,8 @@ recursive_timed_mutex::unlock() _NOEXCEP
 // keep in sync with:  7741191.
 
 #ifndef _LIBCPP_HAS_NO_THREADS
-static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
-static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
+_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
+_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
 #endif
 
 void




More information about the cfe-commits mailing list