Skip to content

Commit

Permalink
Add support for memory and thread sanitizers
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlaltca committed Jan 28, 2024
1 parent 90f2b31 commit 962c93a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES RelWithDebInfo OR
endif()

option(WANT_STRIP "Strip binaries (discard symbols from object files)" OFF)
option(WANT_SANITIZER "Enable debug sanitizers (requires WANT_DEBUG)" OFF)
set(WANT_SANITIZER OFF CACHE STRING "Enable debug sanitizers (requires WANT_DEBUG)")
set_property(CACHE WANT_SANITIZER PROPERTY STRINGS OFF ADDRESS MEMORY THREAD)

if(WANT_ENV_FLAGS)
set(CMAKE_BUILD_TYPE Undefined)
Expand Down Expand Up @@ -234,10 +235,21 @@ else()
set(CMAKE_C_FLAGS "-O0 -g")
endif()
endif()
if(WANT_SANITIZER)
string(TOUPPER "${WANT_SANITIZER}" WANT_SANITIZER )
if(WANT_SANITIZER STREQUAL ADDRESS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_STATUS_DEBUG_SUPPORT "Yes, with sanitizers")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fsanitize=memory -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_STATUS_DEBUG_SUPPORT "Yes, with address,leak and undefined behavior sanitizers")
elseif(WANT_SANITIZER STREQUAL MEMORY)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fPIE -pie -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fPIE -pie -fno-omit-frame-pointer")
set(CMAKE_STATUS_DEBUG_SUPPORT "Yes, with memory sanitizer")
elseif(WANT_SANITIZER STREQUAL THREAD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_STATUS_DEBUG_SUPPORT "Yes, with memory sanitizer")
elseif(NOT WANT_SANITIZER STREQUAL OFF)
message(FATAL_ERROR "Invalid sanitizer ${WANT_SANITIZER}. Available sanitizers: address, memory, thread")
endif()
else()
if(WANT_STRIP)
Expand Down

0 comments on commit 962c93a

Please sign in to comment.