Skip to content

Commit 35273d0

Browse files
committed
Thread and Address Sanitizer CI
Signed-off-by: Tyler Weaver <[email protected]>
1 parent 06d655d commit 35273d0

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

.github/workflows/ci.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This config uses industrial_ci (https://github.com/ros-industrial/industrial_ci.git).
2+
# For troubleshooting, see readme (https://github.com/ros-industrial/industrial_ci/blob/master/README.rst)
3+
4+
name: CI
5+
6+
on:
7+
workflow_dispatch:
8+
pull_request:
9+
push:
10+
branches:
11+
- ros2
12+
13+
jobs:
14+
ci:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
env:
19+
- TARGET_CMAKE_ARGS: -DENABLE_SANITIZER_ADDRESS=ON -DCMAKE_BUILD_TYPE=Debug
20+
NAME: Address Sanitizer
21+
- TARGET_CMAKE_ARGS: -DENABLE_SANITIZER_THREAD=ON -DCMAKE_BUILD_TYPE=Debug
22+
NAME: Thread Sanitizer
23+
env:
24+
ROS_DISTRO: rolling
25+
runs-on: ubuntu-latest
26+
name: ${{ matrix.env.NAME }}
27+
steps:
28+
- uses: actions/checkout@v2
29+
- uses: 'ros-industrial/industrial_ci@master'
30+
env: ${{matrix.env}}

cmake/sanitizers.cmake

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function(enable_sanitizers project_name)
2+
3+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
4+
option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" OFF)
5+
6+
if(ENABLE_COVERAGE)
7+
target_compile_options(${project_name} INTERFACE --coverage -O0 -g -fno-omit-frame-pointer)
8+
target_link_libraries(${project_name} INTERFACE --coverage)
9+
endif()
10+
11+
set(SANITIZERS "")
12+
13+
option(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" OFF)
14+
if(ENABLE_SANITIZER_ADDRESS)
15+
list(APPEND SANITIZERS "address")
16+
endif()
17+
18+
option(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" OFF)
19+
if(ENABLE_SANITIZER_LEAK)
20+
list(APPEND SANITIZERS "leak")
21+
endif()
22+
23+
option(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" OFF)
24+
if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
25+
list(APPEND SANITIZERS "undefined")
26+
endif()
27+
28+
option(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" OFF)
29+
if(ENABLE_SANITIZER_THREAD)
30+
if("address" IN_LIST SANITIZERS OR "leak" IN_LIST SANITIZERS)
31+
message(WARNING "Thread sanitizer does not work with Address and Leak sanitizer enabled")
32+
else()
33+
list(APPEND SANITIZERS "thread")
34+
endif()
35+
endif()
36+
37+
option(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" OFF)
38+
if(ENABLE_SANITIZER_MEMORY AND CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
39+
message(WARNING "Memory sanitizer requires all the code (including libc++) \
40+
to be MSan-instrumented otherwise it reports false positives")
41+
if("address" IN_LIST SANITIZERS
42+
OR "thread" IN_LIST SANITIZERS
43+
OR "leak" IN_LIST SANITIZERS)
44+
message(WARNING "Memory sanitizer does not work with Address, Thread and Leak sanitizer enabled")
45+
else()
46+
list(APPEND SANITIZERS "memory")
47+
endif()
48+
endif()
49+
50+
list(
51+
JOIN
52+
SANITIZERS
53+
","
54+
LIST_OF_SANITIZERS)
55+
56+
endif()
57+
58+
if(LIST_OF_SANITIZERS)
59+
if(NOT
60+
"${LIST_OF_SANITIZERS}"
61+
STREQUAL
62+
"")
63+
target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
64+
target_link_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
65+
endif()
66+
endif()
67+
68+
endfunction()

test/CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ find_package(ament_cmake_gtest REQUIRED)
22

33
include("../cmake/class_loader_hide_library_symbols.cmake")
44

5+
# Link this 'library' to set the compile-time options requested
6+
add_library(project_options INTERFACE)
7+
include(../cmake/sanitizers.cmake)
8+
enable_sanitizers(project_options)
9+
510
add_library(${PROJECT_NAME}_TestPlugins1 EXCLUDE_FROM_ALL SHARED plugins1.cpp)
611
if(ament_cmake_FOUND)
712
target_include_directories(${PROJECT_NAME}_TestPlugins1
@@ -60,7 +65,7 @@ if(TARGET ${PROJECT_NAME}_utest)
6065
PUBLIC "../include" ${console_bridge_INCLUDE_DIRS})
6166
target_link_libraries(${PROJECT_NAME}_utest)
6267
endif()
63-
target_link_libraries(${PROJECT_NAME}_utest ${PROJECT_NAME})
68+
target_link_libraries(${PROJECT_NAME}_utest ${PROJECT_NAME} project_options)
6469
if(NOT WIN32)
6570
target_link_libraries(${PROJECT_NAME}_utest pthread)
6671
endif()
@@ -83,7 +88,7 @@ if(TARGET ${PROJECT_NAME}_unique_ptr_test)
8388
PUBLIC "../include")
8489
target_link_libraries(${PROJECT_NAME}_unique_ptr_test)
8590
endif()
86-
target_link_libraries(${PROJECT_NAME}_unique_ptr_test ${PROJECT_NAME})
91+
target_link_libraries(${PROJECT_NAME}_unique_ptr_test ${PROJECT_NAME} project_options)
8792
if(NOT WIN32)
8893
target_link_libraries(${PROJECT_NAME}_unique_ptr_test pthread)
8994
endif()

0 commit comments

Comments
 (0)