blob: 6a3e7a911be0c4ebf289f53a9d56452203b9ac2b [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 Peskine071d1442023-11-02 20:49:34 +010035#include "test/memory.h"
Gilles Peskinec33940d2023-11-02 18:48:39 +010036
37#include <stdio.h>
38#include <string.h>
39
Gilles Peskinee38eb792023-11-03 18:05:38 +010040#if defined(MBEDTLS_THREADING_C)
41#include <mbedtls/threading.h>
42#endif
43
Gilles Peskineac8cd662023-11-03 17:01:32 +010044/* C99 feature missing from older versions of MSVC */
45#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
46#define /*no-check-names*/ __func__ __FUNCTION__
47#endif
48
Gilles Peskinec33940d2023-11-02 18:48:39 +010049
Gilles Peskine967714d2023-11-02 19:52:32 +010050/* This is an external variable, so the compiler doesn't know that we're never
51 * changing its value.
Gilles Peskine967714d2023-11-02 19:52:32 +010052 */
Gilles Peskine53833512023-11-09 21:46:24 +010053volatile int false_but_the_compiler_does_not_know = 0;
54
Gilles Peskinee9616fd2023-11-21 13:42:40 +010055/* Hide calls to calloc/free from static checkers such as
56 * `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
57 * code where we do mean to cause a runtime error. */
58void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
59void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
60
Gilles Peskine53833512023-11-09 21:46:24 +010061/* Set n bytes at the address p to all-bits-zero, in such a way that
62 * the compiler should not know that p is all-bits-zero. */
Gilles Peskine226f1bc2023-11-10 10:09:27 +010063static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskine53833512023-11-09 21:46:24 +010064{
Gilles Peskine226f1bc2023-11-10 10:09:27 +010065 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskine53833512023-11-09 21:46:24 +010066}
Gilles Peskine967714d2023-11-02 19:52:32 +010067
68
Gilles Peskinec33940d2023-11-02 18:48:39 +010069/****************************************************************/
Gilles Peskine30380da2023-11-02 18:49:52 +010070/* Test framework features */
71/****************************************************************/
72
73void meta_test_fail(const char *name)
74{
75 (void) name;
76 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
77}
78
79
80/****************************************************************/
Gilles Peskine21d8d592023-11-02 19:23:26 +010081/* Platform features */
82/****************************************************************/
83
84void null_pointer_dereference(const char *name)
85{
86 (void) name;
Gilles Peskine226f1bc2023-11-10 10:09:27 +010087 volatile char *volatile p;
Gilles Peskine53833512023-11-09 21:46:24 +010088 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinee00255c2023-11-16 15:11:44 +010089 /* Undefined behavior (read from null data pointer) */
Gilles Peskine967714d2023-11-02 19:52:32 +010090 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine21d8d592023-11-02 19:23:26 +010091}
92
93void null_pointer_call(const char *name)
94{
95 (void) name;
Gilles Peskine226f1bc2023-11-10 10:09:27 +010096 unsigned(*volatile p)(void);
Gilles Peskine53833512023-11-09 21:46:24 +010097 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinee00255c2023-11-16 15:11:44 +010098 /* Undefined behavior (execute null function pointer) */
Gilles Peskinedb2b5c92023-11-03 10:58:57 +010099 /* The pointer representation may be truncated, but we don't care:
100 * the only point of printing it is to have some use of the pointer
101 * to dissuade the compiler from optimizing it away. */
102 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine21d8d592023-11-02 19:23:26 +0100103}
104
105
106/****************************************************************/
Gilles Peskinee38eb792023-11-03 18:05:38 +0100107/* Memory */
Gilles Peskine970584f2023-11-02 19:42:13 +0100108/****************************************************************/
109
110void read_after_free(const char *name)
111{
112 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100113 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine970584f2023-11-02 19:42:13 +0100114 *p = 'a';
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100115 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100116 /* Undefined behavior (read after free) */
Gilles Peskine970584f2023-11-02 19:42:13 +0100117 mbedtls_printf("%u\n", (unsigned) *p);
118}
119
120void double_free(const char *name)
121{
122 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100123 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine970584f2023-11-02 19:42:13 +0100124 *p = 'a';
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100125 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100126 /* Undefined behavior (double free) */
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100127 free_but_the_compiler_does_not_know((void *) p);
Gilles Peskine970584f2023-11-02 19:42:13 +0100128}
129
130void read_uninitialized_stack(const char *name)
131{
132 (void) name;
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100133 char buf[1];
Gilles Peskine970584f2023-11-02 19:42:13 +0100134 if (false_but_the_compiler_does_not_know) {
135 buf[0] = '!';
136 }
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100137 char *volatile p = buf;
138 if (*p != 0) {
Gilles Peskinee00255c2023-11-16 15:11:44 +0100139 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineefc57ca2023-11-10 11:35:36 +0100140 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine970584f2023-11-02 19:42:13 +0100141 }
142}
143
144void memory_leak(const char *name)
145{
146 (void) name;
Gilles Peskinee9616fd2023-11-21 13:42:40 +0100147 volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
Gilles Peskine53833512023-11-09 21:46:24 +0100148 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100149 /* Leak of a heap object */
Gilles Peskine970584f2023-11-02 19:42:13 +0100150}
151
Gilles Peskine071d1442023-11-02 20:49:34 +0100152/* name = "test_memory_poison_%(start)_%(offset)_%(count)"
153 * Poison a region starting at start from an 8-byte aligned origin,
154 * encompassing count bytes. Access the region at offset from the start.
155 */
156void test_memory_poison(const char *name)
157{
158 size_t start = 0, offset = 0, count = 0;
159 if (sscanf(name, "%*[^0-9]%zu%*[^0-9]%zu%*[^0-9]%zu",
160 &start, &offset, &count) != 3) {
161 mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
162 return;
163 }
164
165 union {
166 long long ll;
167 unsigned char buf[32];
168 } aligned;
169 memset(aligned.buf, 'a', sizeof(aligned.buf));
170
171 if (start > sizeof(aligned.buf)) {
172 mbedtls_fprintf(stderr, "%s: start=%zu > size=%zu", __func__,
173 start, sizeof(aligned.buf));
174 return;
175 }
176 if (start + count > sizeof(aligned.buf)) {
177 mbedtls_fprintf(stderr, "%s: start+count=%zu > size=%zu", __func__,
178 start + count, sizeof(aligned.buf));
179 return;
180 }
181 if (offset >= count) {
182 mbedtls_fprintf(stderr, "%s: offset=%zu >= count=%zu", __func__,
183 offset, count);
184 return;
185 }
186
187 MBEDTLS_TEST_MEMORY_POISON(aligned.buf + start, count);
188 mbedtls_printf("%u\n", (unsigned) aligned.buf[start + offset]);
189}
190
Gilles Peskine970584f2023-11-02 19:42:13 +0100191
192/****************************************************************/
Gilles Peskinee38eb792023-11-03 18:05:38 +0100193/* Threading */
194/****************************************************************/
195
196void mutex_lock_not_initialized(const char *name)
197{
198 (void) name;
Gilles Peskine96c87c42023-11-16 15:09:48 +0100199#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee38eb792023-11-03 18:05:38 +0100200 mbedtls_threading_mutex_t mutex;
201 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100202 /* This mutex usage error is detected by our test framework's mutex usage
203 * verification framework. See tests/src/threading_helpers.c. Other
204 * threading implementations (e.g. pthread without our instrumentation)
205 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100206 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
207exit:
208 ;
209#endif
210}
211
212void mutex_unlock_not_initialized(const char *name)
213{
214 (void) name;
Gilles Peskinee38eb792023-11-03 18:05:38 +0100215#if defined(MBEDTLS_THREADING_C)
216 mbedtls_threading_mutex_t mutex;
217 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100218 /* This mutex usage error is detected by our test framework's mutex usage
219 * verification framework. See tests/src/threading_helpers.c. Other
220 * threading implementations (e.g. pthread without our instrumentation)
221 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100222 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
223exit:
224 ;
225#endif
226}
227
228void mutex_free_not_initialized(const char *name)
229{
230 (void) name;
Gilles Peskinee38eb792023-11-03 18:05:38 +0100231#if defined(MBEDTLS_THREADING_C)
232 mbedtls_threading_mutex_t mutex;
233 memset(&mutex, 0, sizeof(mutex));
Gilles Peskinee00255c2023-11-16 15:11:44 +0100234 /* This mutex usage error is detected by our test framework's mutex usage
235 * verification framework. See tests/src/threading_helpers.c. Other
236 * threading implementations (e.g. pthread without our instrumentation)
237 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100238 mbedtls_mutex_free(&mutex);
239#endif
240}
241
242void mutex_double_init(const char *name)
243{
244 (void) name;
245#if defined(MBEDTLS_THREADING_C)
246 mbedtls_threading_mutex_t mutex;
247 mbedtls_mutex_init(&mutex);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100248 /* This mutex usage error is detected by our test framework's mutex usage
249 * verification framework. See tests/src/threading_helpers.c. Other
250 * threading implementations (e.g. pthread without our instrumentation)
251 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100252 mbedtls_mutex_init(&mutex);
253 mbedtls_mutex_free(&mutex);
254#endif
255}
256
257void mutex_double_free(const char *name)
258{
259 (void) name;
260#if defined(MBEDTLS_THREADING_C)
261 mbedtls_threading_mutex_t mutex;
262 mbedtls_mutex_init(&mutex);
263 mbedtls_mutex_free(&mutex);
Gilles Peskinee00255c2023-11-16 15:11:44 +0100264 /* This mutex usage error is detected by our test framework's mutex usage
265 * verification framework. See tests/src/threading_helpers.c. Other
266 * threading implementations (e.g. pthread without our instrumentation)
267 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100268 mbedtls_mutex_free(&mutex);
269#endif
270}
271
272void mutex_leak(const char *name)
273{
274 (void) name;
Gilles Peskine96c87c42023-11-16 15:09:48 +0100275#if defined(MBEDTLS_THREADING_C)
Gilles Peskinee38eb792023-11-03 18:05:38 +0100276 mbedtls_threading_mutex_t mutex;
277 mbedtls_mutex_init(&mutex);
278#endif
Gilles Peskinee00255c2023-11-16 15:11:44 +0100279 /* This mutex usage error is detected by our test framework's mutex usage
280 * verification framework. See tests/src/threading_helpers.c. Other
281 * threading implementations (e.g. pthread without our instrumentation)
282 * might consider this normal usage. */
Gilles Peskinee38eb792023-11-03 18:05:38 +0100283}
284
285
286/****************************************************************/
Gilles Peskinec33940d2023-11-02 18:48:39 +0100287/* Command line entry point */
288/****************************************************************/
289
290typedef struct {
Gilles Peskinec41133b2023-11-10 15:36:15 +0100291 /** Command line argument that will trigger that metatest.
292 *
293 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100294 const char *name;
Gilles Peskinec41133b2023-11-10 15:36:15 +0100295
296 /** Platform under which that metatest is valid.
297 *
298 * - "any": should work anywhere.
299 * - "asan": triggers ASan (Address Sanitizer).
300 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskinee00255c2023-11-16 15:11:44 +0100301 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
302 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
303 * framework (see tests/src/threading_helpers.c).
Gilles Peskinec41133b2023-11-10 15:36:15 +0100304 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100305 const char *platform;
Gilles Peskinec41133b2023-11-10 15:36:15 +0100306
307 /** Function that performs the metatest.
308 *
309 * The function receives the name as an argument. This allows using the
310 * same function to perform multiple variants of a test based on the name.
311 *
312 * When executed on a conforming platform, the function is expected to
313 * either cause a test failure (mbedtls_test_fail()), or cause the
314 * program to abort in some way (e.g. by causing a segfault or by
315 * triggering a sanitizer).
316 *
317 * When executed on a non-conforming platform, the function may return
318 * normally or may have unpredictable behavior.
319 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100320 void (*entry_point)(const char *name);
321} metatest_t;
322
Gilles Peskinec41133b2023-11-10 15:36:15 +0100323/* The list of availble meta-tests. Remember to register new functions here!
324 *
325 * Note that we always compile all the functions, so that `metatest --list`
326 * will always list all the available meta-tests.
Gilles Peskinee00255c2023-11-16 15:11:44 +0100327 *
328 * See the documentation of metatest_t::platform for the meaning of
329 * platform values.
Gilles Peskinec41133b2023-11-10 15:36:15 +0100330 */
Gilles Peskinec33940d2023-11-02 18:48:39 +0100331metatest_t metatests[] = {
Gilles Peskine30380da2023-11-02 18:49:52 +0100332 { "test_fail", "any", meta_test_fail },
Gilles Peskine21d8d592023-11-02 19:23:26 +0100333 { "null_dereference", "any", null_pointer_dereference },
334 { "null_call", "any", null_pointer_call },
Gilles Peskine970584f2023-11-02 19:42:13 +0100335 { "read_after_free", "asan", read_after_free },
336 { "double_free", "asan", double_free },
337 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
338 { "memory_leak", "asan", memory_leak },
Gilles Peskine071d1442023-11-02 20:49:34 +0100339 { "test_memory_poison_0_0_8", "asan", test_memory_poison },
Gilles Peskine962c5da2023-11-02 22:44:32 +0100340 { "test_memory_poison_0_7_8", "asan", test_memory_poison },
341 { "test_memory_poison_0_0_1", "asan", test_memory_poison },
342 { "test_memory_poison_0_1_2", "asan", test_memory_poison },
343 { "test_memory_poison_7_0_8", "asan", test_memory_poison },
344 { "test_memory_poison_7_7_8", "asan", test_memory_poison },
345 { "test_memory_poison_7_0_1", "asan", test_memory_poison },
346 { "test_memory_poison_7_1_2", "asan", test_memory_poison },
Gilles Peskinee38eb792023-11-03 18:05:38 +0100347 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
348 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
349 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
350 { "mutex_double_init", "pthread", mutex_double_init },
351 { "mutex_double_free", "pthread", mutex_double_free },
352 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskinec33940d2023-11-02 18:48:39 +0100353 { NULL, NULL, NULL }
354};
355
356static void help(FILE *out, const char *argv0)
357{
358 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
359 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
360 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
361}
362
363int main(int argc, char *argv[])
364{
365 const char *argv0 = argc > 0 ? argv[0] : "metatest";
366 if (argc != 2) {
367 help(stderr, argv0);
368 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
369 }
370
371 /* Support "-help", "--help", "--list", etc. */
372 const char *command = argv[1];
373 while (*command == '-') {
374 ++command;
375 }
376
377 if (strcmp(argv[1], "help") == 0) {
378 help(stdout, argv0);
379 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
380 }
381 if (strcmp(argv[1], "list") == 0) {
382 for (const metatest_t *p = metatests; p->name != NULL; p++) {
383 mbedtls_printf("%s %s\n", p->name, p->platform);
384 }
385 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
386 }
387
Gilles Peskinee38eb792023-11-03 18:05:38 +0100388#if defined(MBEDTLS_TEST_MUTEX_USAGE)
389 mbedtls_test_mutex_usage_init();
390#endif
391
Gilles Peskinec33940d2023-11-02 18:48:39 +0100392 for (const metatest_t *p = metatests; p->name != NULL; p++) {
393 if (strcmp(argv[1], p->name) == 0) {
394 mbedtls_printf("Running metatest %s...\n", argv[1]);
395 p->entry_point(argv[1]);
Gilles Peskinee38eb792023-11-03 18:05:38 +0100396#if defined(MBEDTLS_TEST_MUTEX_USAGE)
397 mbedtls_test_mutex_usage_check();
398#endif
Gilles Peskinec33940d2023-11-02 18:48:39 +0100399 mbedtls_printf("Running metatest %s... done, result=%d\n",
400 argv[1], (int) mbedtls_test_info.result);
401 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
402 MBEDTLS_EXIT_SUCCESS :
403 MBEDTLS_EXIT_FAILURE);
404 }
405 }
406
407 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
408 argv0, command);
409 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
410}