blob: f97355fd8c3c8dcebaf08305b6d351e3c649cb8b [file] [log] [blame]
Evgeniy Stepanov17d55902012-12-21 10:50:00 +00001================
2MemorySanitizer
3================
4
5.. contents::
6 :local:
7
8Introduction
9============
10
11MemorySanitizer is a detector of uninitialized reads. It consists of a
12compiler instrumentation module and a run-time library.
13
14Typical slowdown introduced by MemorySanitizer is **3x**.
15
16How to build
17============
18
Alexey Samsonov3a433f62015-02-18 22:26:20 +000019Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_.
Evgeniy Stepanov17d55902012-12-21 10:50:00 +000020
21Usage
22=====
23
24Simply compile and link your program with ``-fsanitize=memory`` flag.
25The MemorySanitizer run-time library should be linked to the final
26executable, so make sure to use ``clang`` (not ``ld``) for the final
27link step. When linking shared libraries, the MemorySanitizer run-time
28is not linked, so ``-Wl,-z,defs`` may cause link errors (don't use it
29with MemorySanitizer). To get a reasonable performance add ``-O1`` or
30higher. To get meaninful stack traces in error messages add
31``-fno-omit-frame-pointer``. To get perfect stack traces you may need
32to disable inlining (just use ``-O1``) and tail call elimination
33(``-fno-optimize-sibling-calls``).
34
35.. code-block:: console
Dmitri Gribenko46735cb2012-12-23 18:36:44 +000036
Evgeniy Stepanov17d55902012-12-21 10:50:00 +000037 % 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 Collingbourne54d770c2013-04-09 04:35:11 +000048 % clang -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cc
Evgeniy Stepanov17d55902012-12-21 10:50:00 +000049
50If a bug is detected, the program will print an error message to
51stderr and exit with a non-zero exit code. Currently, MemorySanitizer
52does not symbolize its output by default, so you may need to use a
53separate script to symbolize the result offline (this will be fixed in
54future).
55
56.. code-block:: console
57
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000058 % ./a.out
59 WARNING: MemorySanitizer: use-of-uninitialized-value
Evgeniy Stepanov17d55902012-12-21 10:50:00 +000060 #0 0x7f45944b418a in main umr.cc:6
61 #1 0x7f45938b676c in __libc_start_main libc-start.c:226
Evgeniy Stepanov17d55902012-12-21 10:50:00 +000062
63By default, MemorySanitizer exits on the first detected error.
64
65``__has_feature(memory_sanitizer)``
66------------------------------------
67
68In some cases one may need to execute different code depending on
69whether 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 Serebryany4c0fc992013-02-26 06:58:27 +000080``__attribute__((no_sanitize_memory))``
81-----------------------------------------------
82
Saleem Abdulrasool7f66d752015-10-19 01:24:08 +000083Some code should not be checked by MemorySanitizer. One may use the function
84attribute `no_sanitize_memory` to disable uninitialized checks in a particular
85function. MemorySanitizer may still instrument such functions to avoid false
86positives. This attribute may not be supported by other compilers, so we
87suggest to use it together with ``__has_feature(memory_sanitizer)``.
Kostya Serebryany4c0fc992013-02-26 06:58:27 +000088
Alexey Samsonov2de68332013-08-07 08:23:32 +000089Blacklist
90---------
91
92MemorySanitizer supports ``src`` and ``fun`` entity types in
93:doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer
94checks for certain source files and functions. All "Use of uninitialized value"
95warnings will be suppressed and all values loaded from memory will be
96considered fully initialized.
97
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +000098Report symbolization
99====================
100
101MemorySanitizer uses an external symbolizer to print files and line numbers in
102reports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``,
103or set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it.
104
Evgeniy Stepanov17d55902012-12-21 10:50:00 +0000105Origin Tracking
106===============
107
108MemorySanitizer can track origins of unitialized values, similar to
109Valgrind's --track-origins option. This feature is enabled by
Evgeniy Stepanov6e09bca2015-02-26 15:59:30 +0000110``-fsanitize-memory-track-origins=2`` (or simply
111``-fsanitize-memory-track-origins``) Clang option. With the code from
Evgeniy Stepanov17d55902012-12-21 10:50:00 +0000112the example above,
113
114.. code-block:: console
115
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000116 % 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 Stepanov6e09bca2015-02-26 15:59:30 +0000142By default, MemorySanitizer collects both allocation points and all
143intermediate stores the uninitialized value went through. Origin
144tracking has proved to be very useful for debugging MemorySanitizer
145reports. It slows down program execution by a factor of 1.5x-2x on top
146of the usual MemorySanitizer slowdown.
147
148Clang option ``-fsanitize-memory-track-origins=1`` enabled a slightly
149faster mode when MemorySanitizer collects only allocation points but
150not intermediate stores.
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000151
Evgeniy Stepanov17d55902012-12-21 10:50:00 +0000152Handling external code
153============================
154
155MemorySanitizer requires that all program code is instrumented. This
156also includes any libraries that the program depends on, even libc.
Evgeniy Stepanov2bfcaab2014-03-20 14:58:36 +0000157Failing to achieve this may result in false reports.
Evgeniy Stepanov17d55902012-12-21 10:50:00 +0000158
159Full MemorySanitizer instrumentation is very difficult to achieve. To
160make it easier, MemorySanitizer runtime library includes 70+
161interceptors for the most common libc functions. They make it possible
162to run MemorySanitizer-instrumented programs linked with
163uninstrumented libc. For example, the authors were able to bootstrap
164MemorySanitizer-instrumented Clang compiler by linking it with
Evgeniy Stepanov5e927b62015-01-26 09:17:37 +0000165self-built instrumented libc++ (as a replacement for libstdc++).
Evgeniy Stepanov17d55902012-12-21 10:50:00 +0000166
167Supported Platforms
168===================
169
170MemorySanitizer is supported on
171
Kostya Serebryany870baf82014-01-31 10:49:34 +0000172* Linux x86\_64 (tested on Ubuntu 12.04);
Evgeniy Stepanov17d55902012-12-21 10:50:00 +0000173
174Limitations
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 Collingbourne54d770c2013-04-09 04:35:11 +0000183* 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 Stepanov17d55902012-12-21 10:50:00 +0000187* 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
191Current Status
192==============
193
194MemorySanitizer is an experimental tool. It is known to work on large
195real-world programs, like Clang/LLVM itself.
196
197More Information
198================
199
200`http://code.google.com/p/memory-sanitizer <http://code.google.com/p/memory-sanitizer/>`_
201