Skip to content

Commit

Permalink
Add sanitizer support to Apple platforms
Browse files Browse the repository at this point in the history
This adds support for asan, tsan, and ubsan on Apple platforms.

Fixes bazelbuild#4984 and bazelbuild#6932

Closes bazelbuild#12772.

PiperOrigin-RevId: 352489421
  • Loading branch information
keith authored and copybara-github committed Jan 19, 2021
1 parent 2ecda6e commit 8959dff
Showing 1 changed file with 193 additions and 2 deletions.
195 changes: 193 additions & 2 deletions tools/osx/crosstool/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5475,6 +5475,29 @@ def _impl(ctx):
flag_group(
flags = [
"-D_FORTIFY_SOURCE=1",
],
),
],
with_features = [with_feature_set(not_features = ["asan"])],
),
flag_set(
actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.lto_backend,
ACTION_NAMES.clif_match,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(
flags = [
"-fstack-protector",
"-fcolor-diagnostics",
"-Wall",
Expand Down Expand Up @@ -5523,7 +5546,6 @@ def _impl(ctx):
flags = [
"-g0",
"-O2",
"-D_FORTIFY_SOURCE=1",
"-DNDEBUG",
"-DNS_BLOCK_ASSERTIONS=1",
],
Expand Down Expand Up @@ -5595,6 +5617,29 @@ def _impl(ctx):
flag_group(
flags = [
"-D_FORTIFY_SOURCE=1",
],
),
],
with_features = [with_feature_set(not_features = ["asan"])],
),
flag_set(
actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.lto_backend,
ACTION_NAMES.clif_match,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(
flags = [
"-fstack-protector",
"-fcolor-diagnostics",
"-Wall",
Expand Down Expand Up @@ -5643,7 +5688,6 @@ def _impl(ctx):
flags = [
"-g0",
"-O2",
"-D_FORTIFY_SOURCE=1",
"-DNDEBUG",
"-DNS_BLOCK_ASSERTIONS=1",
],
Expand Down Expand Up @@ -6080,6 +6124,141 @@ def _impl(ctx):
],
)

asan_feature = feature(
name = "asan",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(flags = ["-fsanitize=address"]),
],
with_features = [
with_feature_set(features = ["asan"]),
],
),
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
ACTION_NAMES.objc_executable,
ACTION_NAMES.objcpp_executable,
],
flag_groups = [
flag_group(flags = ["-fsanitize=address"]),
],
with_features = [
with_feature_set(features = ["asan"]),
],
),
],
)

tsan_feature = feature(
name = "tsan",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(flags = ["-fsanitize=thread"]),
],
with_features = [
with_feature_set(features = ["tsan"]),
],
),
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
ACTION_NAMES.objc_executable,
ACTION_NAMES.objcpp_executable,
],
flag_groups = [
flag_group(flags = ["-fsanitize=thread"]),
],
with_features = [
with_feature_set(features = ["tsan"]),
],
),
],
)

ubsan_feature = feature(
name = "ubsan",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(flags = ["-fsanitize=undefined"]),
],
with_features = [
with_feature_set(features = ["ubsan"]),
],
),
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
ACTION_NAMES.objc_executable,
ACTION_NAMES.objcpp_executable,
],
flag_groups = [
flag_group(flags = ["-fsanitize=undefined"]),
],
with_features = [
with_feature_set(features = ["ubsan"]),
],
),
],
)

default_sanitizer_flags_feature = feature(
name = "default_sanitizer_flags",
enabled = True,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(
flags = [
"-O1",
"-gline-tables-only",
"-fno-omit-frame-pointer",
"-fno-sanitize-recover=all",
],
),
],
with_features = [
with_feature_set(features = ["asan"]),
with_feature_set(features = ["tsan"]),
with_feature_set(features = ["ubsan"]),
],
),
],
)

if (ctx.attr.cpu == "ios_arm64" or
ctx.attr.cpu == "ios_arm64e" or
ctx.attr.cpu == "ios_armv7" or
Expand Down Expand Up @@ -6165,6 +6344,10 @@ def _impl(ctx):
compiler_output_flags_feature,
objcopy_embed_flags_feature,
set_install_name,
asan_feature,
tsan_feature,
ubsan_feature,
default_sanitizer_flags_feature,
]
elif (ctx.attr.cpu == "darwin_x86_64" or
ctx.attr.cpu == "darwin_arm64" or
Expand Down Expand Up @@ -6244,6 +6427,10 @@ def _impl(ctx):
objcopy_embed_flags_feature,
dynamic_linking_mode_feature,
set_install_name,
asan_feature,
tsan_feature,
ubsan_feature,
default_sanitizer_flags_feature,
]
elif (ctx.attr.cpu == "armeabi-v7a"):
features = [
Expand Down Expand Up @@ -6321,6 +6508,10 @@ def _impl(ctx):
supports_pic_feature,
objcopy_embed_flags_feature,
set_install_name,
asan_feature,
tsan_feature,
ubsan_feature,
default_sanitizer_flags_feature,
]
else:
fail("Unreachable")
Expand Down

0 comments on commit 8959dff

Please sign in to comment.