Commit 58c34557 authored by David Frank's avatar David Frank Committed by Tobias Lasser

enable sanitizers

parent 1a279437
Pipeline #227894 canceled with stages
in 15 minutes and 44 seconds
......@@ -236,7 +236,7 @@ test-coverage:
- cuda
### Sanitizers ###
### sanitizers ###
cuda-memcheck:
stage: sanitizer
......@@ -251,41 +251,48 @@ cuda-memcheck:
- gcc
- cuda
#thread-sanitizer:
# <<: *nightly_job
# stage: sanitizer
# image: $CUDA_IMAGE
# script:
# - mkdir -p build
# - cd build
# - cmake -DCMAKE_BUILD_TYPE=Debug -DELSA_SANITIZE_THREAD=ON ..
# - make all -j4
# - ctest # let's run tests for now, we should run apps though as well!
# dependencies:
# - test-cuda92
# tags:
# - linux
# - elsa
# - gcc
# - cuda
#
#address-sanitizer:
# <<: *nightly_job
# stage: sanitizer
# image: $CUDA_IMAGE
# script:
# - mkdir -p build
# - cd build
# - cmake -DCMAKE_BUILD_TYPE=Debug -DELSA_SANITIZE_ADDRESS=ON ..
# - make all -j4
# - ctest # let's run tests for now, we should run apps though as well!
# dependencies:
# - test-cuda92
# tags:
# - linux
# - elsa
# - gcc
# - cuda
sanitize-asan-ubsan:
<<: *nightly_job
stage: sanitizer
image: $CUDA_IMAGE
script:
- mkdir -p build
- cd build
- cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DELSA_SANITIZER="Address;Undefined" ..
- ninja tests
tags:
- linux
- elsa
- gcc
- cuda
sanitize-tsan:
<<: *nightly_job
stage: sanitizer
image: $CUDA_IMAGE
script:
- mkdir -p build
- cd build
- CC=clang CXX=clang cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DELSA_SANITIZER="Thread" ..
- ninja tests
tags:
- linux
- elsa
- cuda
sanitize-memsan:
<<: *nightly_job
stage: sanitizer
image: $CUDA_IMAGE
script:
- mkdir -p build
- cd build
- CC=clang CXX=clang cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DELSA_SANITIZER="Memory" ..
- ninja tests
tags:
- linux
- elsa
- cuda
### deploy docs and coverage report ###
......@@ -321,7 +328,7 @@ deploy-docs:
# These jobs, are for local build and test all in one job, should not be run with pipeline
.all-clang:
image: $UBUNTU_IMAGE
image: $CLANG_IMAGE
script:
- mkdir -p build
- cd build
......
......@@ -28,11 +28,14 @@ option(ELSA_BUILD_EXAMPLES "Enable building of examples" ${ELSA_MASTER_PROJECT})
option(ELSA_BUILD_CUDA_PROJECTORS "Enable building (or attempting to) the CUDA projectors" ON)
option(ELSA_BUILD_WITH_MORE_WARNINGS "Enable all and extra warnings when building (-Wall -Wextra)" ON)
option(ELSA_SANITIZE_THREAD "Build elsa with thread sanitizers (TSAN)" OFF)
option(ELSA_SANITIZE_ADDRESS "Build elsa with address and undefined-behavior sanitizers (ASAN and UBSAN)" OFF)
option(ELSA_CUDA_VECTOR "Build elsa with GPU DataContainer support and default" OFF)
set(ELSA_SANITIZER
"" CACHE STRING
"Compile with a sanitizer. Options are: Address, Memory, MemoryWithOrigins, Undefined, Thread, Leak, 'Address;Undefined'"
)
# ------------ general setup -----------
# ------------
......@@ -111,6 +114,13 @@ if(ELSA_CUDA_VECTOR)
endif()
# ------------ setup tools -----------
# ------------
# Includes clang-format, clang-tidy, cmake-format, and sanitizers
include(Tools)
# ------------ setup testing -----------
# ------------
......@@ -157,6 +167,7 @@ else(ELSA_TESTING OR ELSA_BENCHMARKS)
endif(ELSA_TESTING OR ELSA_BENCHMARKS)
# ------------ add code/docs -----------
# ------------
......
# setup sanitizers
if(ELSA_SANITIZE_THREAD AND ELSA_SANITIZE_ADDRESS)
message(FATAL_ERROR "AddressSanitizer is not compatible with ThreadSanitizer.")
endif()
if(ELSA_SANITIZE_ADDRESS)
message(STATUS "Address and undefiened-behavior sanitizer enabled")
set(SANITIZERS "leak,address,undefined,shift,integer-divide-by-zero,unreachable,vla-bound,null,return,signed-integer-overflow")
set(SANITIZER_FLAGS "-ggdb -O1 -fno-sanitize-recover=all -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
endif()
if(ELSA_SANITIZE_THREAD)
message(STATUS "Thread sanitizer enabled")
set(SANITIZER_FLAGS "-ggdb -O1 -fno-sanitize-recover=all -fno-omit-frame-pointer -fsanitize=thread")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${SANITIZER_FLAGS}")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} ${SANITIZER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_LINKER_FLAGS_DEBUG}")
\ No newline at end of file
# Code heavily influenced by https://github.com/StableCoder/cmake-scripts/
# original copyright by George Cave - gcave@stablecoder.ca
# original license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
function(append value)
foreach(variable ${ARGN})
set(${variable}
"${${variable}} ${value}"
PARENT_SCOPE)
endforeach(variable)
endfunction()
if(ELSA_SANITIZER)
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
message(WARNING "Sanitizers should preferably run in Debug mode.")
endif()
append("-fno-omit-frame-pointer -fno-sanitize-recover=all" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
if(UNIX)
set(USING_CLANG FALSE)
if(CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang")
set(USING_CLANG TRUE)
endif()
set(USING_GNU FALSE)
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
set(USING_GNU TRUE)
endif()
if(uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
append("-O1" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
endif()
# Address and UB sanitizer
if(ELSA_SANITIZER MATCHES "([Aa]ddress);([Uu]ndefined)"
OR ELSA_SANITIZER MATCHES "([Uu]ndefined);([Aa]ddress)")
message(STATUS "Building with Address, Undefined sanitizers")
append("-fsanitize=address,undefined" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
# Address sanitizer
elseif(ELSA_SANITIZER MATCHES "([Aa]ddress)")
message(STATUS "Building with Address sanitizer")
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
# Memory sanitizer (only Clang)
elseif(ELSA_SANITIZER MATCHES "([Mm]emory([Ww]ith[Oo]rigins)?)")
if(USING_CLANG AND NOT USING_GNU)
append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
if(ELSA_SANITIZER MATCHES "([Mm]emory[Ww]ith[Oo]rigins)")
message(STATUS "Building with MemoryWithOrigins sanitizer")
append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
else()
message(STATUS "Building with Memory sanitizer")
endif()
else()
message(STATUS "Cannot use Memory sanitizer with GNU compiler. Use Address instead.")
endif()
# UB sanitizer
elseif(ELSA_SANITIZER MATCHES "([Uu]ndefined)")
message(STATUS "Building with Undefined sanitizer")
append("-fsanitize=undefined" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
if(EXISTS "${BLACKLIST_FILE}")
append("-fsanitize-blacklist=${BLACKLIST_FILE}" CMAKE_C_FLAGS
CMAKE_CXX_FLAGS)
endif()
# Thread sanitizer
elseif(ELSA_SANITIZER MATCHES "([Tt]hread)")
message(STATUS "Building with Thread sanitizer")
append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
else()
message(
FATAL_ERROR "Unsupported value of ELSA_SANITIZER: ${ELSA_SANITIZER}")
endif()
elseif(MSVC) # if(UNIX)
if(ELSA_SANITIZER MATCHES "([Aa]ddress)")
message(STATUS "Building with Address sanitizer")
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
else()
message(FATAL_ERROR
"This sanitizer is not yet supported in the MSVC environment: ${ELSA_SANITIZER}"
)
endif()
else() # elseif(MSVC)
message(FATAL_ERROR "ELSA_SANITIZER is not supported on this platform.")
endif() # if (UNIX)
endif() # if(ELSA_SANITIZER)
......@@ -30,13 +30,6 @@ macro(ELSA_TEST NAME)
catch_discover_tests(test_${NAME} TEST_SPEC ${ELSA_JUNIT_ARGUMENTS})
endmacro(ELSA_TEST)
# add sanitizers if in debug mode
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
include(${PROJECT_SOURCE_DIR}/cmake/Sanitizers.cmake)
endif()
# add the elsa modules
add_subdirectory(core)
add_subdirectory(logging)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment