xref: /aosp_15_r20/external/compiler-rt/lib/builtins/emutls.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* ===---------- emutls.c - Implements __emutls_get_address ---------------===
2*7c3d14c8STreehugger Robot  *
3*7c3d14c8STreehugger Robot  *                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot  *
5*7c3d14c8STreehugger Robot  * This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot  * Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot  *
8*7c3d14c8STreehugger Robot  * ===----------------------------------------------------------------------===
9*7c3d14c8STreehugger Robot  */
10*7c3d14c8STreehugger Robot #include <pthread.h>
11*7c3d14c8STreehugger Robot #include <stdint.h>
12*7c3d14c8STreehugger Robot #include <stdlib.h>
13*7c3d14c8STreehugger Robot #include <string.h>
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "int_lib.h"
16*7c3d14c8STreehugger Robot #include "int_util.h"
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot /* Default is not to use posix_memalign, so systems like Android
19*7c3d14c8STreehugger Robot  * can use thread local data without heavier POSIX memory allocators.
20*7c3d14c8STreehugger Robot  */
21*7c3d14c8STreehugger Robot #ifndef EMUTLS_USE_POSIX_MEMALIGN
22*7c3d14c8STreehugger Robot #define EMUTLS_USE_POSIX_MEMALIGN 0
23*7c3d14c8STreehugger Robot #endif
24*7c3d14c8STreehugger Robot 
25*7c3d14c8STreehugger Robot /* For every TLS variable xyz,
26*7c3d14c8STreehugger Robot  * there is one __emutls_control variable named __emutls_v.xyz.
27*7c3d14c8STreehugger Robot  * If xyz has non-zero initial value, __emutls_v.xyz's "value"
28*7c3d14c8STreehugger Robot  * will point to __emutls_t.xyz, which has the initial value.
29*7c3d14c8STreehugger Robot  */
30*7c3d14c8STreehugger Robot typedef unsigned int gcc_word __attribute__((mode(word)));
31*7c3d14c8STreehugger Robot typedef struct __emutls_control {
32*7c3d14c8STreehugger Robot     /* Must use gcc_word here, instead of size_t, to match GCC.  When
33*7c3d14c8STreehugger Robot        gcc_word is larger than size_t, the upper extra bits are all
34*7c3d14c8STreehugger Robot        zeros.  We can use variables of size_t to operate on size and
35*7c3d14c8STreehugger Robot        align.  */
36*7c3d14c8STreehugger Robot     gcc_word size;  /* size of the object in bytes */
37*7c3d14c8STreehugger Robot     gcc_word align;  /* alignment of the object in bytes */
38*7c3d14c8STreehugger Robot     union {
39*7c3d14c8STreehugger Robot         uintptr_t index;  /* data[index-1] is the object address */
40*7c3d14c8STreehugger Robot         void* address;  /* object address, when in single thread env */
41*7c3d14c8STreehugger Robot     } object;
42*7c3d14c8STreehugger Robot     void* value;  /* null or non-zero initial value for the object */
43*7c3d14c8STreehugger Robot } __emutls_control;
44*7c3d14c8STreehugger Robot 
emutls_memalign_alloc(size_t align,size_t size)45*7c3d14c8STreehugger Robot static __inline void *emutls_memalign_alloc(size_t align, size_t size) {
46*7c3d14c8STreehugger Robot     void *base;
47*7c3d14c8STreehugger Robot #if EMUTLS_USE_POSIX_MEMALIGN
48*7c3d14c8STreehugger Robot     if (posix_memalign(&base, align, size) != 0)
49*7c3d14c8STreehugger Robot         abort();
50*7c3d14c8STreehugger Robot #else
51*7c3d14c8STreehugger Robot     #define EXTRA_ALIGN_PTR_BYTES (align - 1 + sizeof(void*))
52*7c3d14c8STreehugger Robot     char* object;
53*7c3d14c8STreehugger Robot     if ((object = malloc(EXTRA_ALIGN_PTR_BYTES + size)) == NULL)
54*7c3d14c8STreehugger Robot         abort();
55*7c3d14c8STreehugger Robot     base = (void*)(((uintptr_t)(object + EXTRA_ALIGN_PTR_BYTES))
56*7c3d14c8STreehugger Robot                     & ~(uintptr_t)(align - 1));
57*7c3d14c8STreehugger Robot 
58*7c3d14c8STreehugger Robot     ((void**)base)[-1] = object;
59*7c3d14c8STreehugger Robot #endif
60*7c3d14c8STreehugger Robot     return base;
61*7c3d14c8STreehugger Robot }
62*7c3d14c8STreehugger Robot 
emutls_memalign_free(void * base)63*7c3d14c8STreehugger Robot static __inline void emutls_memalign_free(void *base) {
64*7c3d14c8STreehugger Robot #if EMUTLS_USE_POSIX_MEMALIGN
65*7c3d14c8STreehugger Robot     free(base);
66*7c3d14c8STreehugger Robot #else
67*7c3d14c8STreehugger Robot     /* The mallocated address is in ((void**)base)[-1] */
68*7c3d14c8STreehugger Robot     free(((void**)base)[-1]);
69*7c3d14c8STreehugger Robot #endif
70*7c3d14c8STreehugger Robot }
71*7c3d14c8STreehugger Robot 
72*7c3d14c8STreehugger Robot /* Emulated TLS objects are always allocated at run-time. */
emutls_allocate_object(__emutls_control * control)73*7c3d14c8STreehugger Robot static __inline void *emutls_allocate_object(__emutls_control *control) {
74*7c3d14c8STreehugger Robot     /* Use standard C types, check with gcc's emutls.o. */
75*7c3d14c8STreehugger Robot     typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
76*7c3d14c8STreehugger Robot     COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(gcc_pointer));
77*7c3d14c8STreehugger Robot     COMPILE_TIME_ASSERT(sizeof(uintptr_t) == sizeof(void*));
78*7c3d14c8STreehugger Robot 
79*7c3d14c8STreehugger Robot     size_t size = control->size;
80*7c3d14c8STreehugger Robot     size_t align = control->align;
81*7c3d14c8STreehugger Robot     void* base;
82*7c3d14c8STreehugger Robot     if (align < sizeof(void*))
83*7c3d14c8STreehugger Robot         align = sizeof(void*);
84*7c3d14c8STreehugger Robot     /* Make sure that align is power of 2. */
85*7c3d14c8STreehugger Robot     if ((align & (align - 1)) != 0)
86*7c3d14c8STreehugger Robot         abort();
87*7c3d14c8STreehugger Robot 
88*7c3d14c8STreehugger Robot     base = emutls_memalign_alloc(align, size);
89*7c3d14c8STreehugger Robot     if (control->value)
90*7c3d14c8STreehugger Robot         memcpy(base, control->value, size);
91*7c3d14c8STreehugger Robot     else
92*7c3d14c8STreehugger Robot         memset(base, 0, size);
93*7c3d14c8STreehugger Robot     return base;
94*7c3d14c8STreehugger Robot }
95*7c3d14c8STreehugger Robot 
96*7c3d14c8STreehugger Robot static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
97*7c3d14c8STreehugger Robot 
98*7c3d14c8STreehugger Robot static size_t emutls_num_object = 0;  /* number of allocated TLS objects */
99*7c3d14c8STreehugger Robot 
100*7c3d14c8STreehugger Robot typedef struct emutls_address_array {
101*7c3d14c8STreehugger Robot     uintptr_t size;  /* number of elements in the 'data' array */
102*7c3d14c8STreehugger Robot     void* data[];
103*7c3d14c8STreehugger Robot } emutls_address_array;
104*7c3d14c8STreehugger Robot 
105*7c3d14c8STreehugger Robot static pthread_key_t emutls_pthread_key;
106*7c3d14c8STreehugger Robot 
emutls_key_destructor(void * ptr)107*7c3d14c8STreehugger Robot static void emutls_key_destructor(void* ptr) {
108*7c3d14c8STreehugger Robot     emutls_address_array* array = (emutls_address_array*)ptr;
109*7c3d14c8STreehugger Robot     uintptr_t i;
110*7c3d14c8STreehugger Robot     for (i = 0; i < array->size; ++i) {
111*7c3d14c8STreehugger Robot         if (array->data[i])
112*7c3d14c8STreehugger Robot             emutls_memalign_free(array->data[i]);
113*7c3d14c8STreehugger Robot     }
114*7c3d14c8STreehugger Robot     free(ptr);
115*7c3d14c8STreehugger Robot }
116*7c3d14c8STreehugger Robot 
emutls_init(void)117*7c3d14c8STreehugger Robot static void emutls_init(void) {
118*7c3d14c8STreehugger Robot     if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
119*7c3d14c8STreehugger Robot         abort();
120*7c3d14c8STreehugger Robot }
121*7c3d14c8STreehugger Robot 
122*7c3d14c8STreehugger Robot /* Returns control->object.index; set index if not allocated yet. */
emutls_get_index(__emutls_control * control)123*7c3d14c8STreehugger Robot static __inline uintptr_t emutls_get_index(__emutls_control *control) {
124*7c3d14c8STreehugger Robot     uintptr_t index = __atomic_load_n(&control->object.index, __ATOMIC_ACQUIRE);
125*7c3d14c8STreehugger Robot     if (!index) {
126*7c3d14c8STreehugger Robot         static pthread_once_t once = PTHREAD_ONCE_INIT;
127*7c3d14c8STreehugger Robot         pthread_once(&once, emutls_init);
128*7c3d14c8STreehugger Robot         pthread_mutex_lock(&emutls_mutex);
129*7c3d14c8STreehugger Robot         index = control->object.index;
130*7c3d14c8STreehugger Robot         if (!index) {
131*7c3d14c8STreehugger Robot             index = ++emutls_num_object;
132*7c3d14c8STreehugger Robot             __atomic_store_n(&control->object.index, index, __ATOMIC_RELEASE);
133*7c3d14c8STreehugger Robot         }
134*7c3d14c8STreehugger Robot         pthread_mutex_unlock(&emutls_mutex);
135*7c3d14c8STreehugger Robot     }
136*7c3d14c8STreehugger Robot     return index;
137*7c3d14c8STreehugger Robot }
138*7c3d14c8STreehugger Robot 
139*7c3d14c8STreehugger Robot /* Updates newly allocated thread local emutls_address_array. */
emutls_check_array_set_size(emutls_address_array * array,uintptr_t size)140*7c3d14c8STreehugger Robot static __inline void emutls_check_array_set_size(emutls_address_array *array,
141*7c3d14c8STreehugger Robot                                                  uintptr_t size) {
142*7c3d14c8STreehugger Robot     if (array == NULL)
143*7c3d14c8STreehugger Robot         abort();
144*7c3d14c8STreehugger Robot     array->size = size;
145*7c3d14c8STreehugger Robot     pthread_setspecific(emutls_pthread_key, (void*)array);
146*7c3d14c8STreehugger Robot }
147*7c3d14c8STreehugger Robot 
148*7c3d14c8STreehugger Robot /* Returns the new 'data' array size, number of elements,
149*7c3d14c8STreehugger Robot  * which must be no smaller than the given index.
150*7c3d14c8STreehugger Robot  */
emutls_new_data_array_size(uintptr_t index)151*7c3d14c8STreehugger Robot static __inline uintptr_t emutls_new_data_array_size(uintptr_t index) {
152*7c3d14c8STreehugger Robot    /* Need to allocate emutls_address_array with one extra slot
153*7c3d14c8STreehugger Robot     * to store the data array size.
154*7c3d14c8STreehugger Robot     * Round up the emutls_address_array size to multiple of 16.
155*7c3d14c8STreehugger Robot     */
156*7c3d14c8STreehugger Robot     return ((index + 1 + 15) & ~((uintptr_t)15)) - 1;
157*7c3d14c8STreehugger Robot }
158*7c3d14c8STreehugger Robot 
159*7c3d14c8STreehugger Robot /* Returns the thread local emutls_address_array.
160*7c3d14c8STreehugger Robot  * Extends its size if necessary to hold address at index.
161*7c3d14c8STreehugger Robot  */
162*7c3d14c8STreehugger Robot static __inline emutls_address_array *
emutls_get_address_array(uintptr_t index)163*7c3d14c8STreehugger Robot emutls_get_address_array(uintptr_t index) {
164*7c3d14c8STreehugger Robot     emutls_address_array* array = pthread_getspecific(emutls_pthread_key);
165*7c3d14c8STreehugger Robot     if (array == NULL) {
166*7c3d14c8STreehugger Robot         uintptr_t new_size = emutls_new_data_array_size(index);
167*7c3d14c8STreehugger Robot         array = malloc(new_size * sizeof(void *) + sizeof(emutls_address_array));
168*7c3d14c8STreehugger Robot         if (array)
169*7c3d14c8STreehugger Robot             memset(array->data, 0, new_size * sizeof(void*));
170*7c3d14c8STreehugger Robot         emutls_check_array_set_size(array, new_size);
171*7c3d14c8STreehugger Robot     } else if (index > array->size) {
172*7c3d14c8STreehugger Robot         uintptr_t orig_size = array->size;
173*7c3d14c8STreehugger Robot         uintptr_t new_size = emutls_new_data_array_size(index);
174*7c3d14c8STreehugger Robot         array = realloc(array, new_size * sizeof(void *) + sizeof(emutls_address_array));
175*7c3d14c8STreehugger Robot         if (array)
176*7c3d14c8STreehugger Robot             memset(array->data + orig_size, 0,
177*7c3d14c8STreehugger Robot                    (new_size - orig_size) * sizeof(void*));
178*7c3d14c8STreehugger Robot         emutls_check_array_set_size(array, new_size);
179*7c3d14c8STreehugger Robot     }
180*7c3d14c8STreehugger Robot     return array;
181*7c3d14c8STreehugger Robot }
182*7c3d14c8STreehugger Robot 
__emutls_get_address(__emutls_control * control)183*7c3d14c8STreehugger Robot void* __emutls_get_address(__emutls_control* control) {
184*7c3d14c8STreehugger Robot     uintptr_t index = emutls_get_index(control);
185*7c3d14c8STreehugger Robot     emutls_address_array* array = emutls_get_address_array(index);
186*7c3d14c8STreehugger Robot     if (array->data[index - 1] == NULL)
187*7c3d14c8STreehugger Robot         array->data[index - 1] = emutls_allocate_object(control);
188*7c3d14c8STreehugger Robot     return array->data[index - 1];
189*7c3d14c8STreehugger Robot }
190