blob: 68e8da6fa0f592c52fb95fcabfc3e0461b7adcb6 [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 Peskine69e8db02023-11-02 19:52:32 +010077 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine80ba8322023-11-02 19:23:26 +010078}
79
80void null_pointer_call(const char *name)
81{
82 (void) name;
Gilles Peskineda6e7a22023-11-10 10:09:27 +010083 unsigned(*volatile p)(void);
Gilles Peskined2fa6982023-11-09 21:46:24 +010084 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinef0d5cf92023-11-03 10:58:57 +010085 /* The pointer representation may be truncated, but we don't care:
86 * the only point of printing it is to have some use of the pointer
87 * to dissuade the compiler from optimizing it away. */
88 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine80ba8322023-11-02 19:23:26 +010089}
90
91
92/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +010093/* Memory */
Gilles Peskineb0f0a642023-11-02 19:42:13 +010094/****************************************************************/
95
96void read_after_free(const char *name)
97{
98 (void) name;
99 volatile char *p = mbedtls_calloc(1, 1);
100 *p = 'a';
101 mbedtls_free((void *) p);
102 mbedtls_printf("%u\n", (unsigned) *p);
103}
104
105void double_free(const char *name)
106{
107 (void) name;
108 volatile char *p = mbedtls_calloc(1, 1);
109 *p = 'a';
110 mbedtls_free((void *) p);
111 mbedtls_free((void *) p);
112}
113
114void read_uninitialized_stack(const char *name)
115{
116 (void) name;
Gilles Peskineccb12152023-11-10 11:35:36 +0100117 char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100118 if (false_but_the_compiler_does_not_know) {
119 buf[0] = '!';
120 }
Gilles Peskineccb12152023-11-10 11:35:36 +0100121 char *volatile p = buf;
122 if (*p != 0) {
123 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100124 }
125}
126
127void memory_leak(const char *name)
128{
129 (void) name;
130 volatile char *p = mbedtls_calloc(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100131 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100132}
133
134
135/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100136/* Threading */
137/****************************************************************/
138
139void mutex_lock_not_initialized(const char *name)
140{
141 (void) name;
142 /* Mutex usage verification is only done with pthread, not with other
143 * threading implementations. See tests/src/threading_helpers.c. */
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100144#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100145 mbedtls_threading_mutex_t mutex;
146 memset(&mutex, 0, sizeof(mutex));
147 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
148exit:
149 ;
150#endif
151}
152
153void mutex_unlock_not_initialized(const char *name)
154{
155 (void) name;
156 /* Mutex usage verification is only done with pthread, not with other
157 * threading implementations. See tests/src/threading_helpers.c. */
158#if defined(MBEDTLS_THREADING_C)
159 mbedtls_threading_mutex_t mutex;
160 memset(&mutex, 0, sizeof(mutex));
161 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
162exit:
163 ;
164#endif
165}
166
167void mutex_free_not_initialized(const char *name)
168{
169 (void) name;
170 /* Mutex usage verification is only done with pthread, not with other
171 * threading implementations. See tests/src/threading_helpers.c. */
172#if defined(MBEDTLS_THREADING_C)
173 mbedtls_threading_mutex_t mutex;
174 memset(&mutex, 0, sizeof(mutex));
175 mbedtls_mutex_free(&mutex);
176#endif
177}
178
179void mutex_double_init(const char *name)
180{
181 (void) name;
182#if defined(MBEDTLS_THREADING_C)
183 mbedtls_threading_mutex_t mutex;
184 mbedtls_mutex_init(&mutex);
185 mbedtls_mutex_init(&mutex);
186 mbedtls_mutex_free(&mutex);
187#endif
188}
189
190void mutex_double_free(const char *name)
191{
192 (void) name;
193#if defined(MBEDTLS_THREADING_C)
194 mbedtls_threading_mutex_t mutex;
195 mbedtls_mutex_init(&mutex);
196 mbedtls_mutex_free(&mutex);
197 mbedtls_mutex_free(&mutex);
198#endif
199}
200
201void mutex_leak(const char *name)
202{
203 (void) name;
204 /* Mutex usage verification is only done with pthread, not with other
205 * threading implementations. See tests/src/threading_helpers.c. */
Gilles Peskinead2a17e2023-11-16 15:09:48 +0100206#if defined(MBEDTLS_THREADING_C)
Gilles Peskine102aea22023-11-03 18:05:38 +0100207 mbedtls_threading_mutex_t mutex;
208 mbedtls_mutex_init(&mutex);
209#endif
210}
211
212
213/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100214/* Command line entry point */
215/****************************************************************/
216
217typedef struct {
Gilles Peskinecce00122023-11-10 15:36:15 +0100218 /** Command line argument that will trigger that metatest.
219 *
220 * Conventionally matches "[a-z0-9_]+". */
Gilles Peskine33406b62023-11-02 18:48:39 +0100221 const char *name;
Gilles Peskinecce00122023-11-10 15:36:15 +0100222
223 /** Platform under which that metatest is valid.
224 *
225 * - "any": should work anywhere.
226 * - "asan": triggers ASan (Address Sanitizer).
227 * - "msan": triggers MSan (Memory Sanitizer).
228 * - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS.
229 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100230 const char *platform;
Gilles Peskinecce00122023-11-10 15:36:15 +0100231
232 /** Function that performs the metatest.
233 *
234 * The function receives the name as an argument. This allows using the
235 * same function to perform multiple variants of a test based on the name.
236 *
237 * When executed on a conforming platform, the function is expected to
238 * either cause a test failure (mbedtls_test_fail()), or cause the
239 * program to abort in some way (e.g. by causing a segfault or by
240 * triggering a sanitizer).
241 *
242 * When executed on a non-conforming platform, the function may return
243 * normally or may have unpredictable behavior.
244 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100245 void (*entry_point)(const char *name);
246} metatest_t;
247
Gilles Peskinecce00122023-11-10 15:36:15 +0100248/* The list of availble meta-tests. Remember to register new functions here!
249 *
250 * Note that we always compile all the functions, so that `metatest --list`
251 * will always list all the available meta-tests.
252 */
Gilles Peskine33406b62023-11-02 18:48:39 +0100253metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100254 { "test_fail", "any", meta_test_fail },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100255 { "null_dereference", "any", null_pointer_dereference },
256 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100257 { "read_after_free", "asan", read_after_free },
258 { "double_free", "asan", double_free },
259 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
260 { "memory_leak", "asan", memory_leak },
Gilles Peskine102aea22023-11-03 18:05:38 +0100261 /* Mutex usage verification is only done with pthread, not with other
262 * threading implementations. See tests/src/threading_helpers.c. */
263 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
264 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
265 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
266 { "mutex_double_init", "pthread", mutex_double_init },
267 { "mutex_double_free", "pthread", mutex_double_free },
268 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100269 { NULL, NULL, NULL }
270};
271
272static void help(FILE *out, const char *argv0)
273{
274 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
275 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
276 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
277}
278
279int main(int argc, char *argv[])
280{
281 const char *argv0 = argc > 0 ? argv[0] : "metatest";
282 if (argc != 2) {
283 help(stderr, argv0);
284 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
285 }
286
287 /* Support "-help", "--help", "--list", etc. */
288 const char *command = argv[1];
289 while (*command == '-') {
290 ++command;
291 }
292
293 if (strcmp(argv[1], "help") == 0) {
294 help(stdout, argv0);
295 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
296 }
297 if (strcmp(argv[1], "list") == 0) {
298 for (const metatest_t *p = metatests; p->name != NULL; p++) {
299 mbedtls_printf("%s %s\n", p->name, p->platform);
300 }
301 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
302 }
303
Gilles Peskine102aea22023-11-03 18:05:38 +0100304#if defined(MBEDTLS_TEST_MUTEX_USAGE)
305 mbedtls_test_mutex_usage_init();
306#endif
307
Gilles Peskine33406b62023-11-02 18:48:39 +0100308 for (const metatest_t *p = metatests; p->name != NULL; p++) {
309 if (strcmp(argv[1], p->name) == 0) {
310 mbedtls_printf("Running metatest %s...\n", argv[1]);
311 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100312#if defined(MBEDTLS_TEST_MUTEX_USAGE)
313 mbedtls_test_mutex_usage_check();
314#endif
Gilles Peskine33406b62023-11-02 18:48:39 +0100315 mbedtls_printf("Running metatest %s... done, result=%d\n",
316 argv[1], (int) mbedtls_test_info.result);
317 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
318 MBEDTLS_EXIT_SUCCESS :
319 MBEDTLS_EXIT_FAILURE);
320 }
321 }
322
323 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
324 argv0, command);
325 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
326}