blob: 5a871e102dd27f4c24445d15bca7667461727dcd [file] [log] [blame]
Gilles Peskine1061ec62021-01-29 21:17:11 +01001/** Mutex usage verification framework. */
2
3/*
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine1061ec62021-01-29 21:17:11 +01006 */
7
8#include <test/helpers.h>
Paul Elliott17c119a2023-12-08 16:55:03 +00009#include <test/threading_helpers.h>
Gilles Peskine1061ec62021-01-29 21:17:11 +010010#include <test/macros.h>
11
Paul Elliott3a4d2f12023-12-08 20:49:47 +000012#include "mbedtls/threading.h"
13
14#if defined(MBEDTLS_THREADING_C)
15
16#if defined(MBEDTLS_THREADING_PTHREAD)
17
18static int threading_thread_create_pthread(mbedtls_test_thread_t *thread, void *(*thread_func)(
19 void *), void *thread_data)
20{
21 if (thread == NULL || thread_func == NULL) {
22 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
23 }
24
25 if (pthread_create(&thread->thread, NULL, thread_func, thread_data)) {
26 return MBEDTLS_ERR_THREADING_THREAD_ERROR;
27 }
28
29 return 0;
30}
31
32static int threading_thread_join_pthread(mbedtls_test_thread_t *thread)
33{
34 if (thread == NULL) {
35 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
36 }
37
38 if (pthread_join(thread->thread, NULL) != 0) {
39 return MBEDTLS_ERR_THREADING_THREAD_ERROR;
40 }
41
42 return 0;
43}
44
45int (*mbedtls_test_thread_create)(mbedtls_test_thread_t *thread, void *(*thread_func)(void *),
46 void *thread_data) = threading_thread_create_pthread;
47int (*mbedtls_test_thread_join)(mbedtls_test_thread_t *thread) = threading_thread_join_pthread;
48
49#endif /* MBEDTLS_THREADING_PTHREAD */
50
51#if defined(MBEDTLS_THREADING_ALT)
52
53static int threading_thread_create_fail(mbedtls_test_thread_t *thread,
54 void *(*thread_func)(void *),
55 void *thread_data)
56{
57 (void) thread;
58 (void) thread_func;
59 (void) thread_data;
60
61 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
62}
63
64static int threading_thread_join_fail(mbedtls_test_thread_t *thread)
65{
66 (void) thread;
67
68 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
69}
70
71int (*mbedtls_test_thread_create)(mbedtls_test_thread_t *thread, void *(*thread_func)(void *),
72 void *thread_data) = threading_thread_create_fail;
73int (*mbedtls_test_thread_join)(mbedtls_test_thread_t *thread) = threading_thread_join_fail;
74
75#endif /* MBEDTLS_THREADING_ALT */
76
Gilles Peskine1061ec62021-01-29 21:17:11 +010077#if defined(MBEDTLS_TEST_MUTEX_USAGE)
78
79#include "mbedtls/threading.h"
80
Gilles Peskine2a4c5982021-01-29 21:18:09 +010081/** Mutex usage verification framework.
82 *
83 * The mutex usage verification code below aims to detect bad usage of
84 * Mbed TLS's mutex abstraction layer at runtime. Note that this is solely
85 * about the use of the mutex itself, not about checking whether the mutex
86 * correctly protects whatever it is supposed to protect.
87 *
88 * The normal usage of a mutex is:
89 * ```
90 * digraph mutex_states {
91 * "UNINITIALIZED"; // the initial state
92 * "IDLE";
93 * "FREED";
94 * "LOCKED";
95 * "UNINITIALIZED" -> "IDLE" [label="init"];
96 * "FREED" -> "IDLE" [label="init"];
97 * "IDLE" -> "LOCKED" [label="lock"];
98 * "LOCKED" -> "IDLE" [label="unlock"];
99 * "IDLE" -> "FREED" [label="free"];
100 * }
101 * ```
102 *
103 * All bad transitions that can be unambiguously detected are reported.
104 * An attempt to use an uninitialized mutex cannot be detected in general
105 * since the memory content may happen to denote a valid state. For the same
106 * reason, a double init cannot be detected.
107 * All-bits-zero is the state of a freed mutex, which is distinct from an
108 * initialized mutex, so attempting to use zero-initialized memory as a mutex
109 * without calling the init function is detected.
110 *
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100111 * The framework attempts to detect missing calls to init and free by counting
112 * calls to init and free. If there are more calls to init than free, this
113 * means that a mutex is not being freed somewhere, which is a memory leak
114 * on platforms where a mutex consumes resources other than the
115 * mbedtls_threading_mutex_t object itself. If there are more calls to free
116 * than init, this indicates a missing init, which is likely to be detected
117 * by an attempt to lock the mutex as well. A limitation of this framework is
118 * that it cannot detect scenarios where there is exactly the same number of
119 * calls to init and free but the calls don't match. A bug like this is
120 * unlikely to happen uniformly throughout the whole test suite though.
121 *
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100122 * If an error is detected, this framework will report what happened and the
123 * test case will be marked as failed. Unfortunately, the error report cannot
124 * indicate the exact location of the problematic call. To locate the error,
125 * use a debugger and set a breakpoint on mbedtls_test_mutex_usage_error().
126 */
Paul Elliott9e259362023-11-15 11:33:32 +0000127enum value_of_mutex_state_field {
128 /* Potential values for the state field of mbedtls_threading_mutex_t.
Gilles Peskine39a1a262021-02-09 15:35:29 +0100129 * Note that MUTEX_FREED must be 0 and MUTEX_IDLE must be 1 for
130 * compatibility with threading_mutex_init_pthread() and
131 * threading_mutex_free_pthread(). MUTEX_LOCKED could be any nonzero
132 * value. */
Paul Elliott5fa986c2023-11-10 14:05:09 +0000133 MUTEX_FREED = 0, //! < Set by mbedtls_test_wrap_mutex_free
134 MUTEX_IDLE = 1, //! < Set by mbedtls_test_wrap_mutex_init and by mbedtls_test_wrap_mutex_unlock
135 MUTEX_LOCKED = 2, //! < Set by mbedtls_test_wrap_mutex_lock
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100136};
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138typedef struct {
139 void (*init)(mbedtls_threading_mutex_t *);
140 void (*free)(mbedtls_threading_mutex_t *);
141 int (*lock)(mbedtls_threading_mutex_t *);
142 int (*unlock)(mbedtls_threading_mutex_t *);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100143} mutex_functions_t;
144static mutex_functions_t mutex_functions;
145
Paul Elliott392ed3f2023-11-24 15:48:28 +0000146/**
147 * The mutex used to guard live_mutexes below and access to the status variable
148 * in every mbedtls_threading_mutex_t.
149 * Note that we are not reporting any errors when locking and unlocking this
150 * mutex. This is for a couple of reasons:
151 *
152 * 1. We have no real way of reporting any errors with this mutex - we cannot
153 * report it back to the caller, as the failure was not that of the mutex
154 * passed in. We could fail the test, but again this would indicate a problem
155 * with the test code that did not exist.
156 *
157 * 2. Any failure to lock is unlikely to be intermittent, and will thus not
158 * give false test results - the overall result would be to turn off the
159 * testing. This is not a situation that is likely to happen with normal
160 * testing and we still have TSan to fall back on should this happen.
161 */
Paul Elliott37746372023-11-12 19:05:57 +0000162mbedtls_threading_mutex_t mbedtls_test_mutex_mutex;
163
Paul Elliott392ed3f2023-11-24 15:48:28 +0000164/**
165 * The total number of calls to mbedtls_mutex_init(), minus the total number
166 * of calls to mbedtls_mutex_free().
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100167 *
Paul Elliott392ed3f2023-11-24 15:48:28 +0000168 * Do not read or write without holding mbedtls_test_mutex_mutex (above). Reset
169 * to 0 after each test case.
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100170 */
171static int live_mutexes;
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex,
174 const char *msg)
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100175{
176 (void) mutex;
Paul Elliott37746372023-11-12 19:05:57 +0000177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (mbedtls_test_info.mutex_usage_error == NULL) {
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100179 mbedtls_test_info.mutex_usage_error = msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 }
181 mbedtls_fprintf(stdout, "[mutex: %s] ", msg);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100182 /* Don't mark the test as failed yet. This way, if the test fails later
183 * for a functional reason, the test framework will report the message
184 * and location for this functional reason. If the test passes,
185 * mbedtls_test_mutex_usage_check() will mark it as failed. */
186}
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100189{
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mutex_functions.init(mutex);
Paul Elliott5fa986c2023-11-10 14:05:09 +0000191
192 if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) {
193 mutex->state = MUTEX_IDLE;
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100194 ++live_mutexes;
Paul Elliott5fa986c2023-11-10 14:05:09 +0000195
196 mutex_functions.unlock(&mbedtls_test_mutex_mutex);
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 }
Gilles Peskine1061ec62021-01-29 21:17:11 +0100198}
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100201{
Paul Elliott37746372023-11-12 19:05:57 +0000202 if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) {
Paul Elliott5fa986c2023-11-10 14:05:09 +0000203
Paul Elliott9e259362023-11-15 11:33:32 +0000204 switch (mutex->state) {
Paul Elliott37746372023-11-12 19:05:57 +0000205 case MUTEX_FREED:
206 mbedtls_test_mutex_usage_error(mutex, "free without init or double free");
207 break;
208 case MUTEX_IDLE:
Paul Elliott9e259362023-11-15 11:33:32 +0000209 mutex->state = MUTEX_FREED;
Paul Elliott37746372023-11-12 19:05:57 +0000210 --live_mutexes;
211 break;
212 case MUTEX_LOCKED:
213 mbedtls_test_mutex_usage_error(mutex, "free without unlock");
214 break;
215 default:
216 mbedtls_test_mutex_usage_error(mutex, "corrupted state");
217 break;
218 }
219
220 mutex_functions.unlock(&mbedtls_test_mutex_mutex);
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 }
222 mutex_functions.free(mutex);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100223}
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100226{
Paul Elliott37746372023-11-12 19:05:57 +0000227 /* Lock the passed in mutex first, so that the only way to change the state
228 * is to hold the passed in and internal mutex - otherwise we create a race
229 * condition. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 int ret = mutex_functions.lock(mutex);
Paul Elliott37746372023-11-12 19:05:57 +0000231 if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) {
Paul Elliott9e259362023-11-15 11:33:32 +0000232 switch (mutex->state) {
Paul Elliott37746372023-11-12 19:05:57 +0000233 case MUTEX_FREED:
234 mbedtls_test_mutex_usage_error(mutex, "lock without init");
235 break;
236 case MUTEX_IDLE:
237 if (ret == 0) {
Paul Elliott9e259362023-11-15 11:33:32 +0000238 mutex->state = MUTEX_LOCKED;
Paul Elliott37746372023-11-12 19:05:57 +0000239 }
240 break;
241 case MUTEX_LOCKED:
242 mbedtls_test_mutex_usage_error(mutex, "double lock");
243 break;
244 default:
245 mbedtls_test_mutex_usage_error(mutex, "corrupted state");
246 break;
247 }
248
249 mutex_functions.unlock(&mbedtls_test_mutex_mutex);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100250 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return ret;
Gilles Peskine1061ec62021-01-29 21:17:11 +0100252}
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100255{
Paul Elliott37746372023-11-12 19:05:57 +0000256 /* Lock the internal mutex first and change state, so that the only way to
257 * change the state is to hold the passed in and internal mutex - otherwise
258 * we create a race condition. */
259 if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) {
Paul Elliott9e259362023-11-15 11:33:32 +0000260 switch (mutex->state) {
Paul Elliott37746372023-11-12 19:05:57 +0000261 case MUTEX_FREED:
262 mbedtls_test_mutex_usage_error(mutex, "unlock without init");
263 break;
264 case MUTEX_IDLE:
265 mbedtls_test_mutex_usage_error(mutex, "unlock without lock");
266 break;
267 case MUTEX_LOCKED:
Paul Elliott9e259362023-11-15 11:33:32 +0000268 mutex->state = MUTEX_IDLE;
Paul Elliott37746372023-11-12 19:05:57 +0000269 break;
270 default:
271 mbedtls_test_mutex_usage_error(mutex, "corrupted state");
272 break;
273 }
274 mutex_functions.unlock(&mbedtls_test_mutex_mutex);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100275 }
Paul Elliott37746372023-11-12 19:05:57 +0000276 return mutex_functions.unlock(mutex);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100277}
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279void mbedtls_test_mutex_usage_init(void)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100280{
281 mutex_functions.init = mbedtls_mutex_init;
282 mutex_functions.free = mbedtls_mutex_free;
283 mutex_functions.lock = mbedtls_mutex_lock;
284 mutex_functions.unlock = mbedtls_mutex_unlock;
285 mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init;
286 mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free;
287 mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock;
288 mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock;
Paul Elliott37746372023-11-12 19:05:57 +0000289
290 mutex_functions.init(&mbedtls_test_mutex_mutex);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100291}
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293void mbedtls_test_mutex_usage_check(void)
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100294{
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (live_mutexes != 0) {
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100296 /* A positive number (more init than free) means that a mutex resource
297 * is leaking (on platforms where a mutex consumes more than the
298 * mbedtls_threading_mutex_t object itself). The rare case of a
299 * negative number means a missing init somewhere. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes);
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100301 live_mutexes = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (mbedtls_test_info.mutex_usage_error == NULL) {
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100303 mbedtls_test_info.mutex_usage_error = "missing free";
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 }
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100305 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (mbedtls_test_info.mutex_usage_error != NULL &&
307 mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100308 /* Functionally, the test passed. But there was a mutex usage error,
309 * so mark the test as failed after all. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100311 }
312 mbedtls_test_info.mutex_usage_error = NULL;
313}
314
Paul Elliottf25d8312023-11-23 18:49:43 +0000315void mbedtls_test_mutex_usage_end(void)
316{
317 mbedtls_mutex_init = mutex_functions.init;
318 mbedtls_mutex_free = mutex_functions.free;
319 mbedtls_mutex_lock = mutex_functions.lock;
320 mbedtls_mutex_unlock = mutex_functions.unlock;
321
322 mutex_functions.free(&mbedtls_test_mutex_mutex);
323}
324
Gilles Peskine1061ec62021-01-29 21:17:11 +0100325#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliott3a4d2f12023-12-08 20:49:47 +0000326
327#endif /* MBEDTLS_THREADING_C */