blob: 3e7c7a6b0d57427301eee68b1a7ed560b9033368 [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:
Lei Zhang2804210e2023-07-20 01:25:4225- linux\_chromium\_asan\_rel\_ng
26- mac\_chromium\_asan\_rel\_ng
27- linux\_chromium\_chromeos\_asan\_rel\_ng (the chromeos=1 build running on a
28Linux machine)
Staphany Park384b99a2019-12-18 03:23:3429
30## Pre-built Chrome binaries
31
32You can grab fresh Chrome binaries built with ASan
33[here](https://commondatastorage.googleapis.com/chromium-browser-asan/index.html).
Chris Thompson4c93a6e2020-12-17 02:20:5134The lists of ASan binaries are _very_ long, but you can filter down to more
35specific releases by specifying a prefix like
36[linux-debug/asan-linux-debug-83](https://commondatastorage.googleapis.com/chromium-browser-asan/index.html?prefix=linux-debug/asan-linux-debug-83).
37This is useful for finding a build for a specific revision, since filenames are of
38the form `asan-<platform>-<buildtype>-<revision>` (but not every revision has an
Chris Bookholtd30029202022-09-08 22:39:3139archived ASan build). The
40[get_asan_chrome](https://source.chromium.org/chromium/chromium/src/+/main:tools/get_asan_chrome/get_asan_chrome.py)
41helper script is a handy way to download builds; its --help flag provides
42usage instructions.
Staphany Park384b99a2019-12-18 03:23:3443
44## Build tests with ASan
45
Nico Weber3b6c2552020-09-09 19:49:4546Building with ASan is easy. Start by compiling `base_unittests` to verify the
47build is working for you (see below). Then, you can compile `chrome`,
48`browser_tests`, etc.. Make sure to compile release builds.
Staphany Park384b99a2019-12-18 03:23:3449
50### Configuring the build
51
52Create an asan build directory by running:
Darwin Huang7d3b5f052019-12-23 19:25:5253```shell
Staphany Park384b99a2019-12-18 03:23:3454gn args out/asan
55```
56
57Enter the following build variables in the editor that will pop up:
Darwin Huang7d3b5f052019-12-23 19:25:5258```python
Staphany Park384b99a2019-12-18 03:23:3459is_asan = true
60is_debug = false # Release build.
61```
62
63Build with:
Darwin Huang7d3b5f052019-12-23 19:25:5264```shell
Staphany Park384b99a2019-12-18 03:23:3465ninja -C out/asan base_unittests
66```
67
68### Goma build
69
Nico Weber3b6c2552020-09-09 19:49:4570ASan builds should work seamlessly with Goma; just add `use_goma=true` in your
71"gn args" Don't forget to use `ninja -j <jobs>` to take advantage of goma.
Staphany Park384b99a2019-12-18 03:23:3472
73### Build options
74
75If you want your stack traces to be precise, you will have to disable inlining
76by setting the GN arg:
Darwin Huang7d3b5f052019-12-23 19:25:5277```shell
Staphany Park384b99a2019-12-18 03:23:3478enable_full_stack_frames_for_profiling = true
79```
80
81Note that this incurs a significant performance hit. Please do not do this on
82buildbots.
83
84If you're working on reproducing ClusterFuzz reports, you might want to add:
Darwin Huang7d3b5f052019-12-23 19:25:5285```shell
Staphany Park384b99a2019-12-18 03:23:3486v8_enable_verify_heap = true
87```
Darwin Huang7d3b5f052019-12-23 19:25:5288in order to enable the `--verify-heap` command line flag for v8 in Release builds.
Staphany Park384b99a2019-12-18 03:23:3489
90## Verify the ASan tool works
91
92**ATTENTION (Linux only)**: These instructions are for running ASan in a way
93that is compatible with the sandbox. However, this is not compatible with
94LeakSanitizer. If you want to debug memory leaks, please use the instructions on
95the
96[LeakSanitizer](https://sites.google.com/a/chromium.org/dev/developers/testing/leaksanitizer)
97page instead.
98
99Now, check that the tool works. Run the following:
Darwin Huang7d3b5f052019-12-23 19:25:52100```shell
101out/asan/base_unittests \
102 --gtest_filter=ToolsSanityTest.DISABLED_AddressSanitizerLocalOOBCrashTest \
Amy Huangaaa8dcb2021-03-16 18:54:34103 --gtest_also_run_disabled_tests
Staphany Park384b99a2019-12-18 03:23:34104```
105
106The test will crash with the following error report:
Darwin Huang7d3b5f052019-12-23 19:25:52107```shell
108==26552== ERROR: AddressSanitizer stack-buffer-overflow on address \
1090x7fff338adb14 at pc 0xac20a7 bp 0x7fff338adad0 sp 0x7fff338adac8
Staphany Park384b99a2019-12-18 03:23:34110WRITE of size 4 at 0x7fff338adb14 thread T0
111 #0 0xac20a7 in base::ToolsSanityTest_DISABLED_AddressSanitizerLocalOOBCrashTest_Test::TestBody() ???:0
112 #1 0xcddbd6 in testing::Test::Run() testing/gtest/src/gtest.cc:2161
113 #2 0xcdf63b in testing::TestInfo::Run() testing/gtest/src/gtest.cc:2338
114... lots more stuff
Darwin Huang7d3b5f052019-12-23 19:25:52115Address 0x7fff338adb14 is located at offset 52 in frame \
116base::ToolsSanityTest_DISABLED_AddressSanitizerLocalOOBCrashTest_Test::TestBody()> of T0's stack:
Staphany Park384b99a2019-12-18 03:23:34117 This frame has 2 object(s):
118 [32, 52) 'array'
119 [96, 104) 'access'
120==26552== ABORTING
121... lots more stuff
122```
123
Samuel Huange9a7bff9d2020-03-04 16:16:03124Congrats, you have a working ASan build! &#x1F64C;
Staphany Park384b99a2019-12-18 03:23:34125
126## Run chrome under ASan
127
Darwin Huang7d3b5f052019-12-23 19:25:52128And finally, have fun with the `out/Release/chrome` binary. The filter script
Amy Huangaaa8dcb2021-03-16 18:54:34129`tools/valgrind/asan/asan_symbolize.py` can be used to symbolize the output,
130although it shouldn't be necessary on Linux and Windows, where Chrome uses the
131llvm-symbolizer in its source tree by default.
Staphany Park384b99a2019-12-18 03:23:34132
133ASan should perfectly work with Chrome's sandbox. You should only need to run
134with `--no-sandbox` on Linux if you're debugging ASan.
135Note: you have to disable the sandbox on Windows until it is supported.
136
137You may need to run with `--disable-gpu` on Linux with NVIDIA driver older than
138295.20.
139
140You will likely need to define environment variable
141[`G_SLICE=always-malloc`](https://developer.gnome.org/glib/unstable/glib-running.html)
142to avoid crashes inside gtk.
Nico Weber3b6c2552020-09-09 19:49:45143`NSS_DISABLE_ARENA_FREE_LIST=1` and `NSS_DISABLE_UNLOAD=1` are required as well.
Staphany Park384b99a2019-12-18 03:23:34144
145When filing a bug found by AddressSanitizer, please add a label
Bruce Dawson07f51d22022-07-01 10:51:53146`Stability-Memory-AddressSanitizer`.
Staphany Park384b99a2019-12-18 03:23:34147
148## ASan runtime options
149
150ASan's behavior can be changed by exporting the `ASAN_OPTIONS` env var. Some of
151the useful options are listed on this page, others can be obtained from running
152an ASanified binary with `ASAN_OPTIONS=help=1`. Note that Chromium sets its own
153defaults for some options, so the default behavior may be different from that
154observed in other projects.
Michael Lippautz9236a2c2021-07-22 21:53:19155See `build/sanitizers/sanitizer_options.cc` for more details.
Staphany Park384b99a2019-12-18 03:23:34156
157## NaCl support under ASan
158
Nico Weber3b6c2552020-09-09 19:49:45159On Linux (and soon on macOS) you can build and run Chromium with NaCl under ASan.
Staphany Park384b99a2019-12-18 03:23:34160Untrusted code (nexe) itself is not instrumented with ASan in this mode, but
161everything else is.
162
Nico Weber3b6c2552020-09-09 19:49:45163To do this, remove `enable_nacl=false` from your `args.gn`, and define
Staphany Park384b99a2019-12-18 03:23:34164`NACL_DANGEROUS_SKIP_QUALIFICATION_TEST=1` in your environment at run time.
165
166Pipe chromium output (stderr) through ``tools/valgrind/asan/asan_symbolize.py
167`pwd`/`` to get function names and line numbers in ASan reports.
Darwin Huang7d3b5f052019-12-23 19:25:52168If you're seeing crashes within `nacl_helper_bootstrap`, try deleting
169`out/Release/nacl_helper`.
Staphany Park384b99a2019-12-18 03:23:34170
171## Building on iOS
172
173It's possible to build and run Chrome tests for iOS simulator (which are x86
174binaries essentially) under ASan. Note that you'll need a Chrome iOS checkout
175for that. It isn't currently possible to build iOS binaries targeting ARM.
176
177Configure your build with `is_asan = true` as described above. Replace your
178build directory as needed:
Darwin Huang7d3b5f052019-12-23 19:25:52179```shell
Staphany Park384b99a2019-12-18 03:23:34180ninja -C out/Release-iphonesimulator base_unittests
Darwin Huang7d3b5f052019-12-23 19:25:52181out/Release-iphonesimulator/iossim -d "iPhone" -s 7.0 \
182 out/Release-iphonesimulator/base_unittests.app/ \
183 --gtest_filter=ToolsSanityTest.DISABLED_AddressSanitizerLocalOOBCrashTest \
184 --gtest_also_run_disabled_tests 2>&1 |
Staphany Park384b99a2019-12-18 03:23:34185tools/valgrind/asan/asan_symbolize.py
186```
187
188You'll see the same report as shown above (see the "Verify the ASan tool works"
189section), with a number of iOS-specific frames.
190
191## Building on Android
192
193Follow [AndroidBuildInstructions](android_build_instructions.md) with minor
194changes:
195
Darwin Huang7d3b5f052019-12-23 19:25:52196```python
197target_os="android"
Darwin Huang7d3b5f052019-12-23 19:25:52198is_asan=true
199is_debug=false
200```
Staphany Park384b99a2019-12-18 03:23:34201
202Running ASan applications on Android requires additional device setup. Chromium
203testing scripts take care of this, so testing works as expected:
Darwin Huang7d3b5f052019-12-23 19:25:52204```shell
205build/android/test_runner.py instrumentation --test-apk ContentShellTest \
206 --test_data content:content/test/data/android/device_files -v -v -v \
207 --tool=asan --release
Staphany Park384b99a2019-12-18 03:23:34208```
209
Hazem Ashmawy382171a2021-06-29 22:05:32210If the above step fails or to run stuff without Chromium testing script (ex.
211ContentShell.apk, or any third party apk or binary), device setup is needed:
Darwin Huang7d3b5f052019-12-23 19:25:52212```shell
Samuel Huange9a7bff9d2020-03-04 16:16:03213tools/android/asan/third_party/asan_device_setup.sh \
Prashanth Swaminathanc2e7afa52023-06-07 18:12:33214 --lib third_party/android_toolchain/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/*/lib/linux
Staphany Park384b99a2019-12-18 03:23:34215# wait a few seconds for the device to reload
216```
Staphany Park384b99a2019-12-18 03:23:34217It only needs to be run once per device. It is safe to run it multiple times.
Samuel Huange9a7bff9d2020-03-04 16:16:03218Examine the output to ensure that setup was successful (you may need to run
219`adb disable-verity` and restart the device first). When this is done, the
220device will run ASan apks as well as normal apks without any further setup.
Staphany Park384b99a2019-12-18 03:23:34221
222To run command-line tools (i.e. binaries), prefix them with `asanwrapper`:
Darwin Huang7d3b5f052019-12-23 19:25:52223```shell
Staphany Park384b99a2019-12-18 03:23:34224adb shell /system/bin/asanwrapper /path/to/binary
225```
226
Darwin Huang7d3b5f052019-12-23 19:25:52227Use `build/android/asan_symbolize.py` to symbolize stack from `adb logcat`. It
228needs the `--output-directory` argument and takes care of translating the device
229path to the unstripped binary in the output directory.
Staphany Park384b99a2019-12-18 03:23:34230
Nico Weber3b6c2552020-09-09 19:49:45231## Building with v8\_target\_arch="arm"
Staphany Park384b99a2019-12-18 03:23:34232
233This is needed to detect addressability bugs in the ARM code emitted by V8 and
234running on an instrumented ARM emulator in a 32-bit x86 Linux Chromium. **You
235probably don't want this, and these instructions have bitrotted because they
236still reference GYP. If you do this successfully, please update!** See
Darwin Huang7d3b5f052019-12-23 19:25:52237https://crbug.com/324207 for some context.
Staphany Park384b99a2019-12-18 03:23:34238
239First, you need to install the 32-bit chroot environment using the
240`build/install-chroot.sh` script (as described in
241https://code.google.com/p/chromium/wiki/LinuxBuild32On64). Second, install the
242build deps:
Darwin Huang7d3b5f052019-12-23 19:25:52243```shell
244precise32 build/install-build-deps.sh \
245 # assuming your schroot wrapper is called 'precise32'
Staphany Park384b99a2019-12-18 03:23:34246```
247
248You'll need to make two symlinks to avoid linking errors:
Darwin Huang7d3b5f052019-12-23 19:25:52249```shell
250sudo ln -s $CHROOT/usr/lib/i386-linux-gnu/libc_nonshared.a \
251 /usr/lib/i386-linux-gnu/libc_nonshared.a
252sudo ln -s $CHROOT/usr/lib/i386-linux-gnu/libpthread_nonshared.a \
253 /usr/lib/i386-linux-gnu/libpthread_nonshared.a
Staphany Park384b99a2019-12-18 03:23:34254```
255
256Now configure and build your Chrome:
Darwin Huang7d3b5f052019-12-23 19:25:52257```shell
258GYP_GENERATOR_FLAGS="output_dir=out_asan_chroot" GYP_DEFINES="asan=1 \
259 disable_nacl=1 v8_target_arch=arm sysroot=/var/lib/chroot/precise32bit/ \
260 chroot_cmd=precise32 host_arch=x86_64 target_arch=ia32" gclient runhooks
Staphany Park384b99a2019-12-18 03:23:34261ninja -C out_asan_chroot/Release chrome
262```
263
264**Note**: `disable_nacl=1` is needed for now.
265
James Cook0c3837bc2021-08-12 01:30:36266## Running on Chrome OS
267
268For the linux-chromeos "emulator" build, run Asan following the instructions
269above, just like you would for Linux.
270
271For Chromebook hardware, add `is_asan = true` to your args.gn and build.
272`deploy_chrome` with `--mount` and `--nostrip`. ASan logs can be found in
273`/var/log/asan/`.
274
275To catch crashes in gdb:
276
277- Edit `/etc/chrome_dev.conf` and add `ASAN_OPTIONS=abort_on_error=1`
278- `restart ui`
279- gdb -p 12345 # Find the pid from /var/log/chrome/chrome
280
281When you trigger the crash, you'll get a SIGABRT in gdb. `bt` will show the
282stack.
283
284See
285[Chrome OS stack traces](https://chromium.googlesource.com/chromiumos/docs/+/main/stack_traces.md)
286for more details.
287
Staphany Park384b99a2019-12-18 03:23:34288## AsanCoverage
289
290AsanCoverage is a minimalistic code coverage implementation built into ASan. For
291general information see
292[https://code.google.com/p/address-sanitizer/wiki/AsanCoverage](https://github.com/google/sanitizers)
293To use AsanCoverage in Chromium, add `use_sanitizer_coverage = true` to your GN
294args. See also the `sanitizer_coverage_flags` variable for configuring it.
295
296Chrome must be terminated gracefully in order for coverage to work. Either close
297the browser, or SIGTERM the browser process. Do not do `killall chrome` or send
298SIGKILL.
Darwin Huang7d3b5f052019-12-23 19:25:52299```shell
300kill <browser_process_pid>
301ls
Staphany Park384b99a2019-12-18 03:23:34302...
303chrome.22575.sancov
304gpu.6916123572022919124.sancov.packed
305zygote.13651804083035800069.sancov.packed
306...
307```
308
309The `gpu.*.sancov.packed` file contains coverage data for the GPU process,
310whereas the `zygote.*.sancov.packed` file contains coverage data for the
311renderers (but not the zygote process). Unpack them to regular `.sancov` files
312like so:
Darwin Huang7d3b5f052019-12-23 19:25:52313```shell
314$ $LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py unpack \
315 *.sancov.packed
Staphany Park384b99a2019-12-18 03:23:34316sancov.py: unpacking gpu.6916123572022919124.sancov.packed
317sancov.py: extracting chrome.22610.sancov
318sancov.py: unpacking zygote.13651804083035800069.sancov.packed
319sancov.py: extracting libpdf.so.12.sancov
320sancov.py: extracting chrome.12.sancov
321sancov.py: extracting libpdf.so.10.sancov
322sancov.py: extracting chrome.10.sancov
323```
324
325Now, e.g., to list the offsets of covered functions in the libpdf.so binary in
326renderer with pid 10:
Darwin Huang7d3b5f052019-12-23 19:25:52327```shell
328$ $LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py print \
329 libpdf.so.10.sancov
Staphany Park384b99a2019-12-18 03:23:34330```