1f4a2713aSLionel Sambuc================ 2f4a2713aSLionel SambucAddressSanitizer 3f4a2713aSLionel Sambuc================ 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc.. contents:: 6f4a2713aSLionel Sambuc :local: 7f4a2713aSLionel Sambuc 8f4a2713aSLionel SambucIntroduction 9f4a2713aSLionel Sambuc============ 10f4a2713aSLionel Sambuc 11f4a2713aSLionel SambucAddressSanitizer is a fast memory error detector. It consists of a compiler 12f4a2713aSLionel Sambucinstrumentation module and a run-time library. The tool can detect the 13f4a2713aSLionel Sambucfollowing types of bugs: 14f4a2713aSLionel Sambuc 15f4a2713aSLionel Sambuc* Out-of-bounds accesses to heap, stack and globals 16f4a2713aSLionel Sambuc* Use-after-free 17f4a2713aSLionel Sambuc* Use-after-return (to some extent) 18f4a2713aSLionel Sambuc* Double-free, invalid free 19*0a6a1f1dSLionel Sambuc* Memory leaks (experimental) 20f4a2713aSLionel Sambuc 21f4a2713aSLionel SambucTypical slowdown introduced by AddressSanitizer is **2x**. 22f4a2713aSLionel Sambuc 23f4a2713aSLionel SambucHow to build 24f4a2713aSLionel Sambuc============ 25f4a2713aSLionel Sambuc 26f4a2713aSLionel SambucFollow the `clang build instructions <../get_started.html>`_. CMake build is 27f4a2713aSLionel Sambucsupported. 28f4a2713aSLionel Sambuc 29f4a2713aSLionel SambucUsage 30f4a2713aSLionel Sambuc===== 31f4a2713aSLionel Sambuc 32f4a2713aSLionel SambucSimply compile and link your program with ``-fsanitize=address`` flag. The 33f4a2713aSLionel SambucAddressSanitizer run-time library should be linked to the final executable, so 34f4a2713aSLionel Sambucmake sure to use ``clang`` (not ``ld``) for the final link step. When linking 35f4a2713aSLionel Sambucshared libraries, the AddressSanitizer run-time is not linked, so 36f4a2713aSLionel Sambuc``-Wl,-z,defs`` may cause link errors (don't use it with AddressSanitizer). To 37f4a2713aSLionel Sambucget a reasonable performance add ``-O1`` or higher. To get nicer stack traces 38f4a2713aSLionel Sambucin error messages add ``-fno-omit-frame-pointer``. To get perfect stack traces 39f4a2713aSLionel Sambucyou may need to disable inlining (just use ``-O1``) and tail call elimination 40f4a2713aSLionel Sambuc(``-fno-optimize-sibling-calls``). 41f4a2713aSLionel Sambuc 42f4a2713aSLionel Sambuc.. code-block:: console 43f4a2713aSLionel Sambuc 44f4a2713aSLionel Sambuc % cat example_UseAfterFree.cc 45f4a2713aSLionel Sambuc int main(int argc, char **argv) { 46f4a2713aSLionel Sambuc int *array = new int[100]; 47f4a2713aSLionel Sambuc delete [] array; 48f4a2713aSLionel Sambuc return array[argc]; // BOOM 49f4a2713aSLionel Sambuc } 50f4a2713aSLionel Sambuc 51f4a2713aSLionel Sambuc # Compile and link 52f4a2713aSLionel Sambuc % clang -O1 -g -fsanitize=address -fno-omit-frame-pointer example_UseAfterFree.cc 53f4a2713aSLionel Sambuc 54f4a2713aSLionel Sambucor: 55f4a2713aSLionel Sambuc 56f4a2713aSLionel Sambuc.. code-block:: console 57f4a2713aSLionel Sambuc 58f4a2713aSLionel Sambuc # Compile 59f4a2713aSLionel Sambuc % clang -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc 60f4a2713aSLionel Sambuc # Link 61f4a2713aSLionel Sambuc % clang -g -fsanitize=address example_UseAfterFree.o 62f4a2713aSLionel Sambuc 63f4a2713aSLionel SambucIf a bug is detected, the program will print an error message to stderr and 64*0a6a1f1dSLionel Sambucexit with a non-zero exit code. To make AddressSanitizer symbolize its output 65*0a6a1f1dSLionel Sambucyou need to set the ``ASAN_SYMBOLIZER_PATH`` environment variable to point to 66*0a6a1f1dSLionel Sambucthe ``llvm-symbolizer`` binary (or make sure ``llvm-symbolizer`` is in your 67*0a6a1f1dSLionel Sambuc``$PATH``): 68f4a2713aSLionel Sambuc 69f4a2713aSLionel Sambuc.. code-block:: console 70f4a2713aSLionel Sambuc 71*0a6a1f1dSLionel Sambuc % ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out 72f4a2713aSLionel Sambuc ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8 73f4a2713aSLionel Sambuc READ of size 4 at 0x7f7ddab8c084 thread T0 74f4a2713aSLionel Sambuc #0 0x403c8c in main example_UseAfterFree.cc:4 75f4a2713aSLionel Sambuc #1 0x7f7ddabcac4d in __libc_start_main ??:0 76f4a2713aSLionel Sambuc 0x7f7ddab8c084 is located 4 bytes inside of 400-byte region [0x7f7ddab8c080,0x7f7ddab8c210) 77f4a2713aSLionel Sambuc freed by thread T0 here: 78f4a2713aSLionel Sambuc #0 0x404704 in operator delete[](void*) ??:0 79f4a2713aSLionel Sambuc #1 0x403c53 in main example_UseAfterFree.cc:4 80f4a2713aSLionel Sambuc #2 0x7f7ddabcac4d in __libc_start_main ??:0 81f4a2713aSLionel Sambuc previously allocated by thread T0 here: 82f4a2713aSLionel Sambuc #0 0x404544 in operator new[](unsigned long) ??:0 83f4a2713aSLionel Sambuc #1 0x403c43 in main example_UseAfterFree.cc:2 84f4a2713aSLionel Sambuc #2 0x7f7ddabcac4d in __libc_start_main ??:0 85f4a2713aSLionel Sambuc ==9442== ABORTING 86f4a2713aSLionel Sambuc 87*0a6a1f1dSLionel SambucIf that does not work for you (e.g. your process is sandboxed), you can use a 88*0a6a1f1dSLionel Sambucseparate script to symbolize the result offline (online symbolization can be 89*0a6a1f1dSLionel Sambucforce disabled by setting ``ASAN_OPTIONS=symbolize=0``): 90*0a6a1f1dSLionel Sambuc 91*0a6a1f1dSLionel Sambuc.. code-block:: console 92*0a6a1f1dSLionel Sambuc 93*0a6a1f1dSLionel Sambuc % ASAN_OPTIONS=symbolize=0 ./a.out 2> log 94*0a6a1f1dSLionel Sambuc % projects/compiler-rt/lib/asan/scripts/asan_symbolize.py / < log | c++filt 95*0a6a1f1dSLionel Sambuc ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8 96*0a6a1f1dSLionel Sambuc READ of size 4 at 0x7f7ddab8c084 thread T0 97*0a6a1f1dSLionel Sambuc #0 0x403c8c in main example_UseAfterFree.cc:4 98*0a6a1f1dSLionel Sambuc #1 0x7f7ddabcac4d in __libc_start_main ??:0 99*0a6a1f1dSLionel Sambuc ... 100*0a6a1f1dSLionel Sambuc 101*0a6a1f1dSLionel SambucNote that on OS X you may need to run ``dsymutil`` on your binary to have the 102*0a6a1f1dSLionel Sambucfile\:line info in the AddressSanitizer reports. 103*0a6a1f1dSLionel Sambuc 104f4a2713aSLionel SambucAddressSanitizer exits on the first detected error. This is by design. 105f4a2713aSLionel SambucOne reason: it makes the generated code smaller and faster (both by 106f4a2713aSLionel Sambuc~5%). Another reason: this makes fixing bugs unavoidable. With Valgrind, 107f4a2713aSLionel Sambucit is often the case that users treat Valgrind warnings as false 108f4a2713aSLionel Sambucpositives (which they are not) and don't fix them. 109f4a2713aSLionel Sambuc 110f4a2713aSLionel Sambuc``__has_feature(address_sanitizer)`` 111f4a2713aSLionel Sambuc------------------------------------ 112f4a2713aSLionel Sambuc 113f4a2713aSLionel SambucIn some cases one may need to execute different code depending on whether 114f4a2713aSLionel SambucAddressSanitizer is enabled. 115f4a2713aSLionel Sambuc:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for 116f4a2713aSLionel Sambucthis purpose. 117f4a2713aSLionel Sambuc 118f4a2713aSLionel Sambuc.. code-block:: c 119f4a2713aSLionel Sambuc 120f4a2713aSLionel Sambuc #if defined(__has_feature) 121f4a2713aSLionel Sambuc # if __has_feature(address_sanitizer) 122f4a2713aSLionel Sambuc // code that builds only under AddressSanitizer 123f4a2713aSLionel Sambuc # endif 124f4a2713aSLionel Sambuc #endif 125f4a2713aSLionel Sambuc 126f4a2713aSLionel Sambuc``__attribute__((no_sanitize_address))`` 127f4a2713aSLionel Sambuc----------------------------------------------- 128f4a2713aSLionel Sambuc 129f4a2713aSLionel SambucSome code should not be instrumented by AddressSanitizer. One may use the 130f4a2713aSLionel Sambucfunction attribute 131f4a2713aSLionel Sambuc:ref:`no_sanitize_address <langext-address_sanitizer>` 132f4a2713aSLionel Sambuc(or a deprecated synonym `no_address_safety_analysis`) 133f4a2713aSLionel Sambucto disable instrumentation of a particular function. This attribute may not be 134f4a2713aSLionel Sambucsupported by other compilers, so we suggest to use it together with 135f4a2713aSLionel Sambuc``__has_feature(address_sanitizer)``. 136f4a2713aSLionel Sambuc 137f4a2713aSLionel SambucInitialization order checking 138f4a2713aSLionel Sambuc----------------------------- 139f4a2713aSLionel Sambuc 140f4a2713aSLionel SambucAddressSanitizer can optionally detect dynamic initialization order problems, 141f4a2713aSLionel Sambucwhen initialization of globals defined in one translation unit uses 142f4a2713aSLionel Sambucglobals defined in another translation unit. To enable this check at runtime, 143f4a2713aSLionel Sambucyou should set environment variable 144f4a2713aSLionel Sambuc``ASAN_OPTIONS=check_initialization_order=1``. 145f4a2713aSLionel Sambuc 146f4a2713aSLionel SambucBlacklist 147f4a2713aSLionel Sambuc--------- 148f4a2713aSLionel Sambuc 149f4a2713aSLionel SambucAddressSanitizer supports ``src`` and ``fun`` entity types in 150f4a2713aSLionel Sambuc:doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports 151f4a2713aSLionel Sambucin the specified source files or functions. Additionally, AddressSanitizer 152f4a2713aSLionel Sambucintroduces ``global`` and ``type`` entity types that can be used to 153f4a2713aSLionel Sambucsuppress error reports for out-of-bound access to globals with certain 154f4a2713aSLionel Sambucnames and types (you may only specify class or struct types). 155f4a2713aSLionel Sambuc 156f4a2713aSLionel SambucYou may use an ``init`` category to suppress reports about initialization-order 157f4a2713aSLionel Sambucproblems happening in certain source files or with certain global variables. 158f4a2713aSLionel Sambuc 159f4a2713aSLionel Sambuc.. code-block:: bash 160f4a2713aSLionel Sambuc 161f4a2713aSLionel Sambuc # Suppress error reports for code in a file or in a function: 162f4a2713aSLionel Sambuc src:bad_file.cpp 163f4a2713aSLionel Sambuc # Ignore all functions with names containing MyFooBar: 164f4a2713aSLionel Sambuc fun:*MyFooBar* 165f4a2713aSLionel Sambuc # Disable out-of-bound checks for global: 166f4a2713aSLionel Sambuc global:bad_array 167f4a2713aSLionel Sambuc # Disable out-of-bound checks for global instances of a given class ... 168*0a6a1f1dSLionel Sambuc type:Namespace::BadClassName 169f4a2713aSLionel Sambuc # ... or a given struct. Use wildcard to deal with anonymous namespace. 170*0a6a1f1dSLionel Sambuc type:Namespace2::*::BadStructName 171f4a2713aSLionel Sambuc # Disable initialization-order checks for globals: 172f4a2713aSLionel Sambuc global:bad_init_global=init 173f4a2713aSLionel Sambuc type:*BadInitClassSubstring*=init 174f4a2713aSLionel Sambuc src:bad/init/files/*=init 175f4a2713aSLionel Sambuc 176*0a6a1f1dSLionel SambucMemory leak detection 177*0a6a1f1dSLionel Sambuc--------------------- 178*0a6a1f1dSLionel Sambuc 179*0a6a1f1dSLionel SambucFor the experimental memory leak detector in AddressSanitizer, see 180*0a6a1f1dSLionel Sambuc:doc:`LeakSanitizer`. 181*0a6a1f1dSLionel Sambuc 182f4a2713aSLionel SambucSupported Platforms 183f4a2713aSLionel Sambuc=================== 184f4a2713aSLionel Sambuc 185f4a2713aSLionel SambucAddressSanitizer is supported on 186f4a2713aSLionel Sambuc 187*0a6a1f1dSLionel Sambuc* Linux i386/x86\_64 (tested on Ubuntu 12.04); 188*0a6a1f1dSLionel Sambuc* MacOS 10.6 - 10.9 (i386/x86\_64). 189*0a6a1f1dSLionel Sambuc* Android ARM 190*0a6a1f1dSLionel Sambuc* FreeBSD i386/x86\_64 (tested on FreeBSD 11-current) 191f4a2713aSLionel Sambuc 192*0a6a1f1dSLionel SambucPorts to various other platforms are in progress. 193f4a2713aSLionel Sambuc 194f4a2713aSLionel SambucLimitations 195f4a2713aSLionel Sambuc=========== 196f4a2713aSLionel Sambuc 197f4a2713aSLionel Sambuc* AddressSanitizer uses more real memory than a native run. Exact overhead 198f4a2713aSLionel Sambuc depends on the allocations sizes. The smaller the allocations you make the 199f4a2713aSLionel Sambuc bigger the overhead is. 200f4a2713aSLionel Sambuc* AddressSanitizer uses more stack memory. We have seen up to 3x increase. 201f4a2713aSLionel Sambuc* On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of 202f4a2713aSLionel Sambuc virtual address space. This means that tools like ``ulimit`` may not work as 203f4a2713aSLionel Sambuc usually expected. 204f4a2713aSLionel Sambuc* Static linking is not supported. 205f4a2713aSLionel Sambuc 206f4a2713aSLionel SambucCurrent Status 207f4a2713aSLionel Sambuc============== 208f4a2713aSLionel Sambuc 209f4a2713aSLionel SambucAddressSanitizer is fully functional on supported platforms starting from LLVM 210f4a2713aSLionel Sambuc3.1. The test suite is integrated into CMake build and can be run with ``make 211f4a2713aSLionel Sambuccheck-asan`` command. 212f4a2713aSLionel Sambuc 213f4a2713aSLionel SambucMore Information 214f4a2713aSLionel Sambuc================ 215f4a2713aSLionel Sambuc 216f4a2713aSLionel Sambuc`http://code.google.com/p/address-sanitizer <http://code.google.com/p/address-sanitizer/>`_ 217f4a2713aSLionel Sambuc 218