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