Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 1 | ================ |
| 2 | MemorySanitizer |
| 3 | ================ |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | Introduction |
| 9 | ============ |
| 10 | |
| 11 | MemorySanitizer is a detector of uninitialized reads. It consists of a |
| 12 | compiler instrumentation module and a run-time library. |
| 13 | |
| 14 | Typical slowdown introduced by MemorySanitizer is **3x**. |
| 15 | |
| 16 | How to build |
| 17 | ============ |
| 18 | |
Alexey Samsonov | 3a433f6 | 2015-02-18 22:26:20 +0000 | [diff] [blame] | 19 | Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 20 | |
| 21 | Usage |
| 22 | ===== |
| 23 | |
| 24 | Simply compile and link your program with ``-fsanitize=memory`` flag. |
| 25 | The MemorySanitizer run-time library should be linked to the final |
| 26 | executable, so make sure to use ``clang`` (not ``ld``) for the final |
| 27 | link step. When linking shared libraries, the MemorySanitizer run-time |
| 28 | is not linked, so ``-Wl,-z,defs`` may cause link errors (don't use it |
| 29 | with MemorySanitizer). To get a reasonable performance add ``-O1`` or |
Sylvestre Ledru | 94a2104 | 2017-06-26 02:45:08 +0000 | [diff] [blame] | 30 | higher. To get meaningful stack traces in error messages add |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 31 | ``-fno-omit-frame-pointer``. To get perfect stack traces you may need |
| 32 | to disable inlining (just use ``-O1``) and tail call elimination |
| 33 | (``-fno-optimize-sibling-calls``). |
| 34 | |
| 35 | .. code-block:: console |
Dmitri Gribenko | 46735cb | 2012-12-23 18:36:44 +0000 | [diff] [blame] | 36 | |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 37 | % cat umr.cc |
| 38 | #include <stdio.h> |
| 39 | |
| 40 | int main(int argc, char** argv) { |
| 41 | int* a = new int[10]; |
| 42 | a[5] = 0; |
| 43 | if (a[argc]) |
| 44 | printf("xx\n"); |
| 45 | return 0; |
| 46 | } |
| 47 | |
Peter Collingbourne | 54d770c | 2013-04-09 04:35:11 +0000 | [diff] [blame] | 48 | % clang -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cc |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 49 | |
| 50 | If a bug is detected, the program will print an error message to |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 51 | stderr and exit with a non-zero exit code. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 52 | |
| 53 | .. code-block:: console |
| 54 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 55 | % ./a.out |
| 56 | WARNING: MemorySanitizer: use-of-uninitialized-value |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 57 | #0 0x7f45944b418a in main umr.cc:6 |
| 58 | #1 0x7f45938b676c in __libc_start_main libc-start.c:226 |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 59 | |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 60 | By default, MemorySanitizer exits on the first detected error. If you |
| 61 | find the error report hard to understand, try enabling |
| 62 | :ref:`origin tracking <msan-origins>`. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 63 | |
| 64 | ``__has_feature(memory_sanitizer)`` |
| 65 | ------------------------------------ |
| 66 | |
| 67 | In some cases one may need to execute different code depending on |
| 68 | whether MemorySanitizer is enabled. :ref:`\_\_has\_feature |
| 69 | <langext-__has_feature-__has_extension>` can be used for this purpose. |
| 70 | |
| 71 | .. code-block:: c |
| 72 | |
| 73 | #if defined(__has_feature) |
| 74 | # if __has_feature(memory_sanitizer) |
| 75 | // code that builds only under MemorySanitizer |
| 76 | # endif |
| 77 | #endif |
| 78 | |
Anna Zaks | 80de16f | 2016-10-27 21:38:44 +0000 | [diff] [blame] | 79 | ``__attribute__((no_sanitize("memory")))`` |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 80 | ----------------------------------------------- |
| 81 | |
Saleem Abdulrasool | 7f66d75 | 2015-10-19 01:24:08 +0000 | [diff] [blame] | 82 | Some code should not be checked by MemorySanitizer. One may use the function |
Anna Zaks | 80de16f | 2016-10-27 21:38:44 +0000 | [diff] [blame] | 83 | attribute ``no_sanitize("memory")`` to disable uninitialized checks in a |
| 84 | particular function. MemorySanitizer may still instrument such functions to |
| 85 | avoid false positives. This attribute may not be supported by other compilers, |
| 86 | so we suggest to use it together with ``__has_feature(memory_sanitizer)``. |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 87 | |
Alexey Samsonov | 2de6833 | 2013-08-07 08:23:32 +0000 | [diff] [blame] | 88 | Blacklist |
| 89 | --------- |
| 90 | |
| 91 | MemorySanitizer supports ``src`` and ``fun`` entity types in |
| 92 | :doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer |
| 93 | checks for certain source files and functions. All "Use of uninitialized value" |
| 94 | warnings will be suppressed and all values loaded from memory will be |
| 95 | considered fully initialized. |
| 96 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 97 | Report symbolization |
| 98 | ==================== |
| 99 | |
| 100 | MemorySanitizer uses an external symbolizer to print files and line numbers in |
| 101 | reports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``, |
| 102 | or set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it. |
| 103 | |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 104 | .. _msan-origins: |
| 105 | |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 106 | Origin Tracking |
| 107 | =============== |
| 108 | |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 109 | MemorySanitizer can track origins of uninitialized values, similar to |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 110 | Valgrind's --track-origins option. This feature is enabled by |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 111 | ``-fsanitize-memory-track-origins=2`` (or simply |
| 112 | ``-fsanitize-memory-track-origins``) Clang option. With the code from |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 113 | the example above, |
| 114 | |
| 115 | .. code-block:: console |
| 116 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 117 | % cat umr2.cc |
| 118 | #include <stdio.h> |
| 119 | |
| 120 | int main(int argc, char** argv) { |
| 121 | int* a = new int[10]; |
| 122 | a[5] = 0; |
| 123 | volatile int b = a[argc]; |
| 124 | if (b) |
| 125 | printf("xx\n"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | % clang -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2 umr2.cc |
| 130 | % ./a.out |
| 131 | WARNING: MemorySanitizer: use-of-uninitialized-value |
| 132 | #0 0x7f7893912f0b in main umr2.cc:7 |
| 133 | #1 0x7f789249b76c in __libc_start_main libc-start.c:226 |
| 134 | |
| 135 | Uninitialized value was stored to memory at |
| 136 | #0 0x7f78938b5c25 in __msan_chain_origin msan.cc:484 |
| 137 | #1 0x7f7893912ecd in main umr2.cc:6 |
| 138 | |
| 139 | Uninitialized value was created by a heap allocation |
| 140 | #0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44 |
| 141 | #1 0x7f7893912e06 in main umr2.cc:4 |
| 142 | |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 143 | By default, MemorySanitizer collects both allocation points and all |
| 144 | intermediate stores the uninitialized value went through. Origin |
| 145 | tracking has proved to be very useful for debugging MemorySanitizer |
| 146 | reports. It slows down program execution by a factor of 1.5x-2x on top |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 147 | of the usual MemorySanitizer slowdown and increases memory overhead. |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 148 | |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 149 | Clang option ``-fsanitize-memory-track-origins=1`` enables a slightly |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 150 | faster mode when MemorySanitizer collects only allocation points but |
| 151 | not intermediate stores. |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 152 | |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 153 | Use-after-destruction detection |
| 154 | =============================== |
| 155 | |
| 156 | You can enable experimental use-after-destruction detection in MemorySanitizer. |
| 157 | After invocation of the destructor, the object will be considered no longer |
| 158 | readable, and using underlying memory will lead to error reports in runtime. |
| 159 | |
| 160 | This feature is still experimental, in order to enable it at runtime you need |
| 161 | to: |
| 162 | |
| 163 | #. Pass addition Clang option ``-fsanitize-memory-use-after-dtor`` during |
| 164 | compilation. |
| 165 | #. Set environment variable `MSAN_OPTIONS=poison_in_dtor=1` before running |
| 166 | the program. |
| 167 | |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 168 | Handling external code |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 169 | ====================== |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 170 | |
| 171 | MemorySanitizer requires that all program code is instrumented. This |
| 172 | also includes any libraries that the program depends on, even libc. |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 173 | Failing to achieve this may result in false reports. |
Kostya Serebryany | 2494e8a | 2016-05-27 15:49:32 +0000 | [diff] [blame] | 174 | For the same reason you may need to replace all inline assembly code that writes to memory |
| 175 | with a pure C/C++ code. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 176 | |
| 177 | Full MemorySanitizer instrumentation is very difficult to achieve. To |
| 178 | make it easier, MemorySanitizer runtime library includes 70+ |
| 179 | interceptors for the most common libc functions. They make it possible |
| 180 | to run MemorySanitizer-instrumented programs linked with |
| 181 | uninstrumented libc. For example, the authors were able to bootstrap |
| 182 | MemorySanitizer-instrumented Clang compiler by linking it with |
Evgeniy Stepanov | 5e927b6 | 2015-01-26 09:17:37 +0000 | [diff] [blame] | 183 | self-built instrumented libc++ (as a replacement for libstdc++). |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 184 | |
| 185 | Supported Platforms |
| 186 | =================== |
| 187 | |
David Carlier | 59a339ab | 2018-07-25 13:55:06 +0000 | [diff] [blame] | 188 | MemorySanitizer is supported on the following OS: |
| 189 | |
| 190 | * Linux |
| 191 | * NetBSD |
| 192 | * FreeBSD |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 193 | |
| 194 | Limitations |
| 195 | =========== |
| 196 | |
| 197 | * MemorySanitizer uses 2x more real memory than a native run, 3x with |
| 198 | origin tracking. |
| 199 | * MemorySanitizer maps (but not reserves) 64 Terabytes of virtual |
| 200 | address space. This means that tools like ``ulimit`` may not work as |
| 201 | usually expected. |
| 202 | * Static linking is not supported. |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 203 | * Older versions of MSan (LLVM 3.7 and older) didn't work with |
| 204 | non-position-independent executables, and could fail on some Linux |
| 205 | kernel versions with disabled ASLR. Refer to documentation for older versions |
| 206 | for more details. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 207 | |
| 208 | Current Status |
| 209 | ============== |
| 210 | |
Alexey Samsonov | 1f7051e | 2015-12-04 22:50:44 +0000 | [diff] [blame] | 211 | MemorySanitizer is known to work on large real-world programs |
| 212 | (like Clang/LLVM itself) that can be recompiled from source, including all |
| 213 | dependent libraries. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 214 | |
| 215 | More Information |
| 216 | ================ |
| 217 | |
Alexey Samsonov | 2e2469d | 2015-12-04 00:38:13 +0000 | [diff] [blame] | 218 | `<https://github.com/google/sanitizers/wiki/MemorySanitizer>`_ |