What's the recommended way to use bazel with address/thread sanitizer?

2,686 views
Skip to first unread message

Ming Zhao

unread,
Jun 2, 2015, 10:08:05 PM6/2/15
to bazel-...@googlegroups.com
I can use command line flags like this:
bazel build --copt -fsanitize=address --copt -O1

I guess I can potentially put these options into bazelrc, but what if
I need to dynamic switch compiler to clang when building with address
sanitizer? Is there recommended way to deal with that?

Thanks.

Austin Schuh

unread,
Jun 3, 2015, 1:02:29 AM6/3/15
to Ming Zhao, bazel-...@googlegroups.com
I've been putting it in the CROSSTOOL file.

  compilation_mode_flags {
    mode: DBG
    compiler_flag: '-fsanitize=address'
    linker_flag: '-fsanitize=address'
  }

is the stanza that I've been using.

Austin

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAN0GiO2k2%3DokkCtKL24xaF6GCyw3%2B_tqayFpazqTuaaq%2B9X6Qw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ming Zhao

unread,
Jun 3, 2015, 11:55:03 AM6/3/15
to Austin Schuh, bazel-...@googlegroups.com
Thanks, that should work.

If we continue this approach, there are still two questions, how do we
allow the user to choose whether to enable address sanitizer or not? I
guess we can define the CROSSTOOL setting in another directory and use
--crosstool_top to select it, or since crosstool toolchain is selected
by the combination or cpu, compiler and glibc, we can probably give
the particular toolchain a special compiler name so that it can be
also chosen by using --compiler gcc_address_sanitizer, etc. It doesn't
seem to be perfect but it should work.

Then it comes to another problem, how to we manage toolchain settings
inside CROSSTOOL file. At the moment it seems like a lot copy pasting
is inevtiable because we can't inherit another toolchain stetting, so
that if I have a toolchain definition to use clang for normal build,
and I want to introduce another toolchain definition for address
sanitizer, I need to copy the most of clang normal build toolchain and
add a few settings just to turn on address sanitizer, which is
sub-optimimal.

Ming Zhao

unread,
Jun 4, 2015, 11:04:45 PM6/4/15
to Austin Schuh, bazel-...@googlegroups.com
Inspired by the link shared by Damien,
https://bazel-review.googlesource.com/#/c/1430/6/site/contributing.md

I think a better to do that is to define address/thread sanitzer
config in .bazel, for example:

build:asan --strip=never
build:asan --copt -fsanitize=address
build:asan --copt -O1
build:asan --copt -fno-omit-frame-pointer
build:asan --linkopt -fsanitize=address

Then I don't have to blow up the CROSSTOOL config with a lot of duplication.
Reply all
Reply to author
Forward
0 new messages