Skip to content

Commit 5637892

Browse files
author
Oliver Lee
committed
Add sanitizer features to unix_cc_toolchain_config
1 parent 1c9661b commit 5637892

6 files changed

+164
-0
lines changed

tests/sanitizers/BUILD.bazel

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
2+
3+
cc_binary(
4+
name = "asan_example",
5+
srcs = ["asan_example.cpp"],
6+
features = ["asan"],
7+
)
8+
9+
cc_binary(
10+
name = "tsan_example",
11+
srcs = ["tsan_example.cpp"],
12+
features = ["tsan"],
13+
)
14+
15+
cc_binary(
16+
name = "ubsan_example",
17+
srcs = ["ubsan_example.cpp"],
18+
features = ["ubsan"],
19+
)
20+
21+
sh_test(
22+
name = "asan_test",
23+
srcs = ["sanitizer_test.sh"],
24+
data = [
25+
":asan_example",
26+
],
27+
env = {
28+
"BINARY": "$(rootpath :asan_example)",
29+
},
30+
args = [
31+
"'ERROR: AddressSanitizer: stack-use-after-scope'",
32+
],
33+
)
34+
35+
sh_test(
36+
name = "tsan_test",
37+
srcs = ["sanitizer_test.sh"],
38+
data = [
39+
":tsan_example",
40+
],
41+
env = {
42+
"BINARY": "$(rootpath :tsan_example)",
43+
},
44+
args = [
45+
"'WARNING: ThreadSanitizer: data race (pid=.*)'",
46+
"'Write of size .* at .* by thread T.*:'",
47+
"'Previous write of size .* at .* by thread T.*:'",
48+
],
49+
)
50+
51+
sh_test(
52+
name = "ubsan_test",
53+
srcs = ["sanitizer_test.sh"],
54+
data = [
55+
":ubsan_example",
56+
],
57+
env = {
58+
"BINARY": "$(rootpath :ubsan_example)",
59+
},
60+
args = [
61+
"'sanitizers/ubsan_example.cpp:3:11: runtime error: division by zero'",
62+
"'SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior sanitizers/ubsan_example.cpp:3:11 in'",
63+
],
64+
)

tests/sanitizers/asan_example.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
auto main() -> int
5+
{
6+
const auto f = [] () -> const auto& {
7+
return std::string{"hello, world!"};
8+
};
9+
std::cout << f() << "\n";
10+
}

tests/sanitizers/sanitizer_test.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
output=$($BINARY 2>&1 || true)
5+
6+
for arg in "$@"; do
7+
echo $output | grep -q "$arg"
8+
done

tests/sanitizers/tsan_example.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <thread>
2+
3+
auto main() -> int
4+
{
5+
auto i = int{};
6+
const auto inc = [&i] { ++i; };
7+
8+
auto t1 = std::thread{inc};
9+
auto t2 = std::thread{inc};
10+
t1.join();
11+
t2.join();
12+
13+
return i;
14+
}

tests/sanitizers/ubsan_example.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
auto main() -> int
2+
{
3+
return 1/0;
4+
}

toolchain/unix_cc_toolchain_config.bzl

+64
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,36 @@ lto_index_actions = [
143143
ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
144144
]
145145

146+
def _sanitizer_feature(name = "", specific_compile_flags = [], specific_link_flags = []):
147+
return feature(
148+
name = name,
149+
flag_sets =[
150+
flag_set(
151+
actions = all_compile_actions,
152+
flag_groups = [
153+
flag_group(flags = [
154+
"-O1",
155+
"-gline-tables-only",
156+
"-fno-omit-frame-pointer",
157+
"-fno-sanitize-recover=all",
158+
] + specific_compile_flags),
159+
],
160+
with_features = [
161+
with_feature_set(features = [name])
162+
],
163+
),
164+
flag_set(
165+
actions = all_link_actions,
166+
flag_groups = [
167+
flag_group(flags = specific_link_flags),
168+
],
169+
with_features = [
170+
with_feature_set(features = [name])
171+
],
172+
),
173+
],
174+
)
175+
146176
def _impl(ctx):
147177
tool_paths = [
148178
tool_path(name = name, path = path)
@@ -1215,6 +1245,37 @@ def _impl(ctx):
12151245
enabled = True,
12161246
)
12171247

1248+
asan_feature = _sanitizer_feature(
1249+
name = "asan",
1250+
specific_compile_flags = [
1251+
"-fsanitize=address",
1252+
"-fno-common",
1253+
],
1254+
specific_link_flags = [
1255+
"-fsanitize=address",
1256+
],
1257+
)
1258+
1259+
tsan_feature = _sanitizer_feature(
1260+
name = "tsan",
1261+
specific_compile_flags = [
1262+
"-fsanitize=thread",
1263+
],
1264+
specific_link_flags = [
1265+
"-fsanitize=thread",
1266+
],
1267+
)
1268+
1269+
ubsan_feature = _sanitizer_feature(
1270+
name = "ubsan",
1271+
specific_compile_flags = [
1272+
"-fsanitize=undefined",
1273+
],
1274+
specific_link_flags = [
1275+
"-fsanitize=undefined",
1276+
],
1277+
)
1278+
12181279
is_linux = ctx.attr.target_libc != "macosx"
12191280
libtool_feature = feature(
12201281
name = "libtool",
@@ -1255,6 +1316,9 @@ def _impl(ctx):
12551316
strip_debug_symbols_feature,
12561317
coverage_feature,
12571318
supports_pic_feature,
1319+
asan_feature,
1320+
tsan_feature,
1321+
ubsan_feature,
12581322
] + (
12591323
[
12601324
supports_start_end_lib_feature,

0 commit comments

Comments
 (0)