Add UBSAN flag to builds

The -DWITH_UBSAN flag enables the undefined behavior detection provided by
GCC.
This commit is contained in:
Markus Mäkelä 2019-05-29 18:49:58 +03:00
parent ee7e63a611
commit 4e6ca33053
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
3 changed files with 20 additions and 0 deletions

View File

@ -48,6 +48,7 @@ find_package(GSSAPI)
find_package(SQLite)
find_package(ASAN)
find_package(TSAN)
find_package(UBSAN)
# Build PCRE2 so we always know the version
# Read BuildPCRE2 for details about how to add pcre2 as a dependency to a target
@ -154,6 +155,9 @@ if (WITH_ASAN AND ASAN_FOUND)
elseif (WITH_TSAN AND TSAN_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
elseif (WITH_UBSAN AND UBSAN_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -D_GNU_SOURCE=1 ${FLAGS}")

14
cmake/FindUBSAN.cmake Normal file
View File

@ -0,0 +1,14 @@
# Find UBSan libraries
#
# The following variables are set:
# UBSAN_FOUND - If UBSan was found
# UBSAN_LIBRARIES - Path to the libubsan library
find_library(UBSAN_LIBRARIES NAMES libubsan.so.0 libubsan.so.1 libubsan.so.2 libubsan.so.3 libubsan.so.4)
if (UBSAN_LIBRARIES)
message(STATUS "Found UBSan libraries: ${UBSAN_LIBRARIES}")
set(UBSAN_FOUND TRUE CACHE INTERNAL "")
else()
message(STATUS "Could not find UBSan")
endif()

View File

@ -72,6 +72,8 @@ target_link_libraries(maxscale-common
if(WITH_ASAN AND ASAN_FOUND)
target_link_libraries(maxscale-common ${ASAN_LIBRARIES})
elseif(WITH_UBSAN AND UBSAN_FOUND)
target_link_libraries(maxscale-common ${UBSAN_LIBRARIES})
endif()
find_library(HAVE_LIBDL NAMES dl)