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 | |
| 25 | #include "mbedtls/threading.h" |
| 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 | * |
| 57 | * If an error is detected, this framework will report what happened and the |
| 58 | * test case will be marked as failed. Unfortunately, the error report cannot |
| 59 | * indicate the exact location of the problematic call. To locate the error, |
| 60 | * use a debugger and set a breakpoint on mbedtls_test_mutex_usage_error(). |
| 61 | */ |
| 62 | enum value_of_mutex_is_valid |
| 63 | { |
| 64 | MUTEX_FREED = 0, //!< Set by threading_mutex_free_pthread |
| 65 | MUTEX_IDLE = 1, //!< Set by threading_mutex_init_pthread and by our unlock |
| 66 | MUTEX_LOCKED = 2, //!< Set by our lock |
| 67 | }; |
| 68 | |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 69 | typedef struct |
| 70 | { |
| 71 | void (*init)( mbedtls_threading_mutex_t * ); |
| 72 | void (*free)( mbedtls_threading_mutex_t * ); |
| 73 | int (*lock)( mbedtls_threading_mutex_t * ); |
| 74 | int (*unlock)( mbedtls_threading_mutex_t * ); |
| 75 | } mutex_functions_t; |
| 76 | static mutex_functions_t mutex_functions; |
| 77 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame^] | 78 | static void mbedtls_test_mutex_usage_error( mbedtls_threading_mutex_t *mutex, |
| 79 | const char *msg ) |
| 80 | { |
| 81 | (void) mutex; |
| 82 | if( mbedtls_test_info.mutex_usage_error == NULL ) |
| 83 | mbedtls_test_info.mutex_usage_error = msg; |
| 84 | mbedtls_fprintf( stdout, "[mutex: %s] ", msg ); |
| 85 | /* Don't mark the test as failed yet. This way, if the test fails later |
| 86 | * for a functional reason, the test framework will report the message |
| 87 | * and location for this functional reason. If the test passes, |
| 88 | * mbedtls_test_mutex_usage_check() will mark it as failed. */ |
| 89 | } |
| 90 | |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 91 | static void mbedtls_test_wrap_mutex_init( mbedtls_threading_mutex_t *mutex ) |
| 92 | { |
| 93 | mutex_functions.init( mutex ); |
| 94 | } |
| 95 | |
| 96 | static void mbedtls_test_wrap_mutex_free( mbedtls_threading_mutex_t *mutex ) |
| 97 | { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame^] | 98 | switch( mutex->is_valid ) |
| 99 | { |
| 100 | case MUTEX_FREED: |
| 101 | mbedtls_test_mutex_usage_error( mutex, "free without init or double free" ); |
| 102 | break; |
| 103 | case MUTEX_IDLE: |
| 104 | /* Do nothing. The underlying free function will reset is_valid |
| 105 | * to 0. */ |
| 106 | break; |
| 107 | case MUTEX_LOCKED: |
| 108 | mbedtls_test_mutex_usage_error( mutex, "free without unlock" ); |
| 109 | break; |
| 110 | default: |
| 111 | mbedtls_test_mutex_usage_error( mutex, "corrupted state" ); |
| 112 | break; |
| 113 | } |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 114 | mutex_functions.free( mutex ); |
| 115 | } |
| 116 | |
| 117 | static int mbedtls_test_wrap_mutex_lock( mbedtls_threading_mutex_t *mutex ) |
| 118 | { |
| 119 | int ret = mutex_functions.lock( mutex ); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame^] | 120 | switch( mutex->is_valid ) |
| 121 | { |
| 122 | case MUTEX_FREED: |
| 123 | mbedtls_test_mutex_usage_error( mutex, "lock without init" ); |
| 124 | break; |
| 125 | case MUTEX_IDLE: |
| 126 | if( ret == 0 ) |
| 127 | mutex->is_valid = 2; |
| 128 | break; |
| 129 | case MUTEX_LOCKED: |
| 130 | mbedtls_test_mutex_usage_error( mutex, "double lock" ); |
| 131 | break; |
| 132 | default: |
| 133 | mbedtls_test_mutex_usage_error( mutex, "corrupted state" ); |
| 134 | break; |
| 135 | } |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 136 | return( ret ); |
| 137 | } |
| 138 | |
| 139 | static int mbedtls_test_wrap_mutex_unlock( mbedtls_threading_mutex_t *mutex ) |
| 140 | { |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame^] | 141 | int ret = mutex_functions.unlock( mutex ); |
| 142 | switch( mutex->is_valid ) |
| 143 | { |
| 144 | case MUTEX_FREED: |
| 145 | mbedtls_test_mutex_usage_error( mutex, "unlock without init" ); |
| 146 | break; |
| 147 | case MUTEX_IDLE: |
| 148 | mbedtls_test_mutex_usage_error( mutex, "unlock without lock" ); |
| 149 | break; |
| 150 | case MUTEX_LOCKED: |
| 151 | if( ret == 0 ) |
| 152 | mutex->is_valid = MUTEX_IDLE; |
| 153 | break; |
| 154 | default: |
| 155 | mbedtls_test_mutex_usage_error( mutex, "corrupted state" ); |
| 156 | break; |
| 157 | } |
| 158 | return( ret ); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void mbedtls_test_mutex_usage_init( void ) |
| 162 | { |
| 163 | mutex_functions.init = mbedtls_mutex_init; |
| 164 | mutex_functions.free = mbedtls_mutex_free; |
| 165 | mutex_functions.lock = mbedtls_mutex_lock; |
| 166 | mutex_functions.unlock = mbedtls_mutex_unlock; |
| 167 | mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init; |
| 168 | mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free; |
| 169 | mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock; |
| 170 | mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock; |
| 171 | } |
| 172 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame^] | 173 | void mbedtls_test_mutex_usage_check( void ) |
| 174 | { |
| 175 | if( mbedtls_test_info.mutex_usage_error != NULL && |
| 176 | mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED ) |
| 177 | { |
| 178 | /* Functionally, the test passed. But there was a mutex usage error, |
| 179 | * so mark the test as failed after all. */ |
| 180 | mbedtls_test_fail( "Mutex usage error", __LINE__, __FILE__ ); |
| 181 | } |
| 182 | mbedtls_test_info.mutex_usage_error = NULL; |
| 183 | } |
| 184 | |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 185 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |