Skip to content

Commit 8959dff

Browse files
keithcopybara-github
authored andcommitted
Add sanitizer support to Apple platforms
This adds support for asan, tsan, and ubsan on Apple platforms. Fixes bazelbuild#4984 and bazelbuild#6932 Closes bazelbuild#12772. PiperOrigin-RevId: 352489421
1 parent 2ecda6e commit 8959dff

File tree

1 file changed

+193
-2
lines changed

1 file changed

+193
-2
lines changed

tools/osx/crosstool/cc_toolchain_config.bzl

+193-2
Original file line numberDiff line numberDiff line change
@@ -5475,6 +5475,29 @@ def _impl(ctx):
54755475
flag_group(
54765476
flags = [
54775477
"-D_FORTIFY_SOURCE=1",
5478+
],
5479+
),
5480+
],
5481+
with_features = [with_feature_set(not_features = ["asan"])],
5482+
),
5483+
flag_set(
5484+
actions = [
5485+
ACTION_NAMES.assemble,
5486+
ACTION_NAMES.preprocess_assemble,
5487+
ACTION_NAMES.linkstamp_compile,
5488+
ACTION_NAMES.c_compile,
5489+
ACTION_NAMES.cpp_compile,
5490+
ACTION_NAMES.cpp_header_parsing,
5491+
ACTION_NAMES.cpp_module_compile,
5492+
ACTION_NAMES.cpp_module_codegen,
5493+
ACTION_NAMES.lto_backend,
5494+
ACTION_NAMES.clif_match,
5495+
ACTION_NAMES.objc_compile,
5496+
ACTION_NAMES.objcpp_compile,
5497+
],
5498+
flag_groups = [
5499+
flag_group(
5500+
flags = [
54785501
"-fstack-protector",
54795502
"-fcolor-diagnostics",
54805503
"-Wall",
@@ -5523,7 +5546,6 @@ def _impl(ctx):
55235546
flags = [
55245547
"-g0",
55255548
"-O2",
5526-
"-D_FORTIFY_SOURCE=1",
55275549
"-DNDEBUG",
55285550
"-DNS_BLOCK_ASSERTIONS=1",
55295551
],
@@ -5595,6 +5617,29 @@ def _impl(ctx):
55955617
flag_group(
55965618
flags = [
55975619
"-D_FORTIFY_SOURCE=1",
5620+
],
5621+
),
5622+
],
5623+
with_features = [with_feature_set(not_features = ["asan"])],
5624+
),
5625+
flag_set(
5626+
actions = [
5627+
ACTION_NAMES.assemble,
5628+
ACTION_NAMES.preprocess_assemble,
5629+
ACTION_NAMES.linkstamp_compile,
5630+
ACTION_NAMES.c_compile,
5631+
ACTION_NAMES.cpp_compile,
5632+
ACTION_NAMES.cpp_header_parsing,
5633+
ACTION_NAMES.cpp_module_compile,
5634+
ACTION_NAMES.cpp_module_codegen,
5635+
ACTION_NAMES.lto_backend,
5636+
ACTION_NAMES.clif_match,
5637+
ACTION_NAMES.objc_compile,
5638+
ACTION_NAMES.objcpp_compile,
5639+
],
5640+
flag_groups = [
5641+
flag_group(
5642+
flags = [
55985643
"-fstack-protector",
55995644
"-fcolor-diagnostics",
56005645
"-Wall",
@@ -5643,7 +5688,6 @@ def _impl(ctx):
56435688
flags = [
56445689
"-g0",
56455690
"-O2",
5646-
"-D_FORTIFY_SOURCE=1",
56475691
"-DNDEBUG",
56485692
"-DNS_BLOCK_ASSERTIONS=1",
56495693
],
@@ -6080,6 +6124,141 @@ def _impl(ctx):
60806124
],
60816125
)
60826126

6127+
asan_feature = feature(
6128+
name = "asan",
6129+
flag_sets = [
6130+
flag_set(
6131+
actions = [
6132+
ACTION_NAMES.c_compile,
6133+
ACTION_NAMES.cpp_compile,
6134+
ACTION_NAMES.objc_compile,
6135+
ACTION_NAMES.objcpp_compile,
6136+
],
6137+
flag_groups = [
6138+
flag_group(flags = ["-fsanitize=address"]),
6139+
],
6140+
with_features = [
6141+
with_feature_set(features = ["asan"]),
6142+
],
6143+
),
6144+
flag_set(
6145+
actions = [
6146+
ACTION_NAMES.cpp_link_executable,
6147+
ACTION_NAMES.cpp_link_dynamic_library,
6148+
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
6149+
ACTION_NAMES.objc_executable,
6150+
ACTION_NAMES.objcpp_executable,
6151+
],
6152+
flag_groups = [
6153+
flag_group(flags = ["-fsanitize=address"]),
6154+
],
6155+
with_features = [
6156+
with_feature_set(features = ["asan"]),
6157+
],
6158+
),
6159+
],
6160+
)
6161+
6162+
tsan_feature = feature(
6163+
name = "tsan",
6164+
flag_sets = [
6165+
flag_set(
6166+
actions = [
6167+
ACTION_NAMES.c_compile,
6168+
ACTION_NAMES.cpp_compile,
6169+
ACTION_NAMES.objc_compile,
6170+
ACTION_NAMES.objcpp_compile,
6171+
],
6172+
flag_groups = [
6173+
flag_group(flags = ["-fsanitize=thread"]),
6174+
],
6175+
with_features = [
6176+
with_feature_set(features = ["tsan"]),
6177+
],
6178+
),
6179+
flag_set(
6180+
actions = [
6181+
ACTION_NAMES.cpp_link_executable,
6182+
ACTION_NAMES.cpp_link_dynamic_library,
6183+
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
6184+
ACTION_NAMES.objc_executable,
6185+
ACTION_NAMES.objcpp_executable,
6186+
],
6187+
flag_groups = [
6188+
flag_group(flags = ["-fsanitize=thread"]),
6189+
],
6190+
with_features = [
6191+
with_feature_set(features = ["tsan"]),
6192+
],
6193+
),
6194+
],
6195+
)
6196+
6197+
ubsan_feature = feature(
6198+
name = "ubsan",
6199+
flag_sets = [
6200+
flag_set(
6201+
actions = [
6202+
ACTION_NAMES.c_compile,
6203+
ACTION_NAMES.cpp_compile,
6204+
ACTION_NAMES.objc_compile,
6205+
ACTION_NAMES.objcpp_compile,
6206+
],
6207+
flag_groups = [
6208+
flag_group(flags = ["-fsanitize=undefined"]),
6209+
],
6210+
with_features = [
6211+
with_feature_set(features = ["ubsan"]),
6212+
],
6213+
),
6214+
flag_set(
6215+
actions = [
6216+
ACTION_NAMES.cpp_link_executable,
6217+
ACTION_NAMES.cpp_link_dynamic_library,
6218+
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
6219+
ACTION_NAMES.objc_executable,
6220+
ACTION_NAMES.objcpp_executable,
6221+
],
6222+
flag_groups = [
6223+
flag_group(flags = ["-fsanitize=undefined"]),
6224+
],
6225+
with_features = [
6226+
with_feature_set(features = ["ubsan"]),
6227+
],
6228+
),
6229+
],
6230+
)
6231+
6232+
default_sanitizer_flags_feature = feature(
6233+
name = "default_sanitizer_flags",
6234+
enabled = True,
6235+
flag_sets = [
6236+
flag_set(
6237+
actions = [
6238+
ACTION_NAMES.c_compile,
6239+
ACTION_NAMES.cpp_compile,
6240+
ACTION_NAMES.objc_compile,
6241+
ACTION_NAMES.objcpp_compile,
6242+
],
6243+
flag_groups = [
6244+
flag_group(
6245+
flags = [
6246+
"-O1",
6247+
"-gline-tables-only",
6248+
"-fno-omit-frame-pointer",
6249+
"-fno-sanitize-recover=all",
6250+
],
6251+
),
6252+
],
6253+
with_features = [
6254+
with_feature_set(features = ["asan"]),
6255+
with_feature_set(features = ["tsan"]),
6256+
with_feature_set(features = ["ubsan"]),
6257+
],
6258+
),
6259+
],
6260+
)
6261+
60836262
if (ctx.attr.cpu == "ios_arm64" or
60846263
ctx.attr.cpu == "ios_arm64e" or
60856264
ctx.attr.cpu == "ios_armv7" or
@@ -6165,6 +6344,10 @@ def _impl(ctx):
61656344
compiler_output_flags_feature,
61666345
objcopy_embed_flags_feature,
61676346
set_install_name,
6347+
asan_feature,
6348+
tsan_feature,
6349+
ubsan_feature,
6350+
default_sanitizer_flags_feature,
61686351
]
61696352
elif (ctx.attr.cpu == "darwin_x86_64" or
61706353
ctx.attr.cpu == "darwin_arm64" or
@@ -6244,6 +6427,10 @@ def _impl(ctx):
62446427
objcopy_embed_flags_feature,
62456428
dynamic_linking_mode_feature,
62466429
set_install_name,
6430+
asan_feature,
6431+
tsan_feature,
6432+
ubsan_feature,
6433+
default_sanitizer_flags_feature,
62476434
]
62486435
elif (ctx.attr.cpu == "armeabi-v7a"):
62496436
features = [
@@ -6321,6 +6508,10 @@ def _impl(ctx):
63216508
supports_pic_feature,
63226509
objcopy_embed_flags_feature,
63236510
set_install_name,
6511+
asan_feature,
6512+
tsan_feature,
6513+
ubsan_feature,
6514+
default_sanitizer_flags_feature,
63246515
]
63256516
else:
63266517
fail("Unreachable")

0 commit comments

Comments
 (0)