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. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright The Mbed TLS Contributors |
| 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 12 | |
| 13 | #include <mbedtls/platform.h> |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 14 | #include <mbedtls/platform_util.h> |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 15 | #include "test/helpers.h" |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 16 | #include "test/macros.h" |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 21 | #if defined(MBEDTLS_THREADING_C) |
| 22 | #include <mbedtls/threading.h> |
| 23 | #endif |
| 24 | |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 25 | |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 26 | /* This is an external variable, so the compiler doesn't know that we're never |
| 27 | * changing its value. |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 28 | */ |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 29 | volatile int false_but_the_compiler_does_not_know = 0; |
| 30 | |
| 31 | /* Set n bytes at the address p to all-bits-zero, in such a way that |
| 32 | * the compiler should not know that p is all-bits-zero. */ |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 33 | 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] | 34 | { |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 35 | memset((void *) p, false_but_the_compiler_does_not_know, n); |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 36 | } |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 37 | |
| 38 | |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 39 | /****************************************************************/ |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 40 | /* Test framework features */ |
| 41 | /****************************************************************/ |
| 42 | |
| 43 | void meta_test_fail(const char *name) |
| 44 | { |
| 45 | (void) name; |
| 46 | mbedtls_test_fail("Forced test failure", __LINE__, __FILE__); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /****************************************************************/ |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 51 | /* Platform features */ |
| 52 | /****************************************************************/ |
| 53 | |
| 54 | void null_pointer_dereference(const char *name) |
| 55 | { |
| 56 | (void) name; |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 57 | volatile char *volatile p; |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 58 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | 69e8db0 | 2023-11-02 19:52:32 +0100 | [diff] [blame] | 59 | mbedtls_printf("%p -> %u\n", p, (unsigned) *p); |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void null_pointer_call(const char *name) |
| 63 | { |
| 64 | (void) name; |
Gilles Peskine | da6e7a2 | 2023-11-10 10:09:27 +0100 | [diff] [blame] | 65 | unsigned(*volatile p)(void); |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 66 | set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p)); |
Gilles Peskine | f0d5cf9 | 2023-11-03 10:58:57 +0100 | [diff] [blame] | 67 | /* The pointer representation may be truncated, but we don't care: |
| 68 | * the only point of printing it is to have some use of the pointer |
| 69 | * to dissuade the compiler from optimizing it away. */ |
| 70 | mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p()); |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | /****************************************************************/ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 75 | /* Memory */ |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 76 | /****************************************************************/ |
| 77 | |
| 78 | void read_after_free(const char *name) |
| 79 | { |
| 80 | (void) name; |
| 81 | volatile char *p = mbedtls_calloc(1, 1); |
| 82 | *p = 'a'; |
| 83 | mbedtls_free((void *) p); |
| 84 | mbedtls_printf("%u\n", (unsigned) *p); |
| 85 | } |
| 86 | |
| 87 | void double_free(const char *name) |
| 88 | { |
| 89 | (void) name; |
| 90 | volatile char *p = mbedtls_calloc(1, 1); |
| 91 | *p = 'a'; |
| 92 | mbedtls_free((void *) p); |
| 93 | mbedtls_free((void *) p); |
| 94 | } |
| 95 | |
| 96 | void read_uninitialized_stack(const char *name) |
| 97 | { |
| 98 | (void) name; |
Gilles Peskine | ccb1215 | 2023-11-10 11:35:36 +0100 | [diff] [blame^] | 99 | char buf[1]; |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 100 | if (false_but_the_compiler_does_not_know) { |
| 101 | buf[0] = '!'; |
| 102 | } |
Gilles Peskine | ccb1215 | 2023-11-10 11:35:36 +0100 | [diff] [blame^] | 103 | char *volatile p = buf; |
| 104 | if (*p != 0) { |
| 105 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void memory_leak(const char *name) |
| 110 | { |
| 111 | (void) name; |
| 112 | volatile char *p = mbedtls_calloc(1, 1); |
Gilles Peskine | d2fa698 | 2023-11-09 21:46:24 +0100 | [diff] [blame] | 113 | mbedtls_printf("%u\n", (unsigned) *p); |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | /****************************************************************/ |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 118 | /* Threading */ |
| 119 | /****************************************************************/ |
| 120 | |
| 121 | void mutex_lock_not_initialized(const char *name) |
| 122 | { |
| 123 | (void) name; |
| 124 | /* Mutex usage verification is only done with pthread, not with other |
| 125 | * threading implementations. See tests/src/threading_helpers.c. */ |
| 126 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 127 | mbedtls_threading_mutex_t mutex; |
| 128 | memset(&mutex, 0, sizeof(mutex)); |
| 129 | TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0); |
| 130 | exit: |
| 131 | ; |
| 132 | #endif |
| 133 | } |
| 134 | |
| 135 | void mutex_unlock_not_initialized(const char *name) |
| 136 | { |
| 137 | (void) name; |
| 138 | /* Mutex usage verification is only done with pthread, not with other |
| 139 | * threading implementations. See tests/src/threading_helpers.c. */ |
| 140 | #if defined(MBEDTLS_THREADING_C) |
| 141 | mbedtls_threading_mutex_t mutex; |
| 142 | memset(&mutex, 0, sizeof(mutex)); |
| 143 | TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0); |
| 144 | exit: |
| 145 | ; |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | void mutex_free_not_initialized(const char *name) |
| 150 | { |
| 151 | (void) name; |
| 152 | /* Mutex usage verification is only done with pthread, not with other |
| 153 | * threading implementations. See tests/src/threading_helpers.c. */ |
| 154 | #if defined(MBEDTLS_THREADING_C) |
| 155 | mbedtls_threading_mutex_t mutex; |
| 156 | memset(&mutex, 0, sizeof(mutex)); |
| 157 | mbedtls_mutex_free(&mutex); |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | void mutex_double_init(const char *name) |
| 162 | { |
| 163 | (void) name; |
| 164 | #if defined(MBEDTLS_THREADING_C) |
| 165 | mbedtls_threading_mutex_t mutex; |
| 166 | mbedtls_mutex_init(&mutex); |
| 167 | mbedtls_mutex_init(&mutex); |
| 168 | mbedtls_mutex_free(&mutex); |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | void mutex_double_free(const char *name) |
| 173 | { |
| 174 | (void) name; |
| 175 | #if defined(MBEDTLS_THREADING_C) |
| 176 | mbedtls_threading_mutex_t mutex; |
| 177 | mbedtls_mutex_init(&mutex); |
| 178 | mbedtls_mutex_free(&mutex); |
| 179 | mbedtls_mutex_free(&mutex); |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | void mutex_leak(const char *name) |
| 184 | { |
| 185 | (void) name; |
| 186 | /* Mutex usage verification is only done with pthread, not with other |
| 187 | * threading implementations. See tests/src/threading_helpers.c. */ |
| 188 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 189 | mbedtls_threading_mutex_t mutex; |
| 190 | mbedtls_mutex_init(&mutex); |
| 191 | #endif |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /****************************************************************/ |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 196 | /* Command line entry point */ |
| 197 | /****************************************************************/ |
| 198 | |
| 199 | typedef struct { |
| 200 | const char *name; |
| 201 | const char *platform; |
| 202 | void (*entry_point)(const char *name); |
| 203 | } metatest_t; |
| 204 | |
| 205 | metatest_t metatests[] = { |
Gilles Peskine | f309fbf | 2023-11-02 18:49:52 +0100 | [diff] [blame] | 206 | { "test_fail", "any", meta_test_fail }, |
Gilles Peskine | 80ba832 | 2023-11-02 19:23:26 +0100 | [diff] [blame] | 207 | { "null_dereference", "any", null_pointer_dereference }, |
| 208 | { "null_call", "any", null_pointer_call }, |
Gilles Peskine | b0f0a64 | 2023-11-02 19:42:13 +0100 | [diff] [blame] | 209 | { "read_after_free", "asan", read_after_free }, |
| 210 | { "double_free", "asan", double_free }, |
| 211 | { "read_uninitialized_stack", "msan", read_uninitialized_stack }, |
| 212 | { "memory_leak", "asan", memory_leak }, |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 213 | /* Mutex usage verification is only done with pthread, not with other |
| 214 | * threading implementations. See tests/src/threading_helpers.c. */ |
| 215 | { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized }, |
| 216 | { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized }, |
| 217 | { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized }, |
| 218 | { "mutex_double_init", "pthread", mutex_double_init }, |
| 219 | { "mutex_double_free", "pthread", mutex_double_free }, |
| 220 | { "mutex_leak", "pthread", mutex_leak }, |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 221 | { NULL, NULL, NULL } |
| 222 | }; |
| 223 | |
| 224 | static void help(FILE *out, const char *argv0) |
| 225 | { |
| 226 | mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0); |
| 227 | mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n"); |
| 228 | mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n"); |
| 229 | } |
| 230 | |
| 231 | int main(int argc, char *argv[]) |
| 232 | { |
| 233 | const char *argv0 = argc > 0 ? argv[0] : "metatest"; |
| 234 | if (argc != 2) { |
| 235 | help(stderr, argv0); |
| 236 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 237 | } |
| 238 | |
| 239 | /* Support "-help", "--help", "--list", etc. */ |
| 240 | const char *command = argv[1]; |
| 241 | while (*command == '-') { |
| 242 | ++command; |
| 243 | } |
| 244 | |
| 245 | if (strcmp(argv[1], "help") == 0) { |
| 246 | help(stdout, argv0); |
| 247 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 248 | } |
| 249 | if (strcmp(argv[1], "list") == 0) { |
| 250 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 251 | mbedtls_printf("%s %s\n", p->name, p->platform); |
| 252 | } |
| 253 | mbedtls_exit(MBEDTLS_EXIT_SUCCESS); |
| 254 | } |
| 255 | |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 256 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 257 | mbedtls_test_mutex_usage_init(); |
| 258 | #endif |
| 259 | |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 260 | for (const metatest_t *p = metatests; p->name != NULL; p++) { |
| 261 | if (strcmp(argv[1], p->name) == 0) { |
| 262 | mbedtls_printf("Running metatest %s...\n", argv[1]); |
| 263 | p->entry_point(argv[1]); |
Gilles Peskine | 102aea2 | 2023-11-03 18:05:38 +0100 | [diff] [blame] | 264 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 265 | mbedtls_test_mutex_usage_check(); |
| 266 | #endif |
Gilles Peskine | 33406b6 | 2023-11-02 18:48:39 +0100 | [diff] [blame] | 267 | mbedtls_printf("Running metatest %s... done, result=%d\n", |
| 268 | argv[1], (int) mbedtls_test_info.result); |
| 269 | mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ? |
| 270 | MBEDTLS_EXIT_SUCCESS : |
| 271 | MBEDTLS_EXIT_FAILURE); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n", |
| 276 | argv0, command); |
| 277 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); |
| 278 | } |