11 AddressSanitizer is a fast memory error detector. It consists of a compiler
12 instrumentation module and a run-time library. The tool can detect the
13 following types of bugs:
15 * Out-of-bounds accesses to heap, stack and globals
17 * Use-after-return (clang flag ``-fsanitize-address-use-after-return=(never|runtime|always)`` default: ``runtime``)
18 * Enable with: ``ASAN_OPTIONS=detect_stack_use_after_return=1`` (already enabled on Linux).
19 * Disable with: ``ASAN_OPTIONS=detect_stack_use_after_return=0``.
20 * Use-after-scope (clang flag ``-fsanitize-address-use-after-scope``)
21 * Double-free, invalid free
22 * Memory leaks (experimental)
24 Typical slowdown introduced by AddressSanitizer is **2x**.
29 Build LLVM/Clang with `CMake <https://llvm.org/docs/CMake.html>`_.
34 Simply compile and link your program with ``-fsanitize=address`` flag. The
35 AddressSanitizer run-time library should be linked to the final executable, so
36 make sure to use ``clang`` (not ``ld``) for the final link step. When linking
37 shared libraries, the AddressSanitizer run-time is not linked, so
38 ``-Wl,-z,defs`` may cause link errors (don't use it with AddressSanitizer). To
39 get a reasonable performance add ``-O1`` or higher. To get nicer stack traces
40 in error messages add ``-fno-omit-frame-pointer``. To get perfect stack traces
41 you may need to disable inlining (just use ``-O1``) and tail call elimination
42 (``-fno-optimize-sibling-calls``).
44 .. code-block:: console
46 % cat example_UseAfterFree.cc
47 int main(int argc, char **argv) {
48 int *array = new int[100];
50 return array[argc]; // BOOM
54 % clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer example_UseAfterFree.cc
58 .. code-block:: console
61 % clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc
63 % clang++ -g -fsanitize=address example_UseAfterFree.o
65 If a bug is detected, the program will print an error message to stderr and
66 exit with a non-zero exit code. AddressSanitizer exits on the first detected error.
69 * This approach allows AddressSanitizer to produce faster and smaller generated code
71 * Fixing bugs becomes unavoidable. AddressSanitizer does not produce
72 false alarms. Once a memory corruption occurs, the program is in an inconsistent
73 state, which could lead to confusing results and potentially misleading
76 If your process is sandboxed and you are running on OS X 10.10 or earlier, you
77 will need to set ``DYLD_INSERT_LIBRARIES`` environment variable and point it to
78 the ASan library that is packaged with the compiler used to build the
79 executable. (You can find the library by searching for dynamic libraries with
80 ``asan`` in their name.) If the environment variable is not set, the process will
81 try to re-exec. Also keep in mind that when moving the executable to another machine,
82 the ASan library will also need to be copied over.
84 Symbolizing the Reports
85 =========================
87 To make AddressSanitizer symbolize its output
88 you need to set the ``ASAN_SYMBOLIZER_PATH`` environment variable to point to
89 the ``llvm-symbolizer`` binary (or make sure ``llvm-symbolizer`` is in your
92 .. code-block:: console
94 % ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out
95 ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8
96 READ of size 4 at 0x7f7ddab8c084 thread T0
97 #0 0x403c8c in main example_UseAfterFree.cc:4
98 #1 0x7f7ddabcac4d in __libc_start_main ??:0
99 0x7f7ddab8c084 is located 4 bytes inside of 400-byte region [0x7f7ddab8c080,0x7f7ddab8c210)
100 freed by thread T0 here:
101 #0 0x404704 in operator delete[](void*) ??:0
102 #1 0x403c53 in main example_UseAfterFree.cc:4
103 #2 0x7f7ddabcac4d in __libc_start_main ??:0
104 previously allocated by thread T0 here:
105 #0 0x404544 in operator new[](unsigned long) ??:0
106 #1 0x403c43 in main example_UseAfterFree.cc:2
107 #2 0x7f7ddabcac4d in __libc_start_main ??:0
110 If that does not work for you (e.g. your process is sandboxed), you can use a
111 separate script to symbolize the result offline (online symbolization can be
112 force disabled by setting ``ASAN_OPTIONS=symbolize=0``):
114 .. code-block:: console
116 % ASAN_OPTIONS=symbolize=0 ./a.out 2> log
117 % projects/compiler-rt/lib/asan/scripts/asan_symbolize.py / < log | c++filt
118 ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8
119 READ of size 4 at 0x7f7ddab8c084 thread T0
120 #0 0x403c8c in main example_UseAfterFree.cc:4
121 #1 0x7f7ddabcac4d in __libc_start_main ??:0
124 Note that on macOS you may need to run ``dsymutil`` on your binary to have the
125 file\:line info in the AddressSanitizer reports.
130 Initialization order checking
131 -----------------------------
133 AddressSanitizer can optionally detect dynamic initialization order problems,
134 when initialization of globals defined in one translation unit uses
135 globals defined in another translation unit. To enable this check at runtime,
136 you should set environment variable
137 ``ASAN_OPTIONS=check_initialization_order=1``.
139 Note that this option is not supported on macOS.
141 Stack Use After Return (UAR)
142 ----------------------------
144 AddressSanitizer can optionally detect stack use after return problems.
145 This is available by default, or explicitly
146 (``-fsanitize-address-use-after-return=runtime``).
147 To disable this check at runtime, set the environment variable
148 ``ASAN_OPTIONS=detect_stack_use_after_return=0``.
150 Enabling this check (``-fsanitize-address-use-after-return=always``) will
151 reduce code size. The code size may be reduced further by completely
152 eliminating this check (``-fsanitize-address-use-after-return=never``).
154 To summarize: ``-fsanitize-address-use-after-return=<mode>``
155 * ``never``: Completely disables detection of UAR errors (reduces code size).
156 * ``runtime``: Adds the code for detection, but it can be disable via the
157 runtime environment (``ASAN_OPTIONS=detect_stack_use_after_return=0``).
158 * ``always``: Enables detection of UAR errors in all cases. (reduces code
159 size, but not as much as ``never``).
161 Memory leak detection
162 ---------------------
164 For more information on leak detector in AddressSanitizer, see
165 :doc:`LeakSanitizer`. The leak detection is turned on by default on Linux,
166 and can be enabled using ``ASAN_OPTIONS=detect_leaks=1`` on macOS;
167 however, it is not yet supported on other platforms.
172 AddressSanitizer is not expected to produce false positives. If you see one,
173 look again; most likely it is a true positive!
175 Suppressing Reports in External Libraries
176 -----------------------------------------
177 Runtime interposition allows AddressSanitizer to find bugs in code that is
178 not being recompiled. If you run into an issue in external libraries, we
179 recommend immediately reporting it to the library maintainer so that it
180 gets addressed. However, you can use the following suppression mechanism
181 to unblock yourself and continue on with the testing. This suppression
182 mechanism should only be used for suppressing issues in external code; it
183 does not work on code recompiled with AddressSanitizer. To suppress errors
184 in external libraries, set the ``ASAN_OPTIONS`` environment variable to point
185 to a suppression file. You can either specify the full path to the file or the
186 path of the file relative to the location of your executable.
190 ASAN_OPTIONS=suppressions=MyASan.supp
192 Use the following format to specify the names of the functions or libraries
193 you want to suppress. You can see these in the error report. Remember that
194 the narrower the scope of the suppression, the more bugs you will be able to
199 interceptor_via_fun:NameOfCFunctionToSuppress
200 interceptor_via_fun:-[ClassName objCMethodToSuppress:]
201 interceptor_via_lib:NameOfTheLibraryToSuppress
203 Conditional Compilation with ``__has_feature(address_sanitizer)``
204 -----------------------------------------------------------------
206 In some cases one may need to execute different code depending on whether
207 AddressSanitizer is enabled.
208 :ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
213 #if defined(__has_feature)
214 # if __has_feature(address_sanitizer)
215 // code that builds only under AddressSanitizer
219 Disabling Instrumentation with ``__attribute__((no_sanitize("address")))``
220 --------------------------------------------------------------------------
222 Some code should not be instrumented by AddressSanitizer. One may use
223 the attribute ``__attribute__((no_sanitize("address")))`` (which has
224 deprecated synonyms `no_sanitize_address` and
225 `no_address_safety_analysis`) to disable instrumentation of a
226 particular function. This attribute may not be supported by other
227 compilers, so we suggest to use it together with
228 ``__has_feature(address_sanitizer)``.
230 The same attribute used on a global variable prevents AddressSanitizer
231 from adding redzones around it and detecting out of bounds accesses.
234 AddressSanitizer also supports
235 ``__attribute__((disable_sanitizer_instrumentation))``. This attribute
236 works similar to ``__attribute__((no_sanitize("address")))``, but it also
237 prevents instrumentation performed by other sanitizers.
239 Suppressing Errors in Recompiled Code (Ignorelist)
240 --------------------------------------------------
242 AddressSanitizer supports ``src`` and ``fun`` entity types in
243 :doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports
244 in the specified source files or functions. Additionally, AddressSanitizer
245 introduces ``global`` and ``type`` entity types that can be used to
246 suppress error reports for out-of-bound access to globals with certain
247 names and types (you may only specify class or struct types).
249 You may use an ``init`` category to suppress reports about initialization-order
250 problems happening in certain source files or with certain global variables.
254 # Suppress error reports for code in a file or in a function:
256 # Ignore all functions with names containing MyFooBar:
258 # Disable out-of-bound checks for global:
260 # Disable out-of-bound checks for global instances of a given class ...
261 type:Namespace::BadClassName
262 # ... or a given struct. Use wildcard to deal with anonymous namespace.
263 type:Namespace2::*::BadStructName
264 # Disable initialization-order checks for globals:
265 global:bad_init_global=init
266 type:*BadInitClassSubstring*=init
267 src:bad/init/files/*=init
269 Suppressing memory leaks
270 ------------------------
272 Memory leak reports produced by :doc:`LeakSanitizer` (if it is run as a part
273 of AddressSanitizer) can be suppressed by a separate file passed as
277 LSAN_OPTIONS=suppressions=MyLSan.supp
279 which contains lines of the form `leak:<pattern>`. Memory leak will be
280 suppressed if pattern matches any function name, source file name, or
281 library name in the symbolized stack trace of the leak report. See
283 <https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions>`_
286 Code generation control
287 =======================
289 Instrumentation code outlining
290 ------------------------------
292 By default AddressSanitizer inlines the instrumentation code to improve the
293 run-time performance, which leads to increased binary size. Using the
294 (clang flag ``-fsanitize-address-outline-instrumentation` default: ``false``)
295 flag forces all code instrumentation to be outlined, which reduces the size
296 of the generated code, but also reduces the run-time performance.
301 * AddressSanitizer uses more real memory than a native run. Exact overhead
302 depends on the allocations sizes. The smaller the allocations you make the
303 bigger the overhead is.
304 * AddressSanitizer uses more stack memory. We have seen up to 3x increase.
305 * On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of
306 virtual address space. This means that tools like ``ulimit`` may not work as
308 * Static linking of executables is not supported.
313 AddressSanitizer is supported on:
315 * Linux i386/x86\_64 (tested on Ubuntu 12.04)
316 * macOS 10.7 - 10.11 (i386/x86\_64)
319 * NetBSD i386/x86\_64
320 * FreeBSD i386/x86\_64 (tested on FreeBSD 11-current)
321 * Windows 8.1+ (i386/x86\_64)
323 Ports to various other platforms are in progress.
328 AddressSanitizer is fully functional on supported platforms starting from LLVM
329 3.1. The test suite is integrated into CMake build and can be run with ``make
330 check-asan`` command.
332 The Windows port is functional and is used by Chrome and Firefox, but it is not
333 as well supported as the other ports.
338 `<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_