blob: ba577776ce2aa28a24ab28b417eafff1ff724f0b [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker2466d932013-09-28 14:40:38 +020018 */
19
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010020/*
Hanno Becker48a816f2018-09-05 15:22:22 +010021 * Ensure gmtime_r is available even with -std=c99; must be defined before
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020022 * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other
23 * platforms.
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010024 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010025#if !defined(_POSIX_C_SOURCE)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020026# define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010027#endif
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010028
Gilles Peskinedb09ef62020-06-03 01:43:33 +020029#include "common.h"
Paul Bakker2466d932013-09-28 14:40:38 +020030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033# include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020034
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# if defined(MBEDTLS_HAVE_TIME_DATE) && \
36 !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Becker323d8012018-09-06 11:30:57 +010037
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038# if !defined(_WIN32) && \
39 (defined(unix) || defined(__unix) || defined(__unix__) || \
40 (defined(__APPLE__) && defined(__MACH__)))
41# include <unistd.h>
42# endif /* !_WIN32 && (unix || __unix || __unix__ || \
43 * (__APPLE__ && __MACH__)) */
Hanno Becker323d8012018-09-06 11:30:57 +010044
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020045# if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
46 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
47 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010048/*
49 * This is a convenience shorthand macro to avoid checking the long
50 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +010051 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010052 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +010053 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010054 */
Hanno Beckerf5106d52018-09-06 12:09:56 +010055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
57# define THREADING_USE_GMTIME
58# endif /* ! ( defined(_WIN32) && !defined(EFIX64) && \
59 !defined(EFI32) ) */
Hanno Beckerf5106d52018-09-06 12:09:56 +010060
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061# endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) \
62 || ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
63 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Hanno Becker323d8012018-09-06 11:30:57 +010064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065# endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010066
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067# if defined(MBEDTLS_THREADING_PTHREAD)
68static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020069{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 if (mutex == NULL)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020071 return;
Paul Bakker2466d932013-09-28 14:40:38 +020072
Gilles Peskine39a1a262021-02-09 15:35:29 +010073 /* A nonzero value of is_valid indicates a successfully initialized
74 * mutex. This is a workaround for not being able to return an error
75 * code for this function. The lock/unlock functions return an error
76 * if is_valid is nonzero. The Mbed TLS unit test code uses this field
77 * to distinguish more states of the mutex; see
78 * tests/src/threading_helpers for details. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079 mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020080}
81
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020083{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084 if (mutex == NULL || !mutex->is_valid)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020085 return;
Paul Bakker2466d932013-09-28 14:40:38 +020086
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020087 (void)pthread_mutex_destroy(&mutex->mutex);
Janos Follath5437a752016-09-26 09:15:44 +010088 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020089}
90
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020092{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093 if (mutex == NULL || !mutex->is_valid)
94 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Paul Bakker2466d932013-09-28 14:40:38 +020095
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 if (pthread_mutex_lock(&mutex->mutex) != 0)
97 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Paul Bakker2466d932013-09-28 14:40:38 +020098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200100}
101
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +0200103{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104 if (mutex == NULL || !mutex->is_valid)
105 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Paul Bakker2466d932013-09-28 14:40:38 +0200106
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107 if (pthread_mutex_unlock(&mutex->mutex) != 0)
108 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Paul Bakker2466d932013-09-28 14:40:38 +0200109
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200110 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200111}
112
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200113void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) =
114 threading_mutex_init_pthread;
115void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) =
116 threading_mutex_free_pthread;
117int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) =
118 threading_mutex_lock_pthread;
119int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) =
120 threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200121
122/*
123 * With phtreads we can statically initialize mutexes
124 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125# define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127# endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200128
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129# if defined(MBEDTLS_THREADING_ALT)
130static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
Paul Bakkerc78c8422013-12-31 11:55:27 +0100131{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200132 ((void)mutex);
133 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Paul Bakkerc78c8422013-12-31 11:55:27 +0100134}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200136{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200137 ((void)mutex);
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200138 return;
139}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100140
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
142void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
143int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
144int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200145
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200146/*
147 * Set functions pointers and initialize global mutexes
148 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
150 void (*mutex_free)(mbedtls_threading_mutex_t *),
151 int (*mutex_lock)(mbedtls_threading_mutex_t *),
152 int (*mutex_unlock)(mbedtls_threading_mutex_t *))
Paul Bakker2466d932013-09-28 14:40:38 +0200153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_mutex_init = mutex_init;
155 mbedtls_mutex_free = mutex_free;
156 mbedtls_mutex_lock = mutex_lock;
157 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200158
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159# if defined(MBEDTLS_FS_IO)
160 mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
161# endif
162# if defined(THREADING_USE_GMTIME)
163 mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
164# endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200165}
166
167/*
168 * Free global mutexes
169 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170void mbedtls_threading_free_alt(void)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200171{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172# if defined(MBEDTLS_FS_IO)
173 mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
174# endif
175# if defined(THREADING_USE_GMTIME)
176 mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
177# endif
Paul Bakker2466d932013-09-28 14:40:38 +0200178}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179# endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200180
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200181/*
182 * Define global mutexes
183 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200184# ifndef MUTEX_INIT
185# define MUTEX_INIT
186# endif
187# if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200188mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200189# endif
190# if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100191mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200192# endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* MBEDTLS_THREADING_C */