| 1 |
#pragma once |
| 2 |
|
| 3 |
#if DXX_HAVE_POISON |
| 4 |
#include <algorithm> |
| 5 |
#endif |
| 6 |
|
| 7 |
#include <cstdint> |
| 8 |
#include <memory> |
| 9 |
#include <span> |
| 10 |
|
| 11 |
#if DXX_HAVE_POISON_VALGRIND |
| 12 |
#include <valgrind/memcheck.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
namespace { |
| 16 |
|
| 17 |
/* Inform Valgrind that the memory range contains writable but |
| 18 |
* unreadable bytes. |
| 19 |
* |
| 20 |
* On non-Valgrind runs, this is a no-op. |
| 21 |
*/ |
| 22 |
[[maybe_unused]] |
| 23 |
static void DXX_MAKE_MEM_UNDEFINED(const std::span<std::byte> s) |
| 24 |
{ |
| 25 |
(void)s; |
| 26 |
#if DXX_HAVE_POISON_VALGRIND |
| 27 |
VALGRIND_MAKE_MEM_UNDEFINED(s.data(), s.size()); |
| 28 |
#define DXX_HAVE_POISON_UNDEFINED 1 |
| 29 |
#endif |
| 30 |
} |
| 31 |
|
| 32 |
template <typename T, std::size_t Extent> |
| 33 |
requires(!std::is_same<T, std::byte>::value) |
| 34 |
static inline void DXX_MAKE_MEM_UNDEFINED(const std::span<T, Extent> s) |
| 35 |
{ |
| 36 |
DXX_MAKE_MEM_UNDEFINED(std::as_writable_bytes(s)); |
| 37 |
} |
| 38 |
|
| 39 |
[[maybe_unused]] |
| 40 |
static void DXX_CHECK_MEM_IS_DEFINED(const std::span<const std::byte> s) |
| 41 |
{ |
| 42 |
(void)s; |
| 43 |
#if DXX_HAVE_POISON_VALGRIND |
| 44 |
VALGRIND_CHECK_MEM_IS_DEFINED(s.data(), s.size()); |
| 45 |
#endif |
| 46 |
} |
| 47 |
|
| 48 |
template <typename T, std::size_t Extent> |
| 49 |
requires(!std::is_same<T, std::byte>::value) |
| 50 |
static inline void DXX_CHECK_MEM_IS_DEFINED(const std::span<const T, Extent> s) |
| 51 |
{ |
| 52 |
DXX_CHECK_MEM_IS_DEFINED(std::as_bytes(s)); |
| 53 |
} |
| 54 |
|
| 55 |
/* Convenience function to invoke |
| 56 |
* `DXX_CHECK_MEM_IS_DEFINED(T *, unsigned long)` for exactly one instance |
| 57 |
* of `T`. |
| 58 |
*/ |
| 59 |
template <typename T> |
| 60 |
static void DXX_CHECK_VAR_IS_DEFINED(const T &b) |
| 61 |
{ |
| 62 |
const auto sb{std::as_bytes(std::span<const T, 1>(std::addressof(b), 1))}; |
| 63 |
DXX_CHECK_MEM_IS_DEFINED(sb); |
| 64 |
constexpr bool sanitize_read_range{ |
| 65 |
#ifdef __SANITIZE_ADDRESS__ |
| 66 |
true |
| 67 |
#else |
| 68 |
false |
| 69 |
#endif |
| 70 |
}; |
| 71 |
if constexpr (DXX_HAVE_POISON && sanitize_read_range) |
| 72 |
{ |
| 73 |
/* If the type has a size that is known to be the same as a supported |
| 74 |
* integer type, use a single `asm` statement to read it. |
| 75 |
* Otherwise, use a loop over the span, which will be less efficient. |
| 76 |
*/ |
| 77 |
#define DXX_READ_BUILTIN_TYPE_IF_SIZE(TYPE) \ |
| 78 |
if constexpr (sizeof(T) == sizeof(TYPE)) \ |
| 79 |
{ \ |
| 80 |
/* Tell the compiler to load the value into a register or memory |
| 81 |
* location, so that this line reads the value and triggers any |
| 82 |
* instrumentation that monitors reads. |
| 83 |
*/ \ |
| 84 |
asm("" :: "rm" (*reinterpret_cast<const TYPE *>(sb.data()))); \ |
| 85 |
} \ |
| 86 |
else \ |
| 87 |
|
| 88 |
DXX_READ_BUILTIN_TYPE_IF_SIZE(uintptr_t) |
| 89 |
DXX_READ_BUILTIN_TYPE_IF_SIZE(uint64_t) |
| 90 |
DXX_READ_BUILTIN_TYPE_IF_SIZE(uint32_t) |
| 91 |
DXX_READ_BUILTIN_TYPE_IF_SIZE(uint16_t) |
| 92 |
DXX_READ_BUILTIN_TYPE_IF_SIZE(uint8_t) |
| 93 |
#undef DXX_READ_BUILTIN_TYPE_IF_SIZE |
| 94 |
{ |
| 95 |
/* As a fallback for unsupported types, read each byte of the value |
| 96 |
* individually. |
| 97 |
*/ |
| 98 |
for (const auto &i : sb) |
| 99 |
asm("" :: "rm" (i)); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/* Convenience function to invoke |
| 105 |
* `DXX_MAKE_MEM_UNDEFINED(T *, unsigned long)` for exactly one instance |
| 106 |
* of `T`. |
| 107 |
*/ |
| 108 |
template <typename T> |
| 109 |
static inline void DXX_MAKE_VAR_UNDEFINED(T &b) |
| 110 |
{ |
| 111 |
DXX_MAKE_MEM_UNDEFINED(std::span<T, 1>(std::addressof(b), 1)); |
| 112 |
} |
| 113 |
|
| 114 |
/* If enabled, overwrite the specified memory range with copies of value `v`. |
| 115 |
* In poison=overwrite builds, this is always enabled. |
| 116 |
* In poison=valgrind builds, this is enabled if running under Valgrind. |
| 117 |
* |
| 118 |
* If disabled, this is a no-op. |
| 119 |
*/ |
| 120 |
[[maybe_unused]] |
| 121 |
static void DXX_POISON_MEMORY_RANGE(const std::span<std::byte> sb, const std::byte v) |
| 122 |
{ |
| 123 |
if constexpr (DXX_HAVE_POISON) |
| 124 |
{ |
| 125 |
if constexpr (!DXX_HAVE_POISON_OVERWRITE) |
| 126 |
{ |
| 127 |
#if DXX_HAVE_POISON_VALGRIND |
| 128 |
if (!RUNNING_ON_VALGRIND) |
| 129 |
#endif |
| 130 |
{ |
| 131 |
return; |
| 132 |
} |
| 133 |
} |
| 134 |
std::fill_n(sb.data(), sb.size(), v); |
| 135 |
} |
| 136 |
else |
| 137 |
(void)sb, (void)v; |
| 138 |
} |
| 139 |
|
| 140 |
/* Poison a memory range, but do not mark it as unreadable. This is |
| 141 |
* necessary when poisoning a buffer that will be written to the file or |
| 142 |
* to a network peer and some of the poisoned fields are expected not to |
| 143 |
* be updated (but cannot be removed from the source because the layout |
| 144 |
* is mandatory). |
| 145 |
*/ |
| 146 |
template <typename T, typename V, std::size_t Extent> |
| 147 |
requires(!std::is_same<T, std::byte>::value) |
| 148 |
static inline void DXX_POISON_MEMORY_RANGE(const std::span<T, Extent> st, const V &v) |
| 149 |
{ |
| 150 |
DXX_POISON_MEMORY_RANGE(std::as_writable_bytes(st), std::byte{v}); |
| 151 |
} |
| 152 |
|
| 153 |
/* Poison a memory range, then mark it unreadable. |
| 154 |
*/ |
| 155 |
template <typename T, std::size_t Extent> |
| 156 |
static void DXX_POISON_MEMORY(const std::span<T, Extent> b, const uint8_t v) |
| 157 |
{ |
| 158 |
DXX_POISON_MEMORY_RANGE(b, std::byte{v}); |
| 159 |
DXX_MAKE_MEM_UNDEFINED(b); |
| 160 |
} |
| 161 |
|
| 162 |
/* Convenience function to invoke |
| 163 |
* `DXX_POISON_MEMORY(T &, unsigned long, V)` for exactly one instance |
| 164 |
* of `T`. |
| 165 |
*/ |
| 166 |
template <typename T> |
| 167 |
static inline void DXX_POISON_VAR(T &b, const uint8_t v) |
| 168 |
{ |
| 169 |
DXX_POISON_MEMORY(std::as_writable_bytes(std::span<T, 1>(std::addressof(b), 1)), v); |
| 170 |
} |
| 171 |
|
| 172 |
/* Convenience function to invoke |
| 173 |
* `DXX_POISON_MEMORY_RANGE(T &, V)` for exactly one |
| 174 |
* instance of `T`. |
| 175 |
*/ |
| 176 |
template <typename T> |
| 177 |
static inline void DXX_POISON_DEFINED_VAR(T &b, const unsigned char v) |
| 178 |
{ |
| 179 |
DXX_POISON_MEMORY_RANGE(std::as_writable_bytes(std::span<T, 1>(std::addressof(b), 1)), std::byte{v}); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
#ifndef DXX_HAVE_POISON_UNDEFINED |
| 185 |
#define DXX_HAVE_POISON_UNDEFINED 0 |
| 186 |
#endif |