blob: 940d9e6baa0fea0412b32dcdbcd568542f18b795 [file] [log] [blame]
Gilles Peskine3fd3d052023-11-02 17:14:01 +01001/**
2 * \file memory.h
3 *
4 * \brief Helper macros and functions related to testing memory management.
5 */
6
7/*
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 */
11
12#ifndef TEST_MEMORY_H
13#define TEST_MEMORY_H
14
15#include "mbedtls/build_info.h"
16#include "mbedtls/platform.h"
Gilles Peskineabfad782023-11-22 18:13:23 +010017#include "test/helpers.h"
Gilles Peskine3fd3d052023-11-02 17:14:01 +010018
Gilles Peskined29cce92023-11-02 20:49:34 +010019/** \def MBEDTLS_TEST_MEMORY_CAN_POISON
20 *
21 * This macro is defined if the tests are compiled with a method to mark
22 * memory as poisoned, which can be used to enforce some memory access
23 * policies.
24 *
David Horstmann81744782024-01-10 14:33:17 +000025 * Support for the C11 thread_local keyword is also required.
26 *
Gilles Peskined29cce92023-11-02 20:49:34 +010027 * Currently, only Asan (Address Sanitizer) is supported.
28 */
David Horstmann81744782024-01-10 14:33:17 +000029#if defined(MBEDTLS_TEST_HAVE_ASAN) && \
David Horstmannb2c9f0e2024-01-31 14:38:15 +000030 (__STDC_VERSION__ >= 201112L) && \
31 !defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskined29cce92023-11-02 20:49:34 +010032# define MBEDTLS_TEST_MEMORY_CAN_POISON
33#endif
34
35/** \def MBEDTLS_TEST_MEMORY_POISON(buf, size)
36 *
37 * Poison a memory area so that any attempt to read or write from it will
38 * cause a runtime failure.
39 *
Gilles Peskine0bdb6dc2023-11-02 22:44:32 +010040 * Depending on the implementation, this may poison a few bytes beyond the
41 * indicated region, but will never poison a separate object on the heap
42 * or a separate object with more than the alignment of a long long.
43 *
Gilles Peskined29cce92023-11-02 20:49:34 +010044 * The behavior is undefined if any part of the memory area is invalid.
45 *
46 * This is a no-op in builds without a poisoning method.
47 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
48 *
49 * \param buf Pointer to the beginning of the memory area to poison.
50 * \param size Size of the memory area in bytes.
51 */
52
53/** \def MBEDTLS_TEST_MEMORY_UNPOISON(buf, size)
54 *
55 * Undo the effect of #MBEDTLS_TEST_MEMORY_POISON.
56 *
57 * The behavior is undefined if any part of the memory area is invalid,
58 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
59 *
60 * This is a no-op in builds without a poisoning method.
61 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
62 *
63 * \param buf Pointer to the beginning of the memory area to unpoison.
64 * \param size Size of the memory area in bytes.
65 */
66
67#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
68
David Horstmann81744782024-01-10 14:33:17 +000069/** Thread-local variable used to enable memory poisoning. This is set and
70 * unset in the test wrappers so that calls to PSA functions from the library
71 * do not poison memory.
72 */
David Horstmannfad038c2024-01-17 14:23:20 +000073extern _Thread_local unsigned int mbedtls_test_memory_poisoning_count;
David Horstmann81744782024-01-10 14:33:17 +000074
Gilles Peskined29cce92023-11-02 20:49:34 +010075/** Poison a memory area so that any attempt to read or write from it will
76 * cause a runtime failure.
77 *
78 * The behavior is undefined if any part of the memory area is invalid.
79 */
80void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
81#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \
David Horstmann81744782024-01-10 14:33:17 +000082 do { \
David Horstmannfad038c2024-01-17 14:23:20 +000083 mbedtls_test_memory_poisoning_count++; \
David Horstmann81744782024-01-10 14:33:17 +000084 mbedtls_test_memory_poison(ptr, size); \
85 } while (0)
Gilles Peskined29cce92023-11-02 20:49:34 +010086
87/** Undo the effect of mbedtls_test_memory_poison().
88 *
89 * This is a no-op if the given area is entirely valid, unpoisoned memory.
90 *
91 * The behavior is undefined if any part of the memory area is invalid,
92 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
93 */
94void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size);
95#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \
David Horstmann81744782024-01-10 14:33:17 +000096 do { \
97 mbedtls_test_memory_unpoison(ptr, size); \
David Horstmannd3efb922024-01-17 15:27:50 +000098 if (mbedtls_test_memory_poisoning_count != 0) { \
99 mbedtls_test_memory_poisoning_count--; \
100 } \
David Horstmann81744782024-01-10 14:33:17 +0000101 } while (0)
Gilles Peskined29cce92023-11-02 20:49:34 +0100102
103#else /* MBEDTLS_TEST_MEMORY_CAN_POISON */
Gilles Peskine81f81322023-11-22 18:13:46 +0100104#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) (ptr), (void) (size))
105#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) ((void) (ptr), (void) (size))
Gilles Peskined29cce92023-11-02 20:49:34 +0100106#endif /* MBEDTLS_TEST_MEMORY_CAN_POISON */
107
Gilles Peskine3fd3d052023-11-02 17:14:01 +0100108#endif /* TEST_MEMORY_H */