blob: 2973cce3fa22ac9c80eb009f526c1072de641957 [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"
Gilles Peskine102aea22023-11-03 18:05:38 +010034#include "test/macros.h"
Gilles Peskine33406b62023-11-02 18:48:39 +010035
36#include <stdio.h>
37#include <string.h>
38
Gilles Peskine102aea22023-11-03 18:05:38 +010039#if defined(MBEDTLS_THREADING_C)
40#include <mbedtls/threading.h>
41#endif
42
Gilles Peskine33406b62023-11-02 18:48:39 +010043
Gilles Peskine69e8db02023-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 Peskine69e8db02023-11-02 19:52:32 +010046 */
Gilles Peskined2fa6982023-11-09 21:46:24 +010047volatile int false_but_the_compiler_does_not_know = 0;
48
49/* Set n bytes at the address p to all-bits-zero, in such a way that
50 * the compiler should not know that p is all-bits-zero. */
Gilles Peskineda6e7a22023-11-10 10:09:27 +010051static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskined2fa6982023-11-09 21:46:24 +010052{
Gilles Peskineda6e7a22023-11-10 10:09:27 +010053 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskined2fa6982023-11-09 21:46:24 +010054}
Gilles Peskine69e8db02023-11-02 19:52:32 +010055
56
Gilles Peskine33406b62023-11-02 18:48:39 +010057/****************************************************************/
Gilles Peskinef309fbf2023-11-02 18:49:52 +010058/* Test framework features */
59/****************************************************************/
60
61void meta_test_fail(const char *name)
62{
63 (void) name;
64 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
65}
66
67
68/****************************************************************/
Gilles Peskine80ba8322023-11-02 19:23:26 +010069/* Platform features */
70/****************************************************************/
71
72void null_pointer_dereference(const char *name)
73{
74 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010075 volatile char *volatile p;
Gilles Peskined2fa6982023-11-09 21:46:24 +010076 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +010077 /* Undefined behavior (read from null data pointer) */
Gilles Peskine69e8db02023-11-02 19:52:32 +010078 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine80ba8322023-11-02 19:23:26 +010079}
80
81void null_pointer_call(const char *name)
82{
83 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010084 unsigned(*volatile p)(void);
Gilles Peskined2fa6982023-11-09 21:46:24 +010085 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine2f40cc02023-11-16 15:11:44 +010086 /* Undefined behavior (execute null function pointer) */
Gilles Peskinef0d5cf92023-11-03 10:58:57 +010087 /* The pointer representation may be truncated, but we don't care:
88 * the only point of printing it is to have some use of the pointer
89 * to dissuade the compiler from optimizing it away. */
90 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine80ba8322023-11-02 19:23:26 +010091}
92
93
94/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +010095/* Memory */
Gilles Peskineb0f0a642023-11-02 19:42:13 +010096/****************************************************************/
97
98void read_after_free(const char *name)
99{
100 (void) name;
101 volatile char *p = mbedtls_calloc(1, 1);
102 *p = 'a';
103 mbedtls_free((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100104 /* Undefined behavior (read after free) */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100105 mbedtls_printf("%u\n", (unsigned) *p);
106}
107
108void double_free(const char *name)
109{
110 (void) name;
111 volatile char *p = mbedtls_calloc(1, 1);
112 *p = 'a';
113 mbedtls_free((void *) p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100114 /* Undefined behavior (double free) */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100115 mbedtls_free((void *) p);
116}
117
118void read_uninitialized_stack(const char *name)
119{
120 (void) name;
Gilles Peskineccb12152023-11-10 11:35:36 +0100121 char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100122 if (false_but_the_compiler_does_not_know) {
123 buf[0] = '!';
124 }
Gilles Peskineccb12152023-11-10 11:35:36 +0100125 char *volatile p = buf;
126 if (*p != 0) {
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100127 /* Unspecified result (read from uninitialized memory) */
Gilles Peskineccb12152023-11-10 11:35:36 +0100128 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100129 }
130}
131
132void memory_leak(const char *name)
133{
134 (void) name;
135 volatile char *p = mbedtls_calloc(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100136 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100137 /* Leak of a heap object */
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100138}
139
140
141/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100142/* Threading */
143/****************************************************************/
144
145void mutex_lock_not_initialized(const char *name)
146{
147 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100148#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100149 mbedtls_threading_mutex_t mutex;
150 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100151 /* This mutex usage error is detected by our test framework's mutex usage
152 * verification framework. See tests/src/threading_helpers.c. Other
153 * threading implementations (e.g. pthread without our instrumentation)
154 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100155 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
156exit:
157 ;
158#endif
159}
160
161void mutex_unlock_not_initialized(const char *name)
162{
163 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100164#if defined(MBEDTLS_THREADING_C)
165 mbedtls_threading_mutex_t mutex;
166 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100167 /* This mutex usage error is detected by our test framework's mutex usage
168 * verification framework. See tests/src/threading_helpers.c. Other
169 * threading implementations (e.g. pthread without our instrumentation)
170 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100171 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
172exit:
173 ;
174#endif
175}
176
177void mutex_free_not_initialized(const char *name)
178{
179 (void) name;
Gilles Peskine102aea22023-11-03 18:05:38 +0100180#if defined(MBEDTLS_THREADING_C)
181 mbedtls_threading_mutex_t mutex;
182 memset(&mutex, 0, sizeof(mutex));
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100183 /* This mutex usage error is detected by our test framework's mutex usage
184 * verification framework. See tests/src/threading_helpers.c. Other
185 * threading implementations (e.g. pthread without our instrumentation)
186 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100187 mbedtls_mutex_free(&mutex);
188#endif
189}
190
191void mutex_double_init(const char *name)
192{
193 (void) name;
194#if defined(MBEDTLS_THREADING_C)
195 mbedtls_threading_mutex_t mutex;
196 mbedtls_mutex_init(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100197 /* This mutex usage error is detected by our test framework's mutex usage
198 * verification framework. See tests/src/threading_helpers.c. Other
199 * threading implementations (e.g. pthread without our instrumentation)
200 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100201 mbedtls_mutex_init(&mutex);
202 mbedtls_mutex_free(&mutex);
203#endif
204}
205
206void mutex_double_free(const char *name)
207{
208 (void) name;
209#if defined(MBEDTLS_THREADING_C)
210 mbedtls_threading_mutex_t mutex;
211 mbedtls_mutex_init(&mutex);
212 mbedtls_mutex_free(&mutex);
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100213 /* This mutex usage error is detected by our test framework's mutex usage
214 * verification framework. See tests/src/threading_helpers.c. Other
215 * threading implementations (e.g. pthread without our instrumentation)
216 * might consider this normal usage. */
Gilles Peskine102aea22023-11-03 18:05:38 +0100217 mbedtls_mutex_free(&mutex);
218#endif
219}
220
221void mutex_leak(const char *name)
222{
223 (void) name;
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100224#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100225 mbedtls_threading_mutex_t mutex;
226 mbedtls_mutex_init(&mutex);
227#endif
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}
233
234
235/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100236/* Command line entry point */
237/****************************************************************/
238
239typedef struct {
Gilles Peskinecce00122023-11-10 15:36:15 +0100240 /** Command line argument that will trigger that metatest.
241 *
242 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskine33406b62023-11-02 18:48:39 +0100243 const char *name;
Gilles Peskinecce00122023-11-10 15:36:15 +0100244
245 /** Platform under which that metatest is valid.
246 *
247 * - "any": should work anywhere.
248 * - "asan": triggers ASan (Address Sanitizer).
249 * - "msan": triggers MSan (Memory Sanitizer).
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100250 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
251 * which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
252 * framework (see tests/src/threading_helpers.c).
Gilles Peskinecce00122023-11-10 15:36:15 +0100253 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100254 const char *platform;
Gilles Peskinecce00122023-11-10 15:36:15 +0100255
256 /** Function that performs the metatest.
257 *
258 * The function receives the name as an argument. This allows using the
259 * same function to perform multiple variants of a test based on the name.
260 *
261 * When executed on a conforming platform, the function is expected to
262 * either cause a test failure (mbedtls_test_fail()), or cause the
263 * program to abort in some way (e.g. by causing a segfault or by
264 * triggering a sanitizer).
265 *
266 * When executed on a non-conforming platform, the function may return
267 * normally or may have unpredictable behavior.
268 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100269 void (*entry_point)(const char *name);
270} metatest_t;
271
Gilles Peskinecce00122023-11-10 15:36:15 +0100272/* The list of availble meta-tests. Remember to register new functions here!
273 *
274 * Note that we always compile all the functions, so that `metatest --list`
275 * will always list all the available meta-tests.
Gilles Peskine2f40cc02023-11-16 15:11:44 +0100276 *
277 * See the documentation of metatest_t::platform for the meaning of
278 * platform values.
Gilles Peskinecce00122023-11-10 15:36:15 +0100279 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100280metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100281 { "test_fail", "any", meta_test_fail },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100282 { "null_dereference", "any", null_pointer_dereference },
283 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100284 { "read_after_free", "asan", read_after_free },
285 { "double_free", "asan", double_free },
286 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
287 { "memory_leak", "asan", memory_leak },
Gilles Peskine102aea22023-11-03 18:05:38 +0100288 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
289 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
290 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
291 { "mutex_double_init", "pthread", mutex_double_init },
292 { "mutex_double_free", "pthread", mutex_double_free },
293 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100294 { NULL, NULL, NULL }
295};
296
297static void help(FILE *out, const char *argv0)
298{
299 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
300 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
301 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
302}
303
304int main(int argc, char *argv[])
305{
306 const char *argv0 = argc > 0 ? argv[0] : "metatest";
307 if (argc != 2) {
308 help(stderr, argv0);
309 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
310 }
311
312 /* Support "-help", "--help", "--list", etc. */
313 const char *command = argv[1];
314 while (*command == '-') {
315 ++command;
316 }
317
318 if (strcmp(argv[1], "help") == 0) {
319 help(stdout, argv0);
320 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
321 }
322 if (strcmp(argv[1], "list") == 0) {
323 for (const metatest_t *p = metatests; p->name != NULL; p++) {
324 mbedtls_printf("%s %s\n", p->name, p->platform);
325 }
326 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
327 }
328
Gilles Peskine102aea22023-11-03 18:05:38 +0100329#if defined(MBEDTLS_TEST_MUTEX_USAGE)
330 mbedtls_test_mutex_usage_init();
331#endif
332
Gilles Peskine33406b62023-11-02 18:48:39 +0100333 for (const metatest_t *p = metatests; p->name != NULL; p++) {
334 if (strcmp(argv[1], p->name) == 0) {
335 mbedtls_printf("Running metatest %s...\n", argv[1]);
336 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100337#if defined(MBEDTLS_TEST_MUTEX_USAGE)
338 mbedtls_test_mutex_usage_check();
339#endif
Gilles Peskine33406b62023-11-02 18:48:39 +0100340 mbedtls_printf("Running metatest %s... done, result=%d\n",
341 argv[1], (int) mbedtls_test_info.result);
342 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
343 MBEDTLS_EXIT_SUCCESS :
344 MBEDTLS_EXIT_FAILURE);
345 }
346 }
347
348 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
349 argv0, command);
350 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
351}