blob: 5e6a15f1d712f218ede0948e772f1bbc1246ecd0 [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. */
Gilles Peskineda6e7a22023-11-10 10:09:27 +010033static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
Gilles Peskined2fa6982023-11-09 21:46:24 +010034{
Gilles Peskineda6e7a22023-11-10 10:09:27 +010035 memset((void *) p, false_but_the_compiler_does_not_know, n);
Gilles Peskined2fa6982023-11-09 21:46:24 +010036}
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 Peskineda6e7a22023-11-10 10:09:27 +010057 volatile char *volatile 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 Peskineda6e7a22023-11-10 10:09:27 +010065 unsigned(*volatile 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;
Gilles Peskineccb12152023-11-10 11:35:36 +010099 char buf[1];
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100100 if (false_but_the_compiler_does_not_know) {
101 buf[0] = '!';
102 }
Gilles Peskineccb12152023-11-10 11:35:36 +0100103 char *volatile p = buf;
104 if (*p != 0) {
105 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100106 }
107}
108
109void memory_leak(const char *name)
110{
111 (void) name;
112 volatile char *p = mbedtls_calloc(1, 1);
Gilles Peskined2fa6982023-11-09 21:46:24 +0100113 mbedtls_printf("%u\n", (unsigned) *p);
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100114}
115
116
117/****************************************************************/
Gilles Peskine102aea22023-11-03 18:05:38 +0100118/* Threading */
119/****************************************************************/
120
121void mutex_lock_not_initialized(const char *name)
122{
123 (void) name;
124 /* Mutex usage verification is only done with pthread, not with other
125 * threading implementations. See tests/src/threading_helpers.c. */
126#if defined(MBEDTLS_THREADING_PTHREAD)
127 mbedtls_threading_mutex_t mutex;
128 memset(&mutex, 0, sizeof(mutex));
129 TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
130exit:
131 ;
132#endif
133}
134
135void mutex_unlock_not_initialized(const char *name)
136{
137 (void) name;
138 /* Mutex usage verification is only done with pthread, not with other
139 * threading implementations. See tests/src/threading_helpers.c. */
140#if defined(MBEDTLS_THREADING_C)
141 mbedtls_threading_mutex_t mutex;
142 memset(&mutex, 0, sizeof(mutex));
143 TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
144exit:
145 ;
146#endif
147}
148
149void mutex_free_not_initialized(const char *name)
150{
151 (void) name;
152 /* Mutex usage verification is only done with pthread, not with other
153 * threading implementations. See tests/src/threading_helpers.c. */
154#if defined(MBEDTLS_THREADING_C)
155 mbedtls_threading_mutex_t mutex;
156 memset(&mutex, 0, sizeof(mutex));
157 mbedtls_mutex_free(&mutex);
158#endif
159}
160
161void mutex_double_init(const char *name)
162{
163 (void) name;
164#if defined(MBEDTLS_THREADING_C)
165 mbedtls_threading_mutex_t mutex;
166 mbedtls_mutex_init(&mutex);
167 mbedtls_mutex_init(&mutex);
168 mbedtls_mutex_free(&mutex);
169#endif
170}
171
172void mutex_double_free(const char *name)
173{
174 (void) name;
175#if defined(MBEDTLS_THREADING_C)
176 mbedtls_threading_mutex_t mutex;
177 mbedtls_mutex_init(&mutex);
178 mbedtls_mutex_free(&mutex);
179 mbedtls_mutex_free(&mutex);
180#endif
181}
182
183void mutex_leak(const char *name)
184{
185 (void) name;
186 /* Mutex usage verification is only done with pthread, not with other
187 * threading implementations. See tests/src/threading_helpers.c. */
188#if defined(MBEDTLS_THREADING_PTHREAD)
189 mbedtls_threading_mutex_t mutex;
190 mbedtls_mutex_init(&mutex);
191#endif
192}
193
194
195/****************************************************************/
Gilles Peskine33406b62023-11-02 18:48:39 +0100196/* Command line entry point */
197/****************************************************************/
198
199typedef struct {
200 const char *name;
201 const char *platform;
202 void (*entry_point)(const char *name);
203} metatest_t;
204
205metatest_t metatests[] = {
Gilles Peskinef309fbf2023-11-02 18:49:52 +0100206 { "test_fail", "any", meta_test_fail },
Gilles Peskine80ba8322023-11-02 19:23:26 +0100207 { "null_dereference", "any", null_pointer_dereference },
208 { "null_call", "any", null_pointer_call },
Gilles Peskineb0f0a642023-11-02 19:42:13 +0100209 { "read_after_free", "asan", read_after_free },
210 { "double_free", "asan", double_free },
211 { "read_uninitialized_stack", "msan", read_uninitialized_stack },
212 { "memory_leak", "asan", memory_leak },
Gilles Peskine102aea22023-11-03 18:05:38 +0100213 /* Mutex usage verification is only done with pthread, not with other
214 * threading implementations. See tests/src/threading_helpers.c. */
215 { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
216 { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
217 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
218 { "mutex_double_init", "pthread", mutex_double_init },
219 { "mutex_double_free", "pthread", mutex_double_free },
220 { "mutex_leak", "pthread", mutex_leak },
Gilles Peskine33406b62023-11-02 18:48:39 +0100221 { NULL, NULL, NULL }
222};
223
224static void help(FILE *out, const char *argv0)
225{
226 mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
227 mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
228 mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
229}
230
231int main(int argc, char *argv[])
232{
233 const char *argv0 = argc > 0 ? argv[0] : "metatest";
234 if (argc != 2) {
235 help(stderr, argv0);
236 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
237 }
238
239 /* Support "-help", "--help", "--list", etc. */
240 const char *command = argv[1];
241 while (*command == '-') {
242 ++command;
243 }
244
245 if (strcmp(argv[1], "help") == 0) {
246 help(stdout, argv0);
247 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
248 }
249 if (strcmp(argv[1], "list") == 0) {
250 for (const metatest_t *p = metatests; p->name != NULL; p++) {
251 mbedtls_printf("%s %s\n", p->name, p->platform);
252 }
253 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
254 }
255
Gilles Peskine102aea22023-11-03 18:05:38 +0100256#if defined(MBEDTLS_TEST_MUTEX_USAGE)
257 mbedtls_test_mutex_usage_init();
258#endif
259
Gilles Peskine33406b62023-11-02 18:48:39 +0100260 for (const metatest_t *p = metatests; p->name != NULL; p++) {
261 if (strcmp(argv[1], p->name) == 0) {
262 mbedtls_printf("Running metatest %s...\n", argv[1]);
263 p->entry_point(argv[1]);
Gilles Peskine102aea22023-11-03 18:05:38 +0100264#if defined(MBEDTLS_TEST_MUTEX_USAGE)
265 mbedtls_test_mutex_usage_check();
266#endif
Gilles Peskine33406b62023-11-02 18:48:39 +0100267 mbedtls_printf("Running metatest %s... done, result=%d\n",
268 argv[1], (int) mbedtls_test_info.result);
269 mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
270 MBEDTLS_EXIT_SUCCESS :
271 MBEDTLS_EXIT_FAILURE);
272 }
273 }
274
275 mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
276 argv0, command);
277 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
278}