1 //===---------- emutls.c - Implements __emutls_get_address ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
16 // There are 4 pthread key cleanup rounds on Bionic. Delay emutls deallocation
17 // to round 2. We need to delay deallocation because:
18 // - Android versions older than M lack __cxa_thread_atexit_impl, so apps
19 // use a pthread key destructor to call C++ destructors.
20 // - Apps might use __thread/thread_local variables in pthread destructors.
21 // We can't wait until the final two rounds, because jemalloc needs two rounds
22 // after the final malloc/free call to free its thread-specific data (see
23 // https://reviews.llvm.org/D46978#1107507).
24 #define EMUTLS_SKIP_DESTRUCTOR_ROUNDS 1
26 #define EMUTLS_SKIP_DESTRUCTOR_ROUNDS 0
29 #if defined(_MSC_VER) && !defined(__clang__)
30 // MSVC raises a warning about a nonstandard extension being used for the 0
31 // sized element in this array. Disable this for warn-as-error builds.
33 #pragma warning(disable : 4206)
36 typedef struct emutls_address_array {
37 uintptr_t skip_destructor_rounds;
38 uintptr_t size; // number of elements in the 'data' array
40 } emutls_address_array;
42 #if defined(_MSC_VER) && !defined(__clang__)
46 static void emutls_shutdown(emutls_address_array *array);
52 static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
53 static pthread_key_t emutls_pthread_key;
54 static bool emutls_key_created = false;
56 typedef unsigned int gcc_word __attribute__((mode(word)));
57 typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
59 // Default is not to use posix_memalign, so systems like Android
60 // can use thread local data without heavier POSIX memory allocators.
61 #ifndef EMUTLS_USE_POSIX_MEMALIGN
62 #define EMUTLS_USE_POSIX_MEMALIGN 0
65 static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
67 #if EMUTLS_USE_POSIX_MEMALIGN
68 if (posix_memalign(&base, align, size) != 0)
71 #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void *))
73 if ((object = (char *)malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
75 base = (void *)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES)) &
76 ~(uintptr_t)(align - 1));
78 ((void **)base)[-1] = object;
83 static __inline void emutls_memalign_free(void *base) {
84 #if EMUTLS_USE_POSIX_MEMALIGN
87 // The mallocated address is in ((void**)base)[-1]
88 free(((void **)base)[-1]);
92 static __inline void emutls_setspecific(emutls_address_array *value) {
93 pthread_setspecific(emutls_pthread_key, (void *)value);
96 static __inline emutls_address_array *emutls_getspecific() {
97 return (emutls_address_array *)pthread_getspecific(emutls_pthread_key);
100 static void emutls_key_destructor(void *ptr) {
101 emutls_address_array *array = (emutls_address_array *)ptr;
102 if (array->skip_destructor_rounds > 0) {
103 // emutls is deallocated using a pthread key destructor. These
104 // destructors are called in several rounds to accommodate destructor
105 // functions that (re)initialize key values with pthread_setspecific.
106 // Delay the emutls deallocation to accommodate other end-of-thread
107 // cleanup tasks like calling thread_local destructors (e.g. the
108 // __cxa_thread_atexit fallback in libc++abi).
109 array->skip_destructor_rounds--;
110 emutls_setspecific(array);
112 emutls_shutdown(array);
117 static __inline void emutls_init(void) {
118 if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
120 emutls_key_created = true;
123 static __inline void emutls_init_once(void) {
124 static pthread_once_t once = PTHREAD_ONCE_INIT;
125 pthread_once(&once, emutls_init);
128 static __inline void emutls_lock() { pthread_mutex_lock(&emutls_mutex); }
130 static __inline void emutls_unlock() { pthread_mutex_unlock(&emutls_mutex); }
139 static LPCRITICAL_SECTION emutls_mutex;
140 static DWORD emutls_tls_index = TLS_OUT_OF_INDEXES;
142 typedef uintptr_t gcc_word;
143 typedef void *gcc_pointer;
145 static void win_error(DWORD last_err, const char *hint) {
147 if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
148 FORMAT_MESSAGE_FROM_SYSTEM |
149 FORMAT_MESSAGE_MAX_WIDTH_MASK,
150 NULL, last_err, 0, (LPSTR)&buffer, 1, NULL)) {
151 fprintf(stderr, "Windows error: %s\n", buffer);
153 fprintf(stderr, "Unkown Windows error: %s\n", hint);
158 static __inline void win_abort(DWORD last_err, const char *hint) {
159 win_error(last_err, hint);
163 static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
164 void *base = _aligned_malloc(size, align);
166 win_abort(GetLastError(), "_aligned_malloc");
170 static __inline void emutls_memalign_free(void *base) { _aligned_free(base); }
172 static void emutls_exit(void) {
174 DeleteCriticalSection(emutls_mutex);
175 _aligned_free(emutls_mutex);
178 if (emutls_tls_index != TLS_OUT_OF_INDEXES) {
179 emutls_shutdown((emutls_address_array *)TlsGetValue(emutls_tls_index));
180 TlsFree(emutls_tls_index);
181 emutls_tls_index = TLS_OUT_OF_INDEXES;
185 #pragma warning(push)
186 #pragma warning(disable : 4100)
187 static BOOL CALLBACK emutls_init(PINIT_ONCE p0, PVOID p1, PVOID *p2) {
189 (LPCRITICAL_SECTION)_aligned_malloc(sizeof(CRITICAL_SECTION), 16);
191 win_error(GetLastError(), "_aligned_malloc");
194 InitializeCriticalSection(emutls_mutex);
196 emutls_tls_index = TlsAlloc();
197 if (emutls_tls_index == TLS_OUT_OF_INDEXES) {
199 win_error(GetLastError(), "TlsAlloc");
202 atexit(&emutls_exit);
206 static __inline void emutls_init_once(void) {
207 static INIT_ONCE once;
208 InitOnceExecuteOnce(&once, emutls_init, NULL, NULL);
211 static __inline void emutls_lock() { EnterCriticalSection(emutls_mutex); }
213 static __inline void emutls_unlock() { LeaveCriticalSection(emutls_mutex); }
215 static __inline void emutls_setspecific(emutls_address_array *value) {
216 if (TlsSetValue(emutls_tls_index, (LPVOID)value) == 0)
217 win_abort(GetLastError(), "TlsSetValue");
220 static __inline emutls_address_array *emutls_getspecific() {
221 LPVOID value = TlsGetValue(emutls_tls_index);
223 const DWORD err = GetLastError();
224 if (err != ERROR_SUCCESS)
225 win_abort(err, "TlsGetValue");
227 return (emutls_address_array *)value;
230 // Provide atomic load/store functions for emutls_get_index if built with MSVC.
231 #if !defined(__ATOMIC_RELEASE)
234 enum { __ATOMIC_ACQUIRE = 2, __ATOMIC_RELEASE = 3 };
236 static __inline uintptr_t __atomic_load_n(void *ptr, unsigned type) {
237 assert(type == __ATOMIC_ACQUIRE);
238 // These return the previous value - but since we do an OR with 0,
239 // it's equivalent to a plain load.
241 return InterlockedOr64(ptr, 0);
243 return InterlockedOr(ptr, 0);
247 static __inline void __atomic_store_n(void *ptr, uintptr_t val, unsigned type) {
248 assert(type == __ATOMIC_RELEASE);
249 InterlockedExchangePointer((void *volatile *)ptr, (void *)val);
252 #endif // __ATOMIC_RELEASE
258 static size_t emutls_num_object = 0; // number of allocated TLS objects
260 // Free the allocated TLS data
261 static void emutls_shutdown(emutls_address_array *array) {
264 for (i = 0; i < array->size; ++i) {
266 emutls_memalign_free(array->data[i]);
271 // For every TLS variable xyz,
272 // there is one __emutls_control variable named __emutls_v.xyz.
273 // If xyz has non-zero initial value, __emutls_v.xyz's "value"
274 // will point to __emutls_t.xyz, which has the initial value.
275 typedef struct __emutls_control {
276 // Must use gcc_word here, instead of size_t, to match GCC. When
277 // gcc_word is larger than size_t, the upper extra bits are all
278 // zeros. We can use variables of size_t to operate on size and
280 gcc_word size; // size of the object in bytes
281 gcc_word align; // alignment of the object in bytes
283 uintptr_t index; // data[index-1] is the object address
284 void *address; // object address, when in single thread env
286 void *value; // null or non-zero initial value for the object
289 // Emulated TLS objects are always allocated at run-time.
290 static __inline void *emutls_allocate_object(__emutls_control *control) {
291 // Use standard C types, check with gcc's emutls.o.
292 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
293 COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void *));
295 size_t size = control->size;
296 size_t align = control->align;
298 if (align < sizeof(void *))
299 align = sizeof(void *);
300 // Make sure that align is power of 2.
301 if ((align & (align - 1)) != 0)
304 base = emutls_memalign_alloc(align, size);
306 memcpy(base, control->value, size);
308 memset(base, 0, size);
312 // Returns control->object.index; set index if not allocated yet.
313 static __inline uintptr_t emutls_get_index(__emutls_control *control) {
314 uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
318 index = control->object.index;
320 index = ++emutls_num_object;
321 __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
328 // Updates newly allocated thread local emutls_address_array.
329 static __inline void emutls_check_array_set_size(emutls_address_array *array,
334 emutls_setspecific(array);
337 // Returns the new 'data' array size, number of elements,
338 // which must be no smaller than the given index.
339 static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
340 // Need to allocate emutls_address_array with extra slots
341 // to store the header.
342 // Round up the emutls_address_array size to multiple of 16.
343 uintptr_t header_words = sizeof(emutls_address_array) / sizeof(void *);
344 return ((index + header_words + 15) & ~((uintptr_t)15)) - header_words;
347 // Returns the size in bytes required for an emutls_address_array with
348 // N number of elements for data field.
349 static __inline uintptr_t emutls_asize(uintptr_t N) {
350 return N * sizeof(void *) + sizeof(emutls_address_array);
353 // Returns the thread local emutls_address_array.
354 // Extends its size if necessary to hold address at index.
355 static __inline emutls_address_array *
356 emutls_get_address_array(uintptr_t index) {
357 emutls_address_array *array = emutls_getspecific();
359 uintptr_t new_size = emutls_new_data_array_size(index);
360 array = (emutls_address_array *)malloc(emutls_asize(new_size));
362 memset(array->data, 0, new_size * sizeof(void *));
363 array->skip_destructor_rounds = EMUTLS_SKIP_DESTRUCTOR_ROUNDS;
365 emutls_check_array_set_size(array, new_size);
366 } else if (index > array->size) {
367 uintptr_t orig_size = array->size;
368 uintptr_t new_size = emutls_new_data_array_size(index);
369 array = (emutls_address_array *)realloc(array, emutls_asize(new_size));
371 memset(array->data + orig_size, 0,
372 (new_size - orig_size) * sizeof(void *));
373 emutls_check_array_set_size(array, new_size);
378 void *__emutls_get_address(__emutls_control *control) {
379 uintptr_t index = emutls_get_index(control);
380 emutls_address_array *array = emutls_get_address_array(index--);
381 if (array->data[index] == NULL)
382 array->data[index] = emutls_allocate_object(control);
383 return array->data[index];
387 // Called by Bionic on dlclose to delete the emutls pthread key.
388 __attribute__((visibility("hidden"))) void __emutls_unregister_key(void) {
389 if (emutls_key_created) {
390 pthread_key_delete(emutls_pthread_key);
391 emutls_key_created = false;