Skip to content

Commit ff40e5f

Browse files
committed
bazel: Add support for TSAN
With this commit we can execute our tests under a thread sanitizer via `bazel test --config=tsan //score/...`
1 parent 5e6c958 commit ff40e5f

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

.bazelrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ common --@score_baselibs//score/json:base_library=nlohmann
1818
common --//score/mw/com/flags:tracing_library=stub
1919
common --extra_toolchains=@gcc_toolchain//:host_gcc_12
2020
build --incompatible_strict_action_env
21-
test --test_tag_filters=-manual
2221
test --test_output=errors
2322
build --experimental_retain_test_configuration_across_testonly #https://github.com/bazelbuild/bazel/issues/6842
2423

quality/sanitizer/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sh_test(
1919
"test_workspace/BUILD.tpl",
2020
"test_workspace/MODULE.bazel",
2121
"test_workspace/asan_fail_heap_out_of_bounds.cpp",
22+
"test_workspace/tsan_fail_data_race.cpp",
2223
],
2324
env_inherit = [
2425
"PATH",
@@ -27,7 +28,7 @@ sh_test(
2728
],
2829
tags = [
2930
"local", # Test marked local because it uses bazel.
30-
"manual", # Due to long-runtime and local dependencies
31+
"manual", # Due to long-runtime and local dependencies
3132
],
3233
deps = ["@bazel_tools//tools/bash/runfiles"],
3334
)

quality/sanitizer/sanitizer.bazelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ test:asan_ubsan --test_env=ASAN_OPTIONS="exitcode=55 allow_addr2line=1 verbosity
2424
test:asan_ubsan --test_env=UBSAN_OPTIONS="exitcode=55 allow_addr2line=1 verbosity=1 coverage=1 print_stacktrace=1 halt_on_error=1"
2525

2626
test:asan --config=asan_ubsan
27+
28+
test:tsan --cxxopt -O0
29+
test:tsan --cxxopt -Og
30+
test:tsan --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux
31+
test:tsan --cxxopt -fsanitize=thread
32+
test:tsan --linkopt -fsanitize=thread
33+
test:tsan --linkopt -fsanitize-link-c++-runtime
34+
test:tsan --platform_suffix=tsan
35+
test:tsan --test_env=TSAN_OPTIONS="exitcode=55 allow_addr2line=1 verbosity=1 coverage=1 detect_deadlocks=1 second_deadlock_stack=1 halt_on_error=0"

quality/sanitizer/sanitizers_functional_test.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ ln -sf "$(rlocation $TEST_WORKSPACE/quality/sanitizer/sanitizer.bazelrc)" "test_
4747
ln -sf "$(rlocation $TEST_WORKSPACE/quality/sanitizer/test_workspace/MODULE.bazel)" "test_workspace/MODULE.bazel"
4848
ln -sf "$(rlocation $TEST_WORKSPACE/quality/sanitizer/test_workspace/BUILD.tpl)" "test_workspace/BUILD"
4949
ln -sf "$(rlocation $TEST_WORKSPACE/quality/sanitizer/test_workspace/asan_fail_heap_out_of_bounds.cpp)" "test_workspace/asan_fail_heap_out_of_bounds.cpp"
50+
ln -sf "$(rlocation $TEST_WORKSPACE/quality/sanitizer/test_workspace/tsan_fail_data_race.cpp)" "test_workspace/tsan_fail_data_race.cpp"
5051

5152
cd test_workspace
5253

5354
if bazel test --config=asan //...; then
5455
exit 1;
55-
else
56-
exit 0;
5756
fi
57+
58+
if bazel test --config=tsan //...; then
59+
exit 1;
60+
fi
61+
62+
exit 0;

quality/sanitizer/test_workspace/BUILD.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ cc_test(
1515
name = "asan_fail_heap_out_of_bounds",
1616
srcs = ["asan_fail_heap_out_of_bounds.cpp"],
1717
)
18+
19+
cc_test(
20+
name = "tsan_fail_data_race",
21+
srcs = ["tsan_fail_data_race.cpp"],
22+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
#include <cstdint>
14+
#include <thread>
15+
16+
// This is a very basic example of a data-race condition, which should be found by a thread-sanitizer
17+
int main()
18+
{
19+
std::uint32_t some_number{0};
20+
21+
std::thread first_thread{[&some_number]() {
22+
for (int i = 0; i < 100; i++)
23+
{
24+
some_number++;
25+
}
26+
}};
27+
28+
std::thread second_thread{[&some_number]() {
29+
for (int i = 0; i < 100; i++)
30+
{
31+
some_number++;
32+
}
33+
}};
34+
35+
first_thread.join();
36+
second_thread.join();
37+
return 0;
38+
}

0 commit comments

Comments
 (0)