blob: 805de2d305e5523cd8e84faf12edd9e0f297107a [file] [log] [blame]
Gilles Peskine33406b62023-11-02 18:48:39 +01001/** \file metatest.c
2 *
3 * \brief Test features of the test framework.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 */
10
11#define MBEDTLS_ALLOW_PRIVATE_ACCESS
12
13#include <mbedtls/platform.h>
Gilles Peskine69e8db02023-11-02 19:52:32 +010014#include <mbedtls/platform_util.h>
Gilles Peskine33406b62023-11-02 18:48:39 +010015#include "test/helpers.h"
Gilles Peskine102aea22023-11-03 18:05:38 +010016#include "test/macros.h"
Gilles Peskine33406b62023-11-02 18:48:39 +010017
18#include <stdio.h>
19#include <string.h>
20
Gilles Peskine102aea22023-11-03 18:05:38 +010021#if defined(MBEDTLS_THREADING_C)
22#include <mbedtls/threading.h>
23#endif
24
Gilles Peskine33406b62023-11-02 18:48:39 +010025
Gilles Peskine69e8db02023-11-02 19:52:32 +010026/* This is an external variable, so the compiler doesn't know that we're never
27 * changing its value.
Gilles Peskine69e8db02023-11-02 19:52:32 +010028 */
Gilles Peskined2fa6982023-11-09 21:46:24 +010029volatile int false_but_the_compiler_does_not_know = 0;
30
31/* Set n bytes at the address p to all-bits-zero, in such a way that
32 * the compiler should not know that p is all-bits-zero. */
33static void set_to_zero_but_the_compiler_does_not_know(void *p, size_t n)
34{
35 memset(p, false_but_the_compiler_does_not_know, n);
36}
Gilles Peskine69e8db02023-11-02 19:52:32 +010037
38
Gilles Peskine33406b62023-11-02 18:48:39 +010039/****************************************************************/
Gilles Peskinef309fbf2023-11-02 18:49:52 +010040/* Test framework features */
41/****************************************************************/
42
43void meta_test_fail(const char *name)
44{
45 (void) name;
46 mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
47}
48
49
50/****************************************************************/
Gilles Peskine80ba8322023-11-02 19:23:26 +010051/* Platform features */
52/****************************************************************/
53
54void null_pointer_dereference(const char *name)
55{
56 (void) name;
Gilles Peskine69e8db02023-11-02 19:52:32 +010057 volatile char *p;
Gilles Peskined2fa6982023-11-09 21:46:24 +010058 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskine69e8db02023-11-02 19:52:32 +010059 mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
Gilles Peskine80ba8322023-11-02 19:23:26 +010060}
61
62void null_pointer_call(const char *name)
63{
64 (void) name;
Gilles Peskine69e8db02023-11-02 19:52:32 +010065 unsigned (*p)(void);
Gilles Peskined2fa6982023-11-09 21:46:24 +010066 set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
Gilles Peskinef0d5cf92023-11-03 10:58:57 +010067 /* The pointer representation may be truncated, but we don't care:
68 * the only point of printing it is to have some use of the pointer
69 * to dissuade the compiler from optimizing it away. */
70 mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
Gilles Peskine80ba8322023-11-02 19:23:26 +010071}
72
73
74/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +010075/* Memory */
Gilles Peskineb0f0a642023-11-02 19:42:13 +010076/****************************************************************/
77
78void read_after_free(const char *name)
79{
80 (void) name;
81 volatile char *p = mbedtls_calloc(1, 1);
82 *p = 'a';
83 mbedtls_free((void *) p);
84 mbedtls_printf("%u\n", (unsigned) *p);
85}
86
87void double_free(const char *name)
88{
89 (void) name;
90 volatile char *p = mbedtls_calloc(1, 1);
91 *p = 'a';
92 mbedtls_free((void *) p);
93 mbedtls_free((void *) p);
94}
95
96void read_uninitialized_stack(const char *name)
97{
98 (void) name;
99 volatile char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100100 if (false_but_the_compiler_does_not_know) {
101 buf[0] = '!';
102 }
103 if (*buf != 0) {
104 mbedtls_printf("%u\n", (unsigned) *buf);
105 }
106}
107
108void memory_leak(const char *name)
109{
110 (void) name;
111 volatile char *p = mbedtls_calloc(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100112 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100113}
114
115
116/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100117/* Threading */
118/****************************************************************/
119
120void mutex_lock_not_initialized(const char *name)
121{
122 (void) name;
123 /* Mutex usage verification is only done with pthread, not with other
124 * threading implementations. See tests/src/threading_helpers.c. */
125#if defined(MBEDTLS_THREADING_PTHREAD)
126 mbedtls_threading_mutex_t mutex;
127 memset(&mutex, 0, sizeof(mutex));
128 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
129exit:
130 ;
131#endif
132}
133
134void mutex_unlock_not_initialized(const char *name)
135{
136 (void) name;
137 /* Mutex usage verification is only done with pthread, not with other
138 * threading implementations. See tests/src/threading_helpers.c. */
139#if defined(MBEDTLS_THREADING_C)
140 mbedtls_threading_mutex_t mutex;
141 memset(&mutex, 0, sizeof(mutex));
142 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
143exit:
144 ;
145#endif
146}
147
148void mutex_free_not_initialized(const char *name)
149{
150 (void) name;
151 /* Mutex usage verification is only done with pthread, not with other
152 * threading implementations. See tests/src/threading_helpers.c. */
153#if defined(MBEDTLS_THREADING_C)
154 mbedtls_threading_mutex_t mutex;
155 memset(&mutex, 0, sizeof(mutex));
156 mbedtls_mutex_free(&mutex);
157#endif
158}
159
160void mutex_double_init(const char *name)
161{
162 (void) name;
163#if defined(MBEDTLS_THREADING_C)
164 mbedtls_threading_mutex_t mutex;
165 mbedtls_mutex_init(&mutex);
166 mbedtls_mutex_init(&mutex);
167 mbedtls_mutex_free(&mutex);
168#endif
169}
170
171void mutex_double_free(const char *name)
172{
173 (void) name;
174#if defined(MBEDTLS_THREADING_C)
175 mbedtls_threading_mutex_t mutex;
176 mbedtls_mutex_init(&mutex);
177 mbedtls_mutex_free(&mutex);
178 mbedtls_mutex_free(&mutex);
179#endif
180}
181
182void mutex_leak(const char *name)
183{
184 (void) name;
185 /* Mutex usage verification is only done with pthread, not with other
186 * threading implementations. See tests/src/threading_helpers.c. */
187#if defined(MBEDTLS_THREADING_PTHREAD)
188 mbedtls_threading_mutex_t mutex;
189 mbedtls_mutex_init(&mutex);
190#endif
191}
192
193
194/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100195/* Command line entry point */
196/****************************************************************/
197
198typedef struct {
199 const char *name;
200 const char *platform;
201 void (*entry_point)(const char *name);
202} metatest_t;
203
204metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100205 { "test_fail", "any", meta_test_fail },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100206 { "null_dereference", "any", null_pointer_dereference },
207 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100208 { "read_after_free", "asan", read_after_free },
209 { "double_free", "asan", double_free },
210 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
211 { "memory_leak", "asan", memory_leak },
Gilles Peskine102aea22023-11-03 18:05:38 +0100212 /* Mutex usage verification is only done with pthread, not with other
213 * threading implementations. See tests/src/threading_helpers.c. */
214 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
215 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
216 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
217 { "mutex_double_init", "pthread", mutex_double_init },
218 { "mutex_double_free", "pthread", mutex_double_free },
219 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100220 { NULL, NULL, NULL }
221};
222
223static void help(FILE *out, const char *argv0)
224{
225 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
226 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
227 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
228}
229
230int main(int argc, char *argv[])
231{
232 const char *argv0 = argc > 0 ? argv[0] : "metatest";
233 if (argc != 2) {
234 help(stderr, argv0);
235 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
236 }
237
238 /* Support "-help", "--help", "--list", etc. */
239 const char *command = argv[1];
240 while (*command == '-') {
241 ++command;
242 }
243
244 if (strcmp(argv[1], "help") == 0) {
245 help(stdout, argv0);
246 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
247 }
248 if (strcmp(argv[1], "list") == 0) {
249 for (const metatest_t *p = metatests; p->name != NULL; p++) {
250 mbedtls_printf("%s %s\n", p->name, p->platform);
251 }
252 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
253 }
254
Gilles Peskine102aea22023-11-03 18:05:38 +0100255#if defined(MBEDTLS_TEST_MUTEX_USAGE)
256 mbedtls_test_mutex_usage_init();
257#endif
258
Gilles Peskine33406b62023-11-02 18:48:39 +0100259 for (const metatest_t *p = metatests; p->name != NULL; p++) {
260 if (strcmp(argv[1], p->name) == 0) {
261 mbedtls_printf("Running metatest %s...\n", argv[1]);
262 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100263#if defined(MBEDTLS_TEST_MUTEX_USAGE)
264 mbedtls_test_mutex_usage_check();
265#endif
Gilles Peskine33406b62023-11-02 18:48:39 +0100266 mbedtls_printf("Running metatest %s... done, result=%d\n",
267 argv[1], (int) mbedtls_test_info.result);
268 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
269 MBEDTLS_EXIT_SUCCESS :
270 MBEDTLS_EXIT_FAILURE);
271 }
272 }
273
274 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
275 argv0, command);
276 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
277}