blob: f610a97a4519fa623bbbc5fcbc62e838b3fe85ed [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"
17
Gilles Peskined29cce92023-11-02 20:49:34 +010018/** \def MBEDTLS_TEST_MEMORY_CAN_POISON
19 *
20 * This macro is defined if the tests are compiled with a method to mark
21 * memory as poisoned, which can be used to enforce some memory access
22 * policies.
23 *
24 * Currently, only Asan (Address Sanitizer) is supported.
25 */
26#if defined(__SANITIZE_ADDRESS__)
27# define MBEDTLS_TEST_HAVE_ASAN
28#endif
29#if defined(__has_feature)
30# if __has_feature(address_sanitizer)
31# define MBEDTLS_TEST_HAVE_ASAN
32# endif
33#endif
34#if defined(MBEDTLS_TEST_HAVE_ASAN)
35# define MBEDTLS_TEST_MEMORY_CAN_POISON
36#endif
37
38/** \def MBEDTLS_TEST_MEMORY_POISON(buf, size)
39 *
40 * Poison a memory area so that any attempt to read or write from it will
41 * cause a runtime failure.
42 *
Gilles Peskine0bdb6dc2023-11-02 22:44:32 +010043 * Depending on the implementation, this may poison a few bytes beyond the
44 * indicated region, but will never poison a separate object on the heap
45 * or a separate object with more than the alignment of a long long.
46 *
Gilles Peskined29cce92023-11-02 20:49:34 +010047 * The behavior is undefined if any part of the memory area is invalid.
48 *
49 * This is a no-op in builds without a poisoning method.
50 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
51 *
52 * \param buf Pointer to the beginning of the memory area to poison.
53 * \param size Size of the memory area in bytes.
54 */
55
56/** \def MBEDTLS_TEST_MEMORY_UNPOISON(buf, size)
57 *
58 * Undo the effect of #MBEDTLS_TEST_MEMORY_POISON.
59 *
60 * The behavior is undefined if any part of the memory area is invalid,
61 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
62 *
63 * This is a no-op in builds without a poisoning method.
64 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
65 *
66 * \param buf Pointer to the beginning of the memory area to unpoison.
67 * \param size Size of the memory area in bytes.
68 */
69
70#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
71
72/** Poison a memory area so that any attempt to read or write from it will
73 * cause a runtime failure.
74 *
75 * The behavior is undefined if any part of the memory area is invalid.
76 */
77void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
78#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \
79 mbedtls_test_memory_poison(ptr, size)
80
81/** Undo the effect of mbedtls_test_memory_poison().
82 *
83 * This is a no-op if the given area is entirely valid, unpoisoned memory.
84 *
85 * The behavior is undefined if any part of the memory area is invalid,
86 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
87 */
88void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size);
89#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \
90 mbedtls_test_memory_unpoison(ptr, size)
91
92#else /* MBEDTLS_TEST_MEMORY_CAN_POISON */
93#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) 0)
94#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) ((void) 0)
95#endif /* MBEDTLS_TEST_MEMORY_CAN_POISON */
96
Gilles Peskine3fd3d052023-11-02 17:14:01 +010097#endif /* TEST_MEMORY_H */