Skip to content

Commit

Permalink
Asan (#323)
Browse files Browse the repository at this point in the history
### What problem were solved in this pull request?

Issue Number: 
close #253 
close #254 

Problem:
参考libzmq,利用sanity工具帮助代码更健壮
  • Loading branch information
hnwyllmm authored Nov 28, 2023
1 parent 90499c9 commit 63b1f03
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)

OPTION(ENABLE_ASAN "Enable build with address sanitizer" ON)
OPTION(ENABLE_TSAN "Build with thread sanitizer" OFF)
OPTION(ENABLE_UBSAN "Build with undefined behavior sanitizer" OFF)
OPTION(WITH_UNIT_TESTS "Compile miniob with unit tests" ON)
OPTION(CONCURRENCY "Support concurrency operations" OFF)
OPTION(STATIC_STDLIB "Link std library static or dynamic, such as libgcc, libstdc++, libasan" OFF)
Expand Down Expand Up @@ -61,12 +63,44 @@ IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND ${STATIC_STDLIB})
ENDIF()

IF (ENABLE_ASAN)
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize-address-use-after-scope")
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND ${STATIC_STDLIB})
ADD_LINK_OPTIONS(-static-libasan)
ENDIF()
ENDIF()

IF (ENABLE_TSAN)
# supported flags
# https://github.com/google/sanitizers/wiki/ThreadSanitizerFlags
MESSAGE(STATUS "Instrumenting with Thread Sanitizer")
SET(TSAN_FLAGS "-fno-omit-frame-pointer -fsanitize=thread")
SET(TSAN_CCFLAGS "${TSAN_CCFLAGS} -mllvm -tsan-instrument-memory-accesses=0")
SET(TSAN_CCFLAGS "${TSAN_CCFLAGS} -mllvm -tsan-instrument-atomics=0")
SET(TSAN_CCFLAGS "${TSAN_CCFLAGS} -mllvm -tsan-instrument-func-entry-exit=1")
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} ${TSAN_FLAGS} ${TSAN_CCFLAGS}")
# -Qunused-arguments 有些编译器不支持,所以先删掉
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND ${STATIC_STDLIB})
ADD_LINK_OPTIONS(-static-libtsan)
ENDIF()
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${TSAN_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TSAN_FLAGS}")
ENDIF (ENABLE_TSAN)

IF (ENABLE_UBSAN)
# Only success on Mac Clang
MESSAGE(STATUS "Instrumenting with Undefined Behavior Sanitizer")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fno-omit-frame-pointer")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize=undefined")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize=implicit-conversion")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize=implicit-integer-truncation")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize=integer")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize=nullability")
SET(UBSAN_FLAGS "${UBSAN_FLAGS} -fsanitize=vptr")
SET(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} ${UBSAN_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${UBSAN_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${UBSAN_FLAGS}")
ENDIF (ENABLE_UBSAN)

IF (CMAKE_INSTALL_PREFIX)
MESSAGE(STATUS "CMAKE_INSTALL_PREFIX has been set as " ${CMAKE_INSTALL_PREFIX} )
ELSEIF(DEFINED ENV{CMAKE_INSTALL_PREFIX})
Expand Down

0 comments on commit 63b1f03

Please sign in to comment.