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