blob: acc6865baf0e13517a2a96358e4a6ad8eec803ad [file] [log] [blame]
Gilles Peskinec33940d2023-11-02 18:48:39 +01001/** \file metatest.c
2 *
3 * \brief Test features of the test framework.
Gilles Peskinec41133b2023-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 Peskinec33940d2023-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
31#include <mbedtls/platform.h>
Gilles Peskine967714d2023-11-02 19:52:32 +010032#include <mbedtls/platform_util.h>
Gilles Peskinec33940d2023-11-02 18:48:39 +010033#include "test/helpers.h"
Gilles Peskinee38eb792023-11-03 18:05:38 +010034#include "test/macros.h"
Gilles Peskinec33940d2023-11-02 18:48:39 +010035
36#include <stdio.h>
37#include <string.h>
38
Gilles Peskinee38eb792023-11-03 18:05:38 +010039#if defined(MBEDTLS_THREADING_C)
40#include <mbedtls/threading.h>
41#endif
42
Gilles Peskinec33940d2023-11-02 18:48:39 +010043
Gilles Peskine967714d2023-11-02 19:52:32 +010044/* This is an external variable, so the compiler doesn't know that we're never
45 * changing its value.
Gilles Peskine967714d2023-11-02 19:52:32 +010046 */
Gilles Peskine53833512023-11-09 21:46:24 +010047volatile int false_but_the_compiler_does_not_know = 0;
48
Gilles Peskinee9616fd2023-11-21 13:42:40 +010049/* Hide calls to calloc/free from static checkers such as
50 * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
51 * code where we do mean to cause a runtime error. */
52void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
53void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
54
Gilles Peskine53833512023-11-09 21:46:24 +010055/* Set n bytes at the address p to all-bits-zero, in such a way that
56 * the compiler should not know that p is all-bits-zero. */
Gilles Peskine226f1bc2023-11-10 10:09:27 +010057static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskine53833512023-11-09 21:46:24 +010058{
Gilles Peskine226f1bc2023-11-10 10:09:27 +010059 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskine53833512023-11-09 21:46:24 +010060}
Gilles Peskine967714d2023-11-02 19:52:32 +010061
62
Gilles Peskinec33940d2023-11-02 18:48:39 +010063/****************************************************************/
Gilles Peskine30380da2023-11-02 18:49:52 +010064/* Test framework features */
65/****************************************************************/
66
67void meta_test_fail(const char *name)
68{
69 (void) name;
70 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
71}
72
Paul Elliott7ebb3c52024-02-13 15:06:10 +000073void meta_test_not_equal(const char *name)
74{
75 int left = 20;
76 int right = 10;
77
78 (void) name;
79
80 TEST_EQUAL(left, right);
81exit:
82 ;
83}
84
85void meta_test_not_le_s(const char *name)
86{
87 int left = 20;
88 int right = 10;
89
90 (void) name;
91
92 TEST_LE_S(left, right);
93exit:
94 ;
95}
96
97void meta_test_not_le_u(const char *name)
98{
99 size_t left = 20;
100 size_t right = 10;
101
102 (void) name;
103
104 TEST_LE_U(left, right);
105exit:
106 ;
107}
Gilles Peskine30380da2023-11-02 18:49:52 +0100108
109/****************************************************************/
Gilles Peskine21d8d592023-11-02 19:23:26 +0100110/* Platform features */
111/****************************************************************/
112
113void null_pointer_dereference(const char *name)
114{
115 (void) name;
Gilles Peskine226f1bc2023-11-10 10:09:27 +0100116 volatile char *volatile p;
Gilles Peskine53833512023-11-09 21:46:24 +0100117 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100118 /* Undefined behavior (read from null data pointer) */
Gilles Peskine967714d2023-11-02 19:52:32 +0100119 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine21d8d592023-11-02 19:23:26 +0100120}
121
122void null_pointer_call(const char *name)
123{
124 (void) name;
Gilles Peskine226f1bc2023-11-10 10:09:27 +0100125 unsigned(*volatile p)(void);
Gilles Peskine53833512023-11-09 21:46:24 +0100126 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100127 /* Undefined behavior (execute null function pointer) */
Gilles Peskinedb2b5c92023-11-03 10:58:57 +0100128 /* The pointer representation may be truncated, but we don't care:
129 * the only point of printing it is to have some use of the pointer
130 * to dissuade the compiler from optimizing it away. */
131 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine21d8d592023-11-02 19:23:26 +0100132}
133
134
135/****************************************************************/
Gilles Peskinee38eb792023-11-03 18:05:38 +0100136/* Memory */
Gilles Peskine970584f2023-11-02 19:42:13 +0100137/****************************************************************/
138
139void read_after_free(const char *name)
140{
141 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100142 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine970584f2023-11-02 19:42:13 +0100143 *p = 'a';
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100144 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100145 /* Undefined behavior (read after free) */
Gilles Peskine970584f2023-11-02 19:42:13 +0100146 mbedtls_printf("%u\n", (unsigned) *p);
147}
148
149void double_free(const char *name)
150{
151 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100152 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine970584f2023-11-02 19:42:13 +0100153 *p = 'a';
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100154 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100155 /* Undefined behavior (double free) */
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100156 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine970584f2023-11-02 19:42:13 +0100157}
158
159void read_uninitialized_stack(const char *name)
160{
161 (void) name;
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100162 char buf[1];
Gilles Peskine970584f2023-11-02 19:42:13 +0100163 if (false_but_the_compiler_does_not_know) {
164 buf[0] = '!';
165 }
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100166 char *volatile p = buf;
167 if (*p != 0) {
Gilles Peskinee00255c2023-11-16 15:11:44 +0100168 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100169 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine970584f2023-11-02 19:42:13 +0100170 }
171}
172
173void memory_leak(const char *name)
174{
175 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100176 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine53833512023-11-09 21:46:24 +0100177 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100178 /* Leak of a heap object */
Gilles Peskine970584f2023-11-02 19:42:13 +0100179}
180
181
182/****************************************************************/
Gilles Peskinee38eb792023-11-03 18:05:38 +0100183/* Threading */
184/****************************************************************/
185
186void mutex_lock_not_initialized(const char *name)
187{
188 (void) name;
Gilles Peskine96c87c42023-11-16 15:09:48 +0100189#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee38eb792023-11-03 18:05:38 +0100190 mbedtls_threading_mutex_t mutex;
191 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100192 /* This mutex usage error is detected by our test framework's mutex usage
193 * verification framework. See tests/src/threading_helpers.c. Other
194 * threading implementations (e.g. pthread without our instrumentation)
195 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100196 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
197exit:
198 ;
199#endif
200}
201
202void mutex_unlock_not_initialized(const char *name)
203{
204 (void) name;
Gilles Peskinee38eb792023-11-03 18:05:38 +0100205#if defined(MBEDTLS_THREADING_C)
206 mbedtls_threading_mutex_t mutex;
207 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100208 /* This mutex usage error is detected by our test framework's mutex usage
209 * verification framework. See tests/src/threading_helpers.c. Other
210 * threading implementations (e.g. pthread without our instrumentation)
211 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100212 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
213exit:
214 ;
215#endif
216}
217
218void mutex_free_not_initialized(const char *name)
219{
220 (void) name;
Gilles Peskinee38eb792023-11-03 18:05:38 +0100221#if defined(MBEDTLS_THREADING_C)
222 mbedtls_threading_mutex_t mutex;
223 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100224 /* This mutex usage error is detected by our test framework's mutex usage
225 * verification framework. See tests/src/threading_helpers.c. Other
226 * threading implementations (e.g. pthread without our instrumentation)
227 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100228 mbedtls_mutex_free(&mutex);
229#endif
230}
231
232void mutex_double_init(const char *name)
233{
234 (void) name;
235#if defined(MBEDTLS_THREADING_C)
236 mbedtls_threading_mutex_t mutex;
237 mbedtls_mutex_init(&mutex);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100238 /* This mutex usage error is detected by our test framework's mutex usage
239 * verification framework. See tests/src/threading_helpers.c. Other
240 * threading implementations (e.g. pthread without our instrumentation)
241 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100242 mbedtls_mutex_init(&mutex);
243 mbedtls_mutex_free(&mutex);
244#endif
245}
246
247void mutex_double_free(const char *name)
248{
249 (void) name;
250#if defined(MBEDTLS_THREADING_C)
251 mbedtls_threading_mutex_t mutex;
252 mbedtls_mutex_init(&mutex);
253 mbedtls_mutex_free(&mutex);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100254 /* This mutex usage error is detected by our test framework's mutex usage
255 * verification framework. See tests/src/threading_helpers.c. Other
256 * threading implementations (e.g. pthread without our instrumentation)
257 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100258 mbedtls_mutex_free(&mutex);
259#endif
260}
261
262void mutex_leak(const char *name)
263{
264 (void) name;
Gilles Peskine96c87c42023-11-16 15:09:48 +0100265#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee38eb792023-11-03 18:05:38 +0100266 mbedtls_threading_mutex_t mutex;
267 mbedtls_mutex_init(&mutex);
268#endif
Gilles Peskinee00255c2023-11-16 15:11:44 +0100269 /* This mutex usage error is detected by our test framework's mutex usage
270 * verification framework. See tests/src/threading_helpers.c. Other
271 * threading implementations (e.g. pthread without our instrumentation)
272 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100273}
274
275
276/****************************************************************/
Gilles Peskinec33940d2023-11-02 18:48:39 +0100277/* Command line entry point */
278/****************************************************************/
279
280typedef struct {
Gilles Peskinec41133b2023-11-10 15:36:15 +0100281 /** Command line argument that will trigger that metatest.
282 *
283 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100284 const char *name;
Gilles Peskinec41133b2023-11-10 15:36:15 +0100285
286 /** Platform under which that metatest is valid.
287 *
288 * - "any": should work anywhere.
289 * - "asan": triggers ASan (Address Sanitizer).
290 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskinee00255c2023-11-16 15:11:44 +0100291 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
292 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
293 * framework (see tests/src/threading_helpers.c).
Gilles Peskinec41133b2023-11-10 15:36:15 +0100294 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100295 const char *platform;
Gilles Peskinec41133b2023-11-10 15:36:15 +0100296
297 /** Function that performs the metatest.
298 *
299 * The function receives the name as an argument. This allows using the
300 * same function to perform multiple variants of a test based on the name.
301 *
302 * When executed on a conforming platform, the function is expected to
303 * either cause a test failure (mbedtls_test_fail()), or cause the
304 * program to abort in some way (e.g. by causing a segfault or by
305 * triggering a sanitizer).
306 *
307 * When executed on a non-conforming platform, the function may return
308 * normally or may have unpredictable behavior.
309 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100310 void (*entry_point)(const char *name);
311} metatest_t;
312
Gilles Peskinec41133b2023-11-10 15:36:15 +0100313/* The list of availble meta-tests. Remember to register new functions here!
314 *
315 * Note that we always compile all the functions, so that `metatest --list`
316 * will always list all the available meta-tests.
Gilles Peskinee00255c2023-11-16 15:11:44 +0100317 *
318 * See the documentation of metatest_t::platform for the meaning of
319 * platform values.
Gilles Peskinec41133b2023-11-10 15:36:15 +0100320 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100321metatest_t metatests[] = {
Gilles Peskine30380da2023-11-02 18:49:52 +0100322 { "test_fail", "any", meta_test_fail },
Paul Elliott7ebb3c52024-02-13 15:06:10 +0000323 { "test_not_equal", "any", meta_test_not_equal },
324 { "test_not_le_s", "any", meta_test_not_le_s },
325 { "test_not_le_u", "any", meta_test_not_le_u },
Gilles Peskine21d8d592023-11-02 19:23:26 +0100326 { "null_dereference", "any", null_pointer_dereference },
327 { "null_call", "any", null_pointer_call },
Gilles Peskine970584f2023-11-02 19:42:13 +0100328 { "read_after_free", "asan", read_after_free },
329 { "double_free", "asan", double_free },
330 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
331 { "memory_leak", "asan", memory_leak },
Gilles Peskinee38eb792023-11-03 18:05:38 +0100332 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
333 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
334 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
335 { "mutex_double_init", "pthread", mutex_double_init },
336 { "mutex_double_free", "pthread", mutex_double_free },
337 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskinec33940d2023-11-02 18:48:39 +0100338 { NULL, NULL, NULL }
339};
340
341static void help(FILE *out, const char *argv0)
342{
343 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
344 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
345 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
346}
347
348int main(int argc, char *argv[])
349{
350 const char *argv0 = argc > 0 ? argv[0] : "metatest";
351 if (argc != 2) {
352 help(stderr, argv0);
353 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
354 }
355
356 /* Support "-help", "--help", "--list", etc. */
357 const char *command = argv[1];
358 while (*command == '-') {
359 ++command;
360 }
361
362 if (strcmp(argv[1], "help") == 0) {
363 help(stdout, argv0);
364 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
365 }
366 if (strcmp(argv[1], "list") == 0) {
367 for (const metatest_t *p = metatests; p->name != NULL; p++) {
368 mbedtls_printf("%s %s\n", p->name, p->platform);
369 }
370 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
371 }
372
Gilles Peskinee38eb792023-11-03 18:05:38 +0100373#if defined(MBEDTLS_TEST_MUTEX_USAGE)
374 mbedtls_test_mutex_usage_init();
375#endif
376
Gilles Peskinec33940d2023-11-02 18:48:39 +0100377 for (const metatest_t *p = metatests; p->name != NULL; p++) {
378 if (strcmp(argv[1], p->name) == 0) {
379 mbedtls_printf("Running metatest %s...\n", argv[1]);
380 p->entry_point(argv[1]);
Gilles Peskinee38eb792023-11-03 18:05:38 +0100381#if defined(MBEDTLS_TEST_MUTEX_USAGE)
382 mbedtls_test_mutex_usage_check();
383#endif
Gilles Peskinec33940d2023-11-02 18:48:39 +0100384 mbedtls_printf("Running metatest %s... done, result=%d\n",
385 argv[1], (int) mbedtls_test_info.result);
386 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
387 MBEDTLS_EXIT_SUCCESS :
388 MBEDTLS_EXIT_FAILURE);
389 }
390 }
391
392 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
393 argv0, command);
394 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
395}