threading.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Threading abstraction layer
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * Ensure gmtime_r is available even with -std=c99; must be defined before
  9. * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms.
  10. */
  11. #if !defined(_POSIX_C_SOURCE)
  12. #define _POSIX_C_SOURCE 200112L
  13. #endif
  14. #include "common.h"
  15. #if defined(MBEDTLS_THREADING_C)
  16. #include "mbedtls/threading.h"
  17. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  18. #if !defined(_WIN32) && (defined(unix) || \
  19. defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
  20. defined(__MACH__)))
  21. #include <unistd.h>
  22. #endif /* !_WIN32 && (unix || __unix || __unix__ ||
  23. * (__APPLE__ && __MACH__)) */
  24. #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
  25. (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
  26. _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
  27. /*
  28. * This is a convenience shorthand macro to avoid checking the long
  29. * preprocessor conditions above. Ideally, we could expose this macro in
  30. * platform_util.h and simply use it in platform_util.c, threading.c and
  31. * threading.h. However, this macro is not part of the Mbed TLS public API, so
  32. * we keep it private by only defining it in this file
  33. */
  34. #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
  35. #define THREADING_USE_GMTIME
  36. #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
  37. #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
  38. ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
  39. _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
  40. #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
  41. #if defined(MBEDTLS_THREADING_PTHREAD)
  42. static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
  43. {
  44. if (mutex == NULL) {
  45. return;
  46. }
  47. /* One problem here is that calling lock on a pthread mutex without first
  48. * having initialised it is undefined behaviour. Obviously we cannot check
  49. * this here in a thread safe manner without a significant performance
  50. * hit, so state transitions are checked in tests only via the state
  51. * variable. Please make sure any new mutex that gets added is exercised in
  52. * tests; see tests/src/threading_helpers.c for more details. */
  53. (void) pthread_mutex_init(&mutex->mutex, NULL);
  54. }
  55. static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
  56. {
  57. if (mutex == NULL) {
  58. return;
  59. }
  60. (void) pthread_mutex_destroy(&mutex->mutex);
  61. }
  62. static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
  63. {
  64. if (mutex == NULL) {
  65. return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
  66. }
  67. if (pthread_mutex_lock(&mutex->mutex) != 0) {
  68. return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  69. }
  70. return 0;
  71. }
  72. static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
  73. {
  74. if (mutex == NULL) {
  75. return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
  76. }
  77. if (pthread_mutex_unlock(&mutex->mutex) != 0) {
  78. return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  79. }
  80. return 0;
  81. }
  82. void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
  83. void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
  84. int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
  85. int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
  86. /*
  87. * With pthreads we can statically initialize mutexes
  88. */
  89. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  90. #endif /* MBEDTLS_THREADING_PTHREAD */
  91. #if defined(MBEDTLS_THREADING_ALT)
  92. static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
  93. {
  94. ((void) mutex);
  95. return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
  96. }
  97. static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
  98. {
  99. ((void) mutex);
  100. return;
  101. }
  102. void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
  103. void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
  104. int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
  105. int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
  106. /*
  107. * Set functions pointers and initialize global mutexes
  108. */
  109. void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
  110. void (*mutex_free)(mbedtls_threading_mutex_t *),
  111. int (*mutex_lock)(mbedtls_threading_mutex_t *),
  112. int (*mutex_unlock)(mbedtls_threading_mutex_t *))
  113. {
  114. mbedtls_mutex_init = mutex_init;
  115. mbedtls_mutex_free = mutex_free;
  116. mbedtls_mutex_lock = mutex_lock;
  117. mbedtls_mutex_unlock = mutex_unlock;
  118. #if defined(MBEDTLS_FS_IO)
  119. mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
  120. #endif
  121. #if defined(THREADING_USE_GMTIME)
  122. mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
  123. #endif
  124. #if defined(MBEDTLS_PSA_CRYPTO_C)
  125. mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex);
  126. mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex);
  127. mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex);
  128. #endif
  129. }
  130. /*
  131. * Free global mutexes
  132. */
  133. void mbedtls_threading_free_alt(void)
  134. {
  135. #if defined(MBEDTLS_FS_IO)
  136. mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
  137. #endif
  138. #if defined(THREADING_USE_GMTIME)
  139. mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
  140. #endif
  141. #if defined(MBEDTLS_PSA_CRYPTO_C)
  142. mbedtls_mutex_free(&mbedtls_threading_key_slot_mutex);
  143. mbedtls_mutex_free(&mbedtls_threading_psa_globaldata_mutex);
  144. mbedtls_mutex_free(&mbedtls_threading_psa_rngdata_mutex);
  145. #endif
  146. }
  147. #endif /* MBEDTLS_THREADING_ALT */
  148. /*
  149. * Define global mutexes
  150. */
  151. #ifndef MUTEX_INIT
  152. #define MUTEX_INIT
  153. #endif
  154. #if defined(MBEDTLS_FS_IO)
  155. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  156. #endif
  157. #if defined(THREADING_USE_GMTIME)
  158. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  159. #endif
  160. #if defined(MBEDTLS_PSA_CRYPTO_C)
  161. mbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex MUTEX_INIT;
  162. mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex MUTEX_INIT;
  163. mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex MUTEX_INIT;
  164. #endif
  165. #endif /* MBEDTLS_THREADING_C */