blob: b03f0cc872e4098d9690be7bb617a9c120e87ff5 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker2466d932013-09-28 14:40:38 +02006 */
7
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +01008/*
Hanno Becker48a816f2018-09-05 15:22:22 +01009 * Ensure gmtime_r is available even with -std=c99; must be defined before
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010010 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
11 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010012#if !defined(_POSIX_C_SOURCE)
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010013#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010014#endif
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010015
Gilles Peskinedb09ef62020-06-03 01:43:33 +020016#include "common.h"
Paul Bakker2466d932013-09-28 14:40:38 +020017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020019
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020021
Hanno Becker6a739782018-09-05 15:06:19 +010022#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Becker323d8012018-09-06 11:30:57 +010023
Hanno Beckercfeb70c2018-09-05 13:50:22 +010024#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +010025 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
26 defined(__MACH__)))
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010027#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +010028#endif /* !_WIN32 && (unix || __unix || __unix__ ||
29 * (__APPLE__ && __MACH__)) */
30
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031#if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
32 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
33 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010034/*
35 * This is a convenience shorthand macro to avoid checking the long
36 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +010037 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010038 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +010039 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010040 */
Hanno Beckerf5106d52018-09-06 12:09:56 +010041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042#if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
Andres Amaya Garciad7177432018-08-08 09:41:17 +010043#define THREADING_USE_GMTIME
Hanno Beckerf5106d52018-09-06 12:09:56 +010044#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
45
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
47 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Chris Jonesd4603232020-11-12 17:10:36 +000048 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Hanno Becker323d8012018-09-06 11:30:57 +010049
Hanno Becker6a739782018-09-05 15:06:19 +010050#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_THREADING_PTHREAD)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020054{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 if (mutex == NULL) {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020056 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 }
Paul Bakker2466d932013-09-28 14:40:38 +020058
Gilles Peskine39a1a262021-02-09 15:35:29 +010059 /* A nonzero value of is_valid indicates a successfully initialized
60 * mutex. This is a workaround for not being able to return an error
61 * code for this function. The lock/unlock functions return an error
62 * if is_valid is nonzero. The Mbed TLS unit test code uses this field
63 * to distinguish more states of the mutex; see
64 * tests/src/threading_helpers for details. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020066}
67
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020069{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 if (mutex == NULL || !mutex->is_valid) {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020071 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 }
Paul Bakker2466d932013-09-28 14:40:38 +020073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 (void) pthread_mutex_destroy(&mutex->mutex);
Janos Follath5437a752016-09-26 09:15:44 +010075 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020076}
77
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020079{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (mutex == NULL || !mutex->is_valid) {
81 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
82 }
Paul Bakker2466d932013-09-28 14:40:38 +020083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 if (pthread_mutex_lock(&mutex->mutex) != 0) {
85 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
86 }
Paul Bakker2466d932013-09-28 14:40:38 +020087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +020089}
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020092{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if (mutex == NULL || !mutex->is_valid) {
94 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
95 }
Paul Bakker2466d932013-09-28 14:40:38 +020096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 if (pthread_mutex_unlock(&mutex->mutex) != 0) {
98 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
99 }
Paul Bakker2466d932013-09-28 14:40:38 +0200100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200102}
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
105void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
106int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
107int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200108
109/*
Artur Allmanne25dc1c2022-03-21 16:11:35 +0200110 * With pthreads we can statically initialize mutexes
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200111 */
112#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_THREADING_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
Paul Bakkerc78c8422013-12-31 11:55:27 +0100118{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 ((void) mutex);
120 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Paul Bakkerc78c8422013-12-31 11:55:27 +0100121}
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200123{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 ((void) mutex);
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200125 return;
126}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
129void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
130int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
131int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200132
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200133/*
134 * Set functions pointers and initialize global mutexes
135 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
137 void (*mutex_free)(mbedtls_threading_mutex_t *),
138 int (*mutex_lock)(mbedtls_threading_mutex_t *),
139 int (*mutex_unlock)(mbedtls_threading_mutex_t *))
Paul Bakker2466d932013-09-28 14:40:38 +0200140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_mutex_init = mutex_init;
142 mbedtls_mutex_free = mutex_free;
143 mbedtls_mutex_lock = mutex_lock;
144 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200145
Gergely Budai13f7fb32017-08-23 14:23:58 +0200146#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
Gergely Budai13f7fb32017-08-23 14:23:58 +0200148#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100149#if defined(THREADING_USE_GMTIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100151#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200152}
153
154/*
155 * Free global mutexes
156 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157void mbedtls_threading_free_alt(void)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200158{
Gergely Budai13f7fb32017-08-23 14:23:58 +0200159#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
Gergely Budai13f7fb32017-08-23 14:23:58 +0200161#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100162#if defined(THREADING_USE_GMTIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100164#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200167
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200168/*
169 * Define global mutexes
170 */
171#ifndef MUTEX_INIT
172#define MUTEX_INIT
173#endif
Gergely Budai13f7fb32017-08-23 14:23:58 +0200174#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200175mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai13f7fb32017-08-23 14:23:58 +0200176#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100177#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100178mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
179#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_THREADING_C */