thread_pthread.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include "internal.h"
  15. #if defined(OPENSSL_PTHREADS)
  16. #include <pthread.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/type_check.h>
  21. OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
  22. "CRYPTO_MUTEX is too small");
  23. void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
  24. if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
  25. abort();
  26. }
  27. }
  28. void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
  29. if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
  30. abort();
  31. }
  32. }
  33. void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
  34. if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
  35. abort();
  36. }
  37. }
  38. void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
  39. if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
  40. abort();
  41. }
  42. }
  43. void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
  44. if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
  45. abort();
  46. }
  47. }
  48. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
  49. pthread_rwlock_destroy((pthread_rwlock_t *) lock);
  50. }
  51. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  52. if (pthread_rwlock_rdlock(&lock->lock) != 0) {
  53. abort();
  54. }
  55. }
  56. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  57. if (pthread_rwlock_wrlock(&lock->lock) != 0) {
  58. abort();
  59. }
  60. }
  61. void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  62. if (pthread_rwlock_unlock(&lock->lock) != 0) {
  63. abort();
  64. }
  65. }
  66. void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  67. if (pthread_rwlock_unlock(&lock->lock) != 0) {
  68. abort();
  69. }
  70. }
  71. void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
  72. if (pthread_once(once, init) != 0) {
  73. abort();
  74. }
  75. }
  76. static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
  77. static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
  78. // thread_local_destructor is called when a thread exits. It releases thread
  79. // local data for that thread only.
  80. static void thread_local_destructor(void *arg) {
  81. if (arg == NULL) {
  82. return;
  83. }
  84. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  85. if (pthread_mutex_lock(&g_destructors_lock) != 0) {
  86. return;
  87. }
  88. OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
  89. pthread_mutex_unlock(&g_destructors_lock);
  90. unsigned i;
  91. void **pointers = arg;
  92. for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  93. if (destructors[i] != NULL) {
  94. destructors[i](pointers[i]);
  95. }
  96. }
  97. OPENSSL_free(pointers);
  98. }
  99. static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
  100. static pthread_key_t g_thread_local_key;
  101. static int g_thread_local_key_created = 0;
  102. // OPENSSL_DANGEROUS_RELEASE_PTHREAD_KEY can be defined to cause
  103. // |pthread_key_delete| to be called in a destructor function. This can be
  104. // useful for programs that dlclose BoringSSL.
  105. //
  106. // Note that dlclose()ing BoringSSL is not supported and will leak memory:
  107. // thread-local values will be leaked as well as anything initialised via a
  108. // once. The |pthread_key_t| is destroyed because they run out very quickly,
  109. // while the other leaks are slow, and this allows code that happens to use
  110. // dlclose() despite all the problems to continue functioning.
  111. //
  112. // This is marked "dangerous" because it can cause multi-threaded processes to
  113. // crash (even if they don't use dlclose): if the destructor runs while other
  114. // threads are still executing then they may end up using an invalid key to
  115. // access thread-local variables.
  116. //
  117. // This may be removed after February 2020.
  118. #if defined(OPENSSL_DANGEROUS_RELEASE_PTHREAD_KEY) && \
  119. (defined(__GNUC__) || defined(__clang__))
  120. // thread_key_destructor is called when the library is unloaded with dlclose.
  121. static void thread_key_destructor(void) __attribute__((destructor, unused));
  122. static void thread_key_destructor(void) {
  123. if (g_thread_local_key_created) {
  124. g_thread_local_key_created = 0;
  125. pthread_key_delete(g_thread_local_key);
  126. }
  127. }
  128. #endif
  129. static void thread_local_init(void) {
  130. g_thread_local_key_created =
  131. pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
  132. }
  133. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  134. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  135. if (!g_thread_local_key_created) {
  136. return NULL;
  137. }
  138. void **pointers = pthread_getspecific(g_thread_local_key);
  139. if (pointers == NULL) {
  140. return NULL;
  141. }
  142. return pointers[index];
  143. }
  144. int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
  145. thread_local_destructor_t destructor) {
  146. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  147. if (!g_thread_local_key_created) {
  148. destructor(value);
  149. return 0;
  150. }
  151. void **pointers = pthread_getspecific(g_thread_local_key);
  152. if (pointers == NULL) {
  153. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  154. if (pointers == NULL) {
  155. destructor(value);
  156. return 0;
  157. }
  158. OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  159. if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
  160. OPENSSL_free(pointers);
  161. destructor(value);
  162. return 0;
  163. }
  164. }
  165. if (pthread_mutex_lock(&g_destructors_lock) != 0) {
  166. destructor(value);
  167. return 0;
  168. }
  169. g_destructors[index] = destructor;
  170. pthread_mutex_unlock(&g_destructors_lock);
  171. pointers[index] = value;
  172. return 1;
  173. }
  174. #endif // OPENSSL_PTHREADS