Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 1 | /** \file metatest.c |
| 2 | * |
| 3 | * \brief Test features of the test framework. |
Gilles Peskine | cce0012 | 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 | 33406b6 | 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 | |
| 31 | #include <mbedtls/platform.h> |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 32 | #include <mbedtls/platform_util.h> |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 33 | #include "test/helpers.h" |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 34 | #include "test/macros.h" |
Gilles Peskine | d29cce9 | 2023-11-02 20:49:34 +0100 | [diff] [blame^] | 35 | #include "test/memory.h" |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 40 | #if defined(MBEDTLS_THREADING_C) |
| 41 | #include <mbedtls/threading.h> |
| 42 | #endif |
| 43 | |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 44 | |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 45 | /* This is an external variable, so the compiler doesn't know that we're never |
| 46 | * changing its value. |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 47 | */ |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 48 | volatile int false_but_the_compiler_does_not_know = 0; |
| 49 | |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 50 | /* Hide calls to calloc/free from static checkers such as |
| 51 | * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about |
| 52 | * code where we do mean to cause a runtime error. */ |
| 53 | void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc; |
| 54 | void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free; |
| 55 | |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 56 | /* Set n bytes at the address p to all-bits-zero, in such a way that |
| 57 | * the compiler should not know that p is all-bits-zero. */ |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 58 | static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n) |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 59 | { |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 60 | memset((void *) p, false_but_the_compiler_does_not_know, n); |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 61 | } |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 62 | |
| 63 | |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 64 | /****************************************************************/ |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 65 | /* Test framework features */ |
| 66 | /****************************************************************/ |
| 67 | |
| 68 | void meta_test_fail(const char *name) |
| 69 | { |
| 70 | (void) name; |
| 71 | mbedtls_test_fail("Forced test failure", __LINE__, __FILE__); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /****************************************************************/ |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 76 | /* Platform features */ |
| 77 | /****************************************************************/ |
| 78 | |
| 79 | void null_pointer_dereference(const char *name) |
| 80 | { |
| 81 | (void) name; |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 82 | volatile char *volatile p; |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 83 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 84 | /* Undefined behavior (read from null data pointer) */ |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 85 | mbedtls_printf("%p -> %u\n", p, (unsigned) *p); |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void null_pointer_call(const char *name) |
| 89 | { |
| 90 | (void) name; |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 91 | unsigned(*volatile p)(void); |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 92 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 93 | /* Undefined behavior (execute null function pointer) */ |
Gilles Peskine | f0d5cf9 | 2023-11-03 10:58:57 +0100 | [diff] [blame] | 94 | /* The pointer representation may be truncated, but we don't care: |
| 95 | * the only point of printing it is to have some use of the pointer |
| 96 | * to dissuade the compiler from optimizing it away. */ |
| 97 | mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p()); |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | /****************************************************************/ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 102 | /* Memory */ |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 103 | /****************************************************************/ |
| 104 | |
| 105 | void read_after_free(const char *name) |
| 106 | { |
| 107 | (void) name; |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 108 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 109 | *p = 'a'; |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 110 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 111 | /* Undefined behavior (read after free) */ |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 112 | mbedtls_printf("%u\n", (unsigned) *p); |
| 113 | } |
| 114 | |
| 115 | void double_free(const char *name) |
| 116 | { |
| 117 | (void) name; |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 118 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 119 | *p = 'a'; |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 120 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 121 | /* Undefined behavior (double free) */ |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 122 | free_but_the_compiler_does_not_know((void *) p); |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void read_uninitialized_stack(const char *name) |
| 126 | { |
| 127 | (void) name; |
Gilles Peskine | ccb1215 | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 128 | char buf[1]; |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 129 | if (false_but_the_compiler_does_not_know) { |
| 130 | buf[0] = '!'; |
| 131 | } |
Gilles Peskine | ccb1215 | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 132 | char *volatile p = buf; |
| 133 | if (*p != 0) { |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 134 | /* Unspecified result (read from uninitialized memory) */ |
Gilles Peskine | ccb1215 | 2023-11-10 11:35:36 +0100 | [diff] [blame] | 135 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | void memory_leak(const char *name) |
| 140 | { |
| 141 | (void) name; |
Gilles Peskine | 7a715c4 | 2023-11-21 13:42:40 +0100 | [diff] [blame] | 142 | volatile char *p = calloc_but_the_compiler_does_not_know(1, 1); |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 143 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 144 | /* Leak of a heap object */ |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Gilles Peskine | d29cce9 | 2023-11-02 20:49:34 +0100 | [diff] [blame^] | 147 | /* name = "test_memory_poison_%(start)_%(offset)_%(count)" |
| 148 | * Poison a region starting at start from an 8-byte aligned origin, |
| 149 | * encompassing count bytes. Access the region at offset from the start. |
| 150 | */ |
| 151 | void test_memory_poison(const char *name) |
| 152 | { |
| 153 | size_t start = 0, offset = 0, count = 0; |
| 154 | if (sscanf(name, "%*[^0-9]%zu%*[^0-9]%zu%*[^0-9]%zu", |
| 155 | &start, &offset, &count) != 3) { |
| 156 | mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | union { |
| 161 | long long ll; |
| 162 | unsigned char buf[32]; |
| 163 | } aligned; |
| 164 | memset(aligned.buf, 'a', sizeof(aligned.buf)); |
| 165 | |
| 166 | if (start > sizeof(aligned.buf)) { |
| 167 | mbedtls_fprintf(stderr, "%s: start=%zu > size=%zu", __func__, |
| 168 | start, sizeof(aligned.buf)); |
| 169 | return; |
| 170 | } |
| 171 | if (start + count > sizeof(aligned.buf)) { |
| 172 | mbedtls_fprintf(stderr, "%s: start+count=%zu > size=%zu", __func__, |
| 173 | start + count, sizeof(aligned.buf)); |
| 174 | return; |
| 175 | } |
| 176 | if (offset >= count) { |
| 177 | mbedtls_fprintf(stderr, "%s: offset=%zu >= count=%zu", __func__, |
| 178 | offset, count); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count); |
| 183 | mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]); |
| 184 | } |
| 185 | |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 186 | |
| 187 | /****************************************************************/ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 188 | /* Threading */ |
| 189 | /****************************************************************/ |
| 190 | |
| 191 | void mutex_lock_not_initialized(const char *name) |
| 192 | { |
| 193 | (void) name; |
Gilles Peskine | ad2a17e | 2023-11-16 15:09:48 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 195 | mbedtls_threading_mutex_t mutex; |
| 196 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 197 | /* This mutex usage error is detected by our test framework's mutex usage |
| 198 | * verification framework. See tests/src/threading_helpers.c. Other |
| 199 | * threading implementations (e.g. pthread without our instrumentation) |
| 200 | * might consider this normal usage. */ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 201 | TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0); |
| 202 | exit: |
| 203 | ; |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | void mutex_unlock_not_initialized(const char *name) |
| 208 | { |
| 209 | (void) name; |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 210 | #if defined(MBEDTLS_THREADING_C) |
| 211 | mbedtls_threading_mutex_t mutex; |
| 212 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 213 | /* This mutex usage error is detected by our test framework's mutex usage |
| 214 | * verification framework. See tests/src/threading_helpers.c. Other |
| 215 | * threading implementations (e.g. pthread without our instrumentation) |
| 216 | * might consider this normal usage. */ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 217 | TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0); |
| 218 | exit: |
| 219 | ; |
| 220 | #endif |
| 221 | } |
| 222 | |
| 223 | void mutex_free_not_initialized(const char *name) |
| 224 | { |
| 225 | (void) name; |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 226 | #if defined(MBEDTLS_THREADING_C) |
| 227 | mbedtls_threading_mutex_t mutex; |
| 228 | memset(&mutex, 0, sizeof(mutex)); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 229 | /* This mutex usage error is detected by our test framework's mutex usage |
| 230 | * verification framework. See tests/src/threading_helpers.c. Other |
| 231 | * threading implementations (e.g. pthread without our instrumentation) |
| 232 | * might consider this normal usage. */ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 233 | mbedtls_mutex_free(&mutex); |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | void mutex_double_init(const char *name) |
| 238 | { |
| 239 | (void) name; |
| 240 | #if defined(MBEDTLS_THREADING_C) |
| 241 | mbedtls_threading_mutex_t mutex; |
| 242 | mbedtls_mutex_init(&mutex); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 243 | /* This mutex usage error is detected by our test framework's mutex usage |
| 244 | * verification framework. See tests/src/threading_helpers.c. Other |
| 245 | * threading implementations (e.g. pthread without our instrumentation) |
| 246 | * might consider this normal usage. */ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 247 | mbedtls_mutex_init(&mutex); |
| 248 | mbedtls_mutex_free(&mutex); |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | void mutex_double_free(const char *name) |
| 253 | { |
| 254 | (void) name; |
| 255 | #if defined(MBEDTLS_THREADING_C) |
| 256 | mbedtls_threading_mutex_t mutex; |
| 257 | mbedtls_mutex_init(&mutex); |
| 258 | mbedtls_mutex_free(&mutex); |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 259 | /* This mutex usage error is detected by our test framework's mutex usage |
| 260 | * verification framework. See tests/src/threading_helpers.c. Other |
| 261 | * threading implementations (e.g. pthread without our instrumentation) |
| 262 | * might consider this normal usage. */ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 263 | mbedtls_mutex_free(&mutex); |
| 264 | #endif |
| 265 | } |
| 266 | |
| 267 | void mutex_leak(const char *name) |
| 268 | { |
| 269 | (void) name; |
Gilles Peskine | ad2a17e | 2023-11-16 15:09:48 +0100 | [diff] [blame] | 270 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 271 | mbedtls_threading_mutex_t mutex; |
| 272 | mbedtls_mutex_init(&mutex); |
| 273 | #endif |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 274 | /* This mutex usage error is detected by our test framework's mutex usage |
| 275 | * verification framework. See tests/src/threading_helpers.c. Other |
| 276 | * threading implementations (e.g. pthread without our instrumentation) |
| 277 | * might consider this normal usage. */ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | /****************************************************************/ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 282 | /* Command line entry point */ |
| 283 | /****************************************************************/ |
| 284 | |
| 285 | typedef struct { |
Gilles Peskine | cce0012 | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 286 | /** Command line argument that will trigger that metatest. |
| 287 | * |
| 288 | * Conventionally matches "[a-z0-9_]+". */ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 289 | const char *name; |
Gilles Peskine | cce0012 | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 290 | |
| 291 | /** Platform under which that metatest is valid. |
| 292 | * |
| 293 | * - "any": should work anywhere. |
| 294 | * - "asan": triggers ASan (Address Sanitizer). |
| 295 | * - "msan": triggers MSan (Memory Sanitizer). |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 296 | * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS, |
| 297 | * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test |
| 298 | * framework (see tests/src/threading_helpers.c). |
Gilles Peskine | cce0012 | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 299 | */ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 300 | const char *platform; |
Gilles Peskine | cce0012 | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 301 | |
| 302 | /** Function that performs the metatest. |
| 303 | * |
| 304 | * The function receives the name as an argument. This allows using the |
| 305 | * same function to perform multiple variants of a test based on the name. |
| 306 | * |
| 307 | * When executed on a conforming platform, the function is expected to |
| 308 | * either cause a test failure (mbedtls_test_fail()), or cause the |
| 309 | * program to abort in some way (e.g. by causing a segfault or by |
| 310 | * triggering a sanitizer). |
| 311 | * |
| 312 | * When executed on a non-conforming platform, the function may return |
| 313 | * normally or may have unpredictable behavior. |
| 314 | */ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 315 | void (*entry_point)(const char *name); |
| 316 | } metatest_t; |
| 317 | |
Gilles Peskine | cce0012 | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 318 | /* The list of availble meta-tests. Remember to register new functions here! |
| 319 | * |
| 320 | * Note that we always compile all the functions, so that `metatest --list` |
| 321 | * will always list all the available meta-tests. |
Gilles Peskine | 2f40cc0 | 2023-11-16 15:11:44 +0100 | [diff] [blame] | 322 | * |
| 323 | * See the documentation of metatest_t::platform for the meaning of |
| 324 | * platform values. |
Gilles Peskine | cce0012 | 2023-11-10 15:36:15 +0100 | [diff] [blame] | 325 | */ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 326 | metatest_t metatests[] = { |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 327 | { "test_fail", "any", meta_test_fail }, |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 328 | { "null_dereference", "any", null_pointer_dereference }, |
| 329 | { "null_call", "any", null_pointer_call }, |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 330 | { "read_after_free", "asan", read_after_free }, |
| 331 | { "double_free", "asan", double_free }, |
| 332 | { "read_uninitialized_stack", "msan", read_uninitialized_stack }, |
| 333 | { "memory_leak", "asan", memory_leak }, |
Gilles Peskine | d29cce9 | 2023-11-02 20:49:34 +0100 | [diff] [blame^] | 334 | { "test_memory_poison_0_0_8", "asan", test_memory_poison }, |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 335 | { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized }, |
| 336 | { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized }, |
| 337 | { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized }, |
| 338 | { "mutex_double_init", "pthread", mutex_double_init }, |
| 339 | { "mutex_double_free", "pthread", mutex_double_free }, |
| 340 | { "mutex_leak", "pthread", mutex_leak }, |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 341 | { NULL, NULL, NULL } |
| 342 | }; |
| 343 | |
| 344 | static void help(FILE *out, const char *argv0) |
| 345 | { |
| 346 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 347 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 348 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 349 | } |
| 350 | |
| 351 | int main(int argc, char *argv[]) |
| 352 | { |
| 353 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 354 | if (argc != 2) { |
| 355 | help(stderr, argv0); |
| 356 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 357 | } |
| 358 | |
| 359 | /* Support "-help", "--help", "--list", etc. */ |
| 360 | const char *command = argv[1]; |
| 361 | while (*command == '-') { |
| 362 | ++command; |
| 363 | } |
| 364 | |
| 365 | if (strcmp(argv[1], "help") == 0) { |
| 366 | help(stdout, argv0); |
| 367 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 368 | } |
| 369 | if (strcmp(argv[1], "list") == 0) { |
| 370 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 371 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 372 | } |
| 373 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 374 | } |
| 375 | |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 376 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 377 | mbedtls_test_mutex_usage_init(); |
| 378 | #endif |
| 379 | |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 380 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 381 | if (strcmp(argv[1], p->name) == 0) { |
| 382 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 383 | p->entry_point(argv[1]); |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 384 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 385 | mbedtls_test_mutex_usage_check(); |
| 386 | #endif |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 387 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 388 | argv[1], (int) mbedtls_test_info.result); |
| 389 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 390 | MBEDTLS_EXIT_SUCCESS : |
| 391 | MBEDTLS_EXIT_FAILURE); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 396 | argv0, command); |
| 397 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 398 | } |