Use ASAN for testing on CI.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-07-18 14:22:21 +08:00
parent 02cf3482ed
commit 6ccc2161da
4 changed files with 26 additions and 0 deletions

View File

@ -136,6 +136,7 @@ jobs:
cmake .. -DCMAKE_CXX_STANDARD=17 \
-DCMAKE_CXX_STANDARD_REQUIRED=TRUE \
-DCMAKE_BUILD_TYPE=Debug \
-DETCD_USE_ASAN=ON \
-DBUILD_ETCD_TESTS=ON \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache

View File

@ -30,6 +30,27 @@ option(BUILD_SHARED_LIBS "Build etcd-cpp-apiv3 shared libraries" ON)
option(BUILD_ETCD_TESTS "Build etcd-cpp-apiv3 test cases" OFF)
option(CMAKE_POSITION_INDEPENDENT_CODE "Build etcd-cpp-apiv3 with -fPIC" ON)
option(ETCD_W_STRICT "Build etcd-cpp-apiv3 with -Werror" ON)
option(ETCD_USE_ASAN "Using address sanitizer to check memory accessing" OFF)
macro(target_add_link_options target scope)
set(options)
set(oneValueArgs)
set(multiValueArgs OPTIONS)
cmake_parse_arguments(target_add_link_options "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(${CMAKE_VERSION} VERSION_LESS "3.13")
target_link_libraries(${target} INTERFACE ${target_add_link_options_OPTIONS})
else()
target_link_options(${target} ${scope} ${target_add_link_options_OPTIONS})
endif()
endmacro()
macro(target_enable_sanitizer target visibility)
if(ETCD_USE_ASAN)
message(STATUS "${target} will be bulit with -fsanitize=address")
target_compile_options(${target} ${visibility} -fno-omit-frame-pointer -fsanitize=address)
target_add_link_options(${target} ${visibility} OPTIONS -fsanitize=address)
endif()
endmacro()
# reference: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#always-full-rpath
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

View File

@ -19,6 +19,7 @@ file(GLOB_RECURSE CPP_CLIENT_CORE_SRC
add_library(etcd-cpp-api-core-objects OBJECT ${CPP_CLIENT_CORE_SRC} ${PROTOBUF_GENERATES})
add_dependencies(etcd-cpp-api-core-objects protobuf_generates)
target_enable_sanitizer(etcd-cpp-api-core-objects PUBLIC)
include_generated_protobuf_files(etcd-cpp-api-core-objects)
# add the core library, includes the sycnhronous client only
@ -29,6 +30,7 @@ target_link_libraries(etcd-cpp-api-core PUBLIC
${OPENSSL_LIBRARIES}
${GRPC_LIBRARIES}
)
target_enable_sanitizer(etcd-cpp-api-core PUBLIC)
include_generated_protobuf_files(etcd-cpp-api-core)
# add the client with asynchronus client
@ -41,6 +43,7 @@ target_link_libraries(etcd-cpp-api PUBLIC
${OPENSSL_LIBRARIES}
${GRPC_LIBRARIES}
)
target_enable_sanitizer(etcd-cpp-api PUBLIC)
include_generated_protobuf_files(etcd-cpp-api)
if("${CMAKE_VERSION}" VERSION_LESS "3.14")

View File

@ -18,6 +18,7 @@ foreach(testfile ${TEST_FILES})
else()
add_executable(${test_name} EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/${testfile})
endif()
target_enable_sanitizer(${test_name} PUBLIC)
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
target_include_directories(${test_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../proto/gen)