1f4a2713aSLionel Sambuc================ 2f4a2713aSLionel SambucMemorySanitizer 3f4a2713aSLionel Sambuc================ 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc.. contents:: 6f4a2713aSLionel Sambuc :local: 7f4a2713aSLionel Sambuc 8f4a2713aSLionel SambucIntroduction 9f4a2713aSLionel Sambuc============ 10f4a2713aSLionel Sambuc 11f4a2713aSLionel SambucMemorySanitizer is a detector of uninitialized reads. It consists of a 12f4a2713aSLionel Sambuccompiler instrumentation module and a run-time library. 13f4a2713aSLionel Sambuc 14f4a2713aSLionel SambucTypical slowdown introduced by MemorySanitizer is **3x**. 15f4a2713aSLionel Sambuc 16f4a2713aSLionel SambucHow to build 17f4a2713aSLionel Sambuc============ 18f4a2713aSLionel Sambuc 19f4a2713aSLionel SambucFollow the `clang build instructions <../get_started.html>`_. CMake 20f4a2713aSLionel Sambucbuild is supported. 21f4a2713aSLionel Sambuc 22f4a2713aSLionel SambucUsage 23f4a2713aSLionel Sambuc===== 24f4a2713aSLionel Sambuc 25f4a2713aSLionel SambucSimply compile and link your program with ``-fsanitize=memory`` flag. 26f4a2713aSLionel SambucThe MemorySanitizer run-time library should be linked to the final 27f4a2713aSLionel Sambucexecutable, so make sure to use ``clang`` (not ``ld``) for the final 28f4a2713aSLionel Sambuclink step. When linking shared libraries, the MemorySanitizer run-time 29f4a2713aSLionel Sambucis not linked, so ``-Wl,-z,defs`` may cause link errors (don't use it 30f4a2713aSLionel Sambucwith MemorySanitizer). To get a reasonable performance add ``-O1`` or 31f4a2713aSLionel Sambuchigher. To get meaninful stack traces in error messages add 32f4a2713aSLionel Sambuc``-fno-omit-frame-pointer``. To get perfect stack traces you may need 33f4a2713aSLionel Sambucto disable inlining (just use ``-O1``) and tail call elimination 34f4a2713aSLionel Sambuc(``-fno-optimize-sibling-calls``). 35f4a2713aSLionel Sambuc 36f4a2713aSLionel Sambuc.. code-block:: console 37f4a2713aSLionel Sambuc 38f4a2713aSLionel Sambuc % cat umr.cc 39f4a2713aSLionel Sambuc #include <stdio.h> 40f4a2713aSLionel Sambuc 41f4a2713aSLionel Sambuc int main(int argc, char** argv) { 42f4a2713aSLionel Sambuc int* a = new int[10]; 43f4a2713aSLionel Sambuc a[5] = 0; 44f4a2713aSLionel Sambuc if (a[argc]) 45f4a2713aSLionel Sambuc printf("xx\n"); 46f4a2713aSLionel Sambuc return 0; 47f4a2713aSLionel Sambuc } 48f4a2713aSLionel Sambuc 49f4a2713aSLionel Sambuc % clang -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cc 50f4a2713aSLionel Sambuc 51f4a2713aSLionel SambucIf a bug is detected, the program will print an error message to 52f4a2713aSLionel Sambucstderr and exit with a non-zero exit code. Currently, MemorySanitizer 53f4a2713aSLionel Sambucdoes not symbolize its output by default, so you may need to use a 54f4a2713aSLionel Sambucseparate script to symbolize the result offline (this will be fixed in 55f4a2713aSLionel Sambucfuture). 56f4a2713aSLionel Sambuc 57f4a2713aSLionel Sambuc.. code-block:: console 58f4a2713aSLionel Sambuc 59*0a6a1f1dSLionel Sambuc % ./a.out 60*0a6a1f1dSLionel Sambuc WARNING: MemorySanitizer: use-of-uninitialized-value 61f4a2713aSLionel Sambuc #0 0x7f45944b418a in main umr.cc:6 62f4a2713aSLionel Sambuc #1 0x7f45938b676c in __libc_start_main libc-start.c:226 63f4a2713aSLionel Sambuc 64f4a2713aSLionel SambucBy default, MemorySanitizer exits on the first detected error. 65f4a2713aSLionel Sambuc 66f4a2713aSLionel Sambuc``__has_feature(memory_sanitizer)`` 67f4a2713aSLionel Sambuc------------------------------------ 68f4a2713aSLionel Sambuc 69f4a2713aSLionel SambucIn some cases one may need to execute different code depending on 70f4a2713aSLionel Sambucwhether MemorySanitizer is enabled. :ref:`\_\_has\_feature 71f4a2713aSLionel Sambuc<langext-__has_feature-__has_extension>` can be used for this purpose. 72f4a2713aSLionel Sambuc 73f4a2713aSLionel Sambuc.. code-block:: c 74f4a2713aSLionel Sambuc 75f4a2713aSLionel Sambuc #if defined(__has_feature) 76f4a2713aSLionel Sambuc # if __has_feature(memory_sanitizer) 77f4a2713aSLionel Sambuc // code that builds only under MemorySanitizer 78f4a2713aSLionel Sambuc # endif 79f4a2713aSLionel Sambuc #endif 80f4a2713aSLionel Sambuc 81f4a2713aSLionel Sambuc``__attribute__((no_sanitize_memory))`` 82f4a2713aSLionel Sambuc----------------------------------------------- 83f4a2713aSLionel Sambuc 84f4a2713aSLionel SambucSome code should not be checked by MemorySanitizer. 85f4a2713aSLionel SambucOne may use the function attribute 86f4a2713aSLionel Sambuc:ref:`no_sanitize_memory <langext-memory_sanitizer>` 87f4a2713aSLionel Sambucto disable uninitialized checks in a particular function. 88f4a2713aSLionel SambucMemorySanitizer may still instrument such functions to avoid false positives. 89f4a2713aSLionel SambucThis attribute may not be 90f4a2713aSLionel Sambucsupported by other compilers, so we suggest to use it together with 91f4a2713aSLionel Sambuc``__has_feature(memory_sanitizer)``. 92f4a2713aSLionel Sambuc 93f4a2713aSLionel SambucBlacklist 94f4a2713aSLionel Sambuc--------- 95f4a2713aSLionel Sambuc 96f4a2713aSLionel SambucMemorySanitizer supports ``src`` and ``fun`` entity types in 97f4a2713aSLionel Sambuc:doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer 98f4a2713aSLionel Sambucchecks for certain source files and functions. All "Use of uninitialized value" 99f4a2713aSLionel Sambucwarnings will be suppressed and all values loaded from memory will be 100f4a2713aSLionel Sambucconsidered fully initialized. 101f4a2713aSLionel Sambuc 102*0a6a1f1dSLionel SambucReport symbolization 103*0a6a1f1dSLionel Sambuc==================== 104*0a6a1f1dSLionel Sambuc 105*0a6a1f1dSLionel SambucMemorySanitizer uses an external symbolizer to print files and line numbers in 106*0a6a1f1dSLionel Sambucreports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``, 107*0a6a1f1dSLionel Sambucor set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it. 108*0a6a1f1dSLionel Sambuc 109f4a2713aSLionel SambucOrigin Tracking 110f4a2713aSLionel Sambuc=============== 111f4a2713aSLionel Sambuc 112f4a2713aSLionel SambucMemorySanitizer can track origins of unitialized values, similar to 113f4a2713aSLionel SambucValgrind's --track-origins option. This feature is enabled by 114f4a2713aSLionel Sambuc``-fsanitize-memory-track-origins`` Clang option. With the code from 115f4a2713aSLionel Sambucthe example above, 116f4a2713aSLionel Sambuc 117f4a2713aSLionel Sambuc.. code-block:: console 118f4a2713aSLionel Sambuc 119f4a2713aSLionel Sambuc % clang -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer -g -O2 umr.cc 120*0a6a1f1dSLionel Sambuc % ./a.out 121*0a6a1f1dSLionel Sambuc WARNING: MemorySanitizer: use-of-uninitialized-value 122*0a6a1f1dSLionel Sambuc #0 0x7f7893912f0b in main umr2.cc:6 123*0a6a1f1dSLionel Sambuc #1 0x7f789249b76c in __libc_start_main libc-start.c:226 124f4a2713aSLionel Sambuc 125*0a6a1f1dSLionel Sambuc Uninitialized value was created by a heap allocation 126*0a6a1f1dSLionel Sambuc #0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44 127*0a6a1f1dSLionel Sambuc #1 0x7f7893912e06 in main umr2.cc:4 128*0a6a1f1dSLionel Sambuc 129*0a6a1f1dSLionel SambucOrigin tracking has proved to be very useful for debugging MemorySanitizer 130f4a2713aSLionel Sambucreports. It slows down program execution by a factor of 1.5x-2x on top 131f4a2713aSLionel Sambucof the usual MemorySanitizer slowdown. 132f4a2713aSLionel Sambuc 133*0a6a1f1dSLionel SambucMemorySanitizer can provide even more information with 134*0a6a1f1dSLionel Sambuc``-fsanitize-memory-track-origins=2`` flag. In this mode reports 135*0a6a1f1dSLionel Sambucinclude information about intermediate stores the uninitialized value went 136*0a6a1f1dSLionel Sambucthrough. 137*0a6a1f1dSLionel Sambuc 138*0a6a1f1dSLionel Sambuc.. code-block:: console 139*0a6a1f1dSLionel Sambuc 140*0a6a1f1dSLionel Sambuc % cat umr2.cc 141*0a6a1f1dSLionel Sambuc #include <stdio.h> 142*0a6a1f1dSLionel Sambuc 143*0a6a1f1dSLionel Sambuc int main(int argc, char** argv) { 144*0a6a1f1dSLionel Sambuc int* a = new int[10]; 145*0a6a1f1dSLionel Sambuc a[5] = 0; 146*0a6a1f1dSLionel Sambuc volatile int b = a[argc]; 147*0a6a1f1dSLionel Sambuc if (b) 148*0a6a1f1dSLionel Sambuc printf("xx\n"); 149*0a6a1f1dSLionel Sambuc return 0; 150*0a6a1f1dSLionel Sambuc } 151*0a6a1f1dSLionel Sambuc 152*0a6a1f1dSLionel Sambuc % clang -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2 umr2.cc 153*0a6a1f1dSLionel Sambuc % ./a.out 154*0a6a1f1dSLionel Sambuc WARNING: MemorySanitizer: use-of-uninitialized-value 155*0a6a1f1dSLionel Sambuc #0 0x7f7893912f0b in main umr2.cc:7 156*0a6a1f1dSLionel Sambuc #1 0x7f789249b76c in __libc_start_main libc-start.c:226 157*0a6a1f1dSLionel Sambuc 158*0a6a1f1dSLionel Sambuc Uninitialized value was stored to memory at 159*0a6a1f1dSLionel Sambuc #0 0x7f78938b5c25 in __msan_chain_origin msan.cc:484 160*0a6a1f1dSLionel Sambuc #1 0x7f7893912ecd in main umr2.cc:6 161*0a6a1f1dSLionel Sambuc 162*0a6a1f1dSLionel Sambuc Uninitialized value was created by a heap allocation 163*0a6a1f1dSLionel Sambuc #0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44 164*0a6a1f1dSLionel Sambuc #1 0x7f7893912e06 in main umr2.cc:4 165*0a6a1f1dSLionel Sambuc 166*0a6a1f1dSLionel Sambuc 167f4a2713aSLionel SambucHandling external code 168f4a2713aSLionel Sambuc============================ 169f4a2713aSLionel Sambuc 170f4a2713aSLionel SambucMemorySanitizer requires that all program code is instrumented. This 171f4a2713aSLionel Sambucalso includes any libraries that the program depends on, even libc. 172*0a6a1f1dSLionel SambucFailing to achieve this may result in false reports. 173f4a2713aSLionel Sambuc 174f4a2713aSLionel SambucFull MemorySanitizer instrumentation is very difficult to achieve. To 175f4a2713aSLionel Sambucmake it easier, MemorySanitizer runtime library includes 70+ 176f4a2713aSLionel Sambucinterceptors for the most common libc functions. They make it possible 177f4a2713aSLionel Sambucto run MemorySanitizer-instrumented programs linked with 178f4a2713aSLionel Sambucuninstrumented libc. For example, the authors were able to bootstrap 179f4a2713aSLionel SambucMemorySanitizer-instrumented Clang compiler by linking it with 180f4a2713aSLionel Sambucself-built instrumented libcxx (as a replacement for libstdc++). 181f4a2713aSLionel Sambuc 182f4a2713aSLionel SambucIn the case when rebuilding all program dependencies with 183f4a2713aSLionel SambucMemorySanitizer is problematic, an experimental MSanDR tool can be 184f4a2713aSLionel Sambucused. It is a DynamoRio-based tool that uses dynamic instrumentation 185f4a2713aSLionel Sambucto avoid false positives due to uninstrumented code. The tool simply 186f4a2713aSLionel Sambucmarks memory from instrumented libraries as fully initialized. See 187f4a2713aSLionel Sambuc`http://code.google.com/p/memory-sanitizer/wiki/Running#Running_with_the_dynamic_tool` 188f4a2713aSLionel Sambucfor more information. 189f4a2713aSLionel Sambuc 190f4a2713aSLionel SambucSupported Platforms 191f4a2713aSLionel Sambuc=================== 192f4a2713aSLionel Sambuc 193f4a2713aSLionel SambucMemorySanitizer is supported on 194f4a2713aSLionel Sambuc 195*0a6a1f1dSLionel Sambuc* Linux x86\_64 (tested on Ubuntu 12.04); 196f4a2713aSLionel Sambuc 197f4a2713aSLionel SambucLimitations 198f4a2713aSLionel Sambuc=========== 199f4a2713aSLionel Sambuc 200f4a2713aSLionel Sambuc* MemorySanitizer uses 2x more real memory than a native run, 3x with 201f4a2713aSLionel Sambuc origin tracking. 202f4a2713aSLionel Sambuc* MemorySanitizer maps (but not reserves) 64 Terabytes of virtual 203f4a2713aSLionel Sambuc address space. This means that tools like ``ulimit`` may not work as 204f4a2713aSLionel Sambuc usually expected. 205f4a2713aSLionel Sambuc* Static linking is not supported. 206f4a2713aSLionel Sambuc* Non-position-independent executables are not supported. Therefore, the 207f4a2713aSLionel Sambuc ``fsanitize=memory`` flag will cause Clang to act as though the ``-fPIE`` 208f4a2713aSLionel Sambuc flag had been supplied if compiling without ``-fPIC``, and as though the 209f4a2713aSLionel Sambuc ``-pie`` flag had been supplied if linking an executable. 210f4a2713aSLionel Sambuc* Depending on the version of Linux kernel, running without ASLR may 211f4a2713aSLionel Sambuc be not supported. Note that GDB disables ASLR by default. To debug 212f4a2713aSLionel Sambuc instrumented programs, use "set disable-randomization off". 213f4a2713aSLionel Sambuc 214f4a2713aSLionel SambucCurrent Status 215f4a2713aSLionel Sambuc============== 216f4a2713aSLionel Sambuc 217f4a2713aSLionel SambucMemorySanitizer is an experimental tool. It is known to work on large 218f4a2713aSLionel Sambucreal-world programs, like Clang/LLVM itself. 219f4a2713aSLionel Sambuc 220f4a2713aSLionel SambucMore Information 221f4a2713aSLionel Sambuc================ 222f4a2713aSLionel Sambuc 223f4a2713aSLionel Sambuc`http://code.google.com/p/memory-sanitizer <http://code.google.com/p/memory-sanitizer/>`_ 224f4a2713aSLionel Sambuc 225