| 1 |
================ |
| 2 |
AddressSanitizer |
| 3 |
================ |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
:local: |
| 7 |
|
| 8 |
Introduction |
| 9 |
============ |
| 10 |
|
| 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: |
| 14 |
|
| 15 |
* Out-of-bounds accesses to heap, stack and globals |
| 16 |
* Use-after-free |
| 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) |
| 23 |
|
| 24 |
Typical slowdown introduced by AddressSanitizer is **2x**. |
| 25 |
|
| 26 |
How to build |
| 27 |
============ |
| 28 |
|
| 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: |
| 32 |
|
| 33 |
.. code-block:: console |
| 34 |
|
| 35 |
$ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="compiler-rt" <path to source>/llvm |
| 36 |
|
| 37 |
Usage |
| 38 |
===== |
| 39 |
|
| 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``). |
| 49 |
|
| 50 |
.. code-block:: console |
| 51 |
|
| 52 |
% cat example_UseAfterFree.cc |
| 53 |
int main(int argc, char **argv) { |
| 54 |
int *array = new int[100]; |
| 55 |
delete [] array; |
| 56 |
return array[argc]; // BOOM |
| 57 |
} |
| 58 |
|
| 59 |
# Compile and link |
| 60 |
% clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer example_UseAfterFree.cc |
| 61 |
|
| 62 |
or: |
| 63 |
|
| 64 |
.. code-block:: console |
| 65 |
|
| 66 |
# Compile |
| 67 |
% clang++ -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc |
| 68 |
# Link |
| 69 |
% clang++ -g -fsanitize=address example_UseAfterFree.o |
| 70 |
|
| 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. |
| 73 |
This is by design: |
| 74 |
|
| 75 |
* This approach allows AddressSanitizer to produce faster and smaller generated code |
| 76 |
(both by ~5%). |
| 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 |
| 80 |
subsequent reports. |
| 81 |
|
| 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. |
| 89 |
|
| 90 |
Symbolizing the Reports |
| 91 |
========================= |
| 92 |
|
| 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 |
| 96 |
``$PATH``): |
| 97 |
|
| 98 |
.. code-block:: console |
| 99 |
|
| 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 |
| 114 |
==9442== ABORTING |
| 115 |
|
| 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``): |
| 119 |
|
| 120 |
.. code-block:: console |
| 121 |
|
| 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 |
| 128 |
... |
| 129 |
|
| 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. |
| 132 |
|
| 133 |
Additional Checks |
| 134 |
================= |
| 135 |
|
| 136 |
Initialization order checking |
| 137 |
----------------------------- |
| 138 |
|
| 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``. |
| 144 |
|
| 145 |
Note that this option is not supported on macOS. |
| 146 |
|
| 147 |
Stack Use After Return (UAR) |
| 148 |
---------------------------- |
| 149 |
|
| 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``. |
| 155 |
|
| 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``). |
| 159 |
|
| 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 disabled 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``). |
| 166 |
|
| 167 |
Container Overflow Detection |
| 168 |
---------------------------- |
| 169 |
|
| 170 |
AddressSanitizer can detect overflows in containers with custom allocators |
| 171 |
(such as std::vector) where the library developers have added calls into the |
| 172 |
AddressSanitizer runtime to indicate which memory is poisoned etc. |
| 173 |
|
| 174 |
In environments where not all the process binaries can be recompiled with |
| 175 |
AddressSanitizer enabled, these checks can cause false positives. |
| 176 |
|
| 177 |
See `Disabling container overflow checks`_ for details on suppressing checks. |
| 178 |
|
| 179 |
Memory leak detection |
| 180 |
--------------------- |
| 181 |
|
| 182 |
For more information on leak detector in AddressSanitizer, see |
| 183 |
:doc:`LeakSanitizer`. The leak detection is turned on by default on Linux, |
| 184 |
and can be enabled using ``ASAN_OPTIONS=detect_leaks=1`` on macOS; |
| 185 |
however, it is not yet supported on other platforms. |
| 186 |
|
| 187 |
Issue Suppression |
| 188 |
================= |
| 189 |
|
| 190 |
AddressSanitizer is not expected to produce false positives. If you see one, |
| 191 |
look again; most likely it is a true positive! |
| 192 |
|
| 193 |
Suppressing Reports in External Libraries |
| 194 |
----------------------------------------- |
| 195 |
Runtime interposition allows AddressSanitizer to find bugs in code that is |
| 196 |
not being recompiled. If you run into an issue in external libraries, we |
| 197 |
recommend immediately reporting it to the library maintainer so that it |
| 198 |
gets addressed. However, you can use the following suppression mechanism |
| 199 |
to unblock yourself and continue on with the testing. This suppression |
| 200 |
mechanism should only be used for suppressing issues in external code; it |
| 201 |
does not work on code recompiled with AddressSanitizer. To suppress errors |
| 202 |
in external libraries, set the ``ASAN_OPTIONS`` environment variable to point |
| 203 |
to a suppression file. You can either specify the full path to the file or the |
| 204 |
path of the file relative to the location of your executable. |
| 205 |
|
| 206 |
.. code-block:: bash |
| 207 |
|
| 208 |
ASAN_OPTIONS=suppressions=MyASan.supp |
| 209 |
|
| 210 |
Use the following format to specify the names of the functions or libraries |
| 211 |
you want to suppress. You can see these in the error report. Remember that |
| 212 |
the narrower the scope of the suppression, the more bugs you will be able to |
| 213 |
catch. |
| 214 |
|
| 215 |
.. code-block:: bash |
| 216 |
|
| 217 |
interceptor_via_fun:NameOfCFunctionToSuppress |
| 218 |
interceptor_via_fun:-[ClassName objCMethodToSuppress:] |
| 219 |
interceptor_via_lib:NameOfTheLibraryToSuppress |
| 220 |
|
| 221 |
Conditional Compilation with ``__has_feature(address_sanitizer)`` |
| 222 |
----------------------------------------------------------------- |
| 223 |
|
| 224 |
In some cases one may need to execute different code depending on whether |
| 225 |
AddressSanitizer is enabled. |
| 226 |
:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for |
| 227 |
this purpose. |
| 228 |
|
| 229 |
.. code-block:: c |
| 230 |
|
| 231 |
#if defined(__has_feature) |
| 232 |
# if __has_feature(address_sanitizer) |
| 233 |
// code that builds only under AddressSanitizer |
| 234 |
# endif |
| 235 |
#endif |
| 236 |
|
| 237 |
Disabling Instrumentation with ``__attribute__((no_sanitize("address")))`` |
| 238 |
-------------------------------------------------------------------------- |
| 239 |
|
| 240 |
Some code should not be instrumented by AddressSanitizer. One may use |
| 241 |
the attribute ``__attribute__((no_sanitize("address")))`` (which has |
| 242 |
deprecated synonyms `no_sanitize_address` and |
| 243 |
`no_address_safety_analysis`) to disable instrumentation of a |
| 244 |
particular function. This attribute may not be supported by other |
| 245 |
compilers, so we suggest to use it together with |
| 246 |
``__has_feature(address_sanitizer)``. |
| 247 |
|
| 248 |
The same attribute used on a global variable prevents AddressSanitizer |
| 249 |
from adding redzones around it and detecting out of bounds accesses. |
| 250 |
|
| 251 |
|
| 252 |
AddressSanitizer also supports |
| 253 |
``__attribute__((disable_sanitizer_instrumentation))``. This attribute |
| 254 |
works similarly to ``__attribute__((no_sanitize("address")))``, but it also |
| 255 |
prevents instrumentation performed by other sanitizers. |
| 256 |
|
| 257 |
Conditional Sanitizer Checks with ``__builtin_allow_sanitize_check`` |
| 258 |
-------------------------------------------------------------------- |
| 259 |
|
| 260 |
The ``__builtin_allow_sanitize_check("address")`` builtin can be used to |
| 261 |
conditionally execute code only when AddressSanitizer is active for the current |
| 262 |
function (after inlining). This is particularly useful for inserting explicit, |
| 263 |
sanitizer-specific checks around operations like syscalls or inline assembly, |
| 264 |
which might otherwise be unchecked by the sanitizer. |
| 265 |
|
| 266 |
Example: |
| 267 |
|
| 268 |
.. code-block:: c |
| 269 |
|
| 270 |
inline __attribute__((always_inline)) |
| 271 |
void copy_to_device(void *addr, size_t size) { |
| 272 |
if (__builtin_allow_sanitize_check("address")) { |
| 273 |
// Custom checks that address range is valid. |
| 274 |
} |
| 275 |
// ... actual device memory copy logic, potentially a syscall ... |
| 276 |
} |
| 277 |
|
| 278 |
void instrumented_function() { |
| 279 |
... |
| 280 |
copy_to_device(buf, sizeof(buf)); // checks are active |
| 281 |
... |
| 282 |
} |
| 283 |
|
| 284 |
__attribute__((no_sanitize("address"))) |
| 285 |
void uninstrumented_function() { |
| 286 |
... |
| 287 |
copy_to_device(buf, sizeof(buf)); // checks are skipped |
| 288 |
... |
| 289 |
} |
| 290 |
|
| 291 |
Disabling container overflow checks |
| 292 |
----------------------------------- |
| 293 |
|
| 294 |
Runtime suppression |
| 295 |
^^^^^^^^^^^^^^^^^^^ |
| 296 |
|
| 297 |
Container overflow checks can be disabled at runtime using the |
| 298 |
``ASAN_OPTIONS=detect_container_overflow=0`` environment variable. |
| 299 |
|
| 300 |
Compile time suppression |
| 301 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
| 302 |
|
| 303 |
``-D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__`` can be used at compile time to |
| 304 |
disable container overflow checks if the container library has added support |
| 305 |
for this define. |
| 306 |
|
| 307 |
To support a standard way to disable container overflow checks at compile time, |
| 308 |
library developers should use this definition in conjunction with the |
| 309 |
AddressSanitizer feature test to conditionally include container overflow |
| 310 |
related code compiled into user code: |
| 311 |
|
| 312 |
The recommended form is |
| 313 |
|
| 314 |
.. code-block:: c |
| 315 |
|
| 316 |
// include the sanitizer common interfaces |
| 317 |
#include <sanitizer/common_interface_defs.h> |
| 318 |
|
| 319 |
#if __has_feature(address_sanitizer) |
| 320 |
// Container overflow detection enabled - include annotations |
| 321 |
__sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid); |
| 322 |
#endif |
| 323 |
|
| 324 |
This pattern ensures that: |
| 325 |
|
| 326 |
* Container overflow annotations are only included when AddressSanitizer is |
| 327 |
enabled |
| 328 |
* Container overflow detection can be disabled by passing |
| 329 |
``-D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__`` to the compiler |
| 330 |
|
| 331 |
Suppressing Errors in Recompiled Code (Ignorelist) |
| 332 |
-------------------------------------------------- |
| 333 |
|
| 334 |
AddressSanitizer supports ``src`` and ``fun`` entity types in |
| 335 |
:doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports |
| 336 |
in the specified source files or functions. Additionally, AddressSanitizer |
| 337 |
introduces ``global`` and ``type`` entity types that can be used to |
| 338 |
suppress error reports for out-of-bound access to globals with certain |
| 339 |
names and types (you may only specify class or struct types). |
| 340 |
|
| 341 |
You may use an ``init`` category to suppress reports about initialization-order |
| 342 |
problems happening in certain source files or with certain global variables. |
| 343 |
|
| 344 |
.. code-block:: bash |
| 345 |
|
| 346 |
# Suppress error reports for code in a file or in a function: |
| 347 |
src:bad_file.cpp |
| 348 |
# Ignore all functions with names containing MyFooBar: |
| 349 |
fun:*MyFooBar* |
| 350 |
# Disable out-of-bound checks for global: |
| 351 |
global:bad_array |
| 352 |
# Disable out-of-bound checks for global instances of a given class ... |
| 353 |
type:Namespace::BadClassName |
| 354 |
# ... or a given struct. Use wildcard to deal with anonymous namespace. |
| 355 |
type:Namespace2::*::BadStructName |
| 356 |
# Disable initialization-order checks for globals: |
| 357 |
global:bad_init_global=init |
| 358 |
type:*BadInitClassSubstring*=init |
| 359 |
src:bad/init/files/*=init |
| 360 |
|
| 361 |
Suppressing memory leaks |
| 362 |
------------------------ |
| 363 |
|
| 364 |
Memory leak reports produced by :doc:`LeakSanitizer` (if it is run as a part |
| 365 |
of AddressSanitizer) can be suppressed by a separate file passed as |
| 366 |
|
| 367 |
.. code-block:: bash |
| 368 |
|
| 369 |
LSAN_OPTIONS=suppressions=MyLSan.supp |
| 370 |
|
| 371 |
which contains lines of the form `leak:<pattern>`. Memory leak will be |
| 372 |
suppressed if pattern matches any function name, source file name, or |
| 373 |
library name in the symbolized stack trace of the leak report. See |
| 374 |
`full documentation |
| 375 |
<https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions>`_ |
| 376 |
for more details. |
| 377 |
|
| 378 |
Code generation control |
| 379 |
======================= |
| 380 |
|
| 381 |
Instrumentation code outlining |
| 382 |
------------------------------ |
| 383 |
|
| 384 |
By default AddressSanitizer inlines the instrumentation code to improve the |
| 385 |
run-time performance, which leads to increased binary size. Using the |
| 386 |
(clang flag ``-fsanitize-address-outline-instrumentation`` default: ``false``) |
| 387 |
flag forces all code instrumentation to be outlined, which reduces the size |
| 388 |
of the generated code, but also reduces the run-time performance. |
| 389 |
|
| 390 |
Limitations |
| 391 |
=========== |
| 392 |
|
| 393 |
* AddressSanitizer uses more real memory than a native run. Exact overhead |
| 394 |
depends on the allocation sizes. The smaller the allocations you make the |
| 395 |
bigger the overhead is. |
| 396 |
* AddressSanitizer uses more stack memory. We have seen up to 3x increase. |
| 397 |
* On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of |
| 398 |
virtual address space. This means that tools like ``ulimit`` may not work as |
| 399 |
usually expected. |
| 400 |
* Static linking of executables is not supported. |
| 401 |
|
| 402 |
Security Considerations |
| 403 |
======================= |
| 404 |
|
| 405 |
AddressSanitizer is a bug detection tool and its runtime is not meant to be |
| 406 |
linked against production executables. While it may be useful for testing, |
| 407 |
AddressSanitizer's runtime was not developed with security-sensitive |
| 408 |
constraints in mind and may compromise the security of the resulting executable. |
| 409 |
|
| 410 |
Supported Platforms |
| 411 |
=================== |
| 412 |
|
| 413 |
AddressSanitizer is supported on: |
| 414 |
|
| 415 |
* Linux |
| 416 |
* macOS |
| 417 |
* iOS Simulator |
| 418 |
* Android |
| 419 |
* NetBSD |
| 420 |
* FreeBSD |
| 421 |
* Windows 8.1+ |
| 422 |
|
| 423 |
Current Status |
| 424 |
============== |
| 425 |
|
| 426 |
AddressSanitizer is fully functional on supported platforms starting from LLVM |
| 427 |
3.1. The test suite is integrated into CMake build and can be run with ``make |
| 428 |
check-asan`` command. |
| 429 |
|
| 430 |
The Windows port is functional and is used by Chrome and Firefox, but it is not |
| 431 |
as well supported as the other ports. |
| 432 |
|
| 433 |
More Information |
| 434 |
================ |
| 435 |
|
| 436 |
`<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ |