2012-12-21 18:50:00 +08:00
|
|
|
================
|
|
|
|
MemorySanitizer
|
|
|
|
================
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:local:
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
============
|
|
|
|
|
|
|
|
MemorySanitizer is a detector of uninitialized reads. It consists of a
|
|
|
|
compiler instrumentation module and a run-time library.
|
|
|
|
|
|
|
|
Typical slowdown introduced by MemorySanitizer is **3x**.
|
|
|
|
|
|
|
|
How to build
|
|
|
|
============
|
|
|
|
|
2015-02-19 06:26:20 +08:00
|
|
|
Build LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
Usage
|
|
|
|
=====
|
|
|
|
|
|
|
|
Simply compile and link your program with ``-fsanitize=memory`` flag.
|
|
|
|
The MemorySanitizer run-time library should be linked to the final
|
|
|
|
executable, so make sure to use ``clang`` (not ``ld``) for the final
|
|
|
|
link step. When linking shared libraries, the MemorySanitizer run-time
|
|
|
|
is not linked, so ``-Wl,-z,defs`` may cause link errors (don't use it
|
|
|
|
with MemorySanitizer). To get a reasonable performance add ``-O1`` or
|
|
|
|
higher. To get meaninful stack traces in error messages add
|
|
|
|
``-fno-omit-frame-pointer``. To get perfect stack traces you may need
|
|
|
|
to disable inlining (just use ``-O1``) and tail call elimination
|
|
|
|
(``-fno-optimize-sibling-calls``).
|
|
|
|
|
|
|
|
.. code-block:: console
|
2012-12-24 02:36:44 +08:00
|
|
|
|
2012-12-21 18:50:00 +08:00
|
|
|
% cat umr.cc
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int* a = new int[10];
|
|
|
|
a[5] = 0;
|
|
|
|
if (a[argc])
|
|
|
|
printf("xx\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-09 12:35:11 +08:00
|
|
|
% clang -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cc
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
If a bug is detected, the program will print an error message to
|
2015-12-05 06:50:44 +08:00
|
|
|
stderr and exit with a non-zero exit code.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2014-03-20 22:58:36 +08:00
|
|
|
% ./a.out
|
|
|
|
WARNING: MemorySanitizer: use-of-uninitialized-value
|
2012-12-21 18:50:00 +08:00
|
|
|
#0 0x7f45944b418a in main umr.cc:6
|
|
|
|
#1 0x7f45938b676c in __libc_start_main libc-start.c:226
|
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
By default, MemorySanitizer exits on the first detected error. If you
|
|
|
|
find the error report hard to understand, try enabling
|
|
|
|
:ref:`origin tracking <msan-origins>`.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
``__has_feature(memory_sanitizer)``
|
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
In some cases one may need to execute different code depending on
|
|
|
|
whether MemorySanitizer is enabled. :ref:`\_\_has\_feature
|
|
|
|
<langext-__has_feature-__has_extension>` can be used for this purpose.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
#if defined(__has_feature)
|
|
|
|
# if __has_feature(memory_sanitizer)
|
|
|
|
// code that builds only under MemorySanitizer
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2013-02-26 14:58:27 +08:00
|
|
|
``__attribute__((no_sanitize_memory))``
|
|
|
|
-----------------------------------------------
|
|
|
|
|
2015-10-19 09:24:08 +08:00
|
|
|
Some code should not be checked by MemorySanitizer. One may use the function
|
|
|
|
attribute `no_sanitize_memory` to disable uninitialized checks in a particular
|
|
|
|
function. MemorySanitizer may still instrument such functions to avoid false
|
|
|
|
positives. This attribute may not be supported by other compilers, so we
|
|
|
|
suggest to use it together with ``__has_feature(memory_sanitizer)``.
|
2013-02-26 14:58:27 +08:00
|
|
|
|
2013-08-07 16:23:32 +08:00
|
|
|
Blacklist
|
|
|
|
---------
|
|
|
|
|
|
|
|
MemorySanitizer supports ``src`` and ``fun`` entity types in
|
|
|
|
:doc:`SanitizerSpecialCaseList`, that can be used to relax MemorySanitizer
|
|
|
|
checks for certain source files and functions. All "Use of uninitialized value"
|
|
|
|
warnings will be suppressed and all values loaded from memory will be
|
|
|
|
considered fully initialized.
|
|
|
|
|
2014-03-20 22:58:36 +08:00
|
|
|
Report symbolization
|
|
|
|
====================
|
|
|
|
|
|
|
|
MemorySanitizer uses an external symbolizer to print files and line numbers in
|
|
|
|
reports. Make sure that ``llvm-symbolizer`` binary is in ``PATH``,
|
|
|
|
or set environment variable ``MSAN_SYMBOLIZER_PATH`` to point to it.
|
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
.. _msan-origins:
|
|
|
|
|
2012-12-21 18:50:00 +08:00
|
|
|
Origin Tracking
|
|
|
|
===============
|
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
MemorySanitizer can track origins of uninitialized values, similar to
|
2012-12-21 18:50:00 +08:00
|
|
|
Valgrind's --track-origins option. This feature is enabled by
|
2015-02-26 23:59:30 +08:00
|
|
|
``-fsanitize-memory-track-origins=2`` (or simply
|
|
|
|
``-fsanitize-memory-track-origins``) Clang option. With the code from
|
2012-12-21 18:50:00 +08:00
|
|
|
the example above,
|
|
|
|
|
2014-03-20 22:58:36 +08:00
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
% cat umr2.cc
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int* a = new int[10];
|
|
|
|
a[5] = 0;
|
|
|
|
volatile int b = a[argc];
|
|
|
|
if (b)
|
|
|
|
printf("xx\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
% clang -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2 umr2.cc
|
|
|
|
% ./a.out
|
|
|
|
WARNING: MemorySanitizer: use-of-uninitialized-value
|
|
|
|
#0 0x7f7893912f0b in main umr2.cc:7
|
|
|
|
#1 0x7f789249b76c in __libc_start_main libc-start.c:226
|
|
|
|
|
|
|
|
Uninitialized value was stored to memory at
|
|
|
|
#0 0x7f78938b5c25 in __msan_chain_origin msan.cc:484
|
|
|
|
#1 0x7f7893912ecd in main umr2.cc:6
|
|
|
|
|
|
|
|
Uninitialized value was created by a heap allocation
|
|
|
|
#0 0x7f7893901cbd in operator new[](unsigned long) msan_new_delete.cc:44
|
|
|
|
#1 0x7f7893912e06 in main umr2.cc:4
|
|
|
|
|
2015-02-26 23:59:30 +08:00
|
|
|
By default, MemorySanitizer collects both allocation points and all
|
|
|
|
intermediate stores the uninitialized value went through. Origin
|
|
|
|
tracking has proved to be very useful for debugging MemorySanitizer
|
|
|
|
reports. It slows down program execution by a factor of 1.5x-2x on top
|
2015-12-05 06:50:44 +08:00
|
|
|
of the usual MemorySanitizer slowdown and increases memory overhead.
|
2015-02-26 23:59:30 +08:00
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
Clang option ``-fsanitize-memory-track-origins=1`` enables a slightly
|
2015-02-26 23:59:30 +08:00
|
|
|
faster mode when MemorySanitizer collects only allocation points but
|
|
|
|
not intermediate stores.
|
2014-03-20 22:58:36 +08:00
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
Use-after-destruction detection
|
|
|
|
===============================
|
|
|
|
|
|
|
|
You can enable experimental use-after-destruction detection in MemorySanitizer.
|
|
|
|
After invocation of the destructor, the object will be considered no longer
|
|
|
|
readable, and using underlying memory will lead to error reports in runtime.
|
|
|
|
|
|
|
|
This feature is still experimental, in order to enable it at runtime you need
|
|
|
|
to:
|
|
|
|
|
|
|
|
#. Pass addition Clang option ``-fsanitize-memory-use-after-dtor`` during
|
|
|
|
compilation.
|
|
|
|
#. Set environment variable `MSAN_OPTIONS=poison_in_dtor=1` before running
|
|
|
|
the program.
|
|
|
|
|
2012-12-21 18:50:00 +08:00
|
|
|
Handling external code
|
2015-12-05 06:50:44 +08:00
|
|
|
======================
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
MemorySanitizer requires that all program code is instrumented. This
|
|
|
|
also includes any libraries that the program depends on, even libc.
|
2014-03-20 22:58:36 +08:00
|
|
|
Failing to achieve this may result in false reports.
|
2016-05-27 23:49:32 +08:00
|
|
|
For the same reason you may need to replace all inline assembly code that writes to memory
|
|
|
|
with a pure C/C++ code.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
Full MemorySanitizer instrumentation is very difficult to achieve. To
|
|
|
|
make it easier, MemorySanitizer runtime library includes 70+
|
|
|
|
interceptors for the most common libc functions. They make it possible
|
|
|
|
to run MemorySanitizer-instrumented programs linked with
|
|
|
|
uninstrumented libc. For example, the authors were able to bootstrap
|
|
|
|
MemorySanitizer-instrumented Clang compiler by linking it with
|
2015-01-26 17:17:37 +08:00
|
|
|
self-built instrumented libc++ (as a replacement for libstdc++).
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
Supported Platforms
|
|
|
|
===================
|
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
MemorySanitizer is supported on Linux x86\_64/MIPS64/AArch64.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
Limitations
|
|
|
|
===========
|
|
|
|
|
|
|
|
* MemorySanitizer uses 2x more real memory than a native run, 3x with
|
|
|
|
origin tracking.
|
|
|
|
* MemorySanitizer maps (but not reserves) 64 Terabytes of virtual
|
|
|
|
address space. This means that tools like ``ulimit`` may not work as
|
|
|
|
usually expected.
|
|
|
|
* Static linking is not supported.
|
2015-12-05 06:50:44 +08:00
|
|
|
* Older versions of MSan (LLVM 3.7 and older) didn't work with
|
|
|
|
non-position-independent executables, and could fail on some Linux
|
|
|
|
kernel versions with disabled ASLR. Refer to documentation for older versions
|
|
|
|
for more details.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
Current Status
|
|
|
|
==============
|
|
|
|
|
2015-12-05 06:50:44 +08:00
|
|
|
MemorySanitizer is known to work on large real-world programs
|
|
|
|
(like Clang/LLVM itself) that can be recompiled from source, including all
|
|
|
|
dependent libraries.
|
2012-12-21 18:50:00 +08:00
|
|
|
|
|
|
|
More Information
|
|
|
|
================
|
|
|
|
|
2015-12-04 08:38:13 +08:00
|
|
|
`<https://github.com/google/sanitizers/wiki/MemorySanitizer>`_
|