Skip to content

Commit

Permalink
Add sanitizer features to unix_cc_toolchain_config
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlee committed Dec 29, 2022
1 parent 9c11145 commit b12f7d8
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1858,4 +1858,85 @@ EOF
fi
}

function test_cc_toolchain_asan_feature() {
mkdir pkg
cat > pkg/BUILD <<EOF
cc_binary(
name = 'asan_example',
srcs = ['asan_example.cc'],
features = ['asan'],
)
EOF

cat > pkg/asan_example.cc <<EOF
int main() {
int* p;
{
int x = 0;
p = &x;
}
return *p;
}
EOF

bazel run //pkg:asan_example &> "$TEST_log" && fail "Should have failed due to asan" || true
expect_log "ERROR: AddressSanitizer: stack-use-after-scope"
}

function test_cc_toolchain_tsan_feature() {
mkdir pkg
cat > pkg/BUILD <<EOF
cc_binary(
name = 'tsan_example',
srcs = ['tsan_example.cc'],
features = ['tsan'],
)
EOF

cat > pkg/tsan_example.cc <<EOF
#include <thread>
int value = 0;
void increment() {
++value;
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
return value;
}
EOF

bazel run //pkg:tsan_example &> "$TEST_log" && fail "Should have failed due to tsan" || true
expect_log "WARNING: ThreadSanitizer: data race"
}

function test_cc_toolchain_ubsan_feature() {
mkdir pkg
cat > pkg/BUILD <<EOF
cc_binary(
name = 'ubsan_example',
srcs = ['ubsan_example.cc'],
features = ['ubsan'],
)
EOF

cat > pkg/ubsan_example.cc <<EOF
int main() {
int array[10];
return array[10];
}
EOF

bazel run //pkg:ubsan_example &> "$TEST_log" && fail "Should have failed due to ubsan" || true
expect_log "runtime error: index 10 out of bounds"
}

run_suite "cc_integration_test"
66 changes: 66 additions & 0 deletions tools/cpp/unix_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ lto_index_actions = [
ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
]

def _sanitizer_feature(name = "", specific_compile_flags = [], specific_link_flags = []):
return feature(
name = name,
flag_sets = [
flag_set(
actions = all_compile_actions,
flag_groups = [
flag_group(flags = [
"-O1",
"-fno-omit-frame-pointer",
"-fno-sanitize-recover=all",
] + specific_compile_flags),
],
with_features = [
with_feature_set(features = [name]),
],
),
flag_set(
actions = all_link_actions,
flag_groups = [
flag_group(flags = specific_link_flags),
],
with_features = [
with_feature_set(features = [name]),
],
),
],
)

def _impl(ctx):
tool_paths = [
tool_path(name = name, path = path)
Expand Down Expand Up @@ -1215,6 +1244,37 @@ def _impl(ctx):
enabled = True,
)

asan_feature = _sanitizer_feature(
name = "asan",
specific_compile_flags = [
"-fsanitize=address",
"-fno-common",
],
specific_link_flags = [
"-fsanitize=address",
],
)

tsan_feature = _sanitizer_feature(
name = "tsan",
specific_compile_flags = [
"-fsanitize=thread",
],
specific_link_flags = [
"-fsanitize=thread",
],
)

ubsan_feature = _sanitizer_feature(
name = "ubsan",
specific_compile_flags = [
"-fsanitize=undefined",
],
specific_link_flags = [
"-fsanitize=undefined",
],
)

is_linux = ctx.attr.target_libc != "macosx"
libtool_feature = feature(
name = "libtool",
Expand Down Expand Up @@ -1255,6 +1315,9 @@ def _impl(ctx):
strip_debug_symbols_feature,
coverage_feature,
supports_pic_feature,
asan_feature,
tsan_feature,
ubsan_feature,
] + (
[
supports_start_end_lib_feature,
Expand Down Expand Up @@ -1290,6 +1353,9 @@ def _impl(ctx):
libtool_feature,
archiver_flags_feature,
supports_pic_feature,
asan_feature,
tsan_feature,
ubsan_feature,
] + (
[
supports_start_end_lib_feature,
Expand Down

0 comments on commit b12f7d8

Please sign in to comment.