| 1 |
================ |
| 2 |
MemorySanitizer |
| 3 |
================ |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
:local: |
| 7 |
|
| 8 |
Introduction |
| 9 |
============ |
| 10 |
|
| 11 |
MemorySanitizer is a detector of uninitialized memory use. It consists of a |
| 12 |
compiler instrumentation module and a run-time library. |
| 13 |
|
| 14 |
Typical slowdown introduced by MemorySanitizer is **3x**. |
| 15 |
|
| 16 |
Here is a not comprehensive of list cases when MemorySanitizer will report an error: |
| 17 |
|
| 18 |
* Uninitialized value was used in a conditional branch. |
| 19 |
* Uninitialized pointer was used for memory accesses. |
| 20 |
* Uninitialized value was passed or returned from a function call, which is considered an undefined behavior. The check can be disabled with ``-fno-sanitize-memory-param-retval``. |
| 21 |
* Uninitialized data was passed into some libc calls. |
| 22 |
|
| 23 |
How to build |
| 24 |
============ |
| 25 |
|
| 26 |
Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>`_. |
| 27 |
|
| 28 |
Usage |
| 29 |
===== |
| 30 |
|
| 31 |
Simply compile and link your program with ``-fsanitize=memory`` flag. |
| 32 |
The MemorySanitizer run-time library should be linked to the final |
| 33 |
executable, so make sure to use ``clang`` (not ``ld``) for the final |
| 34 |
link step. When linking shared libraries, the MemorySanitizer run-time |
| 35 |
is not linked, so ``-Wl,-z,defs`` may cause link errors (don't use it |
| 36 |
with MemorySanitizer). To get a reasonable performance add ``-O1`` or |
| 37 |
higher. To get meaningful stack traces in error messages add |
| 38 |
``-fno-omit-frame-pointer``. To get perfect stack traces you may need |
| 39 |
to disable inlining (just use ``-O1``) and tail call elimination |
| 40 |
(``-fno-optimize-sibling-calls``). |
| 41 |
|
| 42 |
.. code-block:: console |
| 43 |
|
| 44 |
% cat umr.cc |
| 45 |
#include <stdio.h> |
| 46 |
|
| 47 |
int main(int argc, char** argv) { |
| 48 |
int* a = new int[10]; |
| 49 |
a[5] = 0; |
| 50 |
if (a[argc]) |
| 51 |
printf("xx\n"); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
% clang -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cc |
| 56 |
|
| 57 |
If a bug is detected, the program will print an error message to |
| 58 |
stderr and exit with a non-zero exit code. |
| 59 |
|
| 60 |
.. code-block:: console |
| 61 |
|
| 62 |
% ./a.out |
| 63 |
WARNING: MemorySanitizer: use-of-uninitialized-value |
| 64 |
#0 0x7f45944b418a in main umr.cc:6 |
| 65 |
#1 0x7f45938b676c in __libc_start_main libc-start.c:226 |
| 66 |
|
| 67 |
By default, MemorySanitizer exits on the first detected error. If you |
| 68 |
find the error report hard to understand, try enabling |
| 69 |
:ref:`origin tracking <msan-origins>`. |
| 70 |
|
| 71 |
``__has_feature(memory_sanitizer)`` |
| 72 |
------------------------------------ |
| 73 |
|
| 74 |
In some cases one may need to execute different code depending on |
| 75 |
whether MemorySanitizer is enabled. :ref:`\_\_has\_feature |
| 76 |
<langext-__has_feature-__has_extension>` can be used for this purpose. |
| 77 |
|
| 78 |
.. code-block:: c |
| 79 |
|
| 80 |
#if defined(__has_feature) |
| 81 |
# if __has_feature(memory_sanitizer) |
| 82 |
// code that builds only under MemorySanitizer |
| 83 |
# endif |
| 84 |
#endif |
| 85 |
|
| 86 |
``__attribute__((no_sanitize("memory")))`` |
| 87 |
----------------------------------------------- |
| 88 |
|
| 89 |
Some code should not be checked by MemorySanitizer. One may use the function |
| 90 |
attribute ``no_sanitize("memory")`` to disable uninitialized checks in a |
| 91 |
particular function. MemorySanitizer may still instrument such functions to |
| 92 |
avoid false positives. This attribute may not be supported by other compilers, |
| 93 |
so we suggest to use it together with ``__has_feature(memory_sanitizer)``. |
| 94 |
|
| 95 |
``__attribute__((disable_sanitizer_instrumentation))`` |
| 96 |
-------------------------------------------------------- |
| 97 |
|
| 98 |
The ``disable_sanitizer_instrumentation`` attribute can be applied to functions |
| 99 |
to prevent all kinds of instrumentation. As a result, it may introduce false |
| 100 |
positives and therefore should be used with care, and only if absolutely |
| 101 |
required; for example for certain code that cannot tolerate any instrumentation |
| 102 |
and resulting side-effects. This attribute overrides ``no_sanitize("memory")``. |
| 103 |
|
| 104 |
Conditional Sanitizer Checks with ``__builtin_allow_sanitize_check`` |
| 105 |
-------------------------------------------------------------------- |
| 106 |
|
| 107 |
The ``__builtin_allow_sanitize_check("memory")`` builtin can be used to |
| 108 |
conditionally execute code only when MemorySanitizer is active for the current |
| 109 |
function (after inlining). This is particularly useful for inserting explicit, |
| 110 |
sanitizer-specific checks around operations like syscalls or inline assembly, |
| 111 |
which might otherwise be unchecked by the sanitizer. |
| 112 |
|
| 113 |
Example: |
| 114 |
|
| 115 |
.. code-block:: c |
| 116 |
|
| 117 |
inline __attribute__((always_inline)) |
| 118 |
void copy_to_device(void *addr, size_t size) { |
| 119 |
if (__builtin_allow_sanitize_check("memory")) { |
| 120 |
// Custom checks if `data` is initialized. |
| 121 |
} |
| 122 |
// ... actual device memory copy logic, potentially a syscall ... |
| 123 |
} |
| 124 |
|
| 125 |
void instrumented_function() { |
| 126 |
... |
| 127 |
copy_to_device(buf, sizeof(buf)); // checks are active |
| 128 |
... |
| 129 |
} |
| 130 |
|
| 131 |
__attribute__((no_sanitize("memory"))) |
| 132 |
void uninstrumented_function() { |
| 133 |
... |
| 134 |
copy_to_device(buf, sizeof(buf)); // checks are skipped |
| 135 |
... |
| 136 |
} |
| 137 |
|
| 138 |
Ignorelist |
| 139 |
---------- |
| 140 |
|
| 141 |
MemorySanitizer supports ``src`` and ``fun`` entity types in |
| 142 |
:doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer |
| 143 |
checks for certain source files and functions. All "Use of uninitialized value" |
| 144 |
warnings will be suppressed and all values loaded from memory will be |
| 145 |
considered fully initialized. |
| 146 |
|
| 147 |
Report symbolization |
| 148 |
==================== |
| 149 |
|
| 150 |
MemorySanitizer uses an external symbolizer to print files and line numbers in |
| 151 |
reports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``, |
| 152 |
or set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it. |
| 153 |
|
| 154 |
.. _msan-origins: |
| 155 |
|
| 156 |
Origin Tracking |
| 157 |
=============== |
| 158 |
|
| 159 |
MemorySanitizer can track origins of uninitialized values, similar to |
| 160 |
Valgrind's --track-origins option. This feature is enabled by |
| 161 |
``-fsanitize-memory-track-origins=2`` (or simply |
| 162 |
``-fsanitize-memory-track-origins``) Clang option. With the code from |
| 163 |
the example above, |
| 164 |
|
| 165 |
.. code-block:: console |
| 166 |
|
| 167 |
% cat umr2.cc |
| 168 |
#include <stdio.h> |
| 169 |
|
| 170 |
int main(int argc, char** argv) { |
| 171 |
int* a = new int[10]; |
| 172 |
a[5] = 0; |
| 173 |
volatile int b = a[argc]; |
| 174 |
if (b) |
| 175 |
printf("xx\n"); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
% clang -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2 umr2.cc |
| 180 |
% ./a.out |
| 181 |
WARNING: MemorySanitizer: use-of-uninitialized-value |
| 182 |
#0 0x7f7893912f0b in main umr2.cc:7 |
| 183 |
#1 0x7f789249b76c in __libc_start_main libc-start.c:226 |
| 184 |
|
| 185 |
Uninitialized value was stored to memory at |
| 186 |
#0 0x7f78938b5c25 in __msan_chain_origin msan.cc:484 |
| 187 |
#1 0x7f7893912ecd in main umr2.cc:6 |
| 188 |
|
| 189 |
Uninitialized value was created by a heap allocation |
| 190 |
#0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44 |
| 191 |
#1 0x7f7893912e06 in main umr2.cc:4 |
| 192 |
|
| 193 |
By default, MemorySanitizer collects both allocation points and all |
| 194 |
intermediate stores the uninitialized value went through. Origin |
| 195 |
tracking has proved to be very useful for debugging MemorySanitizer |
| 196 |
reports. It slows down program execution by a factor of 1.5x-2x on top |
| 197 |
of the usual MemorySanitizer slowdown and increases memory overhead. |
| 198 |
|
| 199 |
Clang option ``-fsanitize-memory-track-origins=1`` enables a slightly |
| 200 |
faster mode when MemorySanitizer collects only allocation points but |
| 201 |
not intermediate stores. |
| 202 |
|
| 203 |
Use-after-destruction detection |
| 204 |
=============================== |
| 205 |
|
| 206 |
MemorySanitizer includes use-after-destruction detection. After invocation of |
| 207 |
the destructor, the object will be considered no longer readable, and using |
| 208 |
underlying memory will lead to error reports in runtime. Refer to the standard |
| 209 |
for `lifetime <https://eel.is/c++draft/basic.life#1>`_ definition. |
| 210 |
|
| 211 |
This feature can be disabled with either: |
| 212 |
|
| 213 |
#. Pass additional Clang option ``-fno-sanitize-memory-use-after-dtor`` during |
| 214 |
compilation. |
| 215 |
#. Set environment variable `MSAN_OPTIONS=poison_in_dtor=0` before running |
| 216 |
the program. |
| 217 |
|
| 218 |
Handling external code |
| 219 |
====================== |
| 220 |
|
| 221 |
MemorySanitizer requires that all program code is instrumented. This |
| 222 |
also includes any libraries that the program depends on, even libc. |
| 223 |
Failing to achieve this may result in false reports. |
| 224 |
For the same reason you may need to replace all inline assembly code that writes to memory |
| 225 |
with a pure C/C++ code. |
| 226 |
|
| 227 |
Full MemorySanitizer instrumentation is very difficult to achieve. To |
| 228 |
make it easier, MemorySanitizer runtime library includes 70+ |
| 229 |
interceptors for the most common libc functions. They make it possible |
| 230 |
to run MemorySanitizer-instrumented programs linked with |
| 231 |
uninstrumented libc. For example, the authors were able to bootstrap |
| 232 |
MemorySanitizer-instrumented Clang compiler by linking it with |
| 233 |
self-built instrumented libc++ (as a replacement for libstdc++). |
| 234 |
|
| 235 |
Security Considerations |
| 236 |
======================= |
| 237 |
|
| 238 |
MemorySanitizer is a bug detection tool and its runtime is not meant to be |
| 239 |
linked against production executables. While it may be useful for testing, |
| 240 |
MemorySanitizer's runtime was not developed with security-sensitive |
| 241 |
constraints in mind and may compromise the security of the resulting executable. |
| 242 |
|
| 243 |
Supported Platforms |
| 244 |
=================== |
| 245 |
|
| 246 |
MemorySanitizer is supported on the following OS: |
| 247 |
|
| 248 |
* Linux |
| 249 |
* NetBSD |
| 250 |
* FreeBSD |
| 251 |
|
| 252 |
Limitations |
| 253 |
=========== |
| 254 |
|
| 255 |
* MemorySanitizer uses 2x more real memory than a native run, 3x with |
| 256 |
origin tracking. |
| 257 |
* MemorySanitizer maps (but not reserves) 64 Terabytes of virtual |
| 258 |
address space. This means that tools like ``ulimit`` may not work as |
| 259 |
usually expected. |
| 260 |
* Static linking is not supported. |
| 261 |
* Older versions of MSan (LLVM 3.7 and older) didn't work with |
| 262 |
non-position-independent executables, and could fail on some Linux |
| 263 |
kernel versions with disabled ASLR. Refer to documentation for older versions |
| 264 |
for more details. |
| 265 |
* MemorySanitizer might be incompatible with position-independent executables |
| 266 |
from FreeBSD 13 but there is a check done at runtime and throws a warning |
| 267 |
in this case. |
| 268 |
|
| 269 |
Current Status |
| 270 |
============== |
| 271 |
|
| 272 |
MemorySanitizer is known to work on large real-world programs |
| 273 |
(like Clang/LLVM itself) that can be recompiled from source, including all |
| 274 |
dependent libraries. |
| 275 |
|
| 276 |
More Information |
| 277 |
================ |
| 278 |
|
| 279 |
`<https://github.com/google/sanitizers/wiki/MemorySanitizer>`_ |