Skip to content

Commit

Permalink
Add cc_asan_test example
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Sep 29, 2023
1 parent a90b437 commit c1a8dd4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, macos-12, windows-2019]
os: [ubuntu-20.04, macos-12, windows-2022]
bazelversion: [last_rc, last_green]
folder: [".", examples]
exclude:
- os: windows-2019
- os: windows-2022
folder: "."
- os: macos-12
folder: "."
Expand Down Expand Up @@ -64,6 +64,16 @@ jobs:
working-directory: ${{ matrix.folder }}
run: echo "${{ matrix.bazelversion }}" > .bazelversion

- name: Add ASAN runtime DLL to PATH on Windows
if: matrix.os == 'windows-2022'
run: |
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.16.27023"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.16.27023\bin"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.16.27023\bin\Hostx64"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64"
dir "C:\Program Files\Microsoft Visual Studio\Installer"
echo "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64" >> $GITHUB_PATH
- name: bazel test //...
env:
# Bazelisk will download bazel to here.
Expand Down
6 changes: 6 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
common --enable_platform_specific_config
common --enable_bzlmod

# Prevent Java compiler warnings such as:
Expand All @@ -16,5 +17,10 @@ common --check_bazel_compatibility=warning

# Improve build caching by redacting environment variables.
common --incompatible_strict_action_env
# On Windows, PATH doubles as the dynamic library search path. We need its
# unmodified value to find libraries in non-standard location, such as the
# ASAN runtime for cc_asan_test.
common:windows --noincompatible_strict_action_env
common:windows --test_env=PATH
# Allow build to start before all external deps have been fetched.
common --experimental_merged_skyframe_analysis_execution
20 changes: 20 additions & 0 deletions examples/cc_asan_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load(":defs.bzl", "cc_asan_test")

cc_asan_test(
name = "asan_test",
srcs = ["asan_test.cpp"],
env = {
# Effectively invert the exit code so that the test passes if and only
# if the expected ASAN error is detected.
"ASAN_OPTIONS": "exitcode=0:abort_on_error=0",
},
deps = [
":lib",
],
)

cc_library(
name = "lib",
srcs = ["lib.cpp"],
hdrs = ["lib.h"],
)
9 changes: 9 additions & 0 deletions examples/cc_asan_test/asan_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "cc_asan_test/lib.h"

#include <iostream>

int main() {
trigger_asan();
std::cerr << "Did not get expected AddressSanitizer error, is the test instrumented correctly?" << std::endl;
return 1;
}
17 changes: 17 additions & 0 deletions examples/cc_asan_test/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@with_cfg.bzl", "with_cfg")

cc_asan_test, _cc_asan_test_internal = with_cfg(
native.cc_test,
).extend(
"copt",
select({
"@rules_cc//cc/compiler:msvc-cl": ["/fsanitize=address"],
"//conditions:default": ["-fsanitize=address"],
}),
).extend(
"linkopt",
select({
"@rules_cc//cc/compiler:msvc-cl": [],
"//conditions:default": ["-fsanitize=address"],
}),
).build()
8 changes: 8 additions & 0 deletions examples/cc_asan_test/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "cc_asan_test/lib.h"

int trigger_asan() {
int* array = new int[100] {};
delete[] array;
// Triggers a heap-use-after-free error.
return array[0];
}
3 changes: 3 additions & 0 deletions examples/cc_asan_test/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int trigger_asan();

0 comments on commit c1a8dd4

Please sign in to comment.