add cmake option to build with ubsan
@@ -20,6 +20,7 @@ option(MI_BUILD_STATIC "Build static library" ON)
option(MI_BUILD_OBJECT "Build object library" ON)
option(MI_BUILD_TESTS "Build test executables" ON)
option(MI_DEBUG_TSAN "Build with thread sanitizer (needs clang)" OFF)
+option(MI_DEBUG_UBSAN "Build with undefined-behavior sanitizer (needs clang++)" OFF)
option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode (deprecated, use MI_DEBUG_FULL instead)" OFF)
include("cmake/mimalloc-config-version.cmake")
@@ -122,6 +123,34 @@ if(MI_SHOW_ERRORS MATCHES "ON")
list(APPEND mi_defines MI_SHOW_ERRORS=1)
endif()
+if(MI_DEBUG_TSAN MATCHES "ON")
+ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
+ message(STATUS "Build with thread sanitizer (MI_DEBUG_TSAN=ON)")
+ list(APPEND mi_cflags -fsanitize=thread -g -O1)
+ list(APPEND CMAKE_EXE_LINKER_FLAGS -fsanitize=thread)
+ else()
+ message(WARNING "Can only use thread sanitizer with clang (MI_DEBUG_TSAN=ON but ignored)")
+ endif()
+endif()
+
+if(MI_DEBUG_UBSAN MATCHES "ON")
+ if(CMAKE_BUILD_TYPE MATCHES "Debug")
+ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+ message(STATUS "Build with undefined-behavior sanitizer (MI_DEBUG_UBSAN=ON)")
+ list(APPEND mi_cflags -fsanitize=undefined -g)
+ list(APPEND CMAKE_EXE_LINKER_FLAGS -fsanitize=undefined)
+ if (MI_USE_CXX MATCHES "OFF")
+ message(STATUS "(switch to use C++ due to MI_DEBUG_UBSAN)")
+ set(MI_USE_CXX "ON")
+ endif()
+ else()
+ message(WARNING "Can only use undefined-behavior sanitizer with clang++ (MI_DEBUG_UBSAN=ON but ignored)")
+ endif()
+ else()
+ message(WARNING "Can only use thread sanitizer with a debug build (CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})")
+ endif()
+endif()
+
if(MI_USE_CXX MATCHES "ON")
message(STATUS "Use the C++ compiler to compile (MI_USE_CXX=ON)")
set_source_files_properties(${mi_sources} PROPERTIES LANGUAGE CXX )
@@ -134,16 +163,6 @@ if(MI_USE_CXX MATCHES "ON")
endif()
endif()
-if(MI_DEBUG_TSAN MATCHES "ON")
- if(CMAKE_C_COMPILER_ID MATCHES "Clang")
- message(STATUS "Build with thread sanitizer (MI_DEBUG_TSAN=ON)")
- list(APPEND mi_cflags -fsanitize=thread -g -O1)
- list(APPEND CMAKE_EXE_LINKER_FLAGS -fsanitize=thread)
- else()
- message(WARNING "Can only use thread sanitizer with clang (MI_DEBUG_TSAN=ON but ignored)")
- endif()
-endif()
-
# Compiler flags
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU")
list(APPEND mi_cflags -Wall -Wextra -Wno-unknown-pragmas -fvisibility=hidden)
