blob: 27baf7a1dc1de35d1f74064e7612285200f167f3 [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 *
43 * The behavior is undefined if any part of the memory area is invalid.
44 *
45 * This is a no-op in builds without a poisoning method.
46 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
47 *
48 * \param buf Pointer to the beginning of the memory area to poison.
49 * \param size Size of the memory area in bytes.
50 */
51
52/** \def MBEDTLS_TEST_MEMORY_UNPOISON(buf, size)
53 *
54 * Undo the effect of #MBEDTLS_TEST_MEMORY_POISON.
55 *
56 * The behavior is undefined if any part of the memory area is invalid,
57 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
58 *
59 * This is a no-op in builds without a poisoning method.
60 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
61 *
62 * \param buf Pointer to the beginning of the memory area to unpoison.
63 * \param size Size of the memory area in bytes.
64 */
65
66#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
67
68/** Poison a memory area so that any attempt to read or write from it will
69 * cause a runtime failure.
70 *
71 * The behavior is undefined if any part of the memory area is invalid.
72 */
73void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
74#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \
75 mbedtls_test_memory_poison(ptr, size)
76
77/** Undo the effect of mbedtls_test_memory_poison().
78 *
79 * This is a no-op if the given area is entirely valid, unpoisoned memory.
80 *
81 * The behavior is undefined if any part of the memory area is invalid,
82 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
83 */
84void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size);
85#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \
86 mbedtls_test_memory_unpoison(ptr, size)
87
88#else /* MBEDTLS_TEST_MEMORY_CAN_POISON */
89#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) 0)
90#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) ((void) 0)
91#endif /* MBEDTLS_TEST_MEMORY_CAN_POISON */
92
Gilles Peskine3fd3d052023-11-02 17:14:01 +010093#endif /* TEST_MEMORY_H */