blob: 8cf95ee3367030c4184be1c5820e1931ecca6297 [file] [log] [blame]
Gilles Peskine1061ec62021-01-29 21:17:11 +01001/** 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 Peskine2a4c5982021-01-29 21:18:09 +010027/** 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 Peskinef96d3d82021-01-29 22:20:32 +010057 * 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 Peskine2a4c5982021-01-29 21:18:09 +010068 * 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 */
73enum value_of_mutex_is_valid
74{
75 MUTEX_FREED = 0, //!< Set by threading_mutex_free_pthread
76 MUTEX_IDLE = 1, //!< Set by threading_mutex_init_pthread and by our unlock
77 MUTEX_LOCKED = 2, //!< Set by our lock
78};
79
Gilles Peskine1061ec62021-01-29 21:17:11 +010080typedef struct
81{
82 void (*init)( mbedtls_threading_mutex_t * );
83 void (*free)( mbedtls_threading_mutex_t * );
84 int (*lock)( mbedtls_threading_mutex_t * );
85 int (*unlock)( mbedtls_threading_mutex_t * );
86} mutex_functions_t;
87static mutex_functions_t mutex_functions;
88
Gilles Peskinef96d3d82021-01-29 22:20:32 +010089/** The total number of calls to mbedtls_mutex_init(), minus the total number
90 * of calls to mbedtls_mutex_free().
91 *
92 * Reset to 0 after each test case.
93 */
94static int live_mutexes;
95
Gilles Peskine2a4c5982021-01-29 21:18:09 +010096static void mbedtls_test_mutex_usage_error( mbedtls_threading_mutex_t *mutex,
97 const char *msg )
98{
99 (void) mutex;
100 if( mbedtls_test_info.mutex_usage_error == NULL )
101 mbedtls_test_info.mutex_usage_error = msg;
102 mbedtls_fprintf( stdout, "[mutex: %s] ", msg );
103 /* Don't mark the test as failed yet. This way, if the test fails later
104 * for a functional reason, the test framework will report the message
105 * and location for this functional reason. If the test passes,
106 * mbedtls_test_mutex_usage_check() will mark it as failed. */
107}
108
Gilles Peskine1061ec62021-01-29 21:17:11 +0100109static void mbedtls_test_wrap_mutex_init( mbedtls_threading_mutex_t *mutex )
110{
111 mutex_functions.init( mutex );
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100112 if( mutex->is_valid )
113 ++live_mutexes;
Gilles Peskine1061ec62021-01-29 21:17:11 +0100114}
115
116static void mbedtls_test_wrap_mutex_free( mbedtls_threading_mutex_t *mutex )
117{
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100118 switch( mutex->is_valid )
119 {
120 case MUTEX_FREED:
121 mbedtls_test_mutex_usage_error( mutex, "free without init or double free" );
122 break;
123 case MUTEX_IDLE:
124 /* Do nothing. The underlying free function will reset is_valid
125 * to 0. */
126 break;
127 case MUTEX_LOCKED:
128 mbedtls_test_mutex_usage_error( mutex, "free without unlock" );
129 break;
130 default:
131 mbedtls_test_mutex_usage_error( mutex, "corrupted state" );
132 break;
133 }
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100134 if( mutex->is_valid )
135 --live_mutexes;
Gilles Peskine1061ec62021-01-29 21:17:11 +0100136 mutex_functions.free( mutex );
137}
138
139static int mbedtls_test_wrap_mutex_lock( mbedtls_threading_mutex_t *mutex )
140{
141 int ret = mutex_functions.lock( mutex );
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100142 switch( mutex->is_valid )
143 {
144 case MUTEX_FREED:
145 mbedtls_test_mutex_usage_error( mutex, "lock without init" );
146 break;
147 case MUTEX_IDLE:
148 if( ret == 0 )
149 mutex->is_valid = 2;
150 break;
151 case MUTEX_LOCKED:
152 mbedtls_test_mutex_usage_error( mutex, "double lock" );
153 break;
154 default:
155 mbedtls_test_mutex_usage_error( mutex, "corrupted state" );
156 break;
157 }
Gilles Peskine1061ec62021-01-29 21:17:11 +0100158 return( ret );
159}
160
161static int mbedtls_test_wrap_mutex_unlock( mbedtls_threading_mutex_t *mutex )
162{
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100163 int ret = mutex_functions.unlock( mutex );
164 switch( mutex->is_valid )
165 {
166 case MUTEX_FREED:
167 mbedtls_test_mutex_usage_error( mutex, "unlock without init" );
168 break;
169 case MUTEX_IDLE:
170 mbedtls_test_mutex_usage_error( mutex, "unlock without lock" );
171 break;
172 case MUTEX_LOCKED:
173 if( ret == 0 )
174 mutex->is_valid = MUTEX_IDLE;
175 break;
176 default:
177 mbedtls_test_mutex_usage_error( mutex, "corrupted state" );
178 break;
179 }
180 return( ret );
Gilles Peskine1061ec62021-01-29 21:17:11 +0100181}
182
183void mbedtls_test_mutex_usage_init( void )
184{
185 mutex_functions.init = mbedtls_mutex_init;
186 mutex_functions.free = mbedtls_mutex_free;
187 mutex_functions.lock = mbedtls_mutex_lock;
188 mutex_functions.unlock = mbedtls_mutex_unlock;
189 mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init;
190 mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free;
191 mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock;
192 mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock;
193}
194
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100195void mbedtls_test_mutex_usage_check( void )
196{
Gilles Peskinef96d3d82021-01-29 22:20:32 +0100197 if( live_mutexes != 0 )
198 {
199 /* A positive number (more init than free) means that a mutex resource
200 * is leaking (on platforms where a mutex consumes more than the
201 * mbedtls_threading_mutex_t object itself). The rare case of a
202 * negative number means a missing init somewhere. */
203 mbedtls_fprintf( stdout, "[mutex: %d leaked] ", live_mutexes );
204 live_mutexes = 0;
205 if( mbedtls_test_info.mutex_usage_error == NULL )
206 mbedtls_test_info.mutex_usage_error = "missing free";
207 }
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100208 if( mbedtls_test_info.mutex_usage_error != NULL &&
209 mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED )
210 {
211 /* Functionally, the test passed. But there was a mutex usage error,
212 * so mark the test as failed after all. */
213 mbedtls_test_fail( "Mutex usage error", __LINE__, __FILE__ );
214 }
215 mbedtls_test_info.mutex_usage_error = NULL;
216}
217
Gilles Peskine1061ec62021-01-29 21:17:11 +0100218#endif /* MBEDTLS_TEST_MUTEX_USAGE */