| Paul Elliott | 17c119a | 2023-12-08 16:55:03 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file threading_helpers.h | 
|  | 3 | * | 
|  | 4 | * \brief This file contains the prototypes of helper functions for the purpose | 
|  | 5 | *        of testing threading. | 
|  | 6 | */ | 
|  | 7 |  | 
|  | 8 | /* | 
|  | 9 | *  Copyright The Mbed TLS Contributors | 
|  | 10 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
|  | 11 | */ | 
|  | 12 |  | 
|  | 13 | #ifndef THREADING_HELPERS_H | 
|  | 14 | #define THREADING_HELPERS_H | 
|  | 15 |  | 
|  | 16 | #if defined MBEDTLS_THREADING_C | 
|  | 17 |  | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 18 | #include "mbedtls/private_access.h" | 
|  | 19 | #include "mbedtls/build_info.h" | 
|  | 20 |  | 
|  | 21 | /* Most fields of publicly available structs are private and are wrapped with | 
|  | 22 | * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields | 
|  | 23 | * directly (without using the MBEDTLS_PRIVATE wrapper). */ | 
|  | 24 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS | 
|  | 25 |  | 
|  | 26 | #define MBEDTLS_ERR_THREADING_THREAD_ERROR                 -0x001F | 
|  | 27 |  | 
|  | 28 | #if defined(MBEDTLS_THREADING_PTHREAD) | 
|  | 29 | #include <pthread.h> | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 30 | #endif /* MBEDTLS_THREADING_PTHREAD */ | 
|  | 31 |  | 
|  | 32 | #if defined(MBEDTLS_THREADING_ALT) | 
|  | 33 | /* You should define the mbedtls_test_thread_t type in your header */ | 
|  | 34 | #include "threading_alt.h" | 
|  | 35 |  | 
|  | 36 | /** | 
|  | 37 | * \brief                   Set your alternate threading implementation | 
| Paul Elliott | ed3ba3c | 2024-01-19 16:44:23 +0000 | [diff] [blame] | 38 | *                          function pointers for test threads. If used, this | 
|  | 39 | *                          function must be called once in the main thread | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 40 | *                          before any other MbedTLS function is called. | 
|  | 41 | * | 
|  | 42 | * \note                    These functions are part of the testing API only and | 
|  | 43 | *                          thus not considered part of the public API of | 
|  | 44 | *                          MbedTLS and thus may change without notice. | 
|  | 45 | * | 
| Paul Elliott | ed3ba3c | 2024-01-19 16:44:23 +0000 | [diff] [blame] | 46 | * \param thread_create     The thread create function implementation. | 
|  | 47 | * \param thread_join       The thread join function implementation. | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 48 |  | 
|  | 49 | */ | 
|  | 50 | void mbedtls_test_thread_set_alt(int (*thread_create)(mbedtls_test_thread_t *thread, | 
|  | 51 | void *(*thread_func)( | 
|  | 52 | void *), | 
|  | 53 | void *thread_data), | 
|  | 54 | int (*thread_join)(mbedtls_test_thread_t *thread)); | 
|  | 55 |  | 
| Paul Elliott | 24e9a32 | 2024-02-01 12:26:23 +0000 | [diff] [blame] | 56 | #else /* MBEDTLS_THREADING_ALT*/ | 
|  | 57 |  | 
|  | 58 | typedef struct mbedtls_test_thread_t { | 
|  | 59 |  | 
|  | 60 | #if defined(MBEDTLS_THREADING_PTHREAD) | 
|  | 61 | pthread_t MBEDTLS_PRIVATE(thread); | 
|  | 62 | #else /* MBEDTLS_THREADING_PTHREAD */ | 
|  | 63 | /* Make sure this struct is always non-empty */ | 
|  | 64 | unsigned dummy; | 
|  | 65 | #endif | 
|  | 66 |  | 
|  | 67 | } mbedtls_test_thread_t; | 
|  | 68 |  | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 69 | #endif /* MBEDTLS_THREADING_ALT*/ | 
|  | 70 |  | 
|  | 71 | /** | 
| Paul Elliott | ae942ec | 2024-02-01 12:44:01 +0000 | [diff] [blame] | 72 | * \brief                   The function pointers for thread create and thread | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 73 | *                          join. | 
|  | 74 | * | 
| Paul Elliott | ae942ec | 2024-02-01 12:44:01 +0000 | [diff] [blame] | 75 | * \note                    These functions are part of the testing API only | 
|  | 76 | *                          and thus not considered part of the public API of | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 77 | *                          MbedTLS and thus may change without notice. | 
|  | 78 | * | 
| Paul Elliott | ae942ec | 2024-02-01 12:44:01 +0000 | [diff] [blame] | 79 | * \note                    All these functions are expected to work or | 
| Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 80 | *                          the result will be undefined. | 
|  | 81 | */ | 
|  | 82 | extern int (*mbedtls_test_thread_create)(mbedtls_test_thread_t *thread, | 
|  | 83 | void *(*thread_func)(void *), void *thread_data); | 
|  | 84 | extern int (*mbedtls_test_thread_join)(mbedtls_test_thread_t *thread); | 
|  | 85 |  | 
| Paul Elliott | 17c119a | 2023-12-08 16:55:03 +0000 | [diff] [blame] | 86 | #if defined(MBEDTLS_THREADING_PTHREAD) && defined(MBEDTLS_TEST_HOOKS) | 
|  | 87 | #define MBEDTLS_TEST_MUTEX_USAGE | 
|  | 88 | #endif | 
|  | 89 |  | 
|  | 90 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) | 
|  | 91 | /** | 
|  | 92 | *  Activate the mutex usage verification framework. See threading_helpers.c for | 
|  | 93 | *  information. | 
|  | 94 | */ | 
|  | 95 | void mbedtls_test_mutex_usage_init(void); | 
|  | 96 |  | 
|  | 97 | /** | 
|  | 98 | *  Deactivate the mutex usage verification framework. See threading_helpers.c | 
|  | 99 | *  for information. | 
|  | 100 | */ | 
|  | 101 | void mbedtls_test_mutex_usage_end(void); | 
|  | 102 |  | 
|  | 103 | /** | 
|  | 104 | *  Call this function after executing a test case to check for mutex usage | 
|  | 105 | * errors. | 
|  | 106 | */ | 
|  | 107 | void mbedtls_test_mutex_usage_check(void); | 
|  | 108 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ | 
|  | 109 |  | 
|  | 110 | #endif /* MBEDTLS_THREADING_C */ | 
|  | 111 |  | 
|  | 112 | #endif /* THREADING_HELPERS_H */ |