blob: 11f6eb80ec521cd033eb60f9ed73bcb990091fcd [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
Gilles Peskine33406b62023-11-02 18:48:39 +010029
Gilles Peskinee0acf872023-11-03 19:41:44 +010030#include <mbedtls/debug.h>
Gilles Peskine33406b62023-11-02 18:48:39 +010031#include <mbedtls/platform.h>
Gilles Peskine69e8db02023-11-02 19:52:32 +010032#include <mbedtls/platform_util.h>
Gilles Peskine33406b62023-11-02 18:48:39 +010033#include "test/helpers.h"
Paul Elliott17c119a2023-12-08 16:55:03 +000034#include "test/threading_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 Peskine6f8ca292023-11-29 19:42:43 +010037#include "common.h"
Gilles Peskine33406b62023-11-02 18:48:39 +010038
39#include <stdio.h>
40#include <string.h>
41
Gilles Peskine102aea22023-11-03 18:05:38 +010042#if defined(MBEDTLS_THREADING_C)
43#include <mbedtls/threading.h>
44#endif
45
Gilles Peskine33406b62023-11-02 18:48:39 +010046
Gilles Peskine69e8db02023-11-02 19:52:32 +010047/* This is an external variable, so the compiler doesn't know that we're never
48 * changing its value.
Gilles Peskine69e8db02023-11-02 19:52:32 +010049 */
Gilles Peskined2fa6982023-11-09 21:46:24 +010050volatile int false_but_the_compiler_does_not_know = 0;
51
Gilles Peskine7a715c42023-11-21 13:42:40 +010052/* Hide calls to calloc/free from static checkers such as
53 * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
54 * code where we do mean to cause a runtime error. */
55void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
56void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
57
Gilles Peskined2fa6982023-11-09 21:46:24 +010058/* Set n bytes at the address p to all-bits-zero, in such a way that
59 * the compiler should not know that p is all-bits-zero. */
Gilles Peskineda6e7a22023-11-10 10:09:27 +010060static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskined2fa6982023-11-09 21:46:24 +010061{
Gilles Peskineda6e7a22023-11-10 10:09:27 +010062 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskined2fa6982023-11-09 21:46:24 +010063}
Gilles Peskine69e8db02023-11-02 19:52:32 +010064
Gilles Peskine895ebc32023-11-23 12:33:39 +010065/* Simulate an access to the given object, to avoid compiler optimizations
66 * in code that prepares or consumes the object. */
67static void do_nothing_with_object(void *p)
68{
69 (void) p;
70}
71void(*volatile do_nothing_with_object_but_the_compiler_does_not_know)(void *) =
72 do_nothing_with_object;
73
Gilles Peskine69e8db02023-11-02 19:52:32 +010074
Gilles Peskine33406b62023-11-02 18:48:39 +010075/****************************************************************/
Gilles Peskinef309fbf2023-11-02 18:49:52 +010076/* Test framework features */
77/****************************************************************/
78
79void meta_test_fail(const char *name)
80{
81 (void) name;
82 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
83}
84
Paul Elliott60bbfe62024-02-13 15:06:10 +000085void meta_test_not_equal(const char *name)
86{
87 int left = 20;
88 int right = 10;
89
90 (void) name;
91
92 TEST_EQUAL(left, right);
93exit:
94 ;
95}
96
97void meta_test_not_le_s(const char *name)
98{
99 int left = 20;
100 int right = 10;
101
102 (void) name;
103
104 TEST_LE_S(left, right);
105exit:
106 ;
107}
108
109void meta_test_not_le_u(const char *name)
110{
111 size_t left = 20;
112 size_t right = 10;
113
114 (void) name;
115
116 TEST_LE_U(left, right);
117exit:
118 ;
119}
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100120
121/****************************************************************/
Gilles Peskine80ba8322023-11-02 19:23:26 +0100122/* Platform features */
123/****************************************************************/
124
125void null_pointer_dereference(const char *name)
126{
127 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +0100128 volatile char *volatile p;
Gilles Peskined2fa6982023-11-09 21:46:24 +0100129 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100130 /* Undefined behavior (read from null data pointer) */
Gilles Peskine69e8db02023-11-02 19:52:32 +0100131 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine80ba8322023-11-02 19:23:26 +0100132}
133
134void null_pointer_call(const char *name)
135{
136 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +0100137 unsigned(*volatile p)(void);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100138 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100139 /* Undefined behavior (execute null function pointer) */
Gilles Peskinef0d5cf92023-11-03 10:58:57 +0100140 /* The pointer representation may be truncated, but we don't care:
141 * the only point of printing it is to have some use of the pointer
142 * to dissuade the compiler from optimizing it away. */
143 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine80ba8322023-11-02 19:23:26 +0100144}
145
146
147/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100148/* Memory */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100149/****************************************************************/
150
151void read_after_free(const char *name)
152{
153 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100154 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100155 *p = 'a';
Gilles Peskine7a715c42023-11-21 13:42:40 +0100156 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100157 /* Undefined behavior (read after free) */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100158 mbedtls_printf("%u\n", (unsigned) *p);
159}
160
161void double_free(const char *name)
162{
163 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100164 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100165 *p = 'a';
Gilles Peskine7a715c42023-11-21 13:42:40 +0100166 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100167 /* Undefined behavior (double free) */
Gilles Peskine7a715c42023-11-21 13:42:40 +0100168 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100169}
170
171void read_uninitialized_stack(const char *name)
172{
173 (void) name;
Gilles Peskineccb12152023-11-10 11:35:36 +0100174 char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100175 if (false_but_the_compiler_does_not_know) {
176 buf[0] = '!';
177 }
Gilles Peskineccb12152023-11-10 11:35:36 +0100178 char *volatile p = buf;
179 if (*p != 0) {
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100180 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineccb12152023-11-10 11:35:36 +0100181 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100182 }
183}
184
185void memory_leak(const char *name)
186{
187 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100188 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100189 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100190 /* Leak of a heap object */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100191}
192
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100193/* name = "test_memory_poison_%(start)_%(offset)_%(count)_%(direction)"
Gilles Peskined29cce92023-11-02 20:49:34 +0100194 * Poison a region starting at start from an 8-byte aligned origin,
195 * encompassing count bytes. Access the region at offset from the start.
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100196 * %(start), %(offset) and %(count) are decimal integers.
197 * %(direction) is either the character 'r' for read or 'w' for write.
Gilles Peskined29cce92023-11-02 20:49:34 +0100198 */
199void test_memory_poison(const char *name)
200{
201 size_t start = 0, offset = 0, count = 0;
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100202 char direction = 'r';
Gilles Peskinee0acf872023-11-03 19:41:44 +0100203 if (sscanf(name,
204 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
205 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100206 "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
207 "_%c",
208 &start, &offset, &count, &direction) != 4) {
Gilles Peskined29cce92023-11-02 20:49:34 +0100209 mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
210 return;
211 }
212
213 union {
214 long long ll;
215 unsigned char buf[32];
216 } aligned;
217 memset(aligned.buf, 'a', sizeof(aligned.buf));
218
219 if (start > sizeof(aligned.buf)) {
Gilles Peskinee0acf872023-11-03 19:41:44 +0100220 mbedtls_fprintf(stderr,
221 "%s: start=%" MBEDTLS_PRINTF_SIZET
222 " > size=%" MBEDTLS_PRINTF_SIZET,
223 __func__, start, sizeof(aligned.buf));
Gilles Peskined29cce92023-11-02 20:49:34 +0100224 return;
225 }
226 if (start + count > sizeof(aligned.buf)) {
Gilles Peskinee0acf872023-11-03 19:41:44 +0100227 mbedtls_fprintf(stderr,
228 "%s: start+count=%" MBEDTLS_PRINTF_SIZET
229 " > size=%" MBEDTLS_PRINTF_SIZET,
230 __func__, start + count, sizeof(aligned.buf));
Gilles Peskined29cce92023-11-02 20:49:34 +0100231 return;
232 }
233 if (offset >= count) {
Gilles Peskinee0acf872023-11-03 19:41:44 +0100234 mbedtls_fprintf(stderr,
235 "%s: offset=%" MBEDTLS_PRINTF_SIZET
236 " >= count=%" MBEDTLS_PRINTF_SIZET,
237 __func__, offset, count);
Gilles Peskined29cce92023-11-02 20:49:34 +0100238 return;
239 }
240
241 MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count);
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100242
243 if (direction == 'w') {
244 aligned.buf[start + offset] = 'b';
Gilles Peskine895ebc32023-11-23 12:33:39 +0100245 do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf);
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100246 } else {
Gilles Peskine895ebc32023-11-23 12:33:39 +0100247 do_nothing_with_object_but_the_compiler_does_not_know(aligned.buf);
Gilles Peskineef0f01f2023-11-22 18:22:07 +0100248 mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]);
249 }
Gilles Peskined29cce92023-11-02 20:49:34 +0100250}
251
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100252
253/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100254/* Threading */
255/****************************************************************/
256
257void mutex_lock_not_initialized(const char *name)
258{
259 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100260#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100261 mbedtls_threading_mutex_t mutex;
262 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100263 /* This mutex usage error is detected by our test framework's mutex usage
David Horstmann844da452024-03-27 17:30:22 +0000264 * verification framework. See framework/src/threading_helpers.c. Other
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100265 * threading implementations (e.g. pthread without our instrumentation)
266 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100267 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
268exit:
269 ;
270#endif
271}
272
273void mutex_unlock_not_initialized(const char *name)
274{
275 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100276#if defined(MBEDTLS_THREADING_C)
277 mbedtls_threading_mutex_t mutex;
278 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100279 /* This mutex usage error is detected by our test framework's mutex usage
David Horstmann844da452024-03-27 17:30:22 +0000280 * verification framework. See framework/src/threading_helpers.c. Other
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100281 * threading implementations (e.g. pthread without our instrumentation)
282 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100283 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
284exit:
285 ;
286#endif
287}
288
289void mutex_free_not_initialized(const char *name)
290{
291 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100292#if defined(MBEDTLS_THREADING_C)
293 mbedtls_threading_mutex_t mutex;
294 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100295 /* This mutex usage error is detected by our test framework's mutex usage
David Horstmann844da452024-03-27 17:30:22 +0000296 * verification framework. See framework/src/threading_helpers.c. Other
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100297 * threading implementations (e.g. pthread without our instrumentation)
298 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100299 mbedtls_mutex_free(&mutex);
300#endif
301}
302
303void mutex_double_init(const char *name)
304{
305 (void) name;
306#if defined(MBEDTLS_THREADING_C)
307 mbedtls_threading_mutex_t mutex;
308 mbedtls_mutex_init(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100309 /* This mutex usage error is detected by our test framework's mutex usage
David Horstmann844da452024-03-27 17:30:22 +0000310 * verification framework. See framework/src/threading_helpers.c. Other
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100311 * threading implementations (e.g. pthread without our instrumentation)
312 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100313 mbedtls_mutex_init(&mutex);
314 mbedtls_mutex_free(&mutex);
315#endif
316}
317
318void mutex_double_free(const char *name)
319{
320 (void) name;
321#if defined(MBEDTLS_THREADING_C)
322 mbedtls_threading_mutex_t mutex;
323 mbedtls_mutex_init(&mutex);
324 mbedtls_mutex_free(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100325 /* This mutex usage error is detected by our test framework's mutex usage
David Horstmann844da452024-03-27 17:30:22 +0000326 * verification framework. See framework/src/threading_helpers.c. Other
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100327 * threading implementations (e.g. pthread without our instrumentation)
328 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100329 mbedtls_mutex_free(&mutex);
330#endif
331}
332
333void mutex_leak(const char *name)
334{
335 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100336#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100337 mbedtls_threading_mutex_t mutex;
338 mbedtls_mutex_init(&mutex);
339#endif
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100340 /* This mutex usage error is detected by our test framework's mutex usage
David Horstmann844da452024-03-27 17:30:22 +0000341 * verification framework. See framework/src/threading_helpers.c. Other
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100342 * threading implementations (e.g. pthread without our instrumentation)
343 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100344}
345
346
347/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100348/* Command line entry point */
349/****************************************************************/
350
351typedef struct {
Gilles Peskinecce00122023-11-10 15:36:15 +0100352 /** Command line argument that will trigger that metatest.
353 *
354 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskine33406b62023-11-02 18:48:39 +0100355 const char *name;
Gilles Peskinecce00122023-11-10 15:36:15 +0100356
357 /** Platform under which that metatest is valid.
358 *
359 * - "any": should work anywhere.
360 * - "asan": triggers ASan (Address Sanitizer).
361 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100362 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
363 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
David Horstmann844da452024-03-27 17:30:22 +0000364 * framework (see framework/src/threading_helpers.c).
Gilles Peskinecce00122023-11-10 15:36:15 +0100365 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100366 const char *platform;
Gilles Peskinecce00122023-11-10 15:36:15 +0100367
368 /** Function that performs the metatest.
369 *
370 * The function receives the name as an argument. This allows using the
371 * same function to perform multiple variants of a test based on the name.
372 *
373 * When executed on a conforming platform, the function is expected to
374 * either cause a test failure (mbedtls_test_fail()), or cause the
375 * program to abort in some way (e.g. by causing a segfault or by
376 * triggering a sanitizer).
377 *
378 * When executed on a non-conforming platform, the function may return
379 * normally or may have unpredictable behavior.
380 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100381 void (*entry_point)(const char *name);
382} metatest_t;
383
Gilles Peskinecce00122023-11-10 15:36:15 +0100384/* The list of availble meta-tests. Remember to register new functions here!
385 *
386 * Note that we always compile all the functions, so that `metatest --list`
387 * will always list all the available meta-tests.
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100388 *
389 * See the documentation of metatest_t::platform for the meaning of
390 * platform values.
Gilles Peskinecce00122023-11-10 15:36:15 +0100391 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100392metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100393 { "test_fail", "any", meta_test_fail },
Paul Elliott60bbfe62024-02-13 15:06:10 +0000394 { "test_not_equal", "any", meta_test_not_equal },
395 { "test_not_le_s", "any", meta_test_not_le_s },
396 { "test_not_le_u", "any", meta_test_not_le_u },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100397 { "null_dereference", "any", null_pointer_dereference },
398 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100399 { "read_after_free", "asan", read_after_free },
400 { "double_free", "asan", double_free },
401 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
402 { "memory_leak", "asan", memory_leak },
David Horstmann9de6edd2024-01-17 14:53:08 +0000403 { "test_memory_poison_0_0_8_r", "poison", test_memory_poison },
404 { "test_memory_poison_0_0_8_w", "poison", test_memory_poison },
405 { "test_memory_poison_0_7_8_r", "poison", test_memory_poison },
406 { "test_memory_poison_0_7_8_w", "poison", test_memory_poison },
407 { "test_memory_poison_0_0_1_r", "poison", test_memory_poison },
408 { "test_memory_poison_0_0_1_w", "poison", test_memory_poison },
409 { "test_memory_poison_0_1_2_r", "poison", test_memory_poison },
410 { "test_memory_poison_0_1_2_w", "poison", test_memory_poison },
411 { "test_memory_poison_7_0_8_r", "poison", test_memory_poison },
412 { "test_memory_poison_7_0_8_w", "poison", test_memory_poison },
413 { "test_memory_poison_7_7_8_r", "poison", test_memory_poison },
414 { "test_memory_poison_7_7_8_w", "poison", test_memory_poison },
415 { "test_memory_poison_7_0_1_r", "poison", test_memory_poison },
416 { "test_memory_poison_7_0_1_w", "poison", test_memory_poison },
417 { "test_memory_poison_7_1_2_r", "poison", test_memory_poison },
418 { "test_memory_poison_7_1_2_w", "poison", test_memory_poison },
Gilles Peskine102aea22023-11-03 18:05:38 +0100419 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
420 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
421 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
422 { "mutex_double_init", "pthread", mutex_double_init },
423 { "mutex_double_free", "pthread", mutex_double_free },
424 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100425 { NULL, NULL, NULL }
426};
427
428static void help(FILE *out, const char *argv0)
429{
430 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
431 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
432 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
433}
434
435int main(int argc, char *argv[])
436{
437 const char *argv0 = argc > 0 ? argv[0] : "metatest";
438 if (argc != 2) {
439 help(stderr, argv0);
440 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
441 }
442
443 /* Support "-help", "--help", "--list", etc. */
444 const char *command = argv[1];
445 while (*command == '-') {
446 ++command;
447 }
448
449 if (strcmp(argv[1], "help") == 0) {
450 help(stdout, argv0);
451 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
452 }
453 if (strcmp(argv[1], "list") == 0) {
454 for (const metatest_t *p = metatests; p->name != NULL; p++) {
455 mbedtls_printf("%s %s\n", p->name, p->platform);
456 }
457 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
458 }
459
Gilles Peskine102aea22023-11-03 18:05:38 +0100460#if defined(MBEDTLS_TEST_MUTEX_USAGE)
461 mbedtls_test_mutex_usage_init();
462#endif
463
Gilles Peskine33406b62023-11-02 18:48:39 +0100464 for (const metatest_t *p = metatests; p->name != NULL; p++) {
465 if (strcmp(argv[1], p->name) == 0) {
466 mbedtls_printf("Running metatest %s...\n", argv[1]);
467 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100468#if defined(MBEDTLS_TEST_MUTEX_USAGE)
469 mbedtls_test_mutex_usage_check();
470#endif
Paul Elliott4580d4d2023-10-27 18:41:02 +0100471 int result = (int) mbedtls_test_get_result();
472
Gilles Peskine33406b62023-11-02 18:48:39 +0100473 mbedtls_printf("Running metatest %s... done, result=%d\n",
Paul Elliott4580d4d2023-10-27 18:41:02 +0100474 argv[1], result);
475 mbedtls_exit(result == MBEDTLS_TEST_RESULT_SUCCESS ?
Gilles Peskine33406b62023-11-02 18:48:39 +0100476 MBEDTLS_EXIT_SUCCESS :
477 MBEDTLS_EXIT_FAILURE);
478 }
479 }
480
481 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
482 argv0, command);
483 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
484}