1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "internal.h"
16 
17 #if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_NO_THREADS)
18 
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include <openssl/mem.h>
24 #include <openssl/type_check.h>
25 
26 
27 OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
28                        CRYPTO_MUTEX_too_small);
29 
CRYPTO_MUTEX_init(CRYPTO_MUTEX * lock)30 void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
31   if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
32     abort();
33   }
34 }
35 
CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX * lock)36 void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
37   if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
38     abort();
39   }
40 }
41 
CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX * lock)42 void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
43   if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
44     abort();
45   }
46 }
47 
CRYPTO_MUTEX_unlock(CRYPTO_MUTEX * lock)48 void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock) {
49   if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
50     abort();
51   }
52 }
53 
CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX * lock)54 void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
55   pthread_rwlock_destroy((pthread_rwlock_t *) lock);
56 }
57 
CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX * lock)58 void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
59   if (pthread_rwlock_rdlock(&lock->lock) != 0) {
60     abort();
61   }
62 }
63 
CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX * lock)64 void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
65   if (pthread_rwlock_wrlock(&lock->lock) != 0) {
66     abort();
67   }
68 }
69 
CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX * lock)70 void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock) {
71   if (pthread_rwlock_unlock(&lock->lock) != 0) {
72     abort();
73   }
74 }
75 
CRYPTO_once(CRYPTO_once_t * once,void (* init)(void))76 void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
77   pthread_once(once, init);
78 }
79 
80 static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
81 static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
82 
thread_local_destructor(void * arg)83 static void thread_local_destructor(void *arg) {
84   if (arg == NULL) {
85     return;
86   }
87 
88   thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
89   if (pthread_mutex_lock(&g_destructors_lock) != 0) {
90     return;
91   }
92   memcpy(destructors, g_destructors, sizeof(destructors));
93   pthread_mutex_unlock(&g_destructors_lock);
94 
95   unsigned i;
96   void **pointers = arg;
97   for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
98     if (destructors[i] != NULL) {
99       destructors[i](pointers[i]);
100     }
101   }
102 
103   OPENSSL_free(pointers);
104 }
105 
106 static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
107 static pthread_key_t g_thread_local_key;
108 static int g_thread_local_failed = 0;
109 
thread_local_init(void)110 static void thread_local_init(void) {
111   g_thread_local_failed =
112       pthread_key_create(&g_thread_local_key, thread_local_destructor) != 0;
113 }
114 
CRYPTO_get_thread_local(thread_local_data_t index)115 void *CRYPTO_get_thread_local(thread_local_data_t index) {
116   CRYPTO_once(&g_thread_local_init_once, thread_local_init);
117   if (g_thread_local_failed) {
118     return NULL;
119   }
120 
121   void **pointers = pthread_getspecific(g_thread_local_key);
122   if (pointers == NULL) {
123     return NULL;
124   }
125   return pointers[index];
126 }
127 
CRYPTO_set_thread_local(thread_local_data_t index,void * value,thread_local_destructor_t destructor)128 int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
129                             thread_local_destructor_t destructor) {
130   CRYPTO_once(&g_thread_local_init_once, thread_local_init);
131   if (g_thread_local_failed) {
132     destructor(value);
133     return 0;
134   }
135 
136   void **pointers = pthread_getspecific(g_thread_local_key);
137   if (pointers == NULL) {
138     pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
139     if (pointers == NULL) {
140       destructor(value);
141       return 0;
142     }
143     memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
144     if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
145       OPENSSL_free(pointers);
146       destructor(value);
147       return 0;
148     }
149   }
150 
151   if (pthread_mutex_lock(&g_destructors_lock) != 0) {
152     destructor(value);
153     return 0;
154   }
155   g_destructors[index] = destructor;
156   pthread_mutex_unlock(&g_destructors_lock);
157 
158   pointers[index] = value;
159   return 1;
160 }
161 
162 #endif  /* !OPENSSL_WINDOWS && !OPENSSL_NO_THREADS */
163