1*67e74705SXin Li================ 2*67e74705SXin LiAddressSanitizer 3*67e74705SXin Li================ 4*67e74705SXin Li 5*67e74705SXin Li.. contents:: 6*67e74705SXin Li :local: 7*67e74705SXin Li 8*67e74705SXin LiIntroduction 9*67e74705SXin Li============ 10*67e74705SXin Li 11*67e74705SXin LiAddressSanitizer is a fast memory error detector. It consists of a compiler 12*67e74705SXin Liinstrumentation module and a run-time library. The tool can detect the 13*67e74705SXin Lifollowing types of bugs: 14*67e74705SXin Li 15*67e74705SXin Li* Out-of-bounds accesses to heap, stack and globals 16*67e74705SXin Li* Use-after-free 17*67e74705SXin Li* Use-after-return (to some extent) 18*67e74705SXin Li* Double-free, invalid free 19*67e74705SXin Li* Memory leaks (experimental) 20*67e74705SXin Li 21*67e74705SXin LiTypical slowdown introduced by AddressSanitizer is **2x**. 22*67e74705SXin Li 23*67e74705SXin LiHow to build 24*67e74705SXin Li============ 25*67e74705SXin Li 26*67e74705SXin LiBuild LLVM/Clang with `CMake <http://llvm.org/docs/CMake.html>`_. 27*67e74705SXin Li 28*67e74705SXin LiUsage 29*67e74705SXin Li===== 30*67e74705SXin Li 31*67e74705SXin LiSimply compile and link your program with ``-fsanitize=address`` flag. The 32*67e74705SXin LiAddressSanitizer run-time library should be linked to the final executable, so 33*67e74705SXin Limake sure to use ``clang`` (not ``ld``) for the final link step. When linking 34*67e74705SXin Lishared libraries, the AddressSanitizer run-time is not linked, so 35*67e74705SXin Li``-Wl,-z,defs`` may cause link errors (don't use it with AddressSanitizer). To 36*67e74705SXin Liget a reasonable performance add ``-O1`` or higher. To get nicer stack traces 37*67e74705SXin Liin error messages add ``-fno-omit-frame-pointer``. To get perfect stack traces 38*67e74705SXin Liyou may need to disable inlining (just use ``-O1``) and tail call elimination 39*67e74705SXin Li(``-fno-optimize-sibling-calls``). 40*67e74705SXin Li 41*67e74705SXin Li.. code-block:: console 42*67e74705SXin Li 43*67e74705SXin Li % cat example_UseAfterFree.cc 44*67e74705SXin Li int main(int argc, char **argv) { 45*67e74705SXin Li int *array = new int[100]; 46*67e74705SXin Li delete [] array; 47*67e74705SXin Li return array[argc]; // BOOM 48*67e74705SXin Li } 49*67e74705SXin Li 50*67e74705SXin Li # Compile and link 51*67e74705SXin Li % clang -O1 -g -fsanitize=address -fno-omit-frame-pointer example_UseAfterFree.cc 52*67e74705SXin Li 53*67e74705SXin Lior: 54*67e74705SXin Li 55*67e74705SXin Li.. code-block:: console 56*67e74705SXin Li 57*67e74705SXin Li # Compile 58*67e74705SXin Li % clang -O1 -g -fsanitize=address -fno-omit-frame-pointer -c example_UseAfterFree.cc 59*67e74705SXin Li # Link 60*67e74705SXin Li % clang -g -fsanitize=address example_UseAfterFree.o 61*67e74705SXin Li 62*67e74705SXin LiIf a bug is detected, the program will print an error message to stderr and 63*67e74705SXin Liexit with a non-zero exit code. AddressSanitizer exits on the first detected error. 64*67e74705SXin LiThis is by design: 65*67e74705SXin Li 66*67e74705SXin Li* This approach allows AddressSanitizer to produce faster and smaller generated code 67*67e74705SXin Li (both by ~5%). 68*67e74705SXin Li* Fixing bugs becomes unavoidable. AddressSanitizer does not produce 69*67e74705SXin Li false alarms. Once a memory corruption occurs, the program is in an inconsistent 70*67e74705SXin Li state, which could lead to confusing results and potentially misleading 71*67e74705SXin Li subsequent reports. 72*67e74705SXin Li 73*67e74705SXin LiIf your process is sandboxed and you are running on OS X 10.10 or earlier, you 74*67e74705SXin Liwill need to set ``DYLD_INSERT_LIBRARIES`` environment variable and point it to 75*67e74705SXin Lithe ASan library that is packaged with the compiler used to build the 76*67e74705SXin Liexecutable. (You can find the library by searching for dynamic libraries with 77*67e74705SXin Li``asan`` in their name.) If the environment variable is not set, the process will 78*67e74705SXin Litry to re-exec. Also keep in mind that when moving the executable to another machine, 79*67e74705SXin Lithe ASan library will also need to be copied over. 80*67e74705SXin Li 81*67e74705SXin LiSymbolizing the Reports 82*67e74705SXin Li========================= 83*67e74705SXin Li 84*67e74705SXin LiTo make AddressSanitizer symbolize its output 85*67e74705SXin Liyou need to set the ``ASAN_SYMBOLIZER_PATH`` environment variable to point to 86*67e74705SXin Lithe ``llvm-symbolizer`` binary (or make sure ``llvm-symbolizer`` is in your 87*67e74705SXin Li``$PATH``): 88*67e74705SXin Li 89*67e74705SXin Li.. code-block:: console 90*67e74705SXin Li 91*67e74705SXin Li % ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out 92*67e74705SXin Li ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8 93*67e74705SXin Li READ of size 4 at 0x7f7ddab8c084 thread T0 94*67e74705SXin Li #0 0x403c8c in main example_UseAfterFree.cc:4 95*67e74705SXin Li #1 0x7f7ddabcac4d in __libc_start_main ??:0 96*67e74705SXin Li 0x7f7ddab8c084 is located 4 bytes inside of 400-byte region [0x7f7ddab8c080,0x7f7ddab8c210) 97*67e74705SXin Li freed by thread T0 here: 98*67e74705SXin Li #0 0x404704 in operator delete[](void*) ??:0 99*67e74705SXin Li #1 0x403c53 in main example_UseAfterFree.cc:4 100*67e74705SXin Li #2 0x7f7ddabcac4d in __libc_start_main ??:0 101*67e74705SXin Li previously allocated by thread T0 here: 102*67e74705SXin Li #0 0x404544 in operator new[](unsigned long) ??:0 103*67e74705SXin Li #1 0x403c43 in main example_UseAfterFree.cc:2 104*67e74705SXin Li #2 0x7f7ddabcac4d in __libc_start_main ??:0 105*67e74705SXin Li ==9442== ABORTING 106*67e74705SXin Li 107*67e74705SXin LiIf that does not work for you (e.g. your process is sandboxed), you can use a 108*67e74705SXin Liseparate script to symbolize the result offline (online symbolization can be 109*67e74705SXin Liforce disabled by setting ``ASAN_OPTIONS=symbolize=0``): 110*67e74705SXin Li 111*67e74705SXin Li.. code-block:: console 112*67e74705SXin Li 113*67e74705SXin Li % ASAN_OPTIONS=symbolize=0 ./a.out 2> log 114*67e74705SXin Li % projects/compiler-rt/lib/asan/scripts/asan_symbolize.py / < log | c++filt 115*67e74705SXin Li ==9442== ERROR: AddressSanitizer heap-use-after-free on address 0x7f7ddab8c084 at pc 0x403c8c bp 0x7fff87fb82d0 sp 0x7fff87fb82c8 116*67e74705SXin Li READ of size 4 at 0x7f7ddab8c084 thread T0 117*67e74705SXin Li #0 0x403c8c in main example_UseAfterFree.cc:4 118*67e74705SXin Li #1 0x7f7ddabcac4d in __libc_start_main ??:0 119*67e74705SXin Li ... 120*67e74705SXin Li 121*67e74705SXin LiNote that on OS X you may need to run ``dsymutil`` on your binary to have the 122*67e74705SXin Lifile\:line info in the AddressSanitizer reports. 123*67e74705SXin Li 124*67e74705SXin LiAdditional Checks 125*67e74705SXin Li================= 126*67e74705SXin Li 127*67e74705SXin LiInitialization order checking 128*67e74705SXin Li----------------------------- 129*67e74705SXin Li 130*67e74705SXin LiAddressSanitizer can optionally detect dynamic initialization order problems, 131*67e74705SXin Liwhen initialization of globals defined in one translation unit uses 132*67e74705SXin Liglobals defined in another translation unit. To enable this check at runtime, 133*67e74705SXin Liyou should set environment variable 134*67e74705SXin Li``ASAN_OPTIONS=check_initialization_order=1``. 135*67e74705SXin Li 136*67e74705SXin LiNote that this option is not supported on OS X. 137*67e74705SXin Li 138*67e74705SXin LiMemory leak detection 139*67e74705SXin Li--------------------- 140*67e74705SXin Li 141*67e74705SXin LiFor more information on leak detector in AddressSanitizer, see 142*67e74705SXin Li:doc:`LeakSanitizer`. The leak detection is turned on by default on Linux; 143*67e74705SXin Lihowever, it is not yet supported on other platforms. 144*67e74705SXin Li 145*67e74705SXin LiIssue Suppression 146*67e74705SXin Li================= 147*67e74705SXin Li 148*67e74705SXin LiAddressSanitizer is not expected to produce false positives. If you see one, 149*67e74705SXin Lilook again; most likely it is a true positive! 150*67e74705SXin Li 151*67e74705SXin LiSuppressing Reports in External Libraries 152*67e74705SXin Li----------------------------------------- 153*67e74705SXin LiRuntime interposition allows AddressSanitizer to find bugs in code that is 154*67e74705SXin Linot being recompiled. If you run into an issue in external libraries, we 155*67e74705SXin Lirecommend immediately reporting it to the library maintainer so that it 156*67e74705SXin Ligets addressed. However, you can use the following suppression mechanism 157*67e74705SXin Lito unblock yourself and continue on with the testing. This suppression 158*67e74705SXin Limechanism should only be used for suppressing issues in external code; it 159*67e74705SXin Lidoes not work on code recompiled with AddressSanitizer. To suppress errors 160*67e74705SXin Liin external libraries, set the ``ASAN_OPTIONS`` environment variable to point 161*67e74705SXin Lito a suppression file. You can either specify the full path to the file or the 162*67e74705SXin Lipath of the file relative to the location of your executable. 163*67e74705SXin Li 164*67e74705SXin Li.. code-block:: bash 165*67e74705SXin Li 166*67e74705SXin Li ASAN_OPTIONS=suppressions=MyASan.supp 167*67e74705SXin Li 168*67e74705SXin LiUse the following format to specify the names of the functions or libraries 169*67e74705SXin Liyou want to suppress. You can see these in the error report. Remember that 170*67e74705SXin Lithe narrower the scope of the suppression, the more bugs you will be able to 171*67e74705SXin Licatch. 172*67e74705SXin Li 173*67e74705SXin Li.. code-block:: bash 174*67e74705SXin Li 175*67e74705SXin Li interceptor_via_fun:NameOfCFunctionToSuppress 176*67e74705SXin Li interceptor_via_fun:-[ClassName objCMethodToSuppress:] 177*67e74705SXin Li interceptor_via_lib:NameOfTheLibraryToSuppress 178*67e74705SXin Li 179*67e74705SXin LiConditional Compilation with ``__has_feature(address_sanitizer)`` 180*67e74705SXin Li----------------------------------------------------------------- 181*67e74705SXin Li 182*67e74705SXin LiIn some cases one may need to execute different code depending on whether 183*67e74705SXin LiAddressSanitizer is enabled. 184*67e74705SXin Li:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for 185*67e74705SXin Lithis purpose. 186*67e74705SXin Li 187*67e74705SXin Li.. code-block:: c 188*67e74705SXin Li 189*67e74705SXin Li #if defined(__has_feature) 190*67e74705SXin Li # if __has_feature(address_sanitizer) 191*67e74705SXin Li // code that builds only under AddressSanitizer 192*67e74705SXin Li # endif 193*67e74705SXin Li #endif 194*67e74705SXin Li 195*67e74705SXin LiDisabling Instrumentation with ``__attribute__((no_sanitize("address")))`` 196*67e74705SXin Li-------------------------------------------------------------------------- 197*67e74705SXin Li 198*67e74705SXin LiSome code should not be instrumented by AddressSanitizer. One may use the 199*67e74705SXin Lifunction attribute ``__attribute__((no_sanitize("address")))`` (which has 200*67e74705SXin Lideprecated synonyms `no_sanitize_address` and `no_address_safety_analysis`) to 201*67e74705SXin Lidisable instrumentation of a particular function. This attribute may not be 202*67e74705SXin Lisupported by other compilers, so we suggest to use it together with 203*67e74705SXin Li``__has_feature(address_sanitizer)``. 204*67e74705SXin Li 205*67e74705SXin LiSuppressing Errors in Recompiled Code (Blacklist) 206*67e74705SXin Li------------------------------------------------- 207*67e74705SXin Li 208*67e74705SXin LiAddressSanitizer supports ``src`` and ``fun`` entity types in 209*67e74705SXin Li:doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports 210*67e74705SXin Liin the specified source files or functions. Additionally, AddressSanitizer 211*67e74705SXin Liintroduces ``global`` and ``type`` entity types that can be used to 212*67e74705SXin Lisuppress error reports for out-of-bound access to globals with certain 213*67e74705SXin Linames and types (you may only specify class or struct types). 214*67e74705SXin Li 215*67e74705SXin LiYou may use an ``init`` category to suppress reports about initialization-order 216*67e74705SXin Liproblems happening in certain source files or with certain global variables. 217*67e74705SXin Li 218*67e74705SXin Li.. code-block:: bash 219*67e74705SXin Li 220*67e74705SXin Li # Suppress error reports for code in a file or in a function: 221*67e74705SXin Li src:bad_file.cpp 222*67e74705SXin Li # Ignore all functions with names containing MyFooBar: 223*67e74705SXin Li fun:*MyFooBar* 224*67e74705SXin Li # Disable out-of-bound checks for global: 225*67e74705SXin Li global:bad_array 226*67e74705SXin Li # Disable out-of-bound checks for global instances of a given class ... 227*67e74705SXin Li type:Namespace::BadClassName 228*67e74705SXin Li # ... or a given struct. Use wildcard to deal with anonymous namespace. 229*67e74705SXin Li type:Namespace2::*::BadStructName 230*67e74705SXin Li # Disable initialization-order checks for globals: 231*67e74705SXin Li global:bad_init_global=init 232*67e74705SXin Li type:*BadInitClassSubstring*=init 233*67e74705SXin Li src:bad/init/files/*=init 234*67e74705SXin Li 235*67e74705SXin LiSuppressing memory leaks 236*67e74705SXin Li------------------------ 237*67e74705SXin Li 238*67e74705SXin LiMemory leak reports produced by :doc:`LeakSanitizer` (if it is run as a part 239*67e74705SXin Liof AddressSanitizer) can be suppressed by a separate file passed as 240*67e74705SXin Li 241*67e74705SXin Li.. code-block:: bash 242*67e74705SXin Li 243*67e74705SXin Li LSAN_OPTIONS=suppressions=MyLSan.supp 244*67e74705SXin Li 245*67e74705SXin Liwhich contains lines of the form `leak:<pattern>`. Memory leak will be 246*67e74705SXin Lisuppressed if pattern matches any function name, source file name, or 247*67e74705SXin Lilibrary name in the symbolized stack trace of the leak report. See 248*67e74705SXin Li`full documentation 249*67e74705SXin Li<https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer#suppressions>`_ 250*67e74705SXin Lifor more details. 251*67e74705SXin Li 252*67e74705SXin LiLimitations 253*67e74705SXin Li=========== 254*67e74705SXin Li 255*67e74705SXin Li* AddressSanitizer uses more real memory than a native run. Exact overhead 256*67e74705SXin Li depends on the allocations sizes. The smaller the allocations you make the 257*67e74705SXin Li bigger the overhead is. 258*67e74705SXin Li* AddressSanitizer uses more stack memory. We have seen up to 3x increase. 259*67e74705SXin Li* On 64-bit platforms AddressSanitizer maps (but not reserves) 16+ Terabytes of 260*67e74705SXin Li virtual address space. This means that tools like ``ulimit`` may not work as 261*67e74705SXin Li usually expected. 262*67e74705SXin Li* Static linking is not supported. 263*67e74705SXin Li 264*67e74705SXin LiSupported Platforms 265*67e74705SXin Li=================== 266*67e74705SXin Li 267*67e74705SXin LiAddressSanitizer is supported on: 268*67e74705SXin Li 269*67e74705SXin Li* Linux i386/x86\_64 (tested on Ubuntu 12.04) 270*67e74705SXin Li* OS X 10.7 - 10.11 (i386/x86\_64) 271*67e74705SXin Li* iOS Simulator 272*67e74705SXin Li* Android ARM 273*67e74705SXin Li* FreeBSD i386/x86\_64 (tested on FreeBSD 11-current) 274*67e74705SXin Li 275*67e74705SXin LiPorts to various other platforms are in progress. 276*67e74705SXin Li 277*67e74705SXin LiCurrent Status 278*67e74705SXin Li============== 279*67e74705SXin Li 280*67e74705SXin LiAddressSanitizer is fully functional on supported platforms starting from LLVM 281*67e74705SXin Li3.1. The test suite is integrated into CMake build and can be run with ``make 282*67e74705SXin Licheck-asan`` command. 283*67e74705SXin Li 284*67e74705SXin LiMore Information 285*67e74705SXin Li================ 286*67e74705SXin Li 287*67e74705SXin Li`<https://github.com/google/sanitizers/wiki/AddressSanitizer>`_ 288