Skip to content

Commit ea117bf

Browse files
committed
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
1 parent 392caa5 commit ea117bf

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

libcxx/src/algorithm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ template bool __insertion_sort_incomplete<__less<long double>&, long double*>(lo
4848
template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
4949

5050
#ifndef _LIBCPP_HAS_NO_THREADS
51-
static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
51+
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
5252
#endif
5353
unsigned __rs_default::__c_ = 0;
5454

libcxx/src/exception.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#define HAVE_DEPENDENT_EH_ABI 1
3333
#endif
3434
#elif !defined(__GLIBCXX__) // defined(LIBCXX_BUILDING_LIBCXXABI)
35-
static std::terminate_handler __terminate_handler;
36-
static std::unexpected_handler __unexpected_handler;
35+
_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler;
36+
_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
3737
#endif // defined(LIBCXX_BUILDING_LIBCXXABI)
3838

3939
namespace std

libcxx/src/memory.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,15 @@ __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT
154154

155155
#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
156156

157-
static const std::size_t __sp_mut_count = 16;
158-
static __libcpp_mutex_t mut_back_imp[__sp_mut_count] =
157+
_LIBCPP_SAFE_STATIC static const std::size_t __sp_mut_count = 16;
158+
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut_back[__sp_mut_count] =
159159
{
160160
_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
161161
_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
162162
_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
163163
_LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER
164164
};
165165

166-
static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp);
167-
168166
_LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
169167
: __lx(p)
170168
{
@@ -173,13 +171,13 @@ _LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
173171
void
174172
__sp_mut::lock() _NOEXCEPT
175173
{
176-
mutex& m = *static_cast<mutex*>(__lx);
174+
auto m = static_cast<__libcpp_mutex_t*>(__lx);
177175
unsigned count = 0;
178-
while (!m.try_lock())
176+
while (__libcpp_mutex_trylock(m) != 0)
179177
{
180178
if (++count > 16)
181179
{
182-
m.lock();
180+
__libcpp_mutex_lock(m);
183181
break;
184182
}
185183
this_thread::yield();
@@ -189,13 +187,13 @@ __sp_mut::lock() _NOEXCEPT
189187
void
190188
__sp_mut::unlock() _NOEXCEPT
191189
{
192-
static_cast<mutex*>(__lx)->unlock();
190+
__libcpp_mutex_unlock(static_cast<__libcpp_mutex_t*>(__lx));
193191
}
194192

195193
__sp_mut&
196194
__get_sp_mut(const void* p)
197195
{
198-
static __sp_mut muts[__sp_mut_count]
196+
static __sp_mut muts[__sp_mut_count]
199197
{
200198
&mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
201199
&mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],

libcxx/src/mutex.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ recursive_timed_mutex::unlock() _NOEXCEPT
195195
// keep in sync with: 7741191.
196196

197197
#ifndef _LIBCPP_HAS_NO_THREADS
198-
static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
199-
static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
198+
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
199+
_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
200200
#endif
201201

202202
void

0 commit comments

Comments
 (0)