Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 1 | /** Mutex usage verification framework. */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <test/helpers.h> |
| 9 | #include <test/macros.h> |
| 10 | |
| 11 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 12 | |
| 13 | #include "mbedtls/threading.h" |
| 14 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 15 | /** Mutex usage verification framework. |
| 16 | * |
| 17 | * The mutex usage verification code below aims to detect bad usage of |
| 18 | * Mbed TLS's mutex abstraction layer at runtime. Note that this is solely |
| 19 | * about the use of the mutex itself, not about checking whether the mutex |
| 20 | * correctly protects whatever it is supposed to protect. |
| 21 | * |
| 22 | * The normal usage of a mutex is: |
| 23 | * ``` |
| 24 | * digraph mutex_states { |
| 25 | * "UNINITIALIZED"; // the initial state |
| 26 | * "IDLE"; |
| 27 | * "FREED"; |
| 28 | * "LOCKED"; |
| 29 | * "UNINITIALIZED" -> "IDLE" [label="init"]; |
| 30 | * "FREED" -> "IDLE" [label="init"]; |
| 31 | * "IDLE" -> "LOCKED" [label="lock"]; |
| 32 | * "LOCKED" -> "IDLE" [label="unlock"]; |
| 33 | * "IDLE" -> "FREED" [label="free"]; |
| 34 | * } |
| 35 | * ``` |
| 36 | * |
| 37 | * All bad transitions that can be unambiguously detected are reported. |
| 38 | * An attempt to use an uninitialized mutex cannot be detected in general |
| 39 | * since the memory content may happen to denote a valid state. For the same |
| 40 | * reason, a double init cannot be detected. |
| 41 | * All-bits-zero is the state of a freed mutex, which is distinct from an |
| 42 | * initialized mutex, so attempting to use zero-initialized memory as a mutex |
| 43 | * without calling the init function is detected. |
| 44 | * |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 45 | * The framework attempts to detect missing calls to init and free by counting |
| 46 | * calls to init and free. If there are more calls to init than free, this |
| 47 | * means that a mutex is not being freed somewhere, which is a memory leak |
| 48 | * on platforms where a mutex consumes resources other than the |
| 49 | * mbedtls_threading_mutex_t object itself. If there are more calls to free |
| 50 | * than init, this indicates a missing init, which is likely to be detected |
| 51 | * by an attempt to lock the mutex as well. A limitation of this framework is |
| 52 | * that it cannot detect scenarios where there is exactly the same number of |
| 53 | * calls to init and free but the calls don't match. A bug like this is |
| 54 | * unlikely to happen uniformly throughout the whole test suite though. |
| 55 | * |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 56 | * If an error is detected, this framework will report what happened and the |
| 57 | * test case will be marked as failed. Unfortunately, the error report cannot |
| 58 | * indicate the exact location of the problematic call. To locate the error, |
| 59 | * use a debugger and set a breakpoint on mbedtls_test_mutex_usage_error(). |
| 60 | */ |
Paul Elliott | 9e25936 | 2023-11-15 11:33:32 +0000 | [diff] [blame] | 61 | enum value_of_mutex_state_field { |
| 62 | /* Potential values for the state field of mbedtls_threading_mutex_t. |
Gilles Peskine | 39a1a26 | 2021-02-09 15:35:29 +0100 | [diff] [blame] | 63 | * Note that MUTEX_FREED must be 0 and MUTEX_IDLE must be 1 for |
| 64 | * compatibility with threading_mutex_init_pthread() and |
| 65 | * threading_mutex_free_pthread(). MUTEX_LOCKED could be any nonzero |
| 66 | * value. */ |
Paul Elliott | 5fa986c | 2023-11-10 14:05:09 +0000 | [diff] [blame] | 67 | MUTEX_FREED = 0, //! < Set by mbedtls_test_wrap_mutex_free |
| 68 | MUTEX_IDLE = 1, //! < Set by mbedtls_test_wrap_mutex_init and by mbedtls_test_wrap_mutex_unlock |
| 69 | MUTEX_LOCKED = 2, //! < Set by mbedtls_test_wrap_mutex_lock |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | typedef struct { |
| 73 | void (*init)(mbedtls_threading_mutex_t *); |
| 74 | void (*free)(mbedtls_threading_mutex_t *); |
| 75 | int (*lock)(mbedtls_threading_mutex_t *); |
| 76 | int (*unlock)(mbedtls_threading_mutex_t *); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 77 | } mutex_functions_t; |
| 78 | static mutex_functions_t mutex_functions; |
| 79 | |
Paul Elliott | 392ed3f | 2023-11-24 15:48:28 +0000 | [diff] [blame] | 80 | /** |
| 81 | * The mutex used to guard live_mutexes below and access to the status variable |
| 82 | * in every mbedtls_threading_mutex_t. |
| 83 | * Note that we are not reporting any errors when locking and unlocking this |
| 84 | * mutex. This is for a couple of reasons: |
| 85 | * |
| 86 | * 1. We have no real way of reporting any errors with this mutex - we cannot |
| 87 | * report it back to the caller, as the failure was not that of the mutex |
| 88 | * passed in. We could fail the test, but again this would indicate a problem |
| 89 | * with the test code that did not exist. |
| 90 | * |
| 91 | * 2. Any failure to lock is unlikely to be intermittent, and will thus not |
| 92 | * give false test results - the overall result would be to turn off the |
| 93 | * testing. This is not a situation that is likely to happen with normal |
| 94 | * testing and we still have TSan to fall back on should this happen. |
| 95 | */ |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 96 | mbedtls_threading_mutex_t mbedtls_test_mutex_mutex; |
| 97 | |
Paul Elliott | 392ed3f | 2023-11-24 15:48:28 +0000 | [diff] [blame] | 98 | /** |
| 99 | * The total number of calls to mbedtls_mutex_init(), minus the total number |
| 100 | * of calls to mbedtls_mutex_free(). |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 101 | * |
Paul Elliott | 392ed3f | 2023-11-24 15:48:28 +0000 | [diff] [blame] | 102 | * Do not read or write without holding mbedtls_test_mutex_mutex (above). Reset |
| 103 | * to 0 after each test case. |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 104 | */ |
| 105 | static int live_mutexes; |
| 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex, |
| 108 | const char *msg) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 109 | { |
| 110 | (void) mutex; |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 111 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 112 | mbedtls_test_set_mutex_usage_error(msg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | mbedtls_fprintf(stdout, "[mutex: %s] ", msg); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 114 | /* Don't mark the test as failed yet. This way, if the test fails later |
| 115 | * for a functional reason, the test framework will report the message |
| 116 | * and location for this functional reason. If the test passes, |
| 117 | * mbedtls_test_mutex_usage_check() will mark it as failed. */ |
| 118 | } |
| 119 | |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 120 | static int mbedtls_test_mutex_can_test(mbedtls_threading_mutex_t *mutex) |
| 121 | { |
| 122 | /* If we attempt to run tests on this mutex then we are going to run into a |
| 123 | * couple of problems: |
| 124 | * 1. If any test on this mutex fails, we are going to deadlock when |
| 125 | * reporting that failure, as we already hold the mutex at that point. |
| 126 | * 2. Given the 'global' position of the initialization and free of this |
| 127 | * mutex, it will be shown as leaked on the first test run. */ |
Paul Elliott | 3d2db89 | 2024-01-19 20:42:56 +0000 | [diff] [blame^] | 128 | if (mutex == mbedtls_test_get_info_mutex()) { |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 136 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | mutex_functions.init(mutex); |
Paul Elliott | 5fa986c | 2023-11-10 14:05:09 +0000 | [diff] [blame] | 138 | |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 139 | if (mbedtls_test_mutex_can_test(mutex)) { |
| 140 | if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |
| 141 | mutex->state = MUTEX_IDLE; |
| 142 | ++live_mutexes; |
Paul Elliott | 5fa986c | 2023-11-10 14:05:09 +0000 | [diff] [blame] | 143 | |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 144 | mutex_functions.unlock(&mbedtls_test_mutex_mutex); |
| 145 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | } |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 150 | { |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 151 | if (mbedtls_test_mutex_can_test(mutex)) { |
| 152 | if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |
Paul Elliott | 5fa986c | 2023-11-10 14:05:09 +0000 | [diff] [blame] | 153 | |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 154 | switch (mutex->state) { |
| 155 | case MUTEX_FREED: |
| 156 | mbedtls_test_mutex_usage_error(mutex, "free without init or double free"); |
| 157 | break; |
| 158 | case MUTEX_IDLE: |
| 159 | mutex->state = MUTEX_FREED; |
| 160 | --live_mutexes; |
| 161 | break; |
| 162 | case MUTEX_LOCKED: |
| 163 | mbedtls_test_mutex_usage_error(mutex, "free without unlock"); |
| 164 | break; |
| 165 | default: |
| 166 | mbedtls_test_mutex_usage_error(mutex, "corrupted state"); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | mutex_functions.unlock(&mbedtls_test_mutex_mutex); |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 171 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | } |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 173 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | mutex_functions.free(mutex); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 178 | { |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 179 | /* Lock the passed in mutex first, so that the only way to change the state |
| 180 | * is to hold the passed in and internal mutex - otherwise we create a race |
| 181 | * condition. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | int ret = mutex_functions.lock(mutex); |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 183 | |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 184 | if (mbedtls_test_mutex_can_test(mutex)) { |
| 185 | if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |
| 186 | switch (mutex->state) { |
| 187 | case MUTEX_FREED: |
| 188 | mbedtls_test_mutex_usage_error(mutex, "lock without init"); |
| 189 | break; |
| 190 | case MUTEX_IDLE: |
| 191 | if (ret == 0) { |
| 192 | mutex->state = MUTEX_LOCKED; |
| 193 | } |
| 194 | break; |
| 195 | case MUTEX_LOCKED: |
| 196 | mbedtls_test_mutex_usage_error(mutex, "double lock"); |
| 197 | break; |
| 198 | default: |
| 199 | mbedtls_test_mutex_usage_error(mutex, "corrupted state"); |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | mutex_functions.unlock(&mbedtls_test_mutex_mutex); |
| 204 | } |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 205 | } |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | return ret; |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 211 | { |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 212 | /* Lock the internal mutex first and change state, so that the only way to |
| 213 | * change the state is to hold the passed in and internal mutex - otherwise |
| 214 | * we create a race condition. */ |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 215 | if (mbedtls_test_mutex_can_test(mutex)) { |
| 216 | if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |
| 217 | switch (mutex->state) { |
| 218 | case MUTEX_FREED: |
| 219 | mbedtls_test_mutex_usage_error(mutex, "unlock without init"); |
| 220 | break; |
| 221 | case MUTEX_IDLE: |
| 222 | mbedtls_test_mutex_usage_error(mutex, "unlock without lock"); |
| 223 | break; |
| 224 | case MUTEX_LOCKED: |
| 225 | mutex->state = MUTEX_IDLE; |
| 226 | break; |
| 227 | default: |
| 228 | mbedtls_test_mutex_usage_error(mutex, "corrupted state"); |
| 229 | break; |
| 230 | } |
| 231 | mutex_functions.unlock(&mbedtls_test_mutex_mutex); |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 232 | } |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 233 | } |
Paul Elliott | 0710ac4 | 2024-01-09 17:20:58 +0000 | [diff] [blame] | 234 | |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 235 | return mutex_functions.unlock(mutex); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | void mbedtls_test_mutex_usage_init(void) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 239 | { |
| 240 | mutex_functions.init = mbedtls_mutex_init; |
| 241 | mutex_functions.free = mbedtls_mutex_free; |
| 242 | mutex_functions.lock = mbedtls_mutex_lock; |
| 243 | mutex_functions.unlock = mbedtls_mutex_unlock; |
| 244 | mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init; |
| 245 | mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free; |
| 246 | mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock; |
| 247 | mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock; |
Paul Elliott | 3774637 | 2023-11-12 19:05:57 +0000 | [diff] [blame] | 248 | |
| 249 | mutex_functions.init(&mbedtls_test_mutex_mutex); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | void mbedtls_test_mutex_usage_check(void) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 253 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | if (live_mutexes != 0) { |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 255 | /* A positive number (more init than free) means that a mutex resource |
| 256 | * is leaking (on platforms where a mutex consumes more than the |
| 257 | * mbedtls_threading_mutex_t object itself). The rare case of a |
| 258 | * negative number means a missing init somewhere. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes); |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 260 | live_mutexes = 0; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 261 | mbedtls_test_set_mutex_usage_error("missing free"); |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 262 | } |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 263 | if (mbedtls_test_get_mutex_usage_error() != NULL && |
| 264 | mbedtls_test_get_result() != MBEDTLS_TEST_RESULT_FAILED) { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 265 | /* Functionally, the test passed. But there was a mutex usage error, |
| 266 | * so mark the test as failed after all. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 268 | } |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 269 | mbedtls_test_set_mutex_usage_error(NULL); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Paul Elliott | f25d831 | 2023-11-23 18:49:43 +0000 | [diff] [blame] | 272 | void mbedtls_test_mutex_usage_end(void) |
| 273 | { |
| 274 | mbedtls_mutex_init = mutex_functions.init; |
| 275 | mbedtls_mutex_free = mutex_functions.free; |
| 276 | mbedtls_mutex_lock = mutex_functions.lock; |
| 277 | mbedtls_mutex_unlock = mutex_functions.unlock; |
| 278 | |
| 279 | mutex_functions.free(&mbedtls_test_mutex_mutex); |
| 280 | } |
| 281 | |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 282 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |