Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sanitizer features #172

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions tests/sanitizers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")

cc_binary(
name = "asan_example",
srcs = ["asan_example.cpp"],
features = ["asan"],
)

cc_binary(
name = "tsan_example",
srcs = ["tsan_example.cpp"],
features = ["tsan"],
)

cc_binary(
name = "ubsan_example",
srcs = ["ubsan_example.cpp"],
features = ["ubsan"],
)

sh_test(
name = "asan_test",
srcs = ["sanitizer_test.sh"],
args = [
"'ERROR: AddressSanitizer: stack-use-after-scope'",
],
data = [
":asan_example",
],
env = {
"BINARY": "$(rootpath :asan_example)",
},
)

sh_test(
name = "tsan_test",
srcs = ["sanitizer_test.sh"],
args = [
"'WARNING: ThreadSanitizer: data race (pid=.*)'",
"'Write of size .* at .* by thread T.*:'",
"'Previous write of size .* at .* by thread T.*:'",
],
data = [
":tsan_example",
],
env = {
"BINARY": "$(rootpath :tsan_example)",
},
)

sh_test(
name = "ubsan_test",
srcs = ["sanitizer_test.sh"],
args = [
"'sanitizers/ubsan_example.cpp:3:11: runtime error: division by zero'",
"'SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior sanitizers/ubsan_example.cpp:3:11 in'",
],
data = [
":ubsan_example",
],
env = {
"BINARY": "$(rootpath :ubsan_example)",
},
)
10 changes: 10 additions & 0 deletions tests/sanitizers/asan_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include <string>

auto main() -> int
{
const auto f = [] () -> const auto& {
return std::string{"hello, world!"};
};
std::cout << f() << "\n";
}
8 changes: 8 additions & 0 deletions tests/sanitizers/sanitizer_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -eu

output=$($BINARY 2>&1 || true)

for arg in "$@"; do
echo $output | grep -q "$arg"
done
14 changes: 14 additions & 0 deletions tests/sanitizers/tsan_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <thread>

auto main() -> int
{
auto i = int{};
const auto inc = [&i] { ++i; };

auto t1 = std::thread{inc};
auto t2 = std::thread{inc};
t1.join();
t2.join();

return i;
}
4 changes: 4 additions & 0 deletions tests/sanitizers/ubsan_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
auto main() -> int
{
return 1/0;
}
2 changes: 1 addition & 1 deletion toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

load(
"@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl",
"//toolchain:unix_cc_toolchain_config.bzl",
unix_cc_toolchain_config = "cc_toolchain_config",
)
load(
Expand Down
Loading