Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 1 | /** \file metatest.c |
| 2 | * |
| 3 | * \brief Test features of the test framework. |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 4 | * |
| 5 | * When you run this program, it runs a single "meta-test". A meta-test |
| 6 | * performs an operation which should be caught as a failure by our |
| 7 | * test framework. The meta-test passes if this program calls `exit` with |
| 8 | * a nonzero status, or aborts, or is terminated by a signal, or if the |
| 9 | * framework running the program considers the run an error (this happens |
| 10 | * with Valgrind for a memory leak). The non-success of the meta-test |
| 11 | * program means that the test failure has been caught correctly. |
| 12 | * |
| 13 | * Some failures are purely functional: the logic of the code causes the |
| 14 | * test result to be set to FAIL. Other failures come from extra |
| 15 | * instrumentation which is not present in a normal build; for example, |
| 16 | * Asan or Valgrind to detect memory leaks. This is reflected by the |
| 17 | * "platform" associated with each meta-test. |
| 18 | * |
| 19 | * Use the companion script `tests/scripts/run-metatests.sh` to run all |
| 20 | * the meta-tests for a given platform and validate that they trigger a |
| 21 | * detected failure as expected. |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Copyright The Mbed TLS Contributors |
| 26 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 27 | */ |
| 28 | |
| 29 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 30 | |
Gilles Peskine | d751406 | 2023-11-03 19:41:44 +0100 | [diff] [blame] | 31 | #include <mbedtls/debug.h> |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 32 | #include <mbedtls/platform.h> |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 33 | #include <mbedtls/platform_util.h> |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 34 | #include "test/helpers.h" |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 35 | #include "test/macros.h" |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 36 | #include "test/memory.h" |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <string.h> |
| 40 | |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 41 | #if defined(MBEDTLS_THREADING_C) |
| 42 | #include <mbedtls/threading.h> |
| 43 | #endif |
| 44 | |
Gilles Peskine | ac8cd66 | 2023-11-03 17:01:32 +0100 | [diff] [blame] | 45 | /* C99 feature missing from older versions of MSVC */ |
| 46 | #if (defined(_MSC_VER) && (_MSC_VER <= 1900)) |
| 47 | #define /*no-check-names*/ __func__ __FUNCTION__ |
| 48 | #endif |
| 49 | |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 51 | /* This is an external variable, so the compiler doesn't know that we're never |
| 52 | * changing its value. |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 53 | */ |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 54 | volatile int false_but_the_compiler_does_not_know = 0; |
| 55 | |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 56 | /* Hide calls to calloc/free from static checkers such as |
| 57 | * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about |
| 58 | * code where we do mean to cause a runtime error. */ |
| 59 | void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc; |
| 60 | void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free; |
| 61 | |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 62 | /* Set n bytes at the address p to all-bits-zero, in such a way that |
| 63 | * the compiler should not know that p is all-bits-zero. */ |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 64 | static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n) |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 65 | { |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 66 | memset((void *) p, false_but_the_compiler_does_not_know, n); |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 67 | } |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 68 | |
Gilles Peskine | 7d68a19 | 2023-11-23 12:33:39 +0100 | [diff] [blame] | 69 | /* Simulate an access to the given object, to avoid compiler optimizations |
| 70 | * in code that prepares or consumes the object. */ |
| 71 | static void do_nothing_with_object(void *p) |
| 72 | { |
| 73 | (void) p; |
| 74 | } |
| 75 | void(*volatile do_nothing_with_object_but_the_compiler_does_not_know)(void *) = |
| 76 | do_nothing_with_object; |
| 77 | |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 78 | |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 79 | /****************************************************************/ |
Gilles Peskine | 30380da | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 80 | /* Test framework features */ |
| 81 | /****************************************************************/ |
| 82 | |
| 83 | void meta_test_fail(const char *name) |
| 84 | { |
| 85 | (void) name; |
| 86 | mbedtls_test_fail("Forced test failure", __LINE__, __FILE__); |
| 87 | } |
| 88 | |
Paul Elliott | 7ebb3c5 | 2024-02-13 15:06:10 +0000 | [diff] [blame] | 89 | void meta_test_not_equal(const char *name) |
| 90 | { |
| 91 | int left = 20; |
| 92 | int right = 10; |
| 93 | |
| 94 | (void) name; |
| 95 | |
| 96 | TEST_EQUAL(left, right); |
| 97 | exit: |
| 98 | ; |
| 99 | } |
| 100 | |
| 101 | void meta_test_not_le_s(const char *name) |
| 102 | { |
| 103 | int left = 20; |
| 104 | int right = 10; |
| 105 | |
| 106 | (void) name; |
| 107 | |
| 108 | TEST_LE_S(left, right); |
| 109 | exit: |
| 110 | ; |
| 111 | } |
| 112 | |
| 113 | void meta_test_not_le_u(const char *name) |
| 114 | { |
| 115 | size_t left = 20; |
| 116 | size_t right = 10; |
| 117 | |
| 118 | (void) name; |
| 119 | |
| 120 | TEST_LE_U(left, right); |
| 121 | exit: |
| 122 | ; |
| 123 | } |
Gilles Peskine | 30380da | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 124 | |
| 125 | /****************************************************************/ |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 126 | /* Platform features */ |
| 127 | /****************************************************************/ |
| 128 | |
| 129 | void null_pointer_dereference(const char *name) |
| 130 | { |
| 131 | (void) name; |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 132 | volatile char *volatile p; |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 133 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 134 | /* Undefined behavior (read from null data pointer) */ |
Gilles Peskine | 967714d | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 135 | mbedtls_printf("%p -> %u\n", p, (unsigned) *p); |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void null_pointer_call(const char *name) |
| 139 | { |
| 140 | (void) name; |
Gilles Peskine | 226f1bc | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 141 | unsigned(*volatile p)(void); |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 142 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 143 | /* Undefined behavior (execute null function pointer) */ |
Gilles Peskine | db2b5c9 | 2023-11-03 10:58:57 +0100 | [diff] [blame] | 144 | /* The pointer representation may be truncated, but we don't care: |
| 145 | * the only point of printing it is to have some use of the pointer |
| 146 | * to dissuade the compiler from optimizing it away. */ |
| 147 | mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p()); |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /****************************************************************/ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 152 | /* Memory */ |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 153 | /****************************************************************/ |
| 154 | |
| 155 | void read_after_free(const char *name) |
| 156 | { |
| 157 | (void) name; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 158 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 159 | *p = 'a'; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 160 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 161 | /* Undefined behavior (read after free) */ |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 162 | mbedtls_printf("%u\n", (unsigned) *p); |
| 163 | } |
| 164 | |
| 165 | void double_free(const char *name) |
| 166 | { |
| 167 | (void) name; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 168 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 169 | *p = 'a'; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 170 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 171 | /* Undefined behavior (double free) */ |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 172 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void read_uninitialized_stack(const char *name) |
| 176 | { |
| 177 | (void) name; |
Gilles Peskine | efc57ca | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 178 | char buf[1]; |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 179 | if (false_but_the_compiler_does_not_know) { |
| 180 | buf[0] = '!'; |
| 181 | } |
Gilles Peskine | efc57ca | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 182 | char *volatile p = buf; |
| 183 | if (*p != 0) { |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 184 | /* Unspecified result (read from uninitialized memory) */ |
Gilles Peskine | efc57ca | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 185 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void memory_leak(const char *name) |
| 190 | { |
| 191 | (void) name; |
Gilles Peskine | e9616fd | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 192 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | 5383351 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 193 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 194 | /* Leak of a heap object */ |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 197 | /* name = "test_memory_poison_%(start)_%(offset)_%(count)_%(direction)" |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 198 | * Poison a region starting at start from an 8-byte aligned origin, |
| 199 | * encompassing count bytes. Access the region at offset from the start. |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 200 | * %(start), %(offset) and %(count) are decimal integers. |
| 201 | * %(direction) is either the character 'r' for read or 'w' for write. |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 202 | */ |
| 203 | void test_memory_poison(const char *name) |
| 204 | { |
| 205 | size_t start = 0, offset = 0, count = 0; |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 206 | char direction = 'r'; |
Gilles Peskine | d751406 | 2023-11-03 19:41:44 +0100 | [diff] [blame] | 207 | if (sscanf(name, |
| 208 | "%*[^0-9]%" MBEDTLS_PRINTF_SIZET |
| 209 | "%*[^0-9]%" MBEDTLS_PRINTF_SIZET |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 210 | "%*[^0-9]%" MBEDTLS_PRINTF_SIZET |
| 211 | "_%c", |
| 212 | &start, &offset, &count, &direction) != 4) { |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 213 | mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | union { |
| 218 | long long ll; |
| 219 | unsigned char buf[32]; |
| 220 | } aligned; |
| 221 | memset(aligned.buf, 'a', sizeof(aligned.buf)); |
| 222 | |
| 223 | if (start > sizeof(aligned.buf)) { |
Gilles Peskine | d751406 | 2023-11-03 19:41:44 +0100 | [diff] [blame] | 224 | mbedtls_fprintf(stderr, |
| 225 | "%s: start=%" MBEDTLS_PRINTF_SIZET |
| 226 | " > size=%" MBEDTLS_PRINTF_SIZET, |
| 227 | __func__, start, sizeof(aligned.buf)); |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | if (start + count > sizeof(aligned.buf)) { |
Gilles Peskine | d751406 | 2023-11-03 19:41:44 +0100 | [diff] [blame] | 231 | mbedtls_fprintf(stderr, |
| 232 | "%s: start+count=%" MBEDTLS_PRINTF_SIZET |
| 233 | " > size=%" MBEDTLS_PRINTF_SIZET, |
| 234 | __func__, start + count, sizeof(aligned.buf)); |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | if (offset >= count) { |
Gilles Peskine | d751406 | 2023-11-03 19:41:44 +0100 | [diff] [blame] | 238 | mbedtls_fprintf(stderr, |
| 239 | "%s: offset=%" MBEDTLS_PRINTF_SIZET |
| 240 | " >= count=%" MBEDTLS_PRINTF_SIZET, |
| 241 | __func__, offset, count); |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
| 245 | MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count); |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 246 | |
| 247 | if (direction == 'w') { |
| 248 | aligned.buf[start + offset] = 'b'; |
Gilles Peskine | 7d68a19 | 2023-11-23 12:33:39 +0100 | [diff] [blame] | 249 | do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf); |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 250 | } else { |
Gilles Peskine | 7d68a19 | 2023-11-23 12:33:39 +0100 | [diff] [blame] | 251 | do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf); |
Gilles Peskine | 0c7d3ed | 2023-11-22 18:22:07 +0100 | [diff] [blame] | 252 | mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]); |
| 253 | } |
Gilles Peskine | 071d144 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 256 | |
| 257 | /****************************************************************/ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 258 | /* Threading */ |
| 259 | /****************************************************************/ |
| 260 | |
| 261 | void mutex_lock_not_initialized(const char *name) |
| 262 | { |
| 263 | (void) name; |
Gilles Peskine | 96c87c4 | 2023-11-16 15:09:48 +0100 | [diff] [blame] | 264 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 265 | mbedtls_threading_mutex_t mutex; |
| 266 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 267 | /* This mutex usage error is detected by our test framework's mutex usage |
| 268 | * verification framework. See tests/src/threading_helpers.c. Other |
| 269 | * threading implementations (e.g. pthread without our instrumentation) |
| 270 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 271 | TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0); |
| 272 | exit: |
| 273 | ; |
| 274 | #endif |
| 275 | } |
| 276 | |
| 277 | void mutex_unlock_not_initialized(const char *name) |
| 278 | { |
| 279 | (void) name; |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 280 | #if defined(MBEDTLS_THREADING_C) |
| 281 | mbedtls_threading_mutex_t mutex; |
| 282 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 283 | /* This mutex usage error is detected by our test framework's mutex usage |
| 284 | * verification framework. See tests/src/threading_helpers.c. Other |
| 285 | * threading implementations (e.g. pthread without our instrumentation) |
| 286 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 287 | TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0); |
| 288 | exit: |
| 289 | ; |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | void mutex_free_not_initialized(const char *name) |
| 294 | { |
| 295 | (void) name; |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 296 | #if defined(MBEDTLS_THREADING_C) |
| 297 | mbedtls_threading_mutex_t mutex; |
| 298 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 299 | /* This mutex usage error is detected by our test framework's mutex usage |
| 300 | * verification framework. See tests/src/threading_helpers.c. Other |
| 301 | * threading implementations (e.g. pthread without our instrumentation) |
| 302 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 303 | mbedtls_mutex_free(&mutex); |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | void mutex_double_init(const char *name) |
| 308 | { |
| 309 | (void) name; |
| 310 | #if defined(MBEDTLS_THREADING_C) |
| 311 | mbedtls_threading_mutex_t mutex; |
| 312 | mbedtls_mutex_init(&mutex); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 313 | /* This mutex usage error is detected by our test framework's mutex usage |
| 314 | * verification framework. See tests/src/threading_helpers.c. Other |
| 315 | * threading implementations (e.g. pthread without our instrumentation) |
| 316 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 317 | mbedtls_mutex_init(&mutex); |
| 318 | mbedtls_mutex_free(&mutex); |
| 319 | #endif |
| 320 | } |
| 321 | |
| 322 | void mutex_double_free(const char *name) |
| 323 | { |
| 324 | (void) name; |
| 325 | #if defined(MBEDTLS_THREADING_C) |
| 326 | mbedtls_threading_mutex_t mutex; |
| 327 | mbedtls_mutex_init(&mutex); |
| 328 | mbedtls_mutex_free(&mutex); |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 329 | /* This mutex usage error is detected by our test framework's mutex usage |
| 330 | * verification framework. See tests/src/threading_helpers.c. Other |
| 331 | * threading implementations (e.g. pthread without our instrumentation) |
| 332 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 333 | mbedtls_mutex_free(&mutex); |
| 334 | #endif |
| 335 | } |
| 336 | |
| 337 | void mutex_leak(const char *name) |
| 338 | { |
| 339 | (void) name; |
Gilles Peskine | 96c87c4 | 2023-11-16 15:09:48 +0100 | [diff] [blame] | 340 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 341 | mbedtls_threading_mutex_t mutex; |
| 342 | mbedtls_mutex_init(&mutex); |
| 343 | #endif |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 344 | /* This mutex usage error is detected by our test framework's mutex usage |
| 345 | * verification framework. See tests/src/threading_helpers.c. Other |
| 346 | * threading implementations (e.g. pthread without our instrumentation) |
| 347 | * might consider this normal usage. */ |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | /****************************************************************/ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 352 | /* Command line entry point */ |
| 353 | /****************************************************************/ |
| 354 | |
| 355 | typedef struct { |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 356 | /** Command line argument that will trigger that metatest. |
| 357 | * |
| 358 | * Conventionally matches "[a-z0-9_]+". */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 359 | const char *name; |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 360 | |
| 361 | /** Platform under which that metatest is valid. |
| 362 | * |
| 363 | * - "any": should work anywhere. |
| 364 | * - "asan": triggers ASan (Address Sanitizer). |
| 365 | * - "msan": triggers MSan (Memory Sanitizer). |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 366 | * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS, |
| 367 | * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test |
| 368 | * framework (see tests/src/threading_helpers.c). |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 369 | */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 370 | const char *platform; |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 371 | |
| 372 | /** Function that performs the metatest. |
| 373 | * |
| 374 | * The function receives the name as an argument. This allows using the |
| 375 | * same function to perform multiple variants of a test based on the name. |
| 376 | * |
| 377 | * When executed on a conforming platform, the function is expected to |
| 378 | * either cause a test failure (mbedtls_test_fail()), or cause the |
| 379 | * program to abort in some way (e.g. by causing a segfault or by |
| 380 | * triggering a sanitizer). |
| 381 | * |
| 382 | * When executed on a non-conforming platform, the function may return |
| 383 | * normally or may have unpredictable behavior. |
| 384 | */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 385 | void (*entry_point)(const char *name); |
| 386 | } metatest_t; |
| 387 | |
Wenxing Hou | 20e964f | 2024-06-19 11:04:13 +0800 | [diff] [blame] | 388 | /* The list of available meta-tests. Remember to register new functions here! |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 389 | * |
| 390 | * Note that we always compile all the functions, so that `metatest --list` |
| 391 | * will always list all the available meta-tests. |
Gilles Peskine | e00255c | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 392 | * |
| 393 | * See the documentation of metatest_t::platform for the meaning of |
| 394 | * platform values. |
Gilles Peskine | c41133b | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 395 | */ |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 396 | metatest_t metatests[] = { |
Gilles Peskine | 30380da | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 397 | { "test_fail", "any", meta_test_fail }, |
Paul Elliott | 7ebb3c5 | 2024-02-13 15:06:10 +0000 | [diff] [blame] | 398 | { "test_not_equal", "any", meta_test_not_equal }, |
| 399 | { "test_not_le_s", "any", meta_test_not_le_s }, |
| 400 | { "test_not_le_u", "any", meta_test_not_le_u }, |
Gilles Peskine | 21d8d59 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 401 | { "null_dereference", "any", null_pointer_dereference }, |
| 402 | { "null_call", "any", null_pointer_call }, |
Gilles Peskine | 970584f | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 403 | { "read_after_free", "asan", read_after_free }, |
| 404 | { "double_free", "asan", double_free }, |
| 405 | { "read_uninitialized_stack", "msan", read_uninitialized_stack }, |
| 406 | { "memory_leak", "asan", memory_leak }, |
David Horstmann | 1b421b1 | 2024-01-17 14:53:08 +0000 | [diff] [blame] | 407 | { "test_memory_poison_0_0_8_r", "poison", test_memory_poison }, |
| 408 | { "test_memory_poison_0_0_8_w", "poison", test_memory_poison }, |
| 409 | { "test_memory_poison_0_7_8_r", "poison", test_memory_poison }, |
| 410 | { "test_memory_poison_0_7_8_w", "poison", test_memory_poison }, |
| 411 | { "test_memory_poison_0_0_1_r", "poison", test_memory_poison }, |
| 412 | { "test_memory_poison_0_0_1_w", "poison", test_memory_poison }, |
| 413 | { "test_memory_poison_0_1_2_r", "poison", test_memory_poison }, |
| 414 | { "test_memory_poison_0_1_2_w", "poison", test_memory_poison }, |
| 415 | { "test_memory_poison_7_0_8_r", "poison", test_memory_poison }, |
| 416 | { "test_memory_poison_7_0_8_w", "poison", test_memory_poison }, |
| 417 | { "test_memory_poison_7_7_8_r", "poison", test_memory_poison }, |
| 418 | { "test_memory_poison_7_7_8_w", "poison", test_memory_poison }, |
| 419 | { "test_memory_poison_7_0_1_r", "poison", test_memory_poison }, |
| 420 | { "test_memory_poison_7_0_1_w", "poison", test_memory_poison }, |
| 421 | { "test_memory_poison_7_1_2_r", "poison", test_memory_poison }, |
| 422 | { "test_memory_poison_7_1_2_w", "poison", test_memory_poison }, |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 423 | { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized }, |
| 424 | { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized }, |
| 425 | { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized }, |
| 426 | { "mutex_double_init", "pthread", mutex_double_init }, |
| 427 | { "mutex_double_free", "pthread", mutex_double_free }, |
| 428 | { "mutex_leak", "pthread", mutex_leak }, |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 429 | { NULL, NULL, NULL } |
| 430 | }; |
| 431 | |
| 432 | static void help(FILE *out, const char *argv0) |
| 433 | { |
| 434 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 435 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 436 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 437 | } |
| 438 | |
| 439 | int main(int argc, char *argv[]) |
| 440 | { |
| 441 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 442 | if (argc != 2) { |
| 443 | help(stderr, argv0); |
| 444 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 445 | } |
| 446 | |
| 447 | /* Support "-help", "--help", "--list", etc. */ |
| 448 | const char *command = argv[1]; |
| 449 | while (*command == '-') { |
| 450 | ++command; |
| 451 | } |
| 452 | |
| 453 | if (strcmp(argv[1], "help") == 0) { |
| 454 | help(stdout, argv0); |
| 455 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 456 | } |
| 457 | if (strcmp(argv[1], "list") == 0) { |
| 458 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 459 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 460 | } |
| 461 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 462 | } |
| 463 | |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 464 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 465 | mbedtls_test_mutex_usage_init(); |
| 466 | #endif |
| 467 | |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 468 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 469 | if (strcmp(argv[1], p->name) == 0) { |
| 470 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 471 | p->entry_point(argv[1]); |
Gilles Peskine | e38eb79 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 472 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 473 | mbedtls_test_mutex_usage_check(); |
| 474 | #endif |
Gilles Peskine | c33940d | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 475 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 476 | argv[1], (int) mbedtls_test_info.result); |
| 477 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 478 | MBEDTLS_EXIT_SUCCESS : |
| 479 | MBEDTLS_EXIT_FAILURE); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 484 | argv0, command); |
| 485 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 486 | } |