blob: c22ef53f7c47543e69774d28ed3d2563dfb23bf2 [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 *
25 * Currently, only Asan (Address Sanitizer) is supported.
26 */
Gilles Peskined29cce92023-11-02 20:49:34 +010027#if defined(MBEDTLS_TEST_HAVE_ASAN)
28# define MBEDTLS_TEST_MEMORY_CAN_POISON
29#endif
30
31/** \def MBEDTLS_TEST_MEMORY_POISON(buf, size)
32 *
33 * Poison a memory area so that any attempt to read or write from it will
34 * cause a runtime failure.
35 *
Gilles Peskine0bdb6dc2023-11-02 22:44:32 +010036 * Depending on the implementation, this may poison a few bytes beyond the
37 * indicated region, but will never poison a separate object on the heap
38 * or a separate object with more than the alignment of a long long.
39 *
Gilles Peskined29cce92023-11-02 20:49:34 +010040 * The behavior is undefined if any part of the memory area is invalid.
41 *
42 * This is a no-op in builds without a poisoning method.
43 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
44 *
45 * \param buf Pointer to the beginning of the memory area to poison.
46 * \param size Size of the memory area in bytes.
47 */
48
49/** \def MBEDTLS_TEST_MEMORY_UNPOISON(buf, size)
50 *
51 * Undo the effect of #MBEDTLS_TEST_MEMORY_POISON.
52 *
53 * The behavior is undefined if any part of the memory area is invalid,
54 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
55 *
56 * This is a no-op in builds without a poisoning method.
57 * See #MBEDTLS_TEST_MEMORY_CAN_POISON.
58 *
59 * \param buf Pointer to the beginning of the memory area to unpoison.
60 * \param size Size of the memory area in bytes.
61 */
62
63#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
64
65/** Poison a memory area so that any attempt to read or write from it will
66 * cause a runtime failure.
67 *
68 * The behavior is undefined if any part of the memory area is invalid.
69 */
70void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
71#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \
72 mbedtls_test_memory_poison(ptr, size)
73
74/** Undo the effect of mbedtls_test_memory_poison().
75 *
76 * This is a no-op if the given area is entirely valid, unpoisoned memory.
77 *
78 * The behavior is undefined if any part of the memory area is invalid,
79 * or if the memory area contains a mixture of poisoned and unpoisoned parts.
80 */
81void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size);
82#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \
83 mbedtls_test_memory_unpoison(ptr, size)
84
85#else /* MBEDTLS_TEST_MEMORY_CAN_POISON */
Gilles Peskine81f81322023-11-22 18:13:46 +010086#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) (ptr), (void) (size))
87#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) ((void) (ptr), (void) (size))
Gilles Peskined29cce92023-11-02 20:49:34 +010088#endif /* MBEDTLS_TEST_MEMORY_CAN_POISON */
89
Gilles Peskine3fd3d052023-11-02 17:14:01 +010090#endif /* TEST_MEMORY_H */