blob: 82ecf953b12194f94224a4a1a133c2bc72b53af6 [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
31#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 Peskine33406b62023-11-02 18:48:39 +010036
37#include <stdio.h>
38#include <string.h>
39
Gilles Peskine102aea22023-11-03 18:05:38 +010040#if defined(MBEDTLS_THREADING_C)
41#include <mbedtls/threading.h>
42#endif
43
Gilles Peskine33406b62023-11-02 18:48:39 +010044
Gilles Peskine69e8db02023-11-02 19:52:32 +010045/* This is an external variable, so the compiler doesn't know that we're never
46 * changing its value.
Gilles Peskine69e8db02023-11-02 19:52:32 +010047 */
Gilles Peskined2fa6982023-11-09 21:46:24 +010048volatile int false_but_the_compiler_does_not_know = 0;
49
Gilles Peskine7a715c42023-11-21 13:42:40 +010050/* Hide calls to calloc/free from static checkers such as
51 * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
52 * code where we do mean to cause a runtime error. */
53void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
54void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
55
Gilles Peskined2fa6982023-11-09 21:46:24 +010056/* Set n bytes at the address p to all-bits-zero, in such a way that
57 * the compiler should not know that p is all-bits-zero. */
Gilles Peskineda6e7a22023-11-10 10:09:27 +010058static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskined2fa6982023-11-09 21:46:24 +010059{
Gilles Peskineda6e7a22023-11-10 10:09:27 +010060 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskined2fa6982023-11-09 21:46:24 +010061}
Gilles Peskine69e8db02023-11-02 19:52:32 +010062
63
Gilles Peskine33406b62023-11-02 18:48:39 +010064/****************************************************************/
Gilles Peskinef309fbf2023-11-02 18:49:52 +010065/* Test framework features */
66/****************************************************************/
67
68void meta_test_fail(const char *name)
69{
70 (void) name;
71 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
72}
73
74
75/****************************************************************/
Gilles Peskine80ba8322023-11-02 19:23:26 +010076/* Platform features */
77/****************************************************************/
78
79void null_pointer_dereference(const char *name)
80{
81 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010082 volatile char *volatile p;
Gilles Peskined2fa6982023-11-09 21:46:24 +010083 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +010084 /* Undefined behavior (read from null data pointer) */
Gilles Peskine69e8db02023-11-02 19:52:32 +010085 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine80ba8322023-11-02 19:23:26 +010086}
87
88void null_pointer_call(const char *name)
89{
90 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010091 unsigned(*volatile p)(void);
Gilles Peskined2fa6982023-11-09 21:46:24 +010092 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +010093 /* Undefined behavior (execute null function pointer) */
Gilles Peskinef0d5cf92023-11-03 10:58:57 +010094 /* The pointer representation may be truncated, but we don't care:
95 * the only point of printing it is to have some use of the pointer
96 * to dissuade the compiler from optimizing it away. */
97 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine80ba8322023-11-02 19:23:26 +010098}
99
100
101/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100102/* Memory */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100103/****************************************************************/
104
105void read_after_free(const char *name)
106{
107 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100108 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100109 *p = 'a';
Gilles Peskine7a715c42023-11-21 13:42:40 +0100110 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100111 /* Undefined behavior (read after free) */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100112 mbedtls_printf("%u\n", (unsigned) *p);
113}
114
115void double_free(const char *name)
116{
117 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100118 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100119 *p = 'a';
Gilles Peskine7a715c42023-11-21 13:42:40 +0100120 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100121 /* Undefined behavior (double free) */
Gilles Peskine7a715c42023-11-21 13:42:40 +0100122 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100123}
124
125void read_uninitialized_stack(const char *name)
126{
127 (void) name;
Gilles Peskineccb12152023-11-10 11:35:36 +0100128 char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100129 if (false_but_the_compiler_does_not_know) {
130 buf[0] = '!';
131 }
Gilles Peskineccb12152023-11-10 11:35:36 +0100132 char *volatile p = buf;
133 if (*p != 0) {
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100134 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineccb12152023-11-10 11:35:36 +0100135 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100136 }
137}
138
139void memory_leak(const char *name)
140{
141 (void) name;
Gilles Peskine7a715c42023-11-21 13:42:40 +0100142 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100143 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100144 /* Leak of a heap object */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100145}
146
147
148/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100149/* Threading */
150/****************************************************************/
151
152void mutex_lock_not_initialized(const char *name)
153{
154 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100155#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100156 mbedtls_threading_mutex_t mutex;
157 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100158 /* This mutex usage error is detected by our test framework's mutex usage
159 * verification framework. See tests/src/threading_helpers.c. Other
160 * threading implementations (e.g. pthread without our instrumentation)
161 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100162 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
163exit:
164 ;
165#endif
166}
167
168void mutex_unlock_not_initialized(const char *name)
169{
170 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100171#if defined(MBEDTLS_THREADING_C)
172 mbedtls_threading_mutex_t mutex;
173 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100174 /* This mutex usage error is detected by our test framework's mutex usage
175 * verification framework. See tests/src/threading_helpers.c. Other
176 * threading implementations (e.g. pthread without our instrumentation)
177 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100178 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
179exit:
180 ;
181#endif
182}
183
184void mutex_free_not_initialized(const char *name)
185{
186 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100187#if defined(MBEDTLS_THREADING_C)
188 mbedtls_threading_mutex_t mutex;
189 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100190 /* This mutex usage error is detected by our test framework's mutex usage
191 * verification framework. See tests/src/threading_helpers.c. Other
192 * threading implementations (e.g. pthread without our instrumentation)
193 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100194 mbedtls_mutex_free(&mutex);
195#endif
196}
197
198void mutex_double_init(const char *name)
199{
200 (void) name;
201#if defined(MBEDTLS_THREADING_C)
202 mbedtls_threading_mutex_t mutex;
203 mbedtls_mutex_init(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100204 /* This mutex usage error is detected by our test framework's mutex usage
205 * verification framework. See tests/src/threading_helpers.c. Other
206 * threading implementations (e.g. pthread without our instrumentation)
207 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100208 mbedtls_mutex_init(&mutex);
209 mbedtls_mutex_free(&mutex);
210#endif
211}
212
213void mutex_double_free(const char *name)
214{
215 (void) name;
216#if defined(MBEDTLS_THREADING_C)
217 mbedtls_threading_mutex_t mutex;
218 mbedtls_mutex_init(&mutex);
219 mbedtls_mutex_free(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100220 /* This mutex usage error is detected by our test framework's mutex usage
221 * verification framework. See tests/src/threading_helpers.c. Other
222 * threading implementations (e.g. pthread without our instrumentation)
223 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100224 mbedtls_mutex_free(&mutex);
225#endif
226}
227
228void mutex_leak(const char *name)
229{
230 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100231#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100232 mbedtls_threading_mutex_t mutex;
233 mbedtls_mutex_init(&mutex);
234#endif
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100235 /* This mutex usage error is detected by our test framework's mutex usage
236 * verification framework. See tests/src/threading_helpers.c. Other
237 * threading implementations (e.g. pthread without our instrumentation)
238 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100239}
240
241
242/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100243/* Command line entry point */
244/****************************************************************/
245
246typedef struct {
Gilles Peskinecce00122023-11-10 15:36:15 +0100247 /** Command line argument that will trigger that metatest.
248 *
249 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskine33406b62023-11-02 18:48:39 +0100250 const char *name;
Gilles Peskinecce00122023-11-10 15:36:15 +0100251
252 /** Platform under which that metatest is valid.
253 *
254 * - "any": should work anywhere.
255 * - "asan": triggers ASan (Address Sanitizer).
256 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100257 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
258 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
259 * framework (see tests/src/threading_helpers.c).
Gilles Peskinecce00122023-11-10 15:36:15 +0100260 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100261 const char *platform;
Gilles Peskinecce00122023-11-10 15:36:15 +0100262
263 /** Function that performs the metatest.
264 *
265 * The function receives the name as an argument. This allows using the
266 * same function to perform multiple variants of a test based on the name.
267 *
268 * When executed on a conforming platform, the function is expected to
269 * either cause a test failure (mbedtls_test_fail()), or cause the
270 * program to abort in some way (e.g. by causing a segfault or by
271 * triggering a sanitizer).
272 *
273 * When executed on a non-conforming platform, the function may return
274 * normally or may have unpredictable behavior.
275 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100276 void (*entry_point)(const char *name);
277} metatest_t;
278
Gilles Peskinecce00122023-11-10 15:36:15 +0100279/* The list of availble meta-tests. Remember to register new functions here!
280 *
281 * Note that we always compile all the functions, so that `metatest --list`
282 * will always list all the available meta-tests.
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100283 *
284 * See the documentation of metatest_t::platform for the meaning of
285 * platform values.
Gilles Peskinecce00122023-11-10 15:36:15 +0100286 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100287metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100288 { "test_fail", "any", meta_test_fail },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100289 { "null_dereference", "any", null_pointer_dereference },
290 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100291 { "read_after_free", "asan", read_after_free },
292 { "double_free", "asan", double_free },
293 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
294 { "memory_leak", "asan", memory_leak },
Gilles Peskine102aea22023-11-03 18:05:38 +0100295 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
296 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
297 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
298 { "mutex_double_init", "pthread", mutex_double_init },
299 { "mutex_double_free", "pthread", mutex_double_free },
300 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100301 { NULL, NULL, NULL }
302};
303
304static void help(FILE *out, const char *argv0)
305{
306 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
307 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
308 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
309}
310
311int main(int argc, char *argv[])
312{
313 const char *argv0 = argc > 0 ? argv[0] : "metatest";
314 if (argc != 2) {
315 help(stderr, argv0);
316 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
317 }
318
319 /* Support "-help", "--help", "--list", etc. */
320 const char *command = argv[1];
321 while (*command == '-') {
322 ++command;
323 }
324
325 if (strcmp(argv[1], "help") == 0) {
326 help(stdout, argv0);
327 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
328 }
329 if (strcmp(argv[1], "list") == 0) {
330 for (const metatest_t *p = metatests; p->name != NULL; p++) {
331 mbedtls_printf("%s %s\n", p->name, p->platform);
332 }
333 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
334 }
335
Gilles Peskine102aea22023-11-03 18:05:38 +0100336#if defined(MBEDTLS_TEST_MUTEX_USAGE)
337 mbedtls_test_mutex_usage_init();
338#endif
339
Gilles Peskine33406b62023-11-02 18:48:39 +0100340 for (const metatest_t *p = metatests; p->name != NULL; p++) {
341 if (strcmp(argv[1], p->name) == 0) {
342 mbedtls_printf("Running metatest %s...\n", argv[1]);
343 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100344#if defined(MBEDTLS_TEST_MUTEX_USAGE)
345 mbedtls_test_mutex_usage_check();
346#endif
Gilles Peskine33406b62023-11-02 18:48:39 +0100347 mbedtls_printf("Running metatest %s... done, result=%d\n",
348 argv[1], (int) mbedtls_test_info.result);
349 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
350 MBEDTLS_EXIT_SUCCESS :
351 MBEDTLS_EXIT_FAILURE);
352 }
353 }
354
355 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
356 argv0, command);
357 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
358}