blob: 0e0e1a570a89b3e6d16a85ad55417a3c8dcffb06 [file] [log] [blame] [view]
Staphany Park384b99a2019-12-18 03:23:341# AddressSanitizer (ASan)
2
3[AddressSanitizer](https://github.com/google/sanitizers) (ASan) is a fast memory
4error detector based on compiler instrumentation (LLVM). It is fully usable for
Nico Weber3b6c2552020-09-09 19:49:455Chrome on Android, Chrome OS, iOS simulator, Linux, Mac, and 64-bit Windows.
6Additional info on the tool itself is available at
Darwin Huang7d3b5f052019-12-23 19:25:527https://clang.llvm.org/docs/AddressSanitizer.html.
Staphany Park384b99a2019-12-18 03:23:348
9For the memory leak detector built into ASan, see
10[LeakSanitizer](https://sites.google.com/a/chromium.org/dev/developers/testing/leaksanitizer).
11If you want to debug memory leaks, please refer to the instructions on that page
12instead.
13
14## Buildbots and trybots
15
16The [Chromium Memory
Nico Weber3b6c2552020-09-09 19:49:4517waterfall](https://ci.chromium.org/p/chromium/g/chromium.memory/console)
18contains buildbots running Chromium tests under ASan on Linux (Linux ASan/LSan
19bots for the regular Linux build, Linux Chromium OS ASan for the chromeos=1
20build running on Linux), macOS, Chromium OS. Linux and Linux Chromium OS bots
21run with --no-sandbox, but there's an extra Linux bot that enables the sandbox
22(but disables LeakSanitizer).
Staphany Park384b99a2019-12-18 03:23:3423
Darwin Huang7d3b5f052019-12-23 19:25:5224The trybots running Chromium tests on Linux and macOS are:
Nico Weber3b6c2552020-09-09 19:49:4525- linux\_asan (everything except browser\_tests and content\_browsertests)
26- linux\_browser\_asan (browser\_tests and content\_browsertests),
27- mac\_asan (many tests including browser\_tests and content\_browsertests)
28- linux\_chromeos\_asan (the chromeos=1 build running on a Linux machine, many
29tests including browser\_tests and content\_browsertests).
Staphany Park384b99a2019-12-18 03:23:3430
31## Pre-built Chrome binaries
32
33You can grab fresh Chrome binaries built with ASan
34[here](https://commondatastorage.googleapis.com/chromium-browser-asan/index.html).
Chris Thompson4c93a6e2020-12-17 02:20:5135The lists of ASan binaries are _very_ long, but you can filter down to more
36specific releases by specifying a prefix like
37[linux-debug/asan-linux-debug-83](https://commondatastorage.googleapis.com/chromium-browser-asan/index.html?prefix=linux-debug/asan-linux-debug-83).
38This is useful for finding a build for a specific revision, since filenames are of
39the form `asan-<platform>-<buildtype>-<revision>` (but not every revision has an
40archived ASan build).
Staphany Park384b99a2019-12-18 03:23:3441
42## Build tests with ASan
43
Nico Weber3b6c2552020-09-09 19:49:4544Building with ASan is easy. Start by compiling `base_unittests` to verify the
45build is working for you (see below). Then, you can compile `chrome`,
46`browser_tests`, etc.. Make sure to compile release builds.
Staphany Park384b99a2019-12-18 03:23:3447
48### Configuring the build
49
50Create an asan build directory by running:
Darwin Huang7d3b5f052019-12-23 19:25:5251```shell
Staphany Park384b99a2019-12-18 03:23:3452gn args out/asan
53```
54
55Enter the following build variables in the editor that will pop up:
Darwin Huang7d3b5f052019-12-23 19:25:5256```python
Staphany Park384b99a2019-12-18 03:23:3457is_asan = true
58is_debug = false # Release build.
59```
60
61Build with:
Darwin Huang7d3b5f052019-12-23 19:25:5262```shell
Staphany Park384b99a2019-12-18 03:23:3463ninja -C out/asan base_unittests
64```
65
66### Goma build
67
Nico Weber3b6c2552020-09-09 19:49:4568ASan builds should work seamlessly with Goma; just add `use_goma=true` in your
69"gn args" Don't forget to use `ninja -j <jobs>` to take advantage of goma.
Staphany Park384b99a2019-12-18 03:23:3470
71### Build options
72
73If you want your stack traces to be precise, you will have to disable inlining
74by setting the GN arg:
Darwin Huang7d3b5f052019-12-23 19:25:5275```shell
Staphany Park384b99a2019-12-18 03:23:3476enable_full_stack_frames_for_profiling = true
77```
78
79Note that this incurs a significant performance hit. Please do not do this on
80buildbots.
81
82If you're working on reproducing ClusterFuzz reports, you might want to add:
Darwin Huang7d3b5f052019-12-23 19:25:5283```shell
Staphany Park384b99a2019-12-18 03:23:3484v8_enable_verify_heap = true
85```
Darwin Huang7d3b5f052019-12-23 19:25:5286in order to enable the `--verify-heap` command line flag for v8 in Release builds.
Staphany Park384b99a2019-12-18 03:23:3487
88## Verify the ASan tool works
89
90**ATTENTION (Linux only)**: These instructions are for running ASan in a way
91that is compatible with the sandbox. However, this is not compatible with
92LeakSanitizer. If you want to debug memory leaks, please use the instructions on
93the
94[LeakSanitizer](https://sites.google.com/a/chromium.org/dev/developers/testing/leaksanitizer)
95page instead.
96
97Now, check that the tool works. Run the following:
Darwin Huang7d3b5f052019-12-23 19:25:5298```shell
99out/asan/base_unittests \
100 --gtest_filter=ToolsSanityTest.DISABLED_AddressSanitizerLocalOOBCrashTest \
Amy Huangaaa8dcb2021-03-16 18:54:34101 --gtest_also_run_disabled_tests
Staphany Park384b99a2019-12-18 03:23:34102```
103
104The test will crash with the following error report:
Darwin Huang7d3b5f052019-12-23 19:25:52105```shell
106==26552== ERROR: AddressSanitizer stack-buffer-overflow on address \
1070x7fff338adb14 at pc 0xac20a7 bp 0x7fff338adad0 sp 0x7fff338adac8
Staphany Park384b99a2019-12-18 03:23:34108WRITE of size 4 at 0x7fff338adb14 thread T0
109 #0 0xac20a7 in base::ToolsSanityTest_DISABLED_AddressSanitizerLocalOOBCrashTest_Test::TestBody() ???:0
110 #1 0xcddbd6 in testing::Test::Run() testing/gtest/src/gtest.cc:2161
111 #2 0xcdf63b in testing::TestInfo::Run() testing/gtest/src/gtest.cc:2338
112... lots more stuff
Darwin Huang7d3b5f052019-12-23 19:25:52113Address 0x7fff338adb14 is located at offset 52 in frame \
114base::ToolsSanityTest_DISABLED_AddressSanitizerLocalOOBCrashTest_Test::TestBody()> of T0's stack:
Staphany Park384b99a2019-12-18 03:23:34115 This frame has 2 object(s):
116 [32, 52) 'array'
117 [96, 104) 'access'
118==26552== ABORTING
119... lots more stuff
120```
121
Samuel Huange9a7bff9d2020-03-04 16:16:03122Congrats, you have a working ASan build! &#x1F64C;
Staphany Park384b99a2019-12-18 03:23:34123
124## Run chrome under ASan
125
Darwin Huang7d3b5f052019-12-23 19:25:52126And finally, have fun with the `out/Release/chrome` binary. The filter script
Amy Huangaaa8dcb2021-03-16 18:54:34127`tools/valgrind/asan/asan_symbolize.py` can be used to symbolize the output,
128although it shouldn't be necessary on Linux and Windows, where Chrome uses the
129llvm-symbolizer in its source tree by default.
Staphany Park384b99a2019-12-18 03:23:34130
131ASan should perfectly work with Chrome's sandbox. You should only need to run
132with `--no-sandbox` on Linux if you're debugging ASan.
133Note: you have to disable the sandbox on Windows until it is supported.
134
135You may need to run with `--disable-gpu` on Linux with NVIDIA driver older than
136295.20.
137
138You will likely need to define environment variable
139[`G_SLICE=always-malloc`](https://developer.gnome.org/glib/unstable/glib-running.html)
140to avoid crashes inside gtk.
Nico Weber3b6c2552020-09-09 19:49:45141`NSS_DISABLE_ARENA_FREE_LIST=1` and `NSS_DISABLE_UNLOAD=1` are required as well.
Staphany Park384b99a2019-12-18 03:23:34142
143When filing a bug found by AddressSanitizer, please add a label
144`Stability-AddressSanitizer`.
145
146## ASan runtime options
147
148ASan's behavior can be changed by exporting the `ASAN_OPTIONS` env var. Some of
149the useful options are listed on this page, others can be obtained from running
150an ASanified binary with `ASAN_OPTIONS=help=1`. Note that Chromium sets its own
151defaults for some options, so the default behavior may be different from that
152observed in other projects.
153See `base/debug/sanitizer_options.cc` for more details.
154
155## NaCl support under ASan
156
Nico Weber3b6c2552020-09-09 19:49:45157On Linux (and soon on macOS) you can build and run Chromium with NaCl under ASan.
Staphany Park384b99a2019-12-18 03:23:34158Untrusted code (nexe) itself is not instrumented with ASan in this mode, but
159everything else is.
160
Nico Weber3b6c2552020-09-09 19:49:45161To do this, remove `enable_nacl=false` from your `args.gn`, and define
Staphany Park384b99a2019-12-18 03:23:34162`NACL_DANGEROUS_SKIP_QUALIFICATION_TEST=1` in your environment at run time.
163
164Pipe chromium output (stderr) through ``tools/valgrind/asan/asan_symbolize.py
165`pwd`/`` to get function names and line numbers in ASan reports.
Darwin Huang7d3b5f052019-12-23 19:25:52166If you're seeing crashes within `nacl_helper_bootstrap`, try deleting
167`out/Release/nacl_helper`.
Staphany Park384b99a2019-12-18 03:23:34168
169## Building on iOS
170
171It's possible to build and run Chrome tests for iOS simulator (which are x86
172binaries essentially) under ASan. Note that you'll need a Chrome iOS checkout
173for that. It isn't currently possible to build iOS binaries targeting ARM.
174
175Configure your build with `is_asan = true` as described above. Replace your
176build directory as needed:
Darwin Huang7d3b5f052019-12-23 19:25:52177```shell
Staphany Park384b99a2019-12-18 03:23:34178ninja -C out/Release-iphonesimulator base_unittests
Darwin Huang7d3b5f052019-12-23 19:25:52179out/Release-iphonesimulator/iossim -d "iPhone" -s 7.0 \
180 out/Release-iphonesimulator/base_unittests.app/ \
181 --gtest_filter=ToolsSanityTest.DISABLED_AddressSanitizerLocalOOBCrashTest \
182 --gtest_also_run_disabled_tests 2>&1 |
Staphany Park384b99a2019-12-18 03:23:34183tools/valgrind/asan/asan_symbolize.py
184```
185
186You'll see the same report as shown above (see the "Verify the ASan tool works"
187section), with a number of iOS-specific frames.
188
189## Building on Android
190
191Follow [AndroidBuildInstructions](android_build_instructions.md) with minor
192changes:
193
Darwin Huang7d3b5f052019-12-23 19:25:52194```python
195target_os="android"
Darwin Huang7d3b5f052019-12-23 19:25:52196is_asan=true
197is_debug=false
198```
Staphany Park384b99a2019-12-18 03:23:34199
200Running ASan applications on Android requires additional device setup. Chromium
201testing scripts take care of this, so testing works as expected:
Darwin Huang7d3b5f052019-12-23 19:25:52202```shell
203build/android/test_runner.py instrumentation --test-apk ContentShellTest \
204 --test_data content:content/test/data/android/device_files -v -v -v \
205 --tool=asan --release
Staphany Park384b99a2019-12-18 03:23:34206```
207
208To run stuff without Chromium testing script (ex. ContentShell.apk, or any third
209party apk or binary), device setup is needed:
Darwin Huang7d3b5f052019-12-23 19:25:52210```shell
Samuel Huange9a7bff9d2020-03-04 16:16:03211tools/android/asan/third_party/asan_device_setup.sh \
212 --lib third_party/llvm-build/Release+Asserts/lib/clang/*/lib/linux/libclang_rt.asan-arm-android.so
Staphany Park384b99a2019-12-18 03:23:34213# wait a few seconds for the device to reload
214```
215
216It only needs to be run once per device. It is safe to run it multiple times.
Samuel Huange9a7bff9d2020-03-04 16:16:03217Examine the output to ensure that setup was successful (you may need to run
218`adb disable-verity` and restart the device first). When this is done, the
219device will run ASan apks as well as normal apks without any further setup.
Staphany Park384b99a2019-12-18 03:23:34220
221To run command-line tools (i.e. binaries), prefix them with `asanwrapper`:
Darwin Huang7d3b5f052019-12-23 19:25:52222```shell
Staphany Park384b99a2019-12-18 03:23:34223adb shell /system/bin/asanwrapper /path/to/binary
224```
225
Darwin Huang7d3b5f052019-12-23 19:25:52226Use `build/android/asan_symbolize.py` to symbolize stack from `adb logcat`. It
227needs the `--output-directory` argument and takes care of translating the device
228path to the unstripped binary in the output directory.
Staphany Park384b99a2019-12-18 03:23:34229
Nico Weber3b6c2552020-09-09 19:49:45230## Building with v8\_target\_arch="arm"
Staphany Park384b99a2019-12-18 03:23:34231
232This is needed to detect addressability bugs in the ARM code emitted by V8 and
233running on an instrumented ARM emulator in a 32-bit x86 Linux Chromium. **You
234probably don't want this, and these instructions have bitrotted because they
235still reference GYP. If you do this successfully, please update!** See
Darwin Huang7d3b5f052019-12-23 19:25:52236https://crbug.com/324207 for some context.
Staphany Park384b99a2019-12-18 03:23:34237
238First, you need to install the 32-bit chroot environment using the
239`build/install-chroot.sh` script (as described in
240https://code.google.com/p/chromium/wiki/LinuxBuild32On64). Second, install the
241build deps:
Darwin Huang7d3b5f052019-12-23 19:25:52242```shell
243precise32 build/install-build-deps.sh \
244 # assuming your schroot wrapper is called 'precise32'
Staphany Park384b99a2019-12-18 03:23:34245```
246
247You'll need to make two symlinks to avoid linking errors:
Darwin Huang7d3b5f052019-12-23 19:25:52248```shell
249sudo ln -s $CHROOT/usr/lib/i386-linux-gnu/libc_nonshared.a \
250 /usr/lib/i386-linux-gnu/libc_nonshared.a
251sudo ln -s $CHROOT/usr/lib/i386-linux-gnu/libpthread_nonshared.a \
252 /usr/lib/i386-linux-gnu/libpthread_nonshared.a
Staphany Park384b99a2019-12-18 03:23:34253```
254
255Now configure and build your Chrome:
Darwin Huang7d3b5f052019-12-23 19:25:52256```shell
257GYP_GENERATOR_FLAGS="output_dir=out_asan_chroot" GYP_DEFINES="asan=1 \
258 disable_nacl=1 v8_target_arch=arm sysroot=/var/lib/chroot/precise32bit/ \
259 chroot_cmd=precise32 host_arch=x86_64 target_arch=ia32" gclient runhooks
Staphany Park384b99a2019-12-18 03:23:34260ninja -C out_asan_chroot/Release chrome
261```
262
263**Note**: `disable_nacl=1` is needed for now.
264
265## AsanCoverage
266
267AsanCoverage is a minimalistic code coverage implementation built into ASan. For
268general information see
269[https://code.google.com/p/address-sanitizer/wiki/AsanCoverage](https://github.com/google/sanitizers)
270To use AsanCoverage in Chromium, add `use_sanitizer_coverage = true` to your GN
271args. See also the `sanitizer_coverage_flags` variable for configuring it.
272
273Chrome must be terminated gracefully in order for coverage to work. Either close
274the browser, or SIGTERM the browser process. Do not do `killall chrome` or send
275SIGKILL.
Darwin Huang7d3b5f052019-12-23 19:25:52276```shell
277kill <browser_process_pid>
278ls
Staphany Park384b99a2019-12-18 03:23:34279...
280chrome.22575.sancov
281gpu.6916123572022919124.sancov.packed
282zygote.13651804083035800069.sancov.packed
283...
284```
285
286The `gpu.*.sancov.packed` file contains coverage data for the GPU process,
287whereas the `zygote.*.sancov.packed` file contains coverage data for the
288renderers (but not the zygote process). Unpack them to regular `.sancov` files
289like so:
Darwin Huang7d3b5f052019-12-23 19:25:52290```shell
291$ $LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py unpack \
292 *.sancov.packed
Staphany Park384b99a2019-12-18 03:23:34293sancov.py: unpacking gpu.6916123572022919124.sancov.packed
294sancov.py: extracting chrome.22610.sancov
295sancov.py: unpacking zygote.13651804083035800069.sancov.packed
296sancov.py: extracting libpdf.so.12.sancov
297sancov.py: extracting chrome.12.sancov
298sancov.py: extracting libpdf.so.10.sancov
299sancov.py: extracting chrome.10.sancov
300```
301
302Now, e.g., to list the offsets of covered functions in the libpdf.so binary in
303renderer with pid 10:
Darwin Huang7d3b5f052019-12-23 19:25:52304```shell
305$ $LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py print \
306 libpdf.so.10.sancov
Staphany Park384b99a2019-12-18 03:23:34307```