From 6ccc2161da82f116bf510ec5d4159198af787ac9 Mon Sep 17 00:00:00 2001 From: Tao He Date: Mon, 18 Jul 2022 14:22:21 +0800 Subject: [PATCH] Use ASAN for testing on CI. Signed-off-by: Tao He --- .github/workflows/build-test.yml | 1 + CMakeLists.txt | 21 +++++++++++++++++++++ src/CMakeLists.txt | 3 +++ tst/CMakeLists.txt | 1 + 4 files changed, 26 insertions(+) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 589f1b4..07cc7d0 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index ca3eb7c..f16e28e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index eeb852e..a2825db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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") diff --git a/tst/CMakeLists.txt b/tst/CMakeLists.txt index ecae58f..4ce8b0d 100644 --- a/tst/CMakeLists.txt +++ b/tst/CMakeLists.txt @@ -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_include_directories(${test_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../proto/gen)