blob: 00dacba969f405b0bf4e72a3c4d52c3d648e6d35 [file] [log] [blame]
Elliott Hughes44b53ad2013-02-11 20:18:47 +00001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes6f94de32013-02-12 06:06:22 +000029#include <assert.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <limits.h>
33#include <malloc.h>
34#include <memory.h>
Elliott Hughes44b53ad2013-02-11 20:18:47 +000035#include <pthread.h>
Elliott Hughes6f94de32013-02-12 06:06:22 +000036#include <signal.h>
37#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <sys/atomics.h>
41#include <sys/mman.h>
42#include <sys/prctl.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <time.h>
46#include <unistd.h>
Elliott Hughes44b53ad2013-02-11 20:18:47 +000047
Elliott Hughes6f94de32013-02-12 06:06:22 +000048#include "bionic_atomic_inline.h"
49#include "bionic_futex.h"
50#include "bionic_pthread.h"
51#include "bionic_ssp.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000052#include "bionic_tls.h"
Elliott Hughes6f94de32013-02-12 06:06:22 +000053#include "debug_format.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000054#include "pthread_internal.h"
Elliott Hughes6f94de32013-02-12 06:06:22 +000055#include "thread_private.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000056
57/* A technical note regarding our thread-local-storage (TLS) implementation:
58 *
59 * There can be up to BIONIC_TLS_SLOTS independent TLS keys in a given process,
60 * The keys below TLS_SLOT_FIRST_USER_SLOT are reserved for Bionic to hold
61 * special thread-specific variables like errno or a pointer to
62 * the current thread's descriptor. These entries cannot be accessed through
63 * pthread_getspecific() / pthread_setspecific() or pthread_key_delete()
64 *
65 * The 'tls_map_t' type defined below implements a shared global map of
66 * currently created/allocated TLS keys and the destructors associated
67 * with them.
68 *
69 * The global TLS map simply contains a bitmap of allocated keys, and
70 * an array of destructors.
71 *
72 * Each thread has a TLS area that is a simple array of BIONIC_TLS_SLOTS void*
73 * pointers. the TLS area of the main thread is stack-allocated in
74 * __libc_init_common, while the TLS area of other threads is placed at
75 * the top of their stack in pthread_create.
76 *
77 * When pthread_key_delete() is called it will erase the key's bitmap bit
78 * and its destructor, and will also clear the key data in the TLS area of
79 * all created threads. As mandated by Posix, it is the responsibility of
80 * the caller of pthread_key_delete() to properly reclaim the objects that
81 * were pointed to by these data fields (either before or after the call).
82 */
83
84#define TLSMAP_BITS 32
85#define TLSMAP_WORDS ((BIONIC_TLS_SLOTS+TLSMAP_BITS-1)/TLSMAP_BITS)
86#define TLSMAP_WORD(m,k) (m).map[(k)/TLSMAP_BITS]
87#define TLSMAP_MASK(k) (1U << ((k)&(TLSMAP_BITS-1)))
88
89static inline bool IsValidUserKey(pthread_key_t key) {
90 return (key >= TLS_SLOT_FIRST_USER_SLOT && key < BIONIC_TLS_SLOTS);
91}
92
93typedef void (*key_destructor_t)(void*);
94
95struct tls_map_t {
96 bool is_initialized;
97
98 /* bitmap of allocated keys */
99 uint32_t map[TLSMAP_WORDS];
100
101 key_destructor_t key_destructors[BIONIC_TLS_SLOTS];
102};
103
104class ScopedTlsMapAccess {
105 public:
106 ScopedTlsMapAccess() {
107 Lock();
108
109 // If this is the first time the TLS map has been accessed,
110 // mark the slots belonging to well-known keys as being in use.
111 // This isn't currently necessary because the well-known keys
112 // can only be accessed directly by bionic itself, do not have
113 // destructors, and all the functions that touch the TLS map
114 // start after the maximum well-known slot.
115 if (!s_tls_map_.is_initialized) {
116 for (pthread_key_t key = 0; key < TLS_SLOT_FIRST_USER_SLOT; ++key) {
117 SetInUse(key, NULL);
118 }
119 s_tls_map_.is_initialized = true;
120 }
121 }
122
123 ~ScopedTlsMapAccess() {
124 Unlock();
125 }
126
127 int CreateKey(pthread_key_t* result, void (*key_destructor)(void*)) {
128 // Take the first unallocated key.
129 for (int key = 0; key < BIONIC_TLS_SLOTS; ++key) {
130 if (!IsInUse(key)) {
131 SetInUse(key, key_destructor);
132 *result = key;
133 return 0;
134 }
135 }
136
137 // We hit PTHREAD_KEYS_MAX. POSIX says EAGAIN for this case.
138 return EAGAIN;
139 }
140
141 void DeleteKey(pthread_key_t key) {
142 TLSMAP_WORD(s_tls_map_, key) &= ~TLSMAP_MASK(key);
143 s_tls_map_.key_destructors[key] = NULL;
144 }
145
146 bool IsInUse(pthread_key_t key) {
147 return (TLSMAP_WORD(s_tls_map_, key) & TLSMAP_MASK(key)) != 0;
148 }
149
150 void SetInUse(pthread_key_t key, void (*key_destructor)(void*)) {
151 TLSMAP_WORD(s_tls_map_, key) |= TLSMAP_MASK(key);
152 s_tls_map_.key_destructors[key] = key_destructor;
153 }
154
155 // Called from pthread_exit() to remove all TLS key data
156 // from this thread's TLS area. This must call the destructor of all keys
157 // that have a non-NULL data value and a non-NULL destructor.
158 void CleanAll() {
159 void** tls = (void**)__get_tls();
160
161 // Because destructors can do funky things like deleting/creating other
162 // keys, we need to implement this in a loop.
163 for (int rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; --rounds) {
164 size_t called_destructor_count = 0;
165 for (int key = 0; key < BIONIC_TLS_SLOTS; ++key) {
166 if (IsInUse(key)) {
167 void* data = tls[key];
168 void (*key_destructor)(void*) = s_tls_map_.key_destructors[key];
169
170 if (data != NULL && key_destructor != NULL) {
171 // we need to clear the key data now, this will prevent the
172 // destructor (or a later one) from seeing the old value if
173 // it calls pthread_getspecific() for some odd reason
174
175 // we do not do this if 'key_destructor == NULL' just in case another
176 // destructor function might be responsible for manually
177 // releasing the corresponding data.
178 tls[key] = NULL;
179
180 // because the destructor is free to call pthread_key_create
181 // and/or pthread_key_delete, we need to temporarily unlock
182 // the TLS map
183 Unlock();
184 (*key_destructor)(data);
185 Lock();
186 ++called_destructor_count;
187 }
188 }
189 }
190
191 // If we didn't call any destructors, there is no need to check the TLS data again.
192 if (called_destructor_count == 0) {
193 break;
194 }
195 }
196 }
197
198 private:
199 static tls_map_t s_tls_map_;
200 static pthread_mutex_t s_tls_map_lock_;
201
202 void Lock() {
203 pthread_mutex_lock(&s_tls_map_lock_);
204 }
205
206 void Unlock() {
207 pthread_mutex_unlock(&s_tls_map_lock_);
208 }
209};
210
211tls_map_t ScopedTlsMapAccess::s_tls_map_;
212pthread_mutex_t ScopedTlsMapAccess::s_tls_map_lock_;
213
214__LIBC_HIDDEN__ void pthread_key_clean_all() {
215 ScopedTlsMapAccess tls_map;
216 tls_map.CleanAll();
217}
218
219int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {
220 ScopedTlsMapAccess tls_map;
221 return tls_map.CreateKey(key, key_destructor);
222}
223
224// Deletes a pthread_key_t. note that the standard mandates that this does
225// not call the destructors for non-NULL key values. Instead, it is the
226// responsibility of the caller to properly dispose of the corresponding data
227// and resources, using any means it finds suitable.
228int pthread_key_delete(pthread_key_t key) {
229 ScopedTlsMapAccess tls_map;
230
231 if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
232 return EINVAL;
233 }
234
235 // Clear value in all threads.
236 pthread_mutex_lock(&gThreadListLock);
237 for (pthread_internal_t* t = gThreadList; t != NULL; t = t->next) {
238 // Avoid zombie threads with a negative 'join_count'. These are really
239 // already dead and don't have a TLS area anymore.
240
241 // Similarly, it is possible to have t->tls == NULL for threads that
242 // were just recently created through pthread_create() but whose
243 // startup trampoline (__thread_entry) hasn't been run yet by the
244 // scheduler. t->tls will also be NULL after it's stack has been
245 // unmapped but before the ongoing pthread_join() is finished.
246 // so check for this too.
247 if (t->join_count < 0 || !t->tls) {
248 continue;
249 }
250
251 t->tls[key] = NULL;
252 }
253 tls_map.DeleteKey(key);
254
255 pthread_mutex_unlock(&gThreadListLock);
256 return 0;
257}
258
259void* pthread_getspecific(pthread_key_t key) {
260 if (!IsValidUserKey(key)) {
261 return NULL;
262 }
263
264 // For performance reasons, we do not lock/unlock the global TLS map
265 // to check that the key is properly allocated. If the key was not
266 // allocated, the value read from the TLS should always be NULL
267 // due to pthread_key_delete() clearing the values for all threads.
268 return (void *)(((unsigned *)__get_tls())[key]);
269}
270
271int pthread_setspecific(pthread_key_t key, const void* ptr) {
272 ScopedTlsMapAccess tls_map;
273
274 if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
275 return EINVAL;
276 }
277
278 ((uint32_t *)__get_tls())[key] = (uint32_t)ptr;
279 return 0;
280}