blob: 007e0866dec6ff404ff81e4c9362e408e56d1132 [file] [log] [blame]
Evgeniy Stepanovcc603e92012-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
Stephen Hines0e2c34f2015-03-23 12:09:02 -070019Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_.
Evgeniy Stepanovcc603e92012-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 Gribenko184e1c42012-12-23 18:36:44 +000036
Evgeniy Stepanovcc603e92012-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 Collingbourne52ca70d2013-04-09 04:35:11 +000048 % clang -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cc
Evgeniy Stepanovcc603e92012-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
Stephen Hines651f13c2014-04-23 16:59:28 -070058 % ./a.out
59 WARNING: MemorySanitizer: use-of-uninitialized-value
Evgeniy Stepanovcc603e92012-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 Stepanovcc603e92012-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 Serebryany85aee962013-02-26 06:58:27 +000080``__attribute__((no_sanitize_memory))``
81-----------------------------------------------
82
83Some code should not be checked by MemorySanitizer.
84One may use the function attribute
85:ref:`no_sanitize_memory <langext-memory_sanitizer>`
86to disable uninitialized checks in a particular function.
87MemorySanitizer may still instrument such functions to avoid false positives.
88This attribute may not be
89supported by other compilers, so we suggest to use it together with
Evgeniy Stepanovfa203cf2013-08-15 13:57:11 +000090``__has_feature(memory_sanitizer)``.
Kostya Serebryany85aee962013-02-26 06:58:27 +000091
Alexey Samsonov05654ff2013-08-07 08:23:32 +000092Blacklist
93---------
94
95MemorySanitizer supports ``src`` and ``fun`` entity types in
96:doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer
97checks for certain source files and functions. All "Use of uninitialized value"
98warnings will be suppressed and all values loaded from memory will be
99considered fully initialized.
100
Stephen Hines651f13c2014-04-23 16:59:28 -0700101Report symbolization
102====================
103
104MemorySanitizer uses an external symbolizer to print files and line numbers in
105reports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``,
106or set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it.
107
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000108Origin Tracking
109===============
110
111MemorySanitizer can track origins of unitialized values, similar to
112Valgrind's --track-origins option. This feature is enabled by
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700113``-fsanitize-memory-track-origins=2`` (or simply
114``-fsanitize-memory-track-origins``) Clang option. With the code from
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000115the example above,
116
117.. code-block:: console
118
Stephen Hines651f13c2014-04-23 16:59:28 -0700119 % cat umr2.cc
120 #include <stdio.h>
121
122 int main(int argc, char** argv) {
123 int* a = new int[10];
124 a[5] = 0;
125 volatile int b = a[argc];
126 if (b)
127 printf("xx\n");
128 return 0;
129 }
130
131 % clang -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2 umr2.cc
132 % ./a.out
133 WARNING: MemorySanitizer: use-of-uninitialized-value
134 #0 0x7f7893912f0b in main umr2.cc:7
135 #1 0x7f789249b76c in __libc_start_main libc-start.c:226
136
137 Uninitialized value was stored to memory at
138 #0 0x7f78938b5c25 in __msan_chain_origin msan.cc:484
139 #1 0x7f7893912ecd in main umr2.cc:6
140
141 Uninitialized value was created by a heap allocation
142 #0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44
143 #1 0x7f7893912e06 in main umr2.cc:4
144
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700145By default, MemorySanitizer collects both allocation points and all
146intermediate stores the uninitialized value went through. Origin
147tracking has proved to be very useful for debugging MemorySanitizer
148reports. It slows down program execution by a factor of 1.5x-2x on top
149of the usual MemorySanitizer slowdown.
150
151Clang option ``-fsanitize-memory-track-origins=1`` enabled a slightly
152faster mode when MemorySanitizer collects only allocation points but
153not intermediate stores.
Stephen Hines651f13c2014-04-23 16:59:28 -0700154
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000155Handling external code
156============================
157
158MemorySanitizer requires that all program code is instrumented. This
159also includes any libraries that the program depends on, even libc.
Stephen Hines651f13c2014-04-23 16:59:28 -0700160Failing to achieve this may result in false reports.
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000161
162Full MemorySanitizer instrumentation is very difficult to achieve. To
163make it easier, MemorySanitizer runtime library includes 70+
164interceptors for the most common libc functions. They make it possible
165to run MemorySanitizer-instrumented programs linked with
166uninstrumented libc. For example, the authors were able to bootstrap
167MemorySanitizer-instrumented Clang compiler by linking it with
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700168self-built instrumented libc++ (as a replacement for libstdc++).
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000169
170Supported Platforms
171===================
172
173MemorySanitizer is supported on
174
Stephen Hines651f13c2014-04-23 16:59:28 -0700175* Linux x86\_64 (tested on Ubuntu 12.04);
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000176
177Limitations
178===========
179
180* MemorySanitizer uses 2x more real memory than a native run, 3x with
181 origin tracking.
182* MemorySanitizer maps (but not reserves) 64 Terabytes of virtual
183 address space. This means that tools like ``ulimit`` may not work as
184 usually expected.
185* Static linking is not supported.
Peter Collingbourne52ca70d2013-04-09 04:35:11 +0000186* Non-position-independent executables are not supported. Therefore, the
187 ``fsanitize=memory`` flag will cause Clang to act as though the ``-fPIE``
188 flag had been supplied if compiling without ``-fPIC``, and as though the
189 ``-pie`` flag had been supplied if linking an executable.
Evgeniy Stepanovcc603e92012-12-21 10:50:00 +0000190* Depending on the version of Linux kernel, running without ASLR may
191 be not supported. Note that GDB disables ASLR by default. To debug
192 instrumented programs, use "set disable-randomization off".
193
194Current Status
195==============
196
197MemorySanitizer is an experimental tool. It is known to work on large
198real-world programs, like Clang/LLVM itself.
199
200More Information
201================
202
203`http://code.google.com/p/memory-sanitizer <http://code.google.com/p/memory-sanitizer/>`_
204