blob: 357ebd9d93e05a4a08c448ae026e3d9bbead3c6b [file] [log] [blame]
Sean Silva93ca0212012-12-13 01:10:46 +00001================
2AddressSanitizer
3================
4
5.. contents::
6 :local:
7
8Introduction
9============
10
Dmitri Gribenko97555a12012-12-15 21:10:51 +000011AddressSanitizer is a fast memory error detector. It consists of a compiler
12instrumentation module and a run-time library. The tool can detect the
13following types of bugs:
Sean Silva93ca0212012-12-13 01:10:46 +000014
Dmitri Gribenko97555a12012-12-15 21:10:51 +000015* Out-of-bounds accesses to heap, stack and globals
16* Use-after-free
17* Use-after-return (to some extent)
18* Double-free, invalid free
Sean Silva93ca0212012-12-13 01:10:46 +000019
20Typical slowdown introduced by AddressSanitizer is **2x**.
21
22How to build
23============
24
Dmitri Gribenko97555a12012-12-15 21:10:51 +000025Follow the `clang build instructions <../get_started.html>`_. CMake build is
26supported.
Sean Silva93ca0212012-12-13 01:10:46 +000027
28Usage
29=====
30
Dmitri Gribenko97555a12012-12-15 21:10:51 +000031Simply compile and link your program with ``-fsanitize=address`` flag. The
32AddressSanitizer run-time library should be linked to the final executable, so
33make sure to use ``clang`` (not ``ld``) for the final link step. When linking
34shared libraries, the AddressSanitizer run-time is not linked, so
35``-Wl,-z,defs`` may cause link errors (don't use it with AddressSanitizer). To
36get a reasonable performance add ``-O1`` or higher. To get nicer stack traces
37in error messages add ``-fno-omit-frame-pointer``. To get perfect stack traces
38you may need to disable inlining (just use ``-O1``) and tail call elimination
39(``-fno-optimize-sibling-calls``).
Sean Silva93ca0212012-12-13 01:10:46 +000040
Dmitri Gribenko97555a12012-12-15 21:10:51 +000041.. code-block:: console
Sean Silva93ca0212012-12-13 01:10:46 +000042
43 % cat example_UseAfterFree.cc
44 int main(int argc, char **argv) {
45 int *array = new int[100];
46 delete [] array;
47 return array[argc]; // BOOM
48 }
49
Sean Silva93ca0212012-12-13 01:10:46 +000050 # Compile and link
51 % clang -O1 -g -fsanitize=address -fno-omit-frame-pointer example_UseAfterFree.cc
52
Dmitri Gribenko97555a12012-12-15 21:10:51 +000053or:
Sean Silva93ca0212012-12-13 01:10:46 +000054
Dmitri Gribenko97555a12012-12-15 21:10:51 +000055.. code-block:: console
Sean Silva93ca0212012-12-13 01:10:46 +000056
57 # Compile
58 % clang -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc
59 # Link
60 % clang -g -fsanitize=address example_UseAfterFree.o
61
Dmitri Gribenko97555a12012-12-15 21:10:51 +000062If a bug is detected, the program will print an error message to stderr and
63exit with a non-zero exit code. Currently, AddressSanitizer does not symbolize
64its output, so you may need to use a separate script to symbolize the result
65offline (this will be fixed in future).
Sean Silva93ca0212012-12-13 01:10:46 +000066
Dmitri Gribenko97555a12012-12-15 21:10:51 +000067.. code-block:: console
Sean Silva93ca0212012-12-13 01:10:46 +000068
69 % ./a.out 2> log
70 % projects/compiler-rt/lib/asan/scripts/asan_symbolize.py / < log | c++filt
71 ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8
72 READ of size 4 at 0x7f7ddab8c084 thread T0
73 #0 0x403c8c in main example_UseAfterFree.cc:4
74 #1 0x7f7ddabcac4d in __libc_start_main ??:0
75 0x7f7ddab8c084 is located 4 bytes inside of 400-byte region [0x7f7ddab8c080,0x7f7ddab8c210)
76 freed by thread T0 here:
77 #0 0x404704 in operator delete[](void*) ??:0
78 #1 0x403c53 in main example_UseAfterFree.cc:4
79 #2 0x7f7ddabcac4d in __libc_start_main ??:0
80 previously allocated by thread T0 here:
81 #0 0x404544 in operator new[](unsigned long) ??:0
82 #1 0x403c43 in main example_UseAfterFree.cc:2
83 #2 0x7f7ddabcac4d in __libc_start_main ??:0
84 ==9442== ABORTING
85
86AddressSanitizer exits on the first detected error. This is by design.
87One reason: it makes the generated code smaller and faster (both by
88~5%). Another reason: this makes fixing bugs unavoidable. With Valgrind,
89it is often the case that users treat Valgrind warnings as false
90positives (which they are not) and don't fix them.
91
Dmitri Gribenko97555a12012-12-15 21:10:51 +000092``__has_feature(address_sanitizer)``
Sean Silva93ca0212012-12-13 01:10:46 +000093------------------------------------
94
Dmitri Gribenko97555a12012-12-15 21:10:51 +000095In some cases one may need to execute different code depending on whether
96AddressSanitizer is enabled.
97:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
98this purpose.
Sean Silva93ca0212012-12-13 01:10:46 +000099
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000100.. code-block:: c
Sean Silva93ca0212012-12-13 01:10:46 +0000101
102 #if defined(__has_feature)
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000103 # if __has_feature(address_sanitizer)
104 // code that builds only under AddressSanitizer
105 # endif
Sean Silva93ca0212012-12-13 01:10:46 +0000106 #endif
107
108``__attribute__((no_address_safety_analysis))``
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000109-----------------------------------------------
Sean Silva93ca0212012-12-13 01:10:46 +0000110
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000111Some code should not be instrumented by AddressSanitizer. One may use the
112function attribute
113:ref:`no_address_safety_analysis <langext-address_sanitizer>`
114to disable instrumentation of a particular function. This attribute may not be
115supported by other compilers, so we suggest to use it together with
116``__has_feature(address_sanitizer)``. Note: currently, this attribute will be
117lost if the function is inlined.
Sean Silva93ca0212012-12-13 01:10:46 +0000118
119Supported Platforms
120===================
121
122AddressSanitizer is supported on
123
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000124* Linux i386/x86\_64 (tested on Ubuntu 10.04 and 12.04);
125* MacOS 10.6, 10.7 and 10.8 (i386/x86\_64).
Sean Silva93ca0212012-12-13 01:10:46 +0000126
127Support for Linux ARM (and Android ARM) is in progress (it may work, but
128is not guaranteed too).
129
130Limitations
131===========
132
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000133* AddressSanitizer uses more real memory than a native run. Exact overhead
134 depends on the allocations sizes. The smaller the allocations you make the
135 bigger the overhead is.
136* AddressSanitizer uses more stack memory. We have seen up to 3x increase.
137* On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of
138 virtual address space. This means that tools like ``ulimit`` may not work as
139 usually expected.
140* Static linking is not supported.
Sean Silva93ca0212012-12-13 01:10:46 +0000141
142Current Status
143==============
144
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000145AddressSanitizer is fully functional on supported platforms starting from LLVM
1463.1. The test suite is integrated into CMake build and can be run with ``make
147check-asan`` command.
Sean Silva93ca0212012-12-13 01:10:46 +0000148
149More Information
150================
151
Dmitri Gribenko97555a12012-12-15 21:10:51 +0000152`http://code.google.com/p/address-sanitizer <http://code.google.com/p/address-sanitizer/>`_
153