Lines Matching defs:mutex
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
88 * The normal usage of a mutex is:
104 * An attempt to use an uninitialized mutex cannot be detected in general
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
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
117 * by an attempt to lock the mutex as well. A limitation of this framework is
147 * The mutex used to guard live_mutexes below and access to the status variable
150 * mutex. This is for a couple of reasons:
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
173 static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex,
176 (void) mutex;
179 mbedtls_fprintf(stdout, "[mutex: %s] ", msg);
186 static int mbedtls_test_mutex_can_test(mbedtls_threading_mutex_t *mutex)
188 /* If we attempt to run tests on this mutex then we are going to run into a
190 * 1. If any test on this mutex fails, we are going to deadlock when
191 * reporting that failure, as we already hold the mutex at that point.
193 * mutex, it will be shown as leaked on the first test run. */
194 if (mutex == mbedtls_test_get_info_mutex()) {
201 static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex)
203 mutex_functions.init(mutex);
205 if (mbedtls_test_mutex_can_test(mutex)) {
207 mutex->state = MUTEX_IDLE;
215 static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex)
217 if (mbedtls_test_mutex_can_test(mutex)) {
220 switch (mutex->state) {
222 mbedtls_test_mutex_usage_error(mutex, "free without init or double free");
225 mutex->state = MUTEX_FREED;
229 mbedtls_test_mutex_usage_error(mutex, "free without unlock");
232 mbedtls_test_mutex_usage_error(mutex, "corrupted state");
240 mutex_functions.free(mutex);
243 static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex)
245 /* Lock the passed in mutex first, so that the only way to change the state
246 * is to hold the passed in and internal mutex - otherwise we create a race
248 int ret = mutex_functions.lock(mutex);
250 if (mbedtls_test_mutex_can_test(mutex)) {
252 switch (mutex->state) {
254 mbedtls_test_mutex_usage_error(mutex, "lock without init");
258 mutex->state = MUTEX_LOCKED;
262 mbedtls_test_mutex_usage_error(mutex, "double lock");
265 mbedtls_test_mutex_usage_error(mutex, "corrupted state");
276 static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex)
278 /* Lock the internal mutex first and change state, so that the only way to
279 * change the state is to hold the passed in and internal mutex - otherwise
281 if (mbedtls_test_mutex_can_test(mutex)) {
283 switch (mutex->state) {
285 mbedtls_test_mutex_usage_error(mutex, "unlock without init");
288 mbedtls_test_mutex_usage_error(mutex, "unlock without lock");
291 mutex->state = MUTEX_IDLE;
294 mbedtls_test_mutex_usage_error(mutex, "corrupted state");
301 return mutex_functions.unlock(mutex);
322 /* A positive number (more init than free) means that a mutex resource
323 * is leaking (on platforms where a mutex consumes more than the
326 mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes);
332 /* Functionally, the test passed. But there was a mutex usage error,