blob: 6e476b7b32c8daca3a7ea16172892b1ac1b8a405 [file] [log] [blame]
Gilles Peskine33406b62023-11-02 18:48:39 +01001/** \file metatest.c
2 *
3 * \brief Test features of the test framework.
Gilles Peskinecce00122023-11-10 15:36:15 +01004 *
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 Peskine33406b62023-11-02 18:48:39 +010022 */
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 Peskinee0acf872023-11-03 19:41:44 +010031#include <mbedtls/debug.h>
Gilles Peskine33406b62023-11-02 18:48:39 +010032#include <mbedtls/platform.h>
Gilles Peskine69e8db02023-11-02 19:52:32 +010033#include <mbedtls/platform_util.h>
Gilles Peskine33406b62023-11-02 18:48:39 +010034#include "test/helpers.h"
Gilles Peskine102aea22023-11-03 18:05:38 +010035#include "test/macros.h"
Gilles Peskined29cce92023-11-02 20:49:34 +010036#include "test/memory.h"
Gilles Peskine33406b62023-11-02 18:48:39 +010037
38#include <stdio.h>
39#include <string.h>
40
Gilles Peskine102aea22023-11-03 18:05:38 +010041#if defined(MBEDTLS_THREADING_C)
42#include <mbedtls/threading.h>
43#endif
44
Gilles Peskinef5dd0022023-11-03 17:01:32 +010045/* 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 Peskine33406b62023-11-02 18:48:39 +010050
Gilles Peskine69e8db02023-11-02 19:52:32 +010051/* This is an external variable, so the compiler doesn't know that we're never
52 * changing its value.
Gilles Peskine69e8db02023-11-02 19:52:32 +010053 */
Gilles Peskined2fa6982023-11-09 21:46:24 +010054volatile int false_but_the_compiler_does_not_know = 0;
55
Gilles Peskine7a715c42023-11-21 13:42:40 +010056/* 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. */
59void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
60void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
61
Gilles Peskined2fa6982023-11-09 21:46:24 +010062/* 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 Peskineda6e7a22023-11-10 10:09:27 +010064static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskined2fa6982023-11-09 21:46:24 +010065{
Gilles Peskineda6e7a22023-11-10 10:09:27 +010066 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskined2fa6982023-11-09 21:46:24 +010067}
Gilles Peskine69e8db02023-11-02 19:52:32 +010068
69
Gilles Peskine33406b62023-11-02 18:48:39 +010070/****************************************************************/
Gilles Peskinef309fbf2023-11-02 18:49:52 +010071/* Test framework features */
72/****************************************************************/
73
74void meta_test_fail(const char *name)
75{
76 (void) name;
77 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
78}
79
80
81/****************************************************************/
Gilles Peskine80ba8322023-11-02 19:23:26 +010082/* Platform features */
83/****************************************************************/
84
85void null_pointer_dereference(const char *name)
86{
87 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010088 volatile char *volatile p;
Gilles Peskined2fa6982023-11-09 21:46:24 +010089 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +010090 /* Undefined behavior (read from null data pointer) */
Gilles Peskine69e8db02023-11-02 19:52:32 +010091 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine80ba8322023-11-02 19:23:26 +010092}
93
94void null_pointer_call(const char *name)
95{
96 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010097 unsigned(*volatile p)(void);
Gilles Peskined2fa6982023-11-09 21:46:24 +010098 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +010099 /* Undefined behavior (execute null function pointer) */
Gilles Peskinef0d5cf92023-11-03 10:58:57 +0100100 /* 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 Peskine80ba8322023-11-02 19:23:26 +0100104}
105
106
107/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100108/* Memory */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100109/****************************************************************/
110
111void read_after_free(const char *name)
112{
113 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100114 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100115 *p = 'a';
Gilles Peskine7a715c42023-11-21 13:42:40 +0100116 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100117 /* Undefined behavior (read after free) */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100118 mbedtls_printf("%u\n", (unsigned) *p);
119}
120
121void double_free(const char *name)
122{
123 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100124 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100125 *p = 'a';
Gilles Peskine7a715c42023-11-21 13:42:40 +0100126 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100127 /* Undefined behavior (double free) */
Gilles Peskine7a715c42023-11-21 13:42:40 +0100128 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100129}
130
131void read_uninitialized_stack(const char *name)
132{
133 (void) name;
Gilles Peskineccb12152023-11-10 11:35:36 +0100134 char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100135 if (false_but_the_compiler_does_not_know) {
136 buf[0] = '!';
137 }
Gilles Peskineccb12152023-11-10 11:35:36 +0100138 char *volatile p = buf;
139 if (*p != 0) {
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100140 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineccb12152023-11-10 11:35:36 +0100141 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100142 }
143}
144
145void memory_leak(const char *name)
146{
147 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100148 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100149 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100150 /* Leak of a heap object */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100151}
152
Gilles Peskined29cce92023-11-02 20:49:34 +0100153/* name = "test_memory_poison_%(start)_%(offset)_%(count)"
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.
156 */
157void test_memory_poison(const char *name)
158{
159 size_t start = 0, offset = 0, count = 0;
Gilles Peskinee0acf872023-11-03 19:41:44 +0100160 if (sscanf(name,
161 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
162 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
163 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET,
Gilles Peskined29cce92023-11-02 20:49:34 +0100164 &start, &offset, &count) != 3) {
165 mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
166 return;
167 }
168
169 union {
170 long long ll;
171 unsigned char buf[32];
172 } aligned;
173 memset(aligned.buf, 'a', sizeof(aligned.buf));
174
175 if (start > sizeof(aligned.buf)) {
Gilles Peskinee0acf872023-11-03 19:41:44 +0100176 mbedtls_fprintf(stderr,
177 "%s: start=%" MBEDTLS_PRINTF_SIZET
178 " > size=%" MBEDTLS_PRINTF_SIZET,
179 __func__, start, sizeof(aligned.buf));
Gilles Peskined29cce92023-11-02 20:49:34 +0100180 return;
181 }
182 if (start + count > sizeof(aligned.buf)) {
Gilles Peskinee0acf872023-11-03 19:41:44 +0100183 mbedtls_fprintf(stderr,
184 "%s: start+count=%" MBEDTLS_PRINTF_SIZET
185 " > size=%" MBEDTLS_PRINTF_SIZET,
186 __func__, start + count, sizeof(aligned.buf));
Gilles Peskined29cce92023-11-02 20:49:34 +0100187 return;
188 }
189 if (offset >= count) {
Gilles Peskinee0acf872023-11-03 19:41:44 +0100190 mbedtls_fprintf(stderr,
191 "%s: offset=%" MBEDTLS_PRINTF_SIZET
192 " >= count=%" MBEDTLS_PRINTF_SIZET,
193 __func__, offset, count);
Gilles Peskined29cce92023-11-02 20:49:34 +0100194 return;
195 }
196
197 MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count);
198 mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]);
199}
200
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100201
202/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100203/* Threading */
204/****************************************************************/
205
206void mutex_lock_not_initialized(const char *name)
207{
208 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100209#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100210 mbedtls_threading_mutex_t mutex;
211 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100212 /* This mutex usage error is detected by our test framework's mutex usage
213 * verification framework. See tests/src/threading_helpers.c. Other
214 * threading implementations (e.g. pthread without our instrumentation)
215 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100216 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
217exit:
218 ;
219#endif
220}
221
222void mutex_unlock_not_initialized(const char *name)
223{
224 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100225#if defined(MBEDTLS_THREADING_C)
226 mbedtls_threading_mutex_t mutex;
227 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100228 /* This mutex usage error is detected by our test framework's mutex usage
229 * verification framework. See tests/src/threading_helpers.c. Other
230 * threading implementations (e.g. pthread without our instrumentation)
231 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100232 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
233exit:
234 ;
235#endif
236}
237
238void mutex_free_not_initialized(const char *name)
239{
240 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100241#if defined(MBEDTLS_THREADING_C)
242 mbedtls_threading_mutex_t mutex;
243 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100244 /* This mutex usage error is detected by our test framework's mutex usage
245 * verification framework. See tests/src/threading_helpers.c. Other
246 * threading implementations (e.g. pthread without our instrumentation)
247 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100248 mbedtls_mutex_free(&mutex);
249#endif
250}
251
252void mutex_double_init(const char *name)
253{
254 (void) name;
255#if defined(MBEDTLS_THREADING_C)
256 mbedtls_threading_mutex_t mutex;
257 mbedtls_mutex_init(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100258 /* This mutex usage error is detected by our test framework's mutex usage
259 * verification framework. See tests/src/threading_helpers.c. Other
260 * threading implementations (e.g. pthread without our instrumentation)
261 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100262 mbedtls_mutex_init(&mutex);
263 mbedtls_mutex_free(&mutex);
264#endif
265}
266
267void mutex_double_free(const char *name)
268{
269 (void) name;
270#if defined(MBEDTLS_THREADING_C)
271 mbedtls_threading_mutex_t mutex;
272 mbedtls_mutex_init(&mutex);
273 mbedtls_mutex_free(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100274 /* 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 Peskine102aea22023-11-03 18:05:38 +0100278 mbedtls_mutex_free(&mutex);
279#endif
280}
281
282void mutex_leak(const char *name)
283{
284 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100285#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100286 mbedtls_threading_mutex_t mutex;
287 mbedtls_mutex_init(&mutex);
288#endif
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100289 /* This mutex usage error is detected by our test framework's mutex usage
290 * verification framework. See tests/src/threading_helpers.c. Other
291 * threading implementations (e.g. pthread without our instrumentation)
292 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100293}
294
295
296/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100297/* Command line entry point */
298/****************************************************************/
299
300typedef struct {
Gilles Peskinecce00122023-11-10 15:36:15 +0100301 /** Command line argument that will trigger that metatest.
302 *
303 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskine33406b62023-11-02 18:48:39 +0100304 const char *name;
Gilles Peskinecce00122023-11-10 15:36:15 +0100305
306 /** Platform under which that metatest is valid.
307 *
308 * - "any": should work anywhere.
309 * - "asan": triggers ASan (Address Sanitizer).
310 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100311 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
312 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
313 * framework (see tests/src/threading_helpers.c).
Gilles Peskinecce00122023-11-10 15:36:15 +0100314 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100315 const char *platform;
Gilles Peskinecce00122023-11-10 15:36:15 +0100316
317 /** Function that performs the metatest.
318 *
319 * The function receives the name as an argument. This allows using the
320 * same function to perform multiple variants of a test based on the name.
321 *
322 * When executed on a conforming platform, the function is expected to
323 * either cause a test failure (mbedtls_test_fail()), or cause the
324 * program to abort in some way (e.g. by causing a segfault or by
325 * triggering a sanitizer).
326 *
327 * When executed on a non-conforming platform, the function may return
328 * normally or may have unpredictable behavior.
329 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100330 void (*entry_point)(const char *name);
331} metatest_t;
332
Gilles Peskinecce00122023-11-10 15:36:15 +0100333/* The list of availble meta-tests. Remember to register new functions here!
334 *
335 * Note that we always compile all the functions, so that `metatest --list`
336 * will always list all the available meta-tests.
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100337 *
338 * See the documentation of metatest_t::platform for the meaning of
339 * platform values.
Gilles Peskinecce00122023-11-10 15:36:15 +0100340 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100341metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100342 { "test_fail", "any", meta_test_fail },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100343 { "null_dereference", "any", null_pointer_dereference },
344 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100345 { "read_after_free", "asan", read_after_free },
346 { "double_free", "asan", double_free },
347 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
348 { "memory_leak", "asan", memory_leak },
Gilles Peskined29cce92023-11-02 20:49:34 +0100349 { "test_memory_poison_0_0_8", "asan", test_memory_poison },
Gilles Peskine0bdb6dc2023-11-02 22:44:32 +0100350 { "test_memory_poison_0_7_8", "asan", test_memory_poison },
351 { "test_memory_poison_0_0_1", "asan", test_memory_poison },
352 { "test_memory_poison_0_1_2", "asan", test_memory_poison },
353 { "test_memory_poison_7_0_8", "asan", test_memory_poison },
354 { "test_memory_poison_7_7_8", "asan", test_memory_poison },
355 { "test_memory_poison_7_0_1", "asan", test_memory_poison },
356 { "test_memory_poison_7_1_2", "asan", test_memory_poison },
Gilles Peskine102aea22023-11-03 18:05:38 +0100357 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
358 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
359 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
360 { "mutex_double_init", "pthread", mutex_double_init },
361 { "mutex_double_free", "pthread", mutex_double_free },
362 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100363 { NULL, NULL, NULL }
364};
365
366static void help(FILE *out, const char *argv0)
367{
368 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
369 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
370 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
371}
372
373int main(int argc, char *argv[])
374{
375 const char *argv0 = argc > 0 ? argv[0] : "metatest";
376 if (argc != 2) {
377 help(stderr, argv0);
378 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
379 }
380
381 /* Support "-help", "--help", "--list", etc. */
382 const char *command = argv[1];
383 while (*command == '-') {
384 ++command;
385 }
386
387 if (strcmp(argv[1], "help") == 0) {
388 help(stdout, argv0);
389 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
390 }
391 if (strcmp(argv[1], "list") == 0) {
392 for (const metatest_t *p = metatests; p->name != NULL; p++) {
393 mbedtls_printf("%s %s\n", p->name, p->platform);
394 }
395 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
396 }
397
Gilles Peskine102aea22023-11-03 18:05:38 +0100398#if defined(MBEDTLS_TEST_MUTEX_USAGE)
399 mbedtls_test_mutex_usage_init();
400#endif
401
Gilles Peskine33406b62023-11-02 18:48:39 +0100402 for (const metatest_t *p = metatests; p->name != NULL; p++) {
403 if (strcmp(argv[1], p->name) == 0) {
404 mbedtls_printf("Running metatest %s...\n", argv[1]);
405 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100406#if defined(MBEDTLS_TEST_MUTEX_USAGE)
407 mbedtls_test_mutex_usage_check();
408#endif
Gilles Peskine33406b62023-11-02 18:48:39 +0100409 mbedtls_printf("Running metatest %s... done, result=%d\n",
410 argv[1], (int) mbedtls_test_info.result);
411 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
412 MBEDTLS_EXIT_SUCCESS :
413 MBEDTLS_EXIT_FAILURE);
414 }
415 }
416
417 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
418 argv0, command);
419 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
420}