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>`_ and enable
30 the ``compiler-rt`` runtime. An example CMake configuration that will allow
31 for the use/testing of AddressSanitizer:
33 .. code-block:: console
35 $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="compiler-rt" <path to source>/llvm
40 Simply compile and link your program with ``-fsanitize=address`` flag. The
41 AddressSanitizer run-time library should be linked to the final executable, so
42 make sure to use ``clang`` (not ``ld``) for the final link step. When linking
43 shared libraries, the AddressSanitizer run-time is not linked, so
44 ``-Wl,-z,defs`` may cause link errors (don't use it with AddressSanitizer). To
45 get a reasonable performance add ``-O1`` or higher. To get nicer stack traces
46 in error messages add ``-fno-omit-frame-pointer``. To get perfect stack traces
47 you may need to disable inlining (just use ``-O1``) and tail call elimination
48 (``-fno-optimize-sibling-calls``).
50 .. code-block:: console
52 % cat example_UseAfterFree.cc
53 int main(int argc, char **argv) {
54 int *array = new int[100];
56 return array[argc]; // BOOM
60 % clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer example_UseAfterFree.cc
64 .. code-block:: console
67 % clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc
69 % clang++ -g -fsanitize=address example_UseAfterFree.o
71 If a bug is detected, the program will print an error message to stderr and
72 exit with a non-zero exit code. AddressSanitizer exits on the first detected error.
75 * This approach allows AddressSanitizer to produce faster and smaller generated code
77 * Fixing bugs becomes unavoidable. AddressSanitizer does not produce
78 false alarms. Once a memory corruption occurs, the program is in an inconsistent
79 state, which could lead to confusing results and potentially misleading
82 If your process is sandboxed and you are running on OS X 10.10 or earlier, you
83 will need to set ``DYLD_INSERT_LIBRARIES`` environment variable and point it to
84 the ASan library that is packaged with the compiler used to build the
85 executable. (You can find the library by searching for dynamic libraries with
86 ``asan`` in their name.) If the environment variable is not set, the process will
87 try to re-exec. Also keep in mind that when moving the executable to another machine,
88 the ASan library will also need to be copied over.
90 Symbolizing the Reports
91 =========================
93 To make AddressSanitizer symbolize its output
94 you need to set the ``ASAN_SYMBOLIZER_PATH`` environment variable to point to
95 the ``llvm-symbolizer`` binary (or make sure ``llvm-symbolizer`` is in your
98 .. code-block:: console
100 % ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out
101 ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8
102 READ of size 4 at 0x7f7ddab8c084 thread T0
103 #0 0x403c8c in main example_UseAfterFree.cc:4
104 #1 0x7f7ddabcac4d in __libc_start_main ??:0
105 0x7f7ddab8c084 is located 4 bytes inside of 400-byte region [0x7f7ddab8c080,0x7f7ddab8c210)
106 freed by thread T0 here:
107 #0 0x404704 in operator delete[](void*) ??:0
108 #1 0x403c53 in main example_UseAfterFree.cc:4
109 #2 0x7f7ddabcac4d in __libc_start_main ??:0
110 previously allocated by thread T0 here:
111 #0 0x404544 in operator new[](unsigned long) ??:0
112 #1 0x403c43 in main example_UseAfterFree.cc:2
113 #2 0x7f7ddabcac4d in __libc_start_main ??:0
116 If that does not work for you (e.g. your process is sandboxed), you can use a
117 separate script to symbolize the result offline (online symbolization can be
118 force disabled by setting ``ASAN_OPTIONS=symbolize=0``):
120 .. code-block:: console
122 % ASAN_OPTIONS=symbolize=0 ./a.out 2> log
123 % projects/compiler-rt/lib/asan/scripts/asan_symbolize.py / < log | c++filt
124 ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8
125 READ of size 4 at 0x7f7ddab8c084 thread T0
126 #0 0x403c8c in main example_UseAfterFree.cc:4
127 #1 0x7f7ddabcac4d in __libc_start_main ??:0
130 Note that on macOS you may need to run ``dsymutil`` on your binary to have the
131 file\:line info in the AddressSanitizer reports.
136 Initialization order checking
137 -----------------------------
139 AddressSanitizer can optionally detect dynamic initialization order problems,
140 when initialization of globals defined in one translation unit uses
141 globals defined in another translation unit. To enable this check at runtime,
142 you should set environment variable
143 ``ASAN_OPTIONS=check_initialization_order=1``.
145 Note that this option is not supported on macOS.
147 Stack Use After Return (UAR)
148 ----------------------------
150 AddressSanitizer can optionally detect stack use after return problems.
151 This is available by default, or explicitly
152 (``-fsanitize-address-use-after-return=runtime``).
153 To disable this check at runtime, set the environment variable
154 ``ASAN_OPTIONS=detect_stack_use_after_return=0``.
156 Enabling this check (``-fsanitize-address-use-after-return=always``) will
157 reduce code size. The code size may be reduced further by completely
158 eliminating this check (``-fsanitize-address-use-after-return=never``).
160 To summarize: ``-fsanitize-address-use-after-return=<mode>``
161 * ``never``: Completely disables detection of UAR errors (reduces code size).
162 * ``runtime``: Adds the code for detection, but it can be disable via the
163 runtime environment (``ASAN_OPTIONS=detect_stack_use_after_return=0``).
164 * ``always``: Enables detection of UAR errors in all cases. (reduces code
165 size, but not as much as ``never``).
167 Memory leak detection
168 ---------------------
170 For more information on leak detector in AddressSanitizer, see
171 :doc:`LeakSanitizer`. The leak detection is turned on by default on Linux,
172 and can be enabled using ``ASAN_OPTIONS=detect_leaks=1`` on macOS;
173 however, it is not yet supported on other platforms.
178 AddressSanitizer is not expected to produce false positives. If you see one,
179 look again; most likely it is a true positive!
181 Suppressing Reports in External Libraries
182 -----------------------------------------
183 Runtime interposition allows AddressSanitizer to find bugs in code that is
184 not being recompiled. If you run into an issue in external libraries, we
185 recommend immediately reporting it to the library maintainer so that it
186 gets addressed. However, you can use the following suppression mechanism
187 to unblock yourself and continue on with the testing. This suppression
188 mechanism should only be used for suppressing issues in external code; it
189 does not work on code recompiled with AddressSanitizer. To suppress errors
190 in external libraries, set the ``ASAN_OPTIONS`` environment variable to point
191 to a suppression file. You can either specify the full path to the file or the
192 path of the file relative to the location of your executable.
196 ASAN_OPTIONS=suppressions=MyASan.supp
198 Use the following format to specify the names of the functions or libraries
199 you want to suppress. You can see these in the error report. Remember that
200 the narrower the scope of the suppression, the more bugs you will be able to
205 interceptor_via_fun:NameOfCFunctionToSuppress
206 interceptor_via_fun:-[ClassName objCMethodToSuppress:]
207 interceptor_via_lib:NameOfTheLibraryToSuppress
209 Conditional Compilation with ``__has_feature(address_sanitizer)``
210 -----------------------------------------------------------------
212 In some cases one may need to execute different code depending on whether
213 AddressSanitizer is enabled.
214 :ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
219 #if defined(__has_feature)
220 # if __has_feature(address_sanitizer)
221 // code that builds only under AddressSanitizer
225 Disabling Instrumentation with ``__attribute__((no_sanitize("address")))``
226 --------------------------------------------------------------------------
228 Some code should not be instrumented by AddressSanitizer. One may use
229 the attribute ``__attribute__((no_sanitize("address")))`` (which has
230 deprecated synonyms `no_sanitize_address` and
231 `no_address_safety_analysis`) to disable instrumentation of a
232 particular function. This attribute may not be supported by other
233 compilers, so we suggest to use it together with
234 ``__has_feature(address_sanitizer)``.
236 The same attribute used on a global variable prevents AddressSanitizer
237 from adding redzones around it and detecting out of bounds accesses.
240 AddressSanitizer also supports
241 ``__attribute__((disable_sanitizer_instrumentation))``. This attribute
242 works similar to ``__attribute__((no_sanitize("address")))``, but it also
243 prevents instrumentation performed by other sanitizers.
245 Suppressing Errors in Recompiled Code (Ignorelist)
246 --------------------------------------------------
248 AddressSanitizer supports ``src`` and ``fun`` entity types in
249 :doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports
250 in the specified source files or functions. Additionally, AddressSanitizer
251 introduces ``global`` and ``type`` entity types that can be used to
252 suppress error reports for out-of-bound access to globals with certain
253 names and types (you may only specify class or struct types).
255 You may use an ``init`` category to suppress reports about initialization-order
256 problems happening in certain source files or with certain global variables.
260 # Suppress error reports for code in a file or in a function:
262 # Ignore all functions with names containing MyFooBar:
264 # Disable out-of-bound checks for global:
266 # Disable out-of-bound checks for global instances of a given class ...
267 type:Namespace::BadClassName
268 # ... or a given struct. Use wildcard to deal with anonymous namespace.
269 type:Namespace2::*::BadStructName
270 # Disable initialization-order checks for globals:
271 global:bad_init_global=init
272 type:*BadInitClassSubstring*=init
273 src:bad/init/files/*=init
275 Suppressing memory leaks
276 ------------------------
278 Memory leak reports produced by :doc:`LeakSanitizer` (if it is run as a part
279 of AddressSanitizer) can be suppressed by a separate file passed as
283 LSAN_OPTIONS=suppressions=MyLSan.supp
285 which contains lines of the form `leak:<pattern>`. Memory leak will be
286 suppressed if pattern matches any function name, source file name, or
287 library name in the symbolized stack trace of the leak report. See
289 <https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions>`_
292 Code generation control
293 =======================
295 Instrumentation code outlining
296 ------------------------------
298 By default AddressSanitizer inlines the instrumentation code to improve the
299 run-time performance, which leads to increased binary size. Using the
300 (clang flag ``-fsanitize-address-outline-instrumentation` default: ``false``)
301 flag forces all code instrumentation to be outlined, which reduces the size
302 of the generated code, but also reduces the run-time performance.
307 * AddressSanitizer uses more real memory than a native run. Exact overhead
308 depends on the allocations sizes. The smaller the allocations you make the
309 bigger the overhead is.
310 * AddressSanitizer uses more stack memory. We have seen up to 3x increase.
311 * On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of
312 virtual address space. This means that tools like ``ulimit`` may not work as
314 * Static linking of executables is not supported.
316 Security Considerations
317 =======================
319 AddressSanitizer is a bug detection tool and its runtime is not meant to be
320 linked against production executables. While it may be useful for testing,
321 AddressSanitizer's runtime was not developed with security-sensitive
322 constraints in mind and may compromise the security of the resulting executable.
327 AddressSanitizer is supported on:
340 AddressSanitizer is fully functional on supported platforms starting from LLVM
341 3.1. The test suite is integrated into CMake build and can be run with ``make
342 check-asan`` command.
344 The Windows port is functional and is used by Chrome and Firefox, but it is not
345 as well supported as the other ports.
350 `<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_