blob: 0542f33f1aeccf1a927fcd88f62aa0d0a6bcf498 [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
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010022 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
23 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010024#if !defined(_POSIX_C_SOURCE)
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010025#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010026#endif
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010027
Gilles Peskinedb09ef62020-06-03 01:43:33 +020028#include "common.h"
Paul Bakker2466d932013-09-28 14:40:38 +020029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020033
Hanno Becker6a739782018-09-05 15:06:19 +010034#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Becker323d8012018-09-06 11:30:57 +010035
Hanno Beckercfeb70c2018-09-05 13:50:22 +010036#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +010037 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
38 defined(__MACH__)))
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010039#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +010040#endif /* !_WIN32 && (unix || __unix || __unix__ ||
41 * (__APPLE__ && __MACH__)) */
42
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043#if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
44 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
45 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010046/*
47 * This is a convenience shorthand macro to avoid checking the long
48 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +010049 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010050 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +010051 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010052 */
Hanno Beckerf5106d52018-09-06 12:09:56 +010053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054#if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
Andres Amaya Garciad7177432018-08-08 09:41:17 +010055#define THREADING_USE_GMTIME
Hanno Beckerf5106d52018-09-06 12:09:56 +010056#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
57
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
59 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Chris Jonesd4603232020-11-12 17:10:36 +000060 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Hanno Becker323d8012018-09-06 11:30:57 +010061
Hanno Becker6a739782018-09-05 15:06:19 +010062#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_THREADING_PTHREAD)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020066{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if (mutex == NULL) {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020068 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 }
Paul Bakker2466d932013-09-28 14:40:38 +020070
Gilles Peskine39a1a262021-02-09 15:35:29 +010071 /* A nonzero value of is_valid indicates a successfully initialized
72 * mutex. This is a workaround for not being able to return an error
73 * code for this function. The lock/unlock functions return an error
74 * if is_valid is nonzero. The Mbed TLS unit test code uses this field
75 * to distinguish more states of the mutex; see
76 * tests/src/threading_helpers for details. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020078}
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020081{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 if (mutex == NULL || !mutex->is_valid) {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020083 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 }
Paul Bakker2466d932013-09-28 14:40:38 +020085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 (void) pthread_mutex_destroy(&mutex->mutex);
Janos Follath5437a752016-09-26 09:15:44 +010087 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020088}
89
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020091{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 if (mutex == NULL || !mutex->is_valid) {
93 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
94 }
Paul Bakker2466d932013-09-28 14:40:38 +020095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 if (pthread_mutex_lock(&mutex->mutex) != 0) {
97 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
98 }
Paul Bakker2466d932013-09-28 14:40:38 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200101}
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +0200104{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if (mutex == NULL || !mutex->is_valid) {
106 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
107 }
Paul Bakker2466d932013-09-28 14:40:38 +0200108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 if (pthread_mutex_unlock(&mutex->mutex) != 0) {
110 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
111 }
Paul Bakker2466d932013-09-28 14:40:38 +0200112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200114}
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
117void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
118int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
119int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200120
121/*
Artur Allmanne25dc1c2022-03-21 16:11:35 +0200122 * With pthreads we can statically initialize mutexes
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200123 */
124#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_THREADING_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
Paul Bakkerc78c8422013-12-31 11:55:27 +0100130{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 ((void) mutex);
132 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Paul Bakkerc78c8422013-12-31 11:55:27 +0100133}
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200135{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 ((void) mutex);
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200137 return;
138}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
141void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
142int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
143int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200144
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200145/*
146 * Set functions pointers and initialize global mutexes
147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
149 void (*mutex_free)(mbedtls_threading_mutex_t *),
150 int (*mutex_lock)(mbedtls_threading_mutex_t *),
151 int (*mutex_unlock)(mbedtls_threading_mutex_t *))
Paul Bakker2466d932013-09-28 14:40:38 +0200152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_mutex_init = mutex_init;
154 mbedtls_mutex_free = mutex_free;
155 mbedtls_mutex_lock = mutex_lock;
156 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200157
Gergely Budai13f7fb32017-08-23 14:23:58 +0200158#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
Gergely Budai13f7fb32017-08-23 14:23:58 +0200160#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100161#if defined(THREADING_USE_GMTIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100163#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200164}
165
166/*
167 * Free global mutexes
168 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169void mbedtls_threading_free_alt(void)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200170{
Gergely Budai13f7fb32017-08-23 14:23:58 +0200171#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
Gergely Budai13f7fb32017-08-23 14:23:58 +0200173#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100174#if defined(THREADING_USE_GMTIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100176#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200179
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200180/*
181 * Define global mutexes
182 */
183#ifndef MUTEX_INIT
184#define MUTEX_INIT
185#endif
Gergely Budai13f7fb32017-08-23 14:23:58 +0200186#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200187mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai13f7fb32017-08-23 14:23:58 +0200188#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100189#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100190mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
191#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_THREADING_C */