diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-09-28 22:08:13 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-09-28 22:08:13 +0000 |
commit | ea117bf9ad162950a1fdd527f8b75d1bdf22456b (patch) | |
tree | 15a1200a1f8fa5699cd0ad28e13e070b063483fe | |
parent | 392caa538dfa29757f8f1e8635508affab2b1072 (diff) | |
download | bcm5719-llvm-ea117bf9ad162950a1fdd527f8b75d1bdf22456b.tar.gz bcm5719-llvm-ea117bf9ad162950a1fdd527f8b75d1bdf22456b.zip |
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.
llvm-svn: 282640
-rw-r--r-- | libcxx/src/algorithm.cpp | 2 | ||||
-rw-r--r-- | libcxx/src/exception.cpp | 4 | ||||
-rw-r--r-- | libcxx/src/memory.cpp | 16 | ||||
-rw-r--r-- | libcxx/src/mutex.cpp | 4 |
4 files changed, 12 insertions, 14 deletions
diff --git a/libcxx/src/algorithm.cpp b/libcxx/src/algorithm.cpp index e9752b0653e..f036eb7abe1 100644 --- a/libcxx/src/algorithm.cpp +++ b/libcxx/src/algorithm.cpp @@ -48,7 +48,7 @@ template bool __insertion_sort_incomplete<__less<long double>&, long double*>(lo 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; diff --git a/libcxx/src/exception.cpp b/libcxx/src/exception.cpp index e172f642d48..96bd7ee59a0 100644 --- a/libcxx/src/exception.cpp +++ b/libcxx/src/exception.cpp @@ -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 diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp index b685d125ba9..514a2ce2266 100644 --- a/libcxx/src/memory.cpp +++ b/libcxx/src/memory.cpp @@ -154,8 +154,8 @@ __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT #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[__sp_mut_count] = _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(void* p) _NOEXCEPT 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], diff --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp index 7226abc6a51..dc530ceeac0 100644 --- a/libcxx/src/mutex.cpp +++ b/libcxx/src/mutex.cpp @@ -195,8 +195,8 @@ recursive_timed_mutex::unlock() _NOEXCEPT // 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 |