Gilles Peskine | 3fd3d05 | 2023-11-02 17:14:01 +0100 | [diff] [blame] | 1 | /** |
| 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 Peskine | abfad78 | 2023-11-22 18:13:23 +0100 | [diff] [blame^] | 17 | #include "test/helpers.h" |
Gilles Peskine | 3fd3d05 | 2023-11-02 17:14:01 +0100 | [diff] [blame] | 18 | |
Gilles Peskine | d29cce9 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 19 | /** \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 Peskine | d29cce9 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 27 | #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 Peskine | 0bdb6dc | 2023-11-02 22:44:32 +0100 | [diff] [blame] | 36 | * 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 Peskine | d29cce9 | 2023-11-02 20:49:34 +0100 | [diff] [blame] | 40 | * 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 | */ |
| 70 | void 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 | */ |
| 81 | void 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 */ |
| 86 | #define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) 0) |
| 87 | #define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) ((void) 0) |
| 88 | #endif /* MBEDTLS_TEST_MEMORY_CAN_POISON */ |
| 89 | |
Gilles Peskine | 3fd3d05 | 2023-11-02 17:14:01 +0100 | [diff] [blame] | 90 | #endif /* TEST_MEMORY_H */ |