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 |
| 30 | higher. To get meaninful stack traces in error messages add |
| 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 |
| 51 | stderr and exit with a non-zero exit code. Currently, MemorySanitizer |
| 52 | does not symbolize its output by default, so you may need to use a |
| 53 | separate script to symbolize the result offline (this will be fixed in |
| 54 | future). |
| 55 | |
| 56 | .. code-block:: console |
| 57 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 58 | % ./a.out |
| 59 | WARNING: MemorySanitizer: use-of-uninitialized-value |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 60 | #0 0x7f45944b418a in main umr.cc:6 |
| 61 | #1 0x7f45938b676c in __libc_start_main libc-start.c:226 |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 62 | |
| 63 | By default, MemorySanitizer exits on the first detected error. |
| 64 | |
| 65 | ``__has_feature(memory_sanitizer)`` |
| 66 | ------------------------------------ |
| 67 | |
| 68 | In some cases one may need to execute different code depending on |
| 69 | whether MemorySanitizer is enabled. :ref:`\_\_has\_feature |
| 70 | <langext-__has_feature-__has_extension>` can be used for this purpose. |
| 71 | |
| 72 | .. code-block:: c |
| 73 | |
| 74 | #if defined(__has_feature) |
| 75 | # if __has_feature(memory_sanitizer) |
| 76 | // code that builds only under MemorySanitizer |
| 77 | # endif |
| 78 | #endif |
| 79 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 80 | ``__attribute__((no_sanitize_memory))`` |
| 81 | ----------------------------------------------- |
| 82 | |
Saleem Abdulrasool | 7f66d75 | 2015-10-19 01:24:08 +0000 | [diff] [blame^] | 83 | Some code should not be checked by MemorySanitizer. One may use the function |
| 84 | attribute `no_sanitize_memory` to disable uninitialized checks in a particular |
| 85 | function. MemorySanitizer may still instrument such functions to avoid false |
| 86 | positives. This attribute may not be supported by other compilers, so we |
| 87 | suggest to use it together with ``__has_feature(memory_sanitizer)``. |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 88 | |
Alexey Samsonov | 2de6833 | 2013-08-07 08:23:32 +0000 | [diff] [blame] | 89 | Blacklist |
| 90 | --------- |
| 91 | |
| 92 | MemorySanitizer supports ``src`` and ``fun`` entity types in |
| 93 | :doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer |
| 94 | checks for certain source files and functions. All "Use of uninitialized value" |
| 95 | warnings will be suppressed and all values loaded from memory will be |
| 96 | considered fully initialized. |
| 97 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 98 | Report symbolization |
| 99 | ==================== |
| 100 | |
| 101 | MemorySanitizer uses an external symbolizer to print files and line numbers in |
| 102 | reports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``, |
| 103 | or set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it. |
| 104 | |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 105 | Origin Tracking |
| 106 | =============== |
| 107 | |
| 108 | MemorySanitizer can track origins of unitialized values, similar to |
| 109 | Valgrind's --track-origins option. This feature is enabled by |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 110 | ``-fsanitize-memory-track-origins=2`` (or simply |
| 111 | ``-fsanitize-memory-track-origins``) Clang option. With the code from |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 112 | the example above, |
| 113 | |
| 114 | .. code-block:: console |
| 115 | |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 116 | % cat umr2.cc |
| 117 | #include <stdio.h> |
| 118 | |
| 119 | int main(int argc, char** argv) { |
| 120 | int* a = new int[10]; |
| 121 | a[5] = 0; |
| 122 | volatile int b = a[argc]; |
| 123 | if (b) |
| 124 | printf("xx\n"); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | % clang -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2 umr2.cc |
| 129 | % ./a.out |
| 130 | WARNING: MemorySanitizer: use-of-uninitialized-value |
| 131 | #0 0x7f7893912f0b in main umr2.cc:7 |
| 132 | #1 0x7f789249b76c in __libc_start_main libc-start.c:226 |
| 133 | |
| 134 | Uninitialized value was stored to memory at |
| 135 | #0 0x7f78938b5c25 in __msan_chain_origin msan.cc:484 |
| 136 | #1 0x7f7893912ecd in main umr2.cc:6 |
| 137 | |
| 138 | Uninitialized value was created by a heap allocation |
| 139 | #0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44 |
| 140 | #1 0x7f7893912e06 in main umr2.cc:4 |
| 141 | |
Evgeniy Stepanov | 6e09bca | 2015-02-26 15:59:30 +0000 | [diff] [blame] | 142 | By default, MemorySanitizer collects both allocation points and all |
| 143 | intermediate stores the uninitialized value went through. Origin |
| 144 | tracking has proved to be very useful for debugging MemorySanitizer |
| 145 | reports. It slows down program execution by a factor of 1.5x-2x on top |
| 146 | of the usual MemorySanitizer slowdown. |
| 147 | |
| 148 | Clang option ``-fsanitize-memory-track-origins=1`` enabled a slightly |
| 149 | faster mode when MemorySanitizer collects only allocation points but |
| 150 | not intermediate stores. |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 151 | |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 152 | Handling external code |
| 153 | ============================ |
| 154 | |
| 155 | MemorySanitizer requires that all program code is instrumented. This |
| 156 | also includes any libraries that the program depends on, even libc. |
Evgeniy Stepanov | 2bfcaab | 2014-03-20 14:58:36 +0000 | [diff] [blame] | 157 | Failing to achieve this may result in false reports. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 158 | |
| 159 | Full MemorySanitizer instrumentation is very difficult to achieve. To |
| 160 | make it easier, MemorySanitizer runtime library includes 70+ |
| 161 | interceptors for the most common libc functions. They make it possible |
| 162 | to run MemorySanitizer-instrumented programs linked with |
| 163 | uninstrumented libc. For example, the authors were able to bootstrap |
| 164 | MemorySanitizer-instrumented Clang compiler by linking it with |
Evgeniy Stepanov | 5e927b6 | 2015-01-26 09:17:37 +0000 | [diff] [blame] | 165 | self-built instrumented libc++ (as a replacement for libstdc++). |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 166 | |
| 167 | Supported Platforms |
| 168 | =================== |
| 169 | |
| 170 | MemorySanitizer is supported on |
| 171 | |
Kostya Serebryany | 870baf8 | 2014-01-31 10:49:34 +0000 | [diff] [blame] | 172 | * Linux x86\_64 (tested on Ubuntu 12.04); |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 173 | |
| 174 | Limitations |
| 175 | =========== |
| 176 | |
| 177 | * MemorySanitizer uses 2x more real memory than a native run, 3x with |
| 178 | origin tracking. |
| 179 | * MemorySanitizer maps (but not reserves) 64 Terabytes of virtual |
| 180 | address space. This means that tools like ``ulimit`` may not work as |
| 181 | usually expected. |
| 182 | * Static linking is not supported. |
Peter Collingbourne | 54d770c | 2013-04-09 04:35:11 +0000 | [diff] [blame] | 183 | * Non-position-independent executables are not supported. Therefore, the |
| 184 | ``fsanitize=memory`` flag will cause Clang to act as though the ``-fPIE`` |
| 185 | flag had been supplied if compiling without ``-fPIC``, and as though the |
| 186 | ``-pie`` flag had been supplied if linking an executable. |
Evgeniy Stepanov | 17d5590 | 2012-12-21 10:50:00 +0000 | [diff] [blame] | 187 | * Depending on the version of Linux kernel, running without ASLR may |
| 188 | be not supported. Note that GDB disables ASLR by default. To debug |
| 189 | instrumented programs, use "set disable-randomization off". |
| 190 | |
| 191 | Current Status |
| 192 | ============== |
| 193 | |
| 194 | MemorySanitizer is an experimental tool. It is known to work on large |
| 195 | real-world programs, like Clang/LLVM itself. |
| 196 | |
| 197 | More Information |
| 198 | ================ |
| 199 | |
| 200 | `http://code.google.com/p/memory-sanitizer <http://code.google.com/p/memory-sanitizer/>`_ |
| 201 | |