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