Bug 1869043 add a main thread record of track audio outputs r=padenot
[gecko.git] / tools / sanitizer / docs / asan.rst
blob876c1e83f49b33ad5685966cb1ec8f928d8d718b
1 Address Sanitizer
2 =================
4 What is Address Sanitizer?
5 --------------------------
7 Address Sanitizer (ASan) is a fast memory error detector that detects
8 use-after-free and out-of-bound bugs in C/C++ programs. It uses a
9 compile-time instrumentation to check all reads and writes during the
10 execution. In addition, the runtime part replaces the ``malloc`` and
11 ``free`` functions to check dynamically allocated memory. More
12 information on how ASan works can be found on `the Address Sanitizer
13 wiki <https://github.com/google/sanitizers/wiki/AddressSanitizer>`__.
15 A `meta bug called asan-maintenance <https://bugzilla.mozilla.org/show_bug.cgi?id=asan-maintenance>`__
16 is maintained to keep track of all the bugs found with ASan.
18 Downloading artifact builds
19 ---------------------------
21 For Linux and Windows users, the easiest way to get Firefox builds with
22 Address Sanitizer is to download a continuous integration asan build of
23 mozilla-central (updated at least daily):
25 -  mozilla-central optimized builds:
26    `linux <https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.firefox.linux64-asan-opt/artifacts/public/build/target.tar.bz2>`__
27    \|
28    `windows <https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.firefox.win64-asan-opt/artifacts/public/build/target.zip>`__
29    (recommended for testing)
30 -  mozilla-central debug builds:
31    `linux <https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.firefox.linux64-asan-debug/artifacts/public/build/target.tar.bz2>`__
32    \|
33    `windows <https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.firefox.win64-asan-debug/artifacts/public/build/target.zip>`__
34    (recommended for debugging if the optimized builds don't do the job)
36 The fuzzing team also offers a tool called ``fuzzfetch`` to download these and many
37 other CI builds. It makes downloading and unpacking these builds much easier and
38 can be used not just for fuzzing but for all purposes that require a CI build download.
40 You can install ``fuzzfetch`` from
41 `Github <https://github.com/MozillaSecurity/fuzzfetch>`__ or
42 `via pip <https://pypi.org/project/fuzzfetch/>`__.
44 Afterwards, you can run e.g.
48    $ python -m fuzzfetch --asan -n firefox-asan
50 to get the optimized Linux ASan build mentioned above unpacked into a directory called ``firefox-asan``.
51 The ``--debug`` and ``--os`` switches can be used to get the other variants listed above.
53 Creating Try builds
54 -------------------
56 If for some reason you can't use the pre-built binaries mentioned in the
57 previous section (e.g. you want a non-Linux build or you need to test a
58 patch), you can either build Firefox yourself (see the following
59 section) or use the :ref:`try server <Pushing to Try>` to
60 create the customized build for you. Pushing to try requires L1 commit
61 access. If you don't have this access yet you can request access (see
62 `Becoming A Mozilla
63 Committer <https://www.mozilla.org/about/governance/policies/commit/>`__
64 and `Mozilla Commit Access
65 Policy <https://www.mozilla.org/about/governance/policies/commit/access-policy/>`__
66 for the requirements).
68 The tree contains `several mozconfig files for creating asan
69 builds <https://searchfox.org/mozilla-central/search?q=&case=true&path=browser%2Fconfig%2Fmozconfigs%2F*%2F*asan*>`__
70 (the "nightly-asan" files create release builds, whereas the
71 "debug-asan" files create debug+opt builds). For Linux builds, the
72 appropriate configuration file is used by the ``linux64-asan`` target.
73 If you want to create a macOS or Windows build, you'll need to copy the
74 appropriate configuration file over the regular debug configuration
75 before pushing to try. For example:
79    cp browser/config/mozconfigs/macosx64/debug-asan browser/config/mozconfigs/macosx64/debug
81 You can then `push to Try in the usual
82 way </tools/try/index.html#using-try>`__
83 and, once the build is complete, download the appropriate build
84 artifact.
86 Creating local builds on Windows
87 --------------------------------
89 On Windows, ASan is supported only in 64-bit builds.
91 Run ``mach bootstrap`` to get an updated clang-cl in your
92 ``~/.mozbuild`` directory, then use the following
93 :ref:`mozconfig <Configuring Build Options>`:
97    ac_add_options --enable-address-sanitizer
98    ac_add_options --disable-jemalloc
100    export LDFLAGS="clang_rt.asan_dynamic-x86_64.lib clang_rt.asan_dynamic_runtime_thunk-x86_64.lib"
101    CLANG_LIB_DIR="$(cd ~/.mozbuild/clang/lib/clang/*/lib/windows && pwd)"
102    export MOZ_CLANG_RT_ASAN_LIB_PATH="${CLANG_LIB_DIR}/clang_rt.asan_dynamic-x86_64.dll"
103    export PATH=$CLANG_LIB_DIR:$PATH
105 If you launch an ASan build under WinDbg, you may see spurious
106 first-chance Access Violation exceptions. These come from ASan creating
107 shadow memory pages on demand, and can be ignored. Run ``sxi av`` to
108 ignore these exceptions. (You will still catch second-chance Access
109 Violation exceptions if you actually crash.)
111 LeakSanitizer (LSan) is not supported on Windows.
113 Creating local builds on Linux or Mac
114 -------------------------------------
116 Build prerequisites
117 ~~~~~~~~~~~~~~~~~~~
119 LLVM/Clang
120 ^^^^^^^^^^
122 The ASan instrumentation is implemented as an LLVM pass and integrated
123 into Clang. Any clang version that is capable of compiling Firefox has
124 everything needed to do an ASAN build.
126 Building Firefox
127 ~~~~~~~~~~~~~~~~
129 Getting the source
130 ^^^^^^^^^^^^^^^^^^
132 Using that or any later revision, all you need to do is to :ref:`get yourself
133 a clone of mozilla-central <Mercurial overview>`.
135 Adjusting the build configuration
136 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138 Create the build configuration file ``mozconfig`` with the following
139 content in your mozilla-central directory:
143    # Combined .mozconfig file for ASan on Linux+Mac
145    mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/objdir-ff-asan
147    # Enable ASan specific code and build workarounds
148    ac_add_options --enable-address-sanitizer
150    # These three are required by ASan
151    ac_add_options --disable-jemalloc
152    ac_add_options --disable-crashreporter
153    ac_add_options --disable-elf-hack
155    # Keep symbols to symbolize ASan traces later
156    export MOZ_DEBUG_SYMBOLS=1
157    ac_add_options --enable-debug-symbols
158    ac_add_options --disable-install-strip
160    # Settings for an opt build (preferred)
161    # The -gline-tables-only ensures that all the necessary debug information for ASan
162    # is present, but the rest is stripped so the resulting binaries are smaller.
163    ac_add_options --enable-optimize="-O2 -gline-tables-only"
164    ac_add_options --disable-debug
166    # Settings for a debug+opt build
167    #ac_add_options --enable-optimize
168    #ac_add_options --enable-debug
170    # MacOSX only: Uncomment and adjust this path to match your SDK
171    # ac_add_options --with-macos-sdk=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
173 You may also need this, as seen in
174 ``browser/config/mozconfigs/linux64/nightly-asan`` (the config file used
175 for Address Sanitizer builds used for automated testing):
179    # ASan specific options on Linux
180    ac_add_options --enable-valgrind
182 Starting the build process
183 ^^^^^^^^^^^^^^^^^^^^^^^^^^
185 Now you start the build process using the regular ``./mach build``
186 command.
188 Starting Firefox
189 ^^^^^^^^^^^^^^^^
191 After the build has completed, ``./mach run`` with the usual options for
192 running in a debugger (``gdb``, ``lldb``, ``rr``, etc.) work fine, as do
193 the ``--disable-e10s`` and other options.
195 Building only the JavaScript shell
196 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
198 If you want to build only the JavaScript shell instead of doing a full
199 Firefox build, the build script below will probably help you to do so.
200 Execute this script in the ``js/src/`` subdirectory and pass a directory
201 name as the first parameter. The build will then be created in a new
202 subdirectory with that name.
206    #! /bin/sh
208    if [ -z $1 ] ; then
209         echo "usage: $0 <dirname>"
210    elif [ -d $1 ] ; then
211         echo "directory $1 already exists"
212    else
213         autoconf2.13
214         mkdir $1
215         cd $1
216         CC="clang" \
217         CXX="clang++" \
218         CFLAGS="-fsanitize=address" \
219         CXXFLAGS="-fsanitize=address" \
220         LDFLAGS="-fsanitize=address" \
221         ../configure --enable-debug --enable-optimize --enable-address-sanitizer --disable-jemalloc
222    fi
224 Getting Symbols in Address Sanitizer Traces
225 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227 By default, ASan traces are unsymbolized and only print the
228 binary/library and a memory offset instead. In order to get more useful
229 traces, containing symbols, there are two approaches.
231 Using the LLVM Symbolizer (recommended)
232 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
234 LLVM ships with a symbolizer binary that ASan will readily use to
235 immediately output symbolized traces. To use it, just set the
236 environment variable ``ASAN_SYMBOLIZER_PATH`` to reflect the location of
237 your ``llvm-symbolizer`` binary, before running the process. This
238 program is usually included in an LLVM distribution. Stacks without
239 symbols can also be post-processed, see below.
241 .. warning::
243    .. note::
245       **Warning:** On OS X, the content sandbox prevents the symbolizer
246       from running. To use llvm-symbolizer on ASan output from a
247       content process, the content sandbox must be disabled. This can be
248       done by setting ``MOZ_DISABLE_CONTENT_SANDBOX=1`` in your run
249       environment. Setting this in .mozconfig has no effect.
252 Post-Processing Traces with asan_symbolize.py
253 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
255 Instead of using the llvm-symbolizer binary, you can also pipe the
256 output through the ``asan_symbolize.py`` script, shipped with LLVM
257 (``$LLVM_HOME/projects/compiler-rt/lib/asan/scripts/asan_symbolize.py``),
258 often included in LLVM distributions. The disadvantage is that the
259 script will need to use ``addr2line`` to get the symbols, which means
260 that every library will have to be loaded into memory
261 (including``libxul``, which takes a bit).
263 However, in certain situations it makes sense to use this script. For
264 example, if you have/received an unsymbolized trace, then you can still
265 use the script to turn it into a symbolized trace, given that you can
266 get the original binaries that produced the unsymbolized trace. In order
267 for the script to work in such cases, you need to ensure that the paths
268 in the trace point to the actual binaries, or change the paths
269 accordingly.
271 Since the output of the ``asan_symbolize.py`` script is still mangled,
272 you might want to pipe the output also through ``c++filt`` afterwards.
274 Troubleshooting / Known problems
275 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277 Cannot specify -o when generating multiple output files
278 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
280 If you get the error
281 "``cannot specify -o when generating multiple output files"`` from
282 clang, disable ``elf-hack`` in your ``mozconfig`` to work around the
283 issue:
287    ac_add_options --disable-elf-hack
289 Optimized build
290 ^^^^^^^^^^^^^^^
292 Since `an issue with -O2/-Os and
293 ASan <https://github.com/google/sanitizers/issues/20>`__
294 has been resolved, the regular optimizations used by Firefox should work
295 without any problems. The optimized build has only a barely noticeable
296 speed penalty and seems to be even faster than regular debug builds.
298 No "AddressSanitizer: **libc** interceptors initialized" shows after running ./mach run
299 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
303    $ ASAN_OPTIONS=verbosity=2 ./mach run
305 Use the above command instead
307 "An admin user name and password" is required to enter Developer Mode
308 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
310 Please enable **Developer** **mode** by:
314    $ /usr/sbin/DevToolsSecurity -enable
315    Developer mode is now enabled.
317 Debugging issues that ASan finds
318 --------------------------------
320 When ASan discovers an issue it will simply print an error message and
321 exit the app. To stop the app in a debugger before ASan exits it, set a
322 breakpoint on ``__asan::ReportGenericError``. For more info on using
323 ASan and debugging issues that it uncovers, see the page `Address
324 sanitizer and a
325 debugger <https://github.com/google/sanitizers/wiki/AddressSanitizerAndDebugger>`__
326 page on the upstream wiki.
328 ``__asan_describe_address(pointer)`` issued at the debugger prompt or
329 even directly in the code allows outputting lots of information about
330 this memory address (thread and stack of allocation, of deallocation,
331 whether or not it is a bit outside a known buffer, thread and stack of
332 allocation of this buffer, etc.). This can be useful to understand where
333 some buffer that is not aligned was allocated, when doing SIMD work, for
334 example.
336 `rr <https://rr-project.org/>`__ (Linux x86 only) works great with ASan
337 and combined, this combo allows doing some very powerful debugging
338 strategies.
340 LeakSanitizer
341 -------------
343 LeakSanitizer (LSan) is a special execution mode for regular ASan. It
344 takes advantage of how ASan tracks the set of live blocks at any given
345 point to print out the allocation stack of any block that is still alive
346 at shutdown, but is not reachable from the stack, according to a
347 conservative scan. This is very useful for detecting leaks of things
348 such as ``char*`` that do not participate in the usual Gecko shutdown
349 leak detection. LSan is supported on x86_64 Linux and OS X.
351 LSan is enabled by default in ASan builds, as of more recent versions of
352 Clang. To make an ASan build not run LSan, set the environment variable
353 ``ASAN_OPTIONS`` to ``detect_leaks=0`` (or add it as an entry to a
354 ``:``-separated list if it is already set to something). If you want to
355 enable it when it is not for some reason, set it to 1 instead of 0. If
356 LSan is enabled and you are using a non-debug build, you will also want
357 to set the environment variable ``MOZ_CC_RUN_DURING_SHUTDOWN=1``, to
358 ensure that we run shutdown GCs and CCs to avoid spurious leaks.
360 If an object that is reported by LSan is intentionally never freed, a
361 symbol can be added to ``build/sanitizers/lsan_suppressions.txt`` to get
362 LSan to ignore it.
364 For some more information on LSan, see the `Leak Sanitizer wiki
365 page <https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer>`__.
368 A `meta bug called lsan <https://bugzilla.mozilla.org/show_bug.cgi?id=lsan>`__
369 is maintained to keep track of all the bugs found with LSan.
373 Frequently Asked Questions about ASan
374 -------------------------------------
376 How does ASan work exactly?
377 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
379 More information on how ASan works can be found on `the Address Sanitizer wiki <https://github.com/google/sanitizers/wiki/AddressSanitizer>`__.