| 1 |
================= |
| 2 |
Allocation Tokens |
| 3 |
================= |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
:local: |
| 7 |
|
| 8 |
Introduction |
| 9 |
============ |
| 10 |
|
| 11 |
Clang provides support for allocation tokens to enable allocator-level heap |
| 12 |
organization strategies. Clang assigns mode-dependent token IDs to allocation |
| 13 |
calls; the runtime behavior depends entirely on the implementation of a |
| 14 |
compatible memory allocator. |
| 15 |
|
| 16 |
Possible allocator strategies include: |
| 17 |
|
| 18 |
* **Security Hardening**: Placing allocations into separate, isolated heap |
| 19 |
partitions. For example, separating pointer-containing types from raw data |
| 20 |
can mitigate exploits that rely on overflowing a primitive buffer to corrupt |
| 21 |
object metadata. |
| 22 |
|
| 23 |
* **Memory Layout Optimization**: Grouping related allocations to improve data |
| 24 |
locality and cache utilization. |
| 25 |
|
| 26 |
* **Custom Allocation Policies**: Applying different management strategies to |
| 27 |
different partitions. |
| 28 |
|
| 29 |
Token Assignment Mode |
| 30 |
===================== |
| 31 |
|
| 32 |
The default mode to calculate tokens is: |
| 33 |
|
| 34 |
* ``typehashpointersplit``: This mode assigns a token ID based on the hash of |
| 35 |
the allocated type's name, where the top half ID-space is reserved for types |
| 36 |
that contain pointers and the bottom half for types that do not contain |
| 37 |
pointers. |
| 38 |
|
| 39 |
Other token ID assignment modes are supported, but they may be subject to |
| 40 |
change or removal. These may (experimentally) be selected with ``-Xclang |
| 41 |
-falloc-token-mode=<mode>``: |
| 42 |
|
| 43 |
* ``typehash``: This mode assigns a token ID based on the hash of the allocated |
| 44 |
type's name. |
| 45 |
|
| 46 |
* ``random``: This mode assigns a statically-determined random token ID to each |
| 47 |
allocation site. |
| 48 |
|
| 49 |
* ``increment``: This mode assigns a simple, incrementally increasing token ID |
| 50 |
to each allocation site. |
| 51 |
|
| 52 |
The following command-line options affect generated token IDs: |
| 53 |
|
| 54 |
* ``-falloc-token-max=<N>`` |
| 55 |
Configures the maximum number of token IDs. By default the number of tokens |
| 56 |
is bounded by ``SIZE_MAX``. |
| 57 |
|
| 58 |
Querying Token IDs with ``__builtin_infer_alloc_token`` |
| 59 |
======================================================= |
| 60 |
|
| 61 |
For use cases where the token ID must be known at compile time, Clang provides |
| 62 |
a builtin function: |
| 63 |
|
| 64 |
.. code-block:: c |
| 65 |
|
| 66 |
size_t __builtin_infer_alloc_token(<args>, ...); |
| 67 |
|
| 68 |
This builtin returns the token ID inferred from its argument expressions, which |
| 69 |
mirror arguments normally passed to any allocation function. The argument |
| 70 |
expressions are **unevaluated**, so it can be used with expressions that would |
| 71 |
have side effects without any runtime impact. |
| 72 |
|
| 73 |
For example, it can be used as follows: |
| 74 |
|
| 75 |
.. code-block:: c |
| 76 |
|
| 77 |
struct MyType { ... }; |
| 78 |
void *__partition_alloc(size_t size, size_t partition); |
| 79 |
#define partition_alloc(...) __partition_alloc(__VA_ARGS__, __builtin_infer_alloc_token(__VA_ARGS__)) |
| 80 |
|
| 81 |
void foo(void) { |
| 82 |
MyType *x = partition_alloc(sizeof(*x)); |
| 83 |
} |
| 84 |
|
| 85 |
Allocation Token Instrumentation |
| 86 |
================================ |
| 87 |
|
| 88 |
To enable instrumentation of allocation functions, code can be compiled with |
| 89 |
the ``-fsanitize=alloc-token`` flag: |
| 90 |
|
| 91 |
.. code-block:: console |
| 92 |
|
| 93 |
% clang++ -fsanitize=alloc-token example.cc |
| 94 |
|
| 95 |
The instrumentation transforms allocation calls to include a token ID. For |
| 96 |
example: |
| 97 |
|
| 98 |
.. code-block:: c |
| 99 |
|
| 100 |
// Original: |
| 101 |
ptr = malloc(size); |
| 102 |
|
| 103 |
// Instrumented: |
| 104 |
ptr = __alloc_token_malloc(size, <token id>); |
| 105 |
|
| 106 |
Runtime Interface |
| 107 |
----------------- |
| 108 |
|
| 109 |
A compatible runtime must be provided that implements the token-enabled |
| 110 |
allocation functions. The instrumentation generates calls to functions that |
| 111 |
take a final ``size_t token_id`` argument. |
| 112 |
|
| 113 |
.. code-block:: c |
| 114 |
|
| 115 |
// C standard library functions |
| 116 |
void *__alloc_token_malloc(size_t size, size_t token_id); |
| 117 |
void *__alloc_token_calloc(size_t count, size_t size, size_t token_id); |
| 118 |
void *__alloc_token_realloc(void *ptr, size_t size, size_t token_id); |
| 119 |
// ... |
| 120 |
|
| 121 |
// C++ operators (mangled names) |
| 122 |
// operator new(size_t, size_t) |
| 123 |
void *__alloc_token__Znwm(size_t size, size_t token_id); |
| 124 |
// operator new[](size_t, size_t) |
| 125 |
void *__alloc_token__Znam(size_t size, size_t token_id); |
| 126 |
// ... other variants like nothrow, etc., are also instrumented. |
| 127 |
|
| 128 |
Fast ABI |
| 129 |
-------- |
| 130 |
|
| 131 |
An alternative ABI can be enabled with ``-fsanitize-alloc-token-fast-abi``, |
| 132 |
which encodes the token ID in the allocation function name. |
| 133 |
|
| 134 |
.. code-block:: c |
| 135 |
|
| 136 |
void *__alloc_token_0_malloc(size_t size); |
| 137 |
void *__alloc_token_1_malloc(size_t size); |
| 138 |
void *__alloc_token_2_malloc(size_t size); |
| 139 |
... |
| 140 |
void *__alloc_token_0_Znwm(size_t size); |
| 141 |
void *__alloc_token_1_Znwm(size_t size); |
| 142 |
void *__alloc_token_2_Znwm(size_t size); |
| 143 |
... |
| 144 |
|
| 145 |
This ABI provides a more efficient alternative where |
| 146 |
``-falloc-token-max`` is small. |
| 147 |
|
| 148 |
Instrumenting Non-Standard Allocation Functions |
| 149 |
----------------------------------------------- |
| 150 |
|
| 151 |
By default, AllocToken only instruments standard library allocation functions. |
| 152 |
This simplifies adoption, as a compatible allocator only needs to provide |
| 153 |
token-enabled variants for a well-defined set of standard functions. |
| 154 |
|
| 155 |
To extend instrumentation to custom allocation functions, enable broader |
| 156 |
coverage with ``-fsanitize-alloc-token-extended``. Such functions require being |
| 157 |
marked with the `malloc |
| 158 |
<https://clang.llvm.org/docs/AttributeReference.html#malloc>`_ or `alloc_size |
| 159 |
<https://clang.llvm.org/docs/AttributeReference.html#alloc-size>`_ attributes |
| 160 |
(or a combination). |
| 161 |
|
| 162 |
For example: |
| 163 |
|
| 164 |
.. code-block:: c |
| 165 |
|
| 166 |
void *custom_malloc(size_t size) __attribute__((malloc)); |
| 167 |
void *my_malloc(size_t size) __attribute__((alloc_size(1))); |
| 168 |
|
| 169 |
// Original: |
| 170 |
ptr1 = custom_malloc(size); |
| 171 |
ptr2 = my_malloc(size); |
| 172 |
|
| 173 |
// Instrumented: |
| 174 |
ptr1 = __alloc_token_custom_malloc(size, token_id); |
| 175 |
ptr2 = __alloc_token_my_malloc(size, token_id); |
| 176 |
|
| 177 |
Disabling Instrumentation |
| 178 |
------------------------- |
| 179 |
|
| 180 |
To exclude specific functions from instrumentation, you can use the |
| 181 |
``no_sanitize("alloc-token")`` attribute: |
| 182 |
|
| 183 |
.. code-block:: c |
| 184 |
|
| 185 |
__attribute__((no_sanitize("alloc-token"))) |
| 186 |
void* custom_allocator(size_t size) { |
| 187 |
return malloc(size); // Uses original malloc |
| 188 |
} |
| 189 |
|
| 190 |
Note: Independent of any given allocator support, the instrumentation aims to |
| 191 |
remain performance neutral. As such, ``no_sanitize("alloc-token")`` |
| 192 |
functions may be inlined into instrumented functions and vice-versa. If |
| 193 |
correctness is affected, such functions should explicitly be marked |
| 194 |
``noinline``. |
| 195 |
|
| 196 |
The ``__attribute__((disable_sanitizer_instrumentation))`` is also supported to |
| 197 |
disable this and other sanitizer instrumentations. |
| 198 |
|
| 199 |
Suppressions File (Ignorelist) |
| 200 |
------------------------------ |
| 201 |
|
| 202 |
AllocToken respects the ``src`` and ``fun`` entity types in the |
| 203 |
:doc:`SanitizerSpecialCaseList`, which can be used to omit specified source |
| 204 |
files or functions from instrumentation. |
| 205 |
|
| 206 |
.. code-block:: bash |
| 207 |
|
| 208 |
[alloc-token] |
| 209 |
# Exclude specific source files |
| 210 |
src:third_party/allocator.c |
| 211 |
# Exclude function name patterns |
| 212 |
fun:*custom_malloc* |
| 213 |
fun:LowLevel::* |
| 214 |
|
| 215 |
.. code-block:: console |
| 216 |
|
| 217 |
% clang++ -fsanitize=alloc-token -fsanitize-ignorelist=my_ignorelist.txt example.cc |
| 218 |
|
| 219 |
Conditional Compilation with ``__SANITIZE_ALLOC_TOKEN__`` |
| 220 |
----------------------------------------------------------- |
| 221 |
|
| 222 |
In some cases, one may need to execute different code depending on whether |
| 223 |
AllocToken instrumentation is enabled. The ``__SANITIZE_ALLOC_TOKEN__`` macro |
| 224 |
can be used for this purpose. |
| 225 |
|
| 226 |
.. code-block:: c |
| 227 |
|
| 228 |
#ifdef __SANITIZE_ALLOC_TOKEN__ |
| 229 |
// Code specific to -fsanitize=alloc-token builds |
| 230 |
#endif |