Skip to content

Commit 1ed4f04

Browse files
committed
Add support for sanitizers including some GHAs
1 parent bd1dd25 commit 1ed4f04

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: cmake Ubuntu with Address and Undefined Behavior Sanitizers
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
env:
11+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
12+
BUILD_TYPE: Debug
13+
14+
jobs:
15+
build:
16+
# The CMake configure and build commands are platform agnostic and should work equally
17+
# well on Windows or Mac. You can convert this to a matrix build if you need
18+
# cross-platform coverage.
19+
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
20+
runs-on: ${{ matrix.os }}
21+
strategy:
22+
matrix:
23+
os: [ubuntu-22.04]
24+
25+
steps:
26+
- uses: actions/checkout@v2
27+
28+
- name: Install Conan
29+
id: conan
30+
uses: turtlebrowser/get-conan@main
31+
32+
- name: Create default profile
33+
run: conan profile detect
34+
35+
- name: Install conan dependencies
36+
run: conan install conanfile.py -s build_type=${{env.BUILD_TYPE}} --build=missing
37+
38+
- name: Normalize build type
39+
shell: bash
40+
# The build type is Capitalized, e.g. Release, but the preset is all lowercase, e.g. release.
41+
# There is no built in way to do string manipulations on GHA as far as I know.`
42+
run: echo "BUILD_TYPE_LOWERCASE=$(echo "${BUILD_TYPE}" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
43+
44+
- name: Configure CMake
45+
shell: bash
46+
run: cmake --preset conan-${{ env.BUILD_TYPE_LOWERCASE }} -DBTCPP_ENABLE_ASAN:BOOL=ON -DBTCPP_ENABLE_UBSAN:BOOL=ON
47+
48+
- name: Build
49+
shell: bash
50+
run: cmake --build --preset conan-${{ env.BUILD_TYPE_LOWERCASE }}
51+
52+
- name: run test (Linux + Address and Undefined Behavior Sanitizers)
53+
env:
54+
GTEST_COLOR: "On"
55+
ASAN_OPTIONS: "color=always"
56+
UBSAN_OPTIONS: "halt_on_error=1:print_stacktrace=1:color=always"
57+
run: ctest --test-dir build/${{env.BUILD_TYPE}}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: cmake Ubuntu with Thread Sanitizer
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
env:
11+
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
12+
BUILD_TYPE: Debug
13+
14+
jobs:
15+
build:
16+
# The CMake configure and build commands are platform agnostic and should work equally
17+
# well on Windows or Mac. You can convert this to a matrix build if you need
18+
# cross-platform coverage.
19+
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
20+
runs-on: ${{ matrix.os }}
21+
strategy:
22+
matrix:
23+
os: [ubuntu-22.04]
24+
25+
steps:
26+
- uses: actions/checkout@v2
27+
28+
- name: Install Conan
29+
id: conan
30+
uses: turtlebrowser/get-conan@main
31+
32+
- name: Create default profile
33+
run: conan profile detect
34+
35+
- name: Install conan dependencies
36+
run: conan install conanfile.py -s build_type=${{env.BUILD_TYPE}} --build=missing
37+
38+
- name: Normalize build type
39+
shell: bash
40+
# The build type is Capitalized, e.g. Release, but the preset is all lowercase, e.g. release.
41+
# There is no built in way to do string manipulations on GHA as far as I know.`
42+
run: echo "BUILD_TYPE_LOWERCASE=$(echo "${BUILD_TYPE}" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
43+
44+
- name: Configure CMake
45+
shell: bash
46+
run: cmake --preset conan-${{ env.BUILD_TYPE_LOWERCASE }} -DBTCPP_ENABLE_TSAN:BOOL=ON
47+
48+
- name: Build
49+
shell: bash
50+
run: cmake --build --preset conan-${{ env.BUILD_TYPE_LOWERCASE }}
51+
52+
- name: run test (Linux + Thread Sanitizer)
53+
env:
54+
GTEST_COLOR: "On"
55+
TSAN_OPTIONS: "color=always"
56+
run: sudo sysctl vm.mmap_rnd_bits=28 && ctest --test-dir build/${{env.BUILD_TYPE}}

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ option(BTCPP_EXAMPLES "Build tutorials and examples" ON)
1313
option(BUILD_TESTING "Build the unit tests" ON)
1414
option(BTCPP_GROOT_INTERFACE "Add Groot2 connection. Requires ZeroMQ" ON)
1515
option(BTCPP_SQLITE_LOGGING "Add SQLite logging." ON)
16+
option(BTCPP_ENABLE_ASAN "Enable Address Sanitizer" OFF)
17+
option(BTCPP_ENABLE_UBSAN "Enable Undefined Behavior Sanitizer" OFF)
18+
option(BTCPP_ENABLE_TSAN "Enable Thread Sanitizer" OFF)
1619

1720
option(USE_V3_COMPATIBLE_NAMES "Use some alias to compile more easily old 3.x code" OFF)
1821
option(ENABLE_FUZZING "Enable fuzzing builds" OFF)
@@ -54,6 +57,8 @@ endif()
5457
set(CMAKE_CONFIG_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
5558
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CONFIG_PATH}")
5659

60+
include(sanitizers)
61+
5762
set(BTCPP_LIBRARY ${PROJECT_NAME})
5863

5964
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)

cmake/sanitizers.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
if(BTCPP_ENABLE_ASAN OR BTCPP_ENABLE_UBSAN OR BTCPP_ENABLE_TSAN)
2+
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug|RelWithDebInfo")
3+
message(FATAL_ERROR "Sanitizers require debug symbols. Please set CMAKE_BUILD_TYPE to Debug or RelWithDebInfo.")
4+
endif()
5+
add_compile_options(-fno-omit-frame-pointer)
6+
endif()
7+
8+
# Address Sanitizer and Undefined Behavior Sanitizer can be run at the same time.
9+
# Thread Sanitizer requires its own build.
10+
if(BTCPP_ENABLE_TSAN AND (BTCPP_ENABLE_ASAN OR BTCPP_ENABLE_UBSAN))
11+
message(FATAL_ERROR "TSAN is not compatible with ASAN or UBSAN. Please enable only one of them.")
12+
endif()
13+
14+
if(BTCPP_ENABLE_ASAN)
15+
message(STATUS "Address Sanitizer enabled")
16+
add_compile_options(-fsanitize=address)
17+
add_link_options(-fsanitize=address)
18+
endif()
19+
20+
if(BTCPP_ENABLE_UBSAN)
21+
message(STATUS "Undefined Behavior Sanitizer enabled")
22+
add_compile_options(-fsanitize=undefined)
23+
add_link_options(-fsanitize=undefined)
24+
endif()
25+
26+
if(BTCPP_ENABLE_TSAN)
27+
message(STATUS "Thread Sanitizer enabled")
28+
add_compile_options(-fsanitize=thread)
29+
add_link_options(-fsanitize=thread)
30+
endif()

0 commit comments

Comments
 (0)