2023-11-02 18:48:39 +01:00
|
|
|
/** \file metatest.c
|
|
|
|
*
|
|
|
|
* \brief Test features of the test framework.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
|
|
|
#include <mbedtls/platform.h>
|
|
|
|
#include "test/helpers.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
char *p;
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
volatile char c;
|
|
|
|
c = *p;
|
|
|
|
(void) c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void null_pointer_call(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
void (*p)(void);
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-02 19:42:13 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Sanitizers */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
void read_after_free(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
volatile char *p = mbedtls_calloc(1, 1);
|
|
|
|
*p = 'a';
|
|
|
|
mbedtls_free((void *) p);
|
|
|
|
mbedtls_printf("%u\n", (unsigned) *p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void double_free(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
volatile char *p = mbedtls_calloc(1, 1);
|
|
|
|
*p = 'a';
|
|
|
|
mbedtls_free((void *) p);
|
|
|
|
mbedtls_free((void *) p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_uninitialized_stack(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
volatile char buf[1];
|
|
|
|
static int false_but_the_compiler_does_not_know = 0;
|
|
|
|
if (false_but_the_compiler_does_not_know) {
|
|
|
|
buf[0] = '!';
|
|
|
|
}
|
|
|
|
if (*buf != 0) {
|
|
|
|
mbedtls_printf("%u\n", (unsigned) *buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_leak(const char *name)
|
|
|
|
{
|
|
|
|
(void) name;
|
|
|
|
volatile char *p = mbedtls_calloc(1, 1);
|
|
|
|
/* Hint to the compiler that calloc must not be optimized away. */
|
|
|
|
(void) *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-02 18:48:39 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* Command line entry point */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
const char *platform;
|
|
|
|
void (*entry_point)(const char *name);
|
|
|
|
} metatest_t;
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
mbedtls_printf("Running metatest %s... done, result=%d\n",
|
|
|
|
argv[1], (int) mbedtls_test_info.result);
|
|
|
|
mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
|
|
|
|
MBEDTLS_EXIT_SUCCESS :
|
|
|
|
MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_fprintf(stderr, "%s: FATAL: No such metatest: %s\n",
|
|
|
|
argv0, command);
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|