2023-11-02 18:48:39 +01:00
|
|
|
/** \file metatest.c
|
|
|
|
*
|
|
|
|
* \brief Test features of the test framework.
|
2023-11-10 15:36:15 +01:00
|
|
|
*
|
|
|
|
* When you run this program, it runs a single "meta-test". A meta-test
|
|
|
|
* performs an operation which should be caught as a failure by our
|
|
|
|
* test framework. The meta-test passes if this program calls `exit` with
|
|
|
|
* a nonzero status, or aborts, or is terminated by a signal, or if the
|
|
|
|
* framework running the program considers the run an error (this happens
|
|
|
|
* with Valgrind for a memory leak). The non-success of the meta-test
|
|
|
|
* program means that the test failure has been caught correctly.
|
|
|
|
*
|
|
|
|
* Some failures are purely functional: the logic of the code causes the
|
|
|
|
* test result to be set to FAIL. Other failures come from extra
|
|
|
|
* instrumentation which is not present in a normal build; for example,
|
|
|
|
* Asan or Valgrind to detect memory leaks. This is reflected by the
|
|
|
|
* "platform" associated with each meta-test.
|
|
|
|
*
|
|
|
|
* Use the companion script `tests/scripts/run-metatests.sh` to run all
|
|
|
|
* the meta-tests for a given platform and validate that they trigger a
|
|
|
|
* detected failure as expected.
|
2023-11-02 18:48:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <mbedtls/platform.h>
|
2023-11-02 19:52:32 +01:00
|
|
|
#include <mbedtls/platform_util.h>
|
2023-11-02 18:48:39 +01:00
|
|
|
#include "test/helpers.h"
|
2023-12-08 17:55:03 +01:00
|
|
|
#include "test/threading_helpers.h"
|
2023-11-03 18:05:38 +01:00
|
|
|
#include "test/macros.h"
|
2023-11-02 18:48:39 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-11-03 18:05:38 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
#include <mbedtls/threading.h>
|
|
|
|
#endif
|
|
|
|
|
2023-11-02 18:48:39 +01:00
|
|
|
|
2023-11-02 19:52:32 +01:00
|
|
|
/* This is an external variable, so the compiler doesn't know that we're never
|
|
|
|
* changing its value.
|
|
|
|
*/
|
2023-11-09 21:46:24 +01:00
|
|
|
volatile int false_but_the_compiler_does_not_know = 0;
|
|
|
|
|
2023-11-21 13:42:40 +01:00
|
|
|
/* Hide calls to calloc/free from static checkers such as
|
|
|
|
* `gcc-12 -Wuse-after-free`, to avoid compile-time complaints about
|
|
|
|
* code where we do mean to cause a runtime error. */
|
|
|
|
void * (* volatile calloc_but_the_compiler_does_not_know)(size_t, size_t) = mbedtls_calloc;
|
|
|
|
void(*volatile free_but_the_compiler_does_not_know)(void *) = mbedtls_free;
|
|
|
|
|
2023-11-09 21:46:24 +01:00
|
|
|
/* Set n bytes at the address p to all-bits-zero, in such a way that
|
|
|
|
* the compiler should not know that p is all-bits-zero. */
|
2023-11-10 10:09:27 +01:00
|
|
|
static void set_to_zero_but_the_compiler_does_not_know(volatile void *p, size_t n)
|
2023-11-09 21:46:24 +01:00
|
|
|
{
|
2023-11-10 10:09:27 +01:00
|
|
|
memset((void *) p, false_but_the_compiler_does_not_know, n);
|
2023-11-09 21:46:24 +01:00
|
|
|
}
|
2023-11-02 19:52:32 +01:00
|
|
|
|
|
|
|
|
2023-11-02 18:49:52 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Test framework features */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
void meta_test_fail(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
mbedtls_test_fail("Forced test failure", __LINE__, __FILE__);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-02 19:23:26 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Platform features */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
void null_pointer_dereference(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-10 10:09:27 +01:00
|
|
|
volatile char *volatile p;
|
2023-11-09 21:46:24 +01:00
|
|
|
set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
|
2023-11-16 15:11:44 +01:00
|
|
|
/* Undefined behavior (read from null data pointer) */
|
2023-11-02 19:52:32 +01:00
|
|
|
mbedtls_printf("%p -> %u\n", p, (unsigned) *p);
|
2023-11-02 19:23:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void null_pointer_call(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-10 10:09:27 +01:00
|
|
|
unsigned(*volatile p)(void);
|
2023-11-09 21:46:24 +01:00
|
|
|
set_to_zero_but_the_compiler_does_not_know(&p, sizeof(p));
|
2023-11-16 15:11:44 +01:00
|
|
|
/* Undefined behavior (execute null function pointer) */
|
2023-11-03 10:58:57 +01:00
|
|
|
/* The pointer representation may be truncated, but we don't care:
|
|
|
|
* the only point of printing it is to have some use of the pointer
|
|
|
|
* to dissuade the compiler from optimizing it away. */
|
|
|
|
mbedtls_printf("%lx() -> %u\n", (unsigned long) (uintptr_t) p, p());
|
2023-11-02 19:23:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-02 19:42:13 +01:00
|
|
|
/****************************************************************/
|
2023-11-03 18:05:38 +01:00
|
|
|
/* Memory */
|
2023-11-02 19:42:13 +01:00
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
void read_after_free(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-21 13:42:40 +01:00
|
|
|
volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
|
2023-11-02 19:42:13 +01:00
|
|
|
*p = 'a';
|
2023-11-21 13:42:40 +01:00
|
|
|
free_but_the_compiler_does_not_know((void *) p);
|
2023-11-16 15:11:44 +01:00
|
|
|
/* Undefined behavior (read after free) */
|
2023-11-02 19:42:13 +01:00
|
|
|
mbedtls_printf("%u\n", (unsigned) *p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void double_free(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-21 13:42:40 +01:00
|
|
|
volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
|
2023-11-02 19:42:13 +01:00
|
|
|
*p = 'a';
|
2023-11-21 13:42:40 +01:00
|
|
|
free_but_the_compiler_does_not_know((void *) p);
|
2023-11-16 15:11:44 +01:00
|
|
|
/* Undefined behavior (double free) */
|
2023-11-21 13:42:40 +01:00
|
|
|
free_but_the_compiler_does_not_know((void *) p);
|
2023-11-02 19:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_uninitialized_stack(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-10 11:35:36 +01:00
|
|
|
char buf[1];
|
2023-11-02 19:42:13 +01:00
|
|
|
if (false_but_the_compiler_does_not_know) {
|
|
|
|
buf[0] = '!';
|
|
|
|
}
|
2023-11-10 11:35:36 +01:00
|
|
|
char *volatile p = buf;
|
|
|
|
if (*p != 0) {
|
2023-11-16 15:11:44 +01:00
|
|
|
/* Unspecified result (read from uninitialized memory) */
|
2023-11-10 11:35:36 +01:00
|
|
|
mbedtls_printf("%u\n", (unsigned) *p);
|
2023-11-02 19:42:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_leak(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-21 13:42:40 +01:00
|
|
|
volatile char *p = calloc_but_the_compiler_does_not_know(1, 1);
|
2023-11-09 21:46:24 +01:00
|
|
|
mbedtls_printf("%u\n", (unsigned) *p);
|
2023-11-16 15:11:44 +01:00
|
|
|
/* Leak of a heap object */
|
2023-11-02 19:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-03 18:05:38 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Threading */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
void mutex_lock_not_initialized(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-16 15:09:48 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-11-03 18:05:38 +01:00
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
memset(&mutex, 0, sizeof(mutex));
|
2023-11-16 15:11:44 +01:00
|
|
|
/* This mutex usage error is detected by our test framework's mutex usage
|
|
|
|
* verification framework. See tests/src/threading_helpers.c. Other
|
|
|
|
* threading implementations (e.g. pthread without our instrumentation)
|
|
|
|
* might consider this normal usage. */
|
2023-11-03 18:05:38 +01:00
|
|
|
TEST_ASSERT(mbedtls_mutex_lock(&mutex) == 0);
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_unlock_not_initialized(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
memset(&mutex, 0, sizeof(mutex));
|
2023-11-16 15:11:44 +01:00
|
|
|
/* This mutex usage error is detected by our test framework's mutex usage
|
|
|
|
* verification framework. See tests/src/threading_helpers.c. Other
|
|
|
|
* threading implementations (e.g. pthread without our instrumentation)
|
|
|
|
* might consider this normal usage. */
|
2023-11-03 18:05:38 +01:00
|
|
|
TEST_ASSERT(mbedtls_mutex_unlock(&mutex) == 0);
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_free_not_initialized(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
memset(&mutex, 0, sizeof(mutex));
|
2023-11-16 15:11:44 +01:00
|
|
|
/* This mutex usage error is detected by our test framework's mutex usage
|
|
|
|
* verification framework. See tests/src/threading_helpers.c. Other
|
|
|
|
* threading implementations (e.g. pthread without our instrumentation)
|
|
|
|
* might consider this normal usage. */
|
2023-11-03 18:05:38 +01:00
|
|
|
mbedtls_mutex_free(&mutex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_double_init(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
mbedtls_mutex_init(&mutex);
|
2023-11-16 15:11:44 +01:00
|
|
|
/* This mutex usage error is detected by our test framework's mutex usage
|
|
|
|
* verification framework. See tests/src/threading_helpers.c. Other
|
|
|
|
* threading implementations (e.g. pthread without our instrumentation)
|
|
|
|
* might consider this normal usage. */
|
2023-11-03 18:05:38 +01:00
|
|
|
mbedtls_mutex_init(&mutex);
|
|
|
|
mbedtls_mutex_free(&mutex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_double_free(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
mbedtls_mutex_init(&mutex);
|
|
|
|
mbedtls_mutex_free(&mutex);
|
2023-11-16 15:11:44 +01:00
|
|
|
/* This mutex usage error is detected by our test framework's mutex usage
|
|
|
|
* verification framework. See tests/src/threading_helpers.c. Other
|
|
|
|
* threading implementations (e.g. pthread without our instrumentation)
|
|
|
|
* might consider this normal usage. */
|
2023-11-03 18:05:38 +01:00
|
|
|
mbedtls_mutex_free(&mutex);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void mutex_leak(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
2023-11-16 15:09:48 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
2023-11-03 18:05:38 +01:00
|
|
|
mbedtls_threading_mutex_t mutex;
|
|
|
|
mbedtls_mutex_init(&mutex);
|
|
|
|
#endif
|
2023-11-16 15:11:44 +01:00
|
|
|
/* This mutex usage error is detected by our test framework's mutex usage
|
|
|
|
* verification framework. See tests/src/threading_helpers.c. Other
|
|
|
|
* threading implementations (e.g. pthread without our instrumentation)
|
|
|
|
* might consider this normal usage. */
|
2023-11-03 18:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-02 18:48:39 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Command line entry point */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
typedef struct {
|
2023-11-10 15:36:15 +01:00
|
|
|
/** Command line argument that will trigger that metatest.
|
|
|
|
*
|
|
|
|
* Conventionally matches "[a-z0-9_]+". */
|
2023-11-02 18:48:39 +01:00
|
|
|
const char *name;
|
2023-11-10 15:36:15 +01:00
|
|
|
|
|
|
|
/** Platform under which that metatest is valid.
|
|
|
|
*
|
|
|
|
* - "any": should work anywhere.
|
|
|
|
* - "asan": triggers ASan (Address Sanitizer).
|
|
|
|
* - "msan": triggers MSan (Memory Sanitizer).
|
2023-11-16 15:11:44 +01:00
|
|
|
* - "pthread": requires MBEDTLS_THREADING_PTHREAD and MBEDTLS_TEST_HOOKS,
|
|
|
|
* which enables MBEDTLS_TEST_MUTEX_USAGE internally in the test
|
|
|
|
* framework (see tests/src/threading_helpers.c).
|
2023-11-10 15:36:15 +01:00
|
|
|
*/
|
2023-11-02 18:48:39 +01:00
|
|
|
const char *platform;
|
2023-11-10 15:36:15 +01:00
|
|
|
|
|
|
|
/** Function that performs the metatest.
|
|
|
|
*
|
|
|
|
* The function receives the name as an argument. This allows using the
|
|
|
|
* same function to perform multiple variants of a test based on the name.
|
|
|
|
*
|
|
|
|
* When executed on a conforming platform, the function is expected to
|
|
|
|
* either cause a test failure (mbedtls_test_fail()), or cause the
|
|
|
|
* program to abort in some way (e.g. by causing a segfault or by
|
|
|
|
* triggering a sanitizer).
|
|
|
|
*
|
|
|
|
* When executed on a non-conforming platform, the function may return
|
|
|
|
* normally or may have unpredictable behavior.
|
|
|
|
*/
|
2023-11-02 18:48:39 +01:00
|
|
|
void (*entry_point)(const char *name);
|
|
|
|
} metatest_t;
|
|
|
|
|
2023-11-10 15:36:15 +01:00
|
|
|
/* The list of availble meta-tests. Remember to register new functions here!
|
|
|
|
*
|
|
|
|
* Note that we always compile all the functions, so that `metatest --list`
|
|
|
|
* will always list all the available meta-tests.
|
2023-11-16 15:11:44 +01:00
|
|
|
*
|
|
|
|
* See the documentation of metatest_t::platform for the meaning of
|
|
|
|
* platform values.
|
2023-11-10 15:36:15 +01:00
|
|
|
*/
|
2023-11-02 18:48:39 +01:00
|
|
|
metatest_t metatests[] = {
|
2023-11-02 18:49:52 +01:00
|
|
|
{ "test_fail", "any", meta_test_fail },
|
2023-11-02 19:23:26 +01:00
|
|
|
{ "null_dereference", "any", null_pointer_dereference },
|
|
|
|
{ "null_call", "any", null_pointer_call },
|
2023-11-02 19:42:13 +01:00
|
|
|
{ "read_after_free", "asan", read_after_free },
|
|
|
|
{ "double_free", "asan", double_free },
|
|
|
|
{ "read_uninitialized_stack", "msan", read_uninitialized_stack },
|
|
|
|
{ "memory_leak", "asan", memory_leak },
|
2023-11-03 18:05:38 +01:00
|
|
|
{ "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
|
|
|
|
{ "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
|
|
|
|
{ "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },
|
|
|
|
{ "mutex_double_init", "pthread", mutex_double_init },
|
|
|
|
{ "mutex_double_free", "pthread", mutex_double_free },
|
|
|
|
{ "mutex_leak", "pthread", mutex_leak },
|
2023-11-02 18:48:39 +01:00
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void help(FILE *out, const char *argv0)
|
|
|
|
{
|
|
|
|
mbedtls_fprintf(out, "Usage: %s list|TEST\n", argv0);
|
|
|
|
mbedtls_fprintf(out, "Run a meta-test that should cause a test failure.\n");
|
|
|
|
mbedtls_fprintf(out, "With 'list', list the available tests and their platform requirement.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *argv0 = argc > 0 ? argv[0] : "metatest";
|
|
|
|
if (argc != 2) {
|
|
|
|
help(stderr, argv0);
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Support "-help", "--help", "--list", etc. */
|
|
|
|
const char *command = argv[1];
|
|
|
|
while (*command == '-') {
|
|
|
|
++command;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "help") == 0) {
|
|
|
|
help(stdout, argv0);
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "list") == 0) {
|
|
|
|
for (const metatest_t *p = metatests; p->name != NULL; p++) {
|
|
|
|
mbedtls_printf("%s %s\n", p->name, p->platform);
|
|
|
|
}
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2023-11-03 18:05:38 +01:00
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
|
|
|
mbedtls_test_mutex_usage_init();
|
|
|
|
#endif
|
|
|
|
|
2023-11-02 18:48:39 +01:00
|
|
|
for (const metatest_t *p = metatests; p->name != NULL; p++) {
|
|
|
|
if (strcmp(argv[1], p->name) == 0) {
|
|
|
|
mbedtls_printf("Running metatest %s...\n", argv[1]);
|
|
|
|
p->entry_point(argv[1]);
|
2023-11-03 18:05:38 +01:00
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
|
|
|
mbedtls_test_mutex_usage_check();
|
|
|
|
#endif
|
2023-10-27 19:41:02 +02:00
|
|
|
int result = (int) mbedtls_test_get_result();
|
|
|
|
|
2023-11-02 18:48:39 +01:00
|
|
|
mbedtls_printf("Running metatest %s... done, result=%d\n",
|
2023-10-27 19:41:02 +02:00
|
|
|
argv[1], result);
|
|
|
|
mbedtls_exit(result == MBEDTLS_TEST_RESULT_SUCCESS ?
|
2023-11-02 18:48:39 +01:00
|
|
|
MBEDTLS_EXIT_SUCCESS :
|
|
|
|
MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
|
|
|
|
argv0, command);
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|