• Home
  • Raw
  • Download

Lines Matching full:mutex

1 /** Mutex usage verification framework. */
15 /** Mutex usage verification framework.
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
22 * The normal usage of a mutex is:
38 * An attempt to use an uninitialized mutex cannot be detected in general
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
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
51 * by an attempt to lock the mutex as well. A limitation of this framework is
87 static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex, in mbedtls_test_mutex_usage_error() argument
90 (void) mutex; in mbedtls_test_mutex_usage_error()
94 mbedtls_fprintf(stdout, "[mutex: %s] ", msg); in mbedtls_test_mutex_usage_error()
101 static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex) in mbedtls_test_wrap_mutex_init() argument
103 mutex_functions.init(mutex); in mbedtls_test_wrap_mutex_init()
104 if (mutex->is_valid) { in mbedtls_test_wrap_mutex_init()
109 static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex) in mbedtls_test_wrap_mutex_free() argument
111 switch (mutex->is_valid) { in mbedtls_test_wrap_mutex_free()
113 mbedtls_test_mutex_usage_error(mutex, "free without init or double free"); in mbedtls_test_wrap_mutex_free()
120 mbedtls_test_mutex_usage_error(mutex, "free without unlock"); in mbedtls_test_wrap_mutex_free()
123 mbedtls_test_mutex_usage_error(mutex, "corrupted state"); in mbedtls_test_wrap_mutex_free()
126 if (mutex->is_valid) { in mbedtls_test_wrap_mutex_free()
129 mutex_functions.free(mutex); in mbedtls_test_wrap_mutex_free()
132 static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex) in mbedtls_test_wrap_mutex_lock() argument
134 int ret = mutex_functions.lock(mutex); in mbedtls_test_wrap_mutex_lock()
135 switch (mutex->is_valid) { in mbedtls_test_wrap_mutex_lock()
137 mbedtls_test_mutex_usage_error(mutex, "lock without init"); in mbedtls_test_wrap_mutex_lock()
141 mutex->is_valid = 2; in mbedtls_test_wrap_mutex_lock()
145 mbedtls_test_mutex_usage_error(mutex, "double lock"); in mbedtls_test_wrap_mutex_lock()
148 mbedtls_test_mutex_usage_error(mutex, "corrupted state"); in mbedtls_test_wrap_mutex_lock()
154 static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex) in mbedtls_test_wrap_mutex_unlock() argument
156 int ret = mutex_functions.unlock(mutex); in mbedtls_test_wrap_mutex_unlock()
157 switch (mutex->is_valid) { in mbedtls_test_wrap_mutex_unlock()
159 mbedtls_test_mutex_usage_error(mutex, "unlock without init"); in mbedtls_test_wrap_mutex_unlock()
162 mbedtls_test_mutex_usage_error(mutex, "unlock without lock"); in mbedtls_test_wrap_mutex_unlock()
166 mutex->is_valid = MUTEX_IDLE; in mbedtls_test_wrap_mutex_unlock()
170 mbedtls_test_mutex_usage_error(mutex, "corrupted state"); in mbedtls_test_wrap_mutex_unlock()
191 /* A positive number (more init than free) means that a mutex resource in mbedtls_test_mutex_usage_check()
192 * is leaking (on platforms where a mutex consumes more than the in mbedtls_test_mutex_usage_check()
195 mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes); in mbedtls_test_mutex_usage_check()
203 /* Functionally, the test passed. But there was a mutex usage error, in mbedtls_test_mutex_usage_check()
205 mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__); in mbedtls_test_mutex_usage_check()