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 |
| 5 | * 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. |
| 18 | */ |
| 19 | |
| 20 | #include <test/helpers.h> |
| 21 | #include <test/macros.h> |
| 22 | |
| 23 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 24 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 25 | # include "mbedtls/threading.h" |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 26 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 27 | /** Mutex usage verification framework. |
| 28 | * |
| 29 | * The mutex usage verification code below aims to detect bad usage of |
| 30 | * Mbed TLS's mutex abstraction layer at runtime. Note that this is solely |
| 31 | * about the use of the mutex itself, not about checking whether the mutex |
| 32 | * correctly protects whatever it is supposed to protect. |
| 33 | * |
| 34 | * The normal usage of a mutex is: |
| 35 | * ``` |
| 36 | * digraph mutex_states { |
| 37 | * "UNINITIALIZED"; // the initial state |
| 38 | * "IDLE"; |
| 39 | * "FREED"; |
| 40 | * "LOCKED"; |
| 41 | * "UNINITIALIZED" -> "IDLE" [label="init"]; |
| 42 | * "FREED" -> "IDLE" [label="init"]; |
| 43 | * "IDLE" -> "LOCKED" [label="lock"]; |
| 44 | * "LOCKED" -> "IDLE" [label="unlock"]; |
| 45 | * "IDLE" -> "FREED" [label="free"]; |
| 46 | * } |
| 47 | * ``` |
| 48 | * |
| 49 | * All bad transitions that can be unambiguously detected are reported. |
| 50 | * An attempt to use an uninitialized mutex cannot be detected in general |
| 51 | * since the memory content may happen to denote a valid state. For the same |
| 52 | * reason, a double init cannot be detected. |
| 53 | * All-bits-zero is the state of a freed mutex, which is distinct from an |
| 54 | * initialized mutex, so attempting to use zero-initialized memory as a mutex |
| 55 | * without calling the init function is detected. |
| 56 | * |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 57 | * The framework attempts to detect missing calls to init and free by counting |
| 58 | * calls to init and free. If there are more calls to init than free, this |
| 59 | * means that a mutex is not being freed somewhere, which is a memory leak |
| 60 | * on platforms where a mutex consumes resources other than the |
| 61 | * mbedtls_threading_mutex_t object itself. If there are more calls to free |
| 62 | * than init, this indicates a missing init, which is likely to be detected |
| 63 | * by an attempt to lock the mutex as well. A limitation of this framework is |
| 64 | * that it cannot detect scenarios where there is exactly the same number of |
| 65 | * calls to init and free but the calls don't match. A bug like this is |
| 66 | * unlikely to happen uniformly throughout the whole test suite though. |
| 67 | * |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 68 | * If an error is detected, this framework will report what happened and the |
| 69 | * test case will be marked as failed. Unfortunately, the error report cannot |
| 70 | * indicate the exact location of the problematic call. To locate the error, |
| 71 | * use a debugger and set a breakpoint on mbedtls_test_mutex_usage_error(). |
| 72 | */ |
Gilles Peskine | 39a1a26 | 2021-02-09 15:35:29 +0100 | [diff] [blame] | 73 | enum value_of_mutex_is_valid_field |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 74 | { |
Gilles Peskine | 39a1a26 | 2021-02-09 15:35:29 +0100 | [diff] [blame] | 75 | /* Potential values for the is_valid field of mbedtls_threading_mutex_t. |
| 76 | * Note that MUTEX_FREED must be 0 and MUTEX_IDLE must be 1 for |
| 77 | * compatibility with threading_mutex_init_pthread() and |
| 78 | * threading_mutex_free_pthread(). MUTEX_LOCKED could be any nonzero |
| 79 | * value. */ |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 80 | MUTEX_FREED = 0, //!< Set by threading_mutex_free_pthread |
| 81 | MUTEX_IDLE = 1, //!< Set by threading_mutex_init_pthread and by our unlock |
| 82 | MUTEX_LOCKED = 2, //!< Set by our lock |
| 83 | }; |
| 84 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 85 | typedef struct { |
| 86 | void (*init)(mbedtls_threading_mutex_t *); |
| 87 | void (*free)(mbedtls_threading_mutex_t *); |
| 88 | int (*lock)(mbedtls_threading_mutex_t *); |
| 89 | int (*unlock)(mbedtls_threading_mutex_t *); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 90 | } mutex_functions_t; |
| 91 | static mutex_functions_t mutex_functions; |
| 92 | |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 93 | /** The total number of calls to mbedtls_mutex_init(), minus the total number |
| 94 | * of calls to mbedtls_mutex_free(). |
| 95 | * |
| 96 | * Reset to 0 after each test case. |
| 97 | */ |
| 98 | static int live_mutexes; |
| 99 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 100 | static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex, |
| 101 | const char *msg) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 102 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 103 | (void)mutex; |
| 104 | if (mbedtls_test_info.mutex_usage_error == NULL) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 105 | mbedtls_test_info.mutex_usage_error = msg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 106 | mbedtls_fprintf(stdout, "[mutex: %s] ", msg); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 107 | /* Don't mark the test as failed yet. This way, if the test fails later |
| 108 | * for a functional reason, the test framework will report the message |
| 109 | * and location for this functional reason. If the test passes, |
| 110 | * mbedtls_test_mutex_usage_check() will mark it as failed. */ |
| 111 | } |
| 112 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 113 | static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 114 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 115 | mutex_functions.init(mutex); |
| 116 | if (mutex->is_valid) |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 117 | ++live_mutexes; |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 120 | static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 121 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 122 | switch (mutex->is_valid) { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 123 | case MUTEX_FREED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 124 | mbedtls_test_mutex_usage_error(mutex, |
| 125 | "free without init or double free"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 126 | break; |
| 127 | case MUTEX_IDLE: |
| 128 | /* Do nothing. The underlying free function will reset is_valid |
| 129 | * to 0. */ |
| 130 | break; |
| 131 | case MUTEX_LOCKED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 132 | mbedtls_test_mutex_usage_error(mutex, "free without unlock"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 133 | break; |
| 134 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 135 | mbedtls_test_mutex_usage_error(mutex, "corrupted state"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 136 | break; |
| 137 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 138 | if (mutex->is_valid) |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 139 | --live_mutexes; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 140 | mutex_functions.free(mutex); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 143 | static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 144 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 145 | int ret = mutex_functions.lock(mutex); |
| 146 | switch (mutex->is_valid) { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 147 | case MUTEX_FREED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 148 | mbedtls_test_mutex_usage_error(mutex, "lock without init"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 149 | break; |
| 150 | case MUTEX_IDLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 151 | if (ret == 0) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 152 | mutex->is_valid = 2; |
| 153 | break; |
| 154 | case MUTEX_LOCKED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 155 | mbedtls_test_mutex_usage_error(mutex, "double lock"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 156 | break; |
| 157 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 158 | mbedtls_test_mutex_usage_error(mutex, "corrupted state"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 159 | break; |
| 160 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 161 | return ret; |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 164 | static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 165 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 166 | int ret = mutex_functions.unlock(mutex); |
| 167 | switch (mutex->is_valid) { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 168 | case MUTEX_FREED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 169 | mbedtls_test_mutex_usage_error(mutex, "unlock without init"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 170 | break; |
| 171 | case MUTEX_IDLE: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 172 | mbedtls_test_mutex_usage_error(mutex, "unlock without lock"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 173 | break; |
| 174 | case MUTEX_LOCKED: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 175 | if (ret == 0) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 176 | mutex->is_valid = MUTEX_IDLE; |
| 177 | break; |
| 178 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 179 | mbedtls_test_mutex_usage_error(mutex, "corrupted state"); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 180 | break; |
| 181 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 182 | return ret; |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 185 | void mbedtls_test_mutex_usage_init(void) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 186 | { |
| 187 | mutex_functions.init = mbedtls_mutex_init; |
| 188 | mutex_functions.free = mbedtls_mutex_free; |
| 189 | mutex_functions.lock = mbedtls_mutex_lock; |
| 190 | mutex_functions.unlock = mbedtls_mutex_unlock; |
| 191 | mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init; |
| 192 | mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free; |
| 193 | mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock; |
| 194 | mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock; |
| 195 | } |
| 196 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 197 | void mbedtls_test_mutex_usage_check(void) |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 198 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 199 | if (live_mutexes != 0) { |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 200 | /* A positive number (more init than free) means that a mutex resource |
| 201 | * is leaking (on platforms where a mutex consumes more than the |
| 202 | * mbedtls_threading_mutex_t object itself). The rare case of a |
| 203 | * negative number means a missing init somewhere. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 204 | mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes); |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 205 | live_mutexes = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 206 | if (mbedtls_test_info.mutex_usage_error == NULL) |
Gilles Peskine | f96d3d8 | 2021-01-29 22:20:32 +0100 | [diff] [blame] | 207 | mbedtls_test_info.mutex_usage_error = "missing free"; |
| 208 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 209 | if (mbedtls_test_info.mutex_usage_error != NULL && |
| 210 | mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 211 | /* Functionally, the test passed. But there was a mutex usage error, |
| 212 | * so mark the test as failed after all. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 213 | mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 214 | } |
| 215 | mbedtls_test_info.mutex_usage_error = NULL; |
| 216 | } |
| 217 | |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 218 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |