Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 1 | # AddressSanitizer (ASan) |
| 2 | |
| 3 | [AddressSanitizer](https://github.com/google/sanitizers) (ASan) is a fast memory |
| 4 | error detector based on compiler instrumentation (LLVM). It is fully usable for |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 5 | Chrome on Android, Chrome OS, iOS simulator, Linux, Mac, and 64-bit Windows. |
| 6 | Additional info on the tool itself is available at |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 7 | https://clang.llvm.org/docs/AddressSanitizer.html. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 8 | |
| 9 | For the memory leak detector built into ASan, see |
| 10 | [LeakSanitizer](https://sites.google.com/a/chromium.org/dev/developers/testing/leaksanitizer). |
| 11 | If you want to debug memory leaks, please refer to the instructions on that page |
| 12 | instead. |
| 13 | |
| 14 | ## Buildbots and trybots |
| 15 | |
| 16 | The [Chromium Memory |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 17 | waterfall](https://ci.chromium.org/p/chromium/g/chromium.memory/console) |
| 18 | contains buildbots running Chromium tests under ASan on Linux (Linux ASan/LSan |
| 19 | bots for the regular Linux build, Linux Chromium OS ASan for the chromeos=1 |
| 20 | build running on Linux), macOS, Chromium OS. Linux and Linux Chromium OS bots |
| 21 | run with --no-sandbox, but there's an extra Linux bot that enables the sandbox |
| 22 | (but disables LeakSanitizer). |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 23 | |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 24 | The trybots running Chromium tests on Linux and macOS are: |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 25 | - 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 |
| 29 | tests including browser\_tests and content\_browsertests). |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 30 | |
| 31 | ## Pre-built Chrome binaries |
| 32 | |
| 33 | You can grab fresh Chrome binaries built with ASan |
| 34 | [here](https://commondatastorage.googleapis.com/chromium-browser-asan/index.html). |
Chris Thompson | 4c93a6e | 2020-12-17 02:20:51 | [diff] [blame] | 35 | The lists of ASan binaries are _very_ long, but you can filter down to more |
| 36 | specific 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). |
| 38 | This is useful for finding a build for a specific revision, since filenames are of |
| 39 | the form `asan-<platform>-<buildtype>-<revision>` (but not every revision has an |
| 40 | archived ASan build). |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 41 | |
| 42 | ## Build tests with ASan |
| 43 | |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 44 | Building with ASan is easy. Start by compiling `base_unittests` to verify the |
| 45 | build is working for you (see below). Then, you can compile `chrome`, |
| 46 | `browser_tests`, etc.. Make sure to compile release builds. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 47 | |
| 48 | ### Configuring the build |
| 49 | |
| 50 | Create an asan build directory by running: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 51 | ```shell |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 52 | gn args out/asan |
| 53 | ``` |
| 54 | |
| 55 | Enter the following build variables in the editor that will pop up: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 56 | ```python |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 57 | is_asan = true |
| 58 | is_debug = false # Release build. |
| 59 | ``` |
| 60 | |
| 61 | Build with: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 62 | ```shell |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 63 | ninja -C out/asan base_unittests |
| 64 | ``` |
| 65 | |
| 66 | ### Goma build |
| 67 | |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 68 | ASan 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 Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 70 | |
| 71 | ### Build options |
| 72 | |
| 73 | If you want your stack traces to be precise, you will have to disable inlining |
| 74 | by setting the GN arg: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 75 | ```shell |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 76 | enable_full_stack_frames_for_profiling = true |
| 77 | ``` |
| 78 | |
| 79 | Note that this incurs a significant performance hit. Please do not do this on |
| 80 | buildbots. |
| 81 | |
| 82 | If you're working on reproducing ClusterFuzz reports, you might want to add: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 83 | ```shell |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 84 | v8_enable_verify_heap = true |
| 85 | ``` |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 86 | in order to enable the `--verify-heap` command line flag for v8 in Release builds. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 87 | |
| 88 | ## Verify the ASan tool works |
| 89 | |
| 90 | **ATTENTION (Linux only)**: These instructions are for running ASan in a way |
| 91 | that is compatible with the sandbox. However, this is not compatible with |
| 92 | LeakSanitizer. If you want to debug memory leaks, please use the instructions on |
| 93 | the |
| 94 | [LeakSanitizer](https://sites.google.com/a/chromium.org/dev/developers/testing/leaksanitizer) |
| 95 | page instead. |
| 96 | |
| 97 | Now, check that the tool works. Run the following: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 98 | ```shell |
| 99 | out/asan/base_unittests \ |
| 100 | --gtest_filter=ToolsSanityTest.DISABLED_AddressSanitizerLocalOOBCrashTest \ |
Amy Huang | aaa8dcb | 2021-03-16 18:54:34 | [diff] [blame] | 101 | --gtest_also_run_disabled_tests |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 102 | ``` |
| 103 | |
| 104 | The test will crash with the following error report: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 105 | ```shell |
| 106 | ==26552== ERROR: AddressSanitizer stack-buffer-overflow on address \ |
| 107 | 0x7fff338adb14 at pc 0xac20a7 bp 0x7fff338adad0 sp 0x7fff338adac8 |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 108 | WRITE 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 Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 113 | Address 0x7fff338adb14 is located at offset 52 in frame \ |
| 114 | base::ToolsSanityTest_DISABLED_AddressSanitizerLocalOOBCrashTest_Test::TestBody()> of T0's stack: |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 115 | This frame has 2 object(s): |
| 116 | [32, 52) 'array' |
| 117 | [96, 104) 'access' |
| 118 | ==26552== ABORTING |
| 119 | ... lots more stuff |
| 120 | ``` |
| 121 | |
Samuel Huang | e9a7bff9d | 2020-03-04 16:16:03 | [diff] [blame] | 122 | Congrats, you have a working ASan build! 🙌 |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 123 | |
| 124 | ## Run chrome under ASan |
| 125 | |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 126 | And finally, have fun with the `out/Release/chrome` binary. The filter script |
Amy Huang | aaa8dcb | 2021-03-16 18:54:34 | [diff] [blame] | 127 | `tools/valgrind/asan/asan_symbolize.py` can be used to symbolize the output, |
| 128 | although it shouldn't be necessary on Linux and Windows, where Chrome uses the |
| 129 | llvm-symbolizer in its source tree by default. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 130 | |
| 131 | ASan should perfectly work with Chrome's sandbox. You should only need to run |
| 132 | with `--no-sandbox` on Linux if you're debugging ASan. |
| 133 | Note: you have to disable the sandbox on Windows until it is supported. |
| 134 | |
| 135 | You may need to run with `--disable-gpu` on Linux with NVIDIA driver older than |
| 136 | 295.20. |
| 137 | |
| 138 | You will likely need to define environment variable |
| 139 | [`G_SLICE=always-malloc`](https://developer.gnome.org/glib/unstable/glib-running.html) |
| 140 | to avoid crashes inside gtk. |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 141 | `NSS_DISABLE_ARENA_FREE_LIST=1` and `NSS_DISABLE_UNLOAD=1` are required as well. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 142 | |
| 143 | When filing a bug found by AddressSanitizer, please add a label |
| 144 | `Stability-AddressSanitizer`. |
| 145 | |
| 146 | ## ASan runtime options |
| 147 | |
| 148 | ASan's behavior can be changed by exporting the `ASAN_OPTIONS` env var. Some of |
| 149 | the useful options are listed on this page, others can be obtained from running |
| 150 | an ASanified binary with `ASAN_OPTIONS=help=1`. Note that Chromium sets its own |
| 151 | defaults for some options, so the default behavior may be different from that |
| 152 | observed in other projects. |
| 153 | See `base/debug/sanitizer_options.cc` for more details. |
| 154 | |
| 155 | ## NaCl support under ASan |
| 156 | |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 157 | On Linux (and soon on macOS) you can build and run Chromium with NaCl under ASan. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 158 | Untrusted code (nexe) itself is not instrumented with ASan in this mode, but |
| 159 | everything else is. |
| 160 | |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 161 | To do this, remove `enable_nacl=false` from your `args.gn`, and define |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 162 | `NACL_DANGEROUS_SKIP_QUALIFICATION_TEST=1` in your environment at run time. |
| 163 | |
| 164 | Pipe chromium output (stderr) through ``tools/valgrind/asan/asan_symbolize.py |
| 165 | `pwd`/`` to get function names and line numbers in ASan reports. |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 166 | If you're seeing crashes within `nacl_helper_bootstrap`, try deleting |
| 167 | `out/Release/nacl_helper`. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 168 | |
| 169 | ## Building on iOS |
| 170 | |
| 171 | It's possible to build and run Chrome tests for iOS simulator (which are x86 |
| 172 | binaries essentially) under ASan. Note that you'll need a Chrome iOS checkout |
| 173 | for that. It isn't currently possible to build iOS binaries targeting ARM. |
| 174 | |
| 175 | Configure your build with `is_asan = true` as described above. Replace your |
| 176 | build directory as needed: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 177 | ```shell |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 178 | ninja -C out/Release-iphonesimulator base_unittests |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 179 | out/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 Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 183 | tools/valgrind/asan/asan_symbolize.py |
| 184 | ``` |
| 185 | |
| 186 | You'll see the same report as shown above (see the "Verify the ASan tool works" |
| 187 | section), with a number of iOS-specific frames. |
| 188 | |
| 189 | ## Building on Android |
| 190 | |
| 191 | Follow [AndroidBuildInstructions](android_build_instructions.md) with minor |
| 192 | changes: |
| 193 | |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 194 | ```python |
| 195 | target_os="android" |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 196 | is_asan=true |
| 197 | is_debug=false |
| 198 | ``` |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 199 | |
| 200 | Running ASan applications on Android requires additional device setup. Chromium |
| 201 | testing scripts take care of this, so testing works as expected: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 202 | ```shell |
| 203 | build/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 Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 206 | ``` |
| 207 | |
| 208 | To run stuff without Chromium testing script (ex. ContentShell.apk, or any third |
| 209 | party apk or binary), device setup is needed: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 210 | ```shell |
Samuel Huang | e9a7bff9d | 2020-03-04 16:16:03 | [diff] [blame] | 211 | tools/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 Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 213 | # wait a few seconds for the device to reload |
| 214 | ``` |
| 215 | |
| 216 | It only needs to be run once per device. It is safe to run it multiple times. |
Samuel Huang | e9a7bff9d | 2020-03-04 16:16:03 | [diff] [blame] | 217 | Examine 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 |
| 219 | device will run ASan apks as well as normal apks without any further setup. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 220 | |
| 221 | To run command-line tools (i.e. binaries), prefix them with `asanwrapper`: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 222 | ```shell |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 223 | adb shell /system/bin/asanwrapper /path/to/binary |
| 224 | ``` |
| 225 | |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 226 | Use `build/android/asan_symbolize.py` to symbolize stack from `adb logcat`. It |
| 227 | needs the `--output-directory` argument and takes care of translating the device |
| 228 | path to the unstripped binary in the output directory. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 229 | |
Nico Weber | 3b6c255 | 2020-09-09 19:49:45 | [diff] [blame] | 230 | ## Building with v8\_target\_arch="arm" |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 231 | |
| 232 | This is needed to detect addressability bugs in the ARM code emitted by V8 and |
| 233 | running on an instrumented ARM emulator in a 32-bit x86 Linux Chromium. **You |
| 234 | probably don't want this, and these instructions have bitrotted because they |
| 235 | still reference GYP. If you do this successfully, please update!** See |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 236 | https://crbug.com/324207 for some context. |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 237 | |
| 238 | First, you need to install the 32-bit chroot environment using the |
| 239 | `build/install-chroot.sh` script (as described in |
| 240 | https://code.google.com/p/chromium/wiki/LinuxBuild32On64). Second, install the |
| 241 | build deps: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 242 | ```shell |
| 243 | precise32 build/install-build-deps.sh \ |
| 244 | # assuming your schroot wrapper is called 'precise32' |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 245 | ``` |
| 246 | |
| 247 | You'll need to make two symlinks to avoid linking errors: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 248 | ```shell |
| 249 | sudo ln -s $CHROOT/usr/lib/i386-linux-gnu/libc_nonshared.a \ |
| 250 | /usr/lib/i386-linux-gnu/libc_nonshared.a |
| 251 | sudo ln -s $CHROOT/usr/lib/i386-linux-gnu/libpthread_nonshared.a \ |
| 252 | /usr/lib/i386-linux-gnu/libpthread_nonshared.a |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 253 | ``` |
| 254 | |
| 255 | Now configure and build your Chrome: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 256 | ```shell |
| 257 | GYP_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 Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 260 | ninja -C out_asan_chroot/Release chrome |
| 261 | ``` |
| 262 | |
| 263 | **Note**: `disable_nacl=1` is needed for now. |
| 264 | |
| 265 | ## AsanCoverage |
| 266 | |
| 267 | AsanCoverage is a minimalistic code coverage implementation built into ASan. For |
| 268 | general information see |
| 269 | [https://code.google.com/p/address-sanitizer/wiki/AsanCoverage](https://github.com/google/sanitizers) |
| 270 | To use AsanCoverage in Chromium, add `use_sanitizer_coverage = true` to your GN |
| 271 | args. See also the `sanitizer_coverage_flags` variable for configuring it. |
| 272 | |
| 273 | Chrome must be terminated gracefully in order for coverage to work. Either close |
| 274 | the browser, or SIGTERM the browser process. Do not do `killall chrome` or send |
| 275 | SIGKILL. |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 276 | ```shell |
| 277 | kill <browser_process_pid> |
| 278 | ls |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 279 | ... |
| 280 | chrome.22575.sancov |
| 281 | gpu.6916123572022919124.sancov.packed |
| 282 | zygote.13651804083035800069.sancov.packed |
| 283 | ... |
| 284 | ``` |
| 285 | |
| 286 | The `gpu.*.sancov.packed` file contains coverage data for the GPU process, |
| 287 | whereas the `zygote.*.sancov.packed` file contains coverage data for the |
| 288 | renderers (but not the zygote process). Unpack them to regular `.sancov` files |
| 289 | like so: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 290 | ```shell |
| 291 | $ $LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py unpack \ |
| 292 | *.sancov.packed |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 293 | sancov.py: unpacking gpu.6916123572022919124.sancov.packed |
| 294 | sancov.py: extracting chrome.22610.sancov |
| 295 | sancov.py: unpacking zygote.13651804083035800069.sancov.packed |
| 296 | sancov.py: extracting libpdf.so.12.sancov |
| 297 | sancov.py: extracting chrome.12.sancov |
| 298 | sancov.py: extracting libpdf.so.10.sancov |
| 299 | sancov.py: extracting chrome.10.sancov |
| 300 | ``` |
| 301 | |
| 302 | Now, e.g., to list the offsets of covered functions in the libpdf.so binary in |
| 303 | renderer with pid 10: |
Darwin Huang | 7d3b5f05 | 2019-12-23 19:25:52 | [diff] [blame] | 304 | ```shell |
| 305 | $ $LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py print \ |
| 306 | libpdf.so.10.sancov |
Staphany Park | 384b99a | 2019-12-18 03:23:34 | [diff] [blame] | 307 | ``` |