Skip to content

Commit b203586

Browse files
ldionnesmeenai
andcommitted
[libc++abi] Always use thread_local for cxa_exception_storage
This was previously guarded by HAS_THREAD_LOCAL, which was never set by CMake and had to be specified manually. Android has been setting this to solve android/ndk#1200 [1], but every compiler and platform libc++abi supports should have thread_local by now, so we can just get rid of the fallback implementation and simplify things significantly (including removing the now unused fallback calloc). This is a re-application of https://reviews.llvm.org/D138461. Fixes #78207. [1]: https://android-review.googlesource.com/c/toolchain/llvm-project/+/1285596 Co-Authored-By: Shoaib Meenai <[email protected]>
1 parent e37a600 commit b203586

File tree

3 files changed

+1
-77
lines changed

3 files changed

+1
-77
lines changed

libcxxabi/src/cxa_exception_storage.cpp

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include "cxa_exception.h"
1414

15-
#include <__thread/support.h>
16-
1715
#if defined(_LIBCXXABI_HAS_NO_THREADS)
1816

1917
namespace __cxxabiv1 {
@@ -24,7 +22,7 @@ extern "C" {
2422
} // extern "C"
2523
} // namespace __cxxabiv1
2624

27-
#elif defined(HAS_THREAD_LOCAL)
25+
#else
2826

2927
namespace __cxxabiv1 {
3028
namespace {
@@ -40,64 +38,4 @@ extern "C" {
4038
} // extern "C"
4139
} // namespace __cxxabiv1
4240

43-
#else
44-
45-
#include "abort_message.h"
46-
#include "fallback_malloc.h"
47-
48-
#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
49-
#pragma comment(lib, "pthread")
50-
#endif
51-
52-
// In general, we treat all threading errors as fatal.
53-
// We cannot call std::terminate() because that will in turn
54-
// call __cxa_get_globals() and cause infinite recursion.
55-
56-
namespace __cxxabiv1 {
57-
namespace {
58-
std::__libcpp_tls_key key_;
59-
std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER;
60-
61-
void _LIBCPP_TLS_DESTRUCTOR_CC destruct_(void *p) {
62-
__free_with_fallback(p);
63-
if (0 != std::__libcpp_tls_set(key_, NULL))
64-
abort_message("cannot zero out thread value for __cxa_get_globals()");
65-
}
66-
67-
void construct_() {
68-
if (0 != std::__libcpp_tls_create(&key_, destruct_))
69-
abort_message("cannot create thread specific key for __cxa_get_globals()");
70-
}
71-
} // namespace
72-
73-
extern "C" {
74-
__cxa_eh_globals *__cxa_get_globals() {
75-
// Try to get the globals for this thread
76-
__cxa_eh_globals *retVal = __cxa_get_globals_fast();
77-
78-
// If this is the first time we've been asked for these globals, create them
79-
if (NULL == retVal) {
80-
retVal = static_cast<__cxa_eh_globals*>(
81-
__calloc_with_fallback(1, sizeof(__cxa_eh_globals)));
82-
if (NULL == retVal)
83-
abort_message("cannot allocate __cxa_eh_globals");
84-
if (0 != std::__libcpp_tls_set(key_, retVal))
85-
abort_message("std::__libcpp_tls_set failure in __cxa_get_globals()");
86-
}
87-
return retVal;
88-
}
89-
90-
// Note that this implementation will reliably return NULL if not
91-
// preceded by a call to __cxa_get_globals(). This is an extension
92-
// to the Itanium ABI and is taken advantage of in several places in
93-
// libc++abi.
94-
__cxa_eh_globals *__cxa_get_globals_fast() {
95-
// First time through, create the key.
96-
if (0 != std::__libcpp_execute_once(&flag_, construct_))
97-
abort_message("execute once failure in __cxa_get_globals_fast()");
98-
return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_));
99-
}
100-
} // extern "C"
101-
} // namespace __cxxabiv1
102-
10341
#endif

libcxxabi/src/fallback_malloc.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,6 @@ void* __aligned_malloc_with_fallback(size_t size) {
271271
return fallback_malloc(size);
272272
}
273273

274-
void* __calloc_with_fallback(size_t count, size_t size) {
275-
void* ptr = ::calloc(count, size);
276-
if (NULL != ptr)
277-
return ptr;
278-
// if calloc fails, fall back to emergency stash
279-
ptr = fallback_malloc(size * count);
280-
if (NULL != ptr)
281-
::memset(ptr, 0, size * count);
282-
return ptr;
283-
}
284-
285274
void __aligned_free_with_fallback(void* ptr) {
286275
if (is_fallback_ptr(ptr))
287276
fallback_free(ptr);

libcxxabi/src/fallback_malloc.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ namespace __cxxabiv1 {
1717
// Allocate some memory from _somewhere_
1818
_LIBCXXABI_HIDDEN void * __aligned_malloc_with_fallback(size_t size);
1919

20-
// Allocate and zero-initialize memory from _somewhere_
21-
_LIBCXXABI_HIDDEN void * __calloc_with_fallback(size_t count, size_t size);
22-
2320
_LIBCXXABI_HIDDEN void __aligned_free_with_fallback(void *ptr);
2421
_LIBCXXABI_HIDDEN void __free_with_fallback(void *ptr);
2522

0 commit comments

Comments
 (0)