mirror of
				https://github.com/Mbed-TLS/mbedtls.git
				synced 2025-10-30 10:45:34 +03:00 
			
		
		
		
	Add ability to exclude mutex from tests
We need to be able to exclude mbedtls_test_info_mutex() from the normal tests, as this mutex has to be locked to report mutex errors, and also reports as leaked, due to where it is initialised / free'd. Signed-off-by: Paul Elliott <paul.elliott@arm.com>
This commit is contained in:
		| @@ -117,10 +117,28 @@ static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex, | |||||||
|      * mbedtls_test_mutex_usage_check() will mark it as failed. */ |      * mbedtls_test_mutex_usage_check() will mark it as failed. */ | ||||||
| } | } | ||||||
|  |  | ||||||
|  | extern mbedtls_threading_mutex_t mbedtls_test_info_mutex; | ||||||
|  |  | ||||||
|  | static int mbedtls_test_mutex_can_test(mbedtls_threading_mutex_t *mutex) | ||||||
|  | { | ||||||
|  |     /* If we attempt to run tests on this mutex then we are going to run into a | ||||||
|  |      * couple of problems: | ||||||
|  |      * 1. If any test on this mutex fails, we are going to deadlock when | ||||||
|  |      * reporting that failure, as we already hold the mutex at that point. | ||||||
|  |      * 2. Given the 'global' position of the initialization and free of this | ||||||
|  |      * mutex, it will be shown as leaked on the first test run. */ | ||||||
|  |     if (mutex == &mbedtls_test_info_mutex) { | ||||||
|  |         return 0; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return 1; | ||||||
|  | } | ||||||
|  |  | ||||||
| static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex) | static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex) | ||||||
| { | { | ||||||
|     mutex_functions.init(mutex); |     mutex_functions.init(mutex); | ||||||
|  |  | ||||||
|  |     if (mbedtls_test_mutex_can_test(mutex)) { | ||||||
|         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { | ||||||
|             mutex->state = MUTEX_IDLE; |             mutex->state = MUTEX_IDLE; | ||||||
|             ++live_mutexes; |             ++live_mutexes; | ||||||
| @@ -128,9 +146,11 @@ static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex) | |||||||
|             mutex_functions.unlock(&mbedtls_test_mutex_mutex); |             mutex_functions.unlock(&mbedtls_test_mutex_mutex); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex) | static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex) | ||||||
| { | { | ||||||
|  |     if (mbedtls_test_mutex_can_test(mutex)) { | ||||||
|         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { | ||||||
|  |  | ||||||
|             switch (mutex->state) { |             switch (mutex->state) { | ||||||
| @@ -151,6 +171,8 @@ static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex) | |||||||
|  |  | ||||||
|             mutex_functions.unlock(&mbedtls_test_mutex_mutex); |             mutex_functions.unlock(&mbedtls_test_mutex_mutex); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     mutex_functions.free(mutex); |     mutex_functions.free(mutex); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -160,6 +182,8 @@ static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex) | |||||||
|      * is to hold the passed in and internal mutex - otherwise we create a race |      * is to hold the passed in and internal mutex - otherwise we create a race | ||||||
|      * condition. */ |      * condition. */ | ||||||
|     int ret = mutex_functions.lock(mutex); |     int ret = mutex_functions.lock(mutex); | ||||||
|  |  | ||||||
|  |     if (mbedtls_test_mutex_can_test(mutex)) { | ||||||
|         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { | ||||||
|             switch (mutex->state) { |             switch (mutex->state) { | ||||||
|                 case MUTEX_FREED: |                 case MUTEX_FREED: | ||||||
| @@ -180,6 +204,8 @@ static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex) | |||||||
|  |  | ||||||
|             mutex_functions.unlock(&mbedtls_test_mutex_mutex); |             mutex_functions.unlock(&mbedtls_test_mutex_mutex); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     return ret; |     return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -188,6 +214,7 @@ static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex) | |||||||
|     /* Lock the internal mutex first and change state, so that the only way to |     /* Lock the internal mutex first and change state, so that the only way to | ||||||
|      * change the state is to hold the passed in and internal mutex - otherwise |      * change the state is to hold the passed in and internal mutex - otherwise | ||||||
|      * we create a race condition. */ |      * we create a race condition. */ | ||||||
|  |     if (mbedtls_test_mutex_can_test(mutex)) { | ||||||
|         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { |         if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { | ||||||
|             switch (mutex->state) { |             switch (mutex->state) { | ||||||
|                 case MUTEX_FREED: |                 case MUTEX_FREED: | ||||||
| @@ -205,6 +232,8 @@ static int mbedtls_test_wrap_mutex_unlock(mbedtls_threading_mutex_t *mutex) | |||||||
|             } |             } | ||||||
|             mutex_functions.unlock(&mbedtls_test_mutex_mutex); |             mutex_functions.unlock(&mbedtls_test_mutex_mutex); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     return mutex_functions.unlock(mutex); |     return mutex_functions.unlock(mutex); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user