Skip to content

Commit

Permalink
#11453: Add options to enable compilation with sanitizers
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickroberts committed Sep 3, 2024
1 parent 15e3ed0 commit fbf65c3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
46 changes: 42 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ message(STATUS "Build shared libs: ${BUILD_SHARED_LIBS}")
option(ENABLE_ASAN "Enable build with AddressSanitizer" OFF)
message(STATUS "Build with ASAN: ${ENABLE_ASAN}")

set(SANITIZER_ENABLED ${ENABLE_ASAN})

option(ENABLE_MSAN "Enable build with MemorySanitizer" OFF)
message(STATUS "Build with MSAN: ${ENABLE_MSAN}")

if(SANITIZER_ENABLED AND ENABLE_MSAN)
message(FATAL_ERROR "Multiple sanitizers are not supported")
elseif(ENABLE_MSAN)
set(SANITIZER_ENABLED ${ENABLE_MSAN})
endif()

option(ENABLE_TSAN "Enable build with ThreadSanitizer" OFF)
message(STATUS "Build with TSAN: ${ENABLE_TSAN}")

if(SANITIZER_ENABLED AND ENABLE_TSAN)
message(FATAL_ERROR "Multiple sanitizers are not supported")
elseif(ENABLE_TSAN)
set(SANITIZER_ENABLED ${ENABLE_TSAN})
endif()

option(ENABLE_UBSAN "Enable build with UndefinedBehaviorSanitizer" OFF)
message(STATUS "Build with UBSAN: ${ENABLE_UBSAN}")

if(SANITIZER_ENABLED AND ENABLE_UBSAN)
message(FATAL_ERROR "Multiple sanitizers are not supported")
endif()

unset(SANITIZER_ENABLED)

include(GNUInstallDirs)
set(CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}")
set(CMAKE_INSTALL_LIBDIR "${PROJECT_BINARY_DIR}/lib")
Expand Down Expand Up @@ -169,10 +198,19 @@ if(ENABLE_TRACY)
target_compile_options(compiler_flags INTERFACE -DTRACY_ENABLE -fno-omit-frame-pointer)
target_link_options(linker_flags INTERFACE -rdynamic)
endif()
if(ENABLE_ASAN)
target_compile_options(compiler_flags INTERFACE -fsanitize=address)
target_link_options(linker_flags INTERFACE -fsanitize=address)
endif()
target_compile_options(
compiler_flags
INTERFACE $<$<BOOL:${ENABLE_ASAN}>:-fsanitize=address>
$<$<BOOL:${ENABLE_MSAN}>:-fsanitize=memory>
$<$<BOOL:${ENABLE_TSAN}>:-fsanitize=thread>
$<$<BOOL:${ENABLE_UBSAN}>:-fsanitize=undefined>)
target_link_options(
linker_flags
INTERFACE
$<$<BOOL:${ENABLE_ASAN}>:-fsanitize=address>
$<$<BOOL:${ENABLE_MSAN}>:-fsanitize=memory>
$<$<BOOL:${ENABLE_TSAN}>:-fsanitize=thread>
$<$<BOOL:${ENABLE_UBSAN}>:-fsanitize=undefined>)

string(TOUPPER $ENV{ARCH_NAME} ARCH_NAME_DEF)
target_compile_options(compiler_flags INTERFACE -DARCH_${ARCH_NAME_DEF})
Expand Down
32 changes: 31 additions & 1 deletion build_metal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ show_help() {
echo " -b Set the build type. Default is Release. Other options are Debug, RelWithDebInfo, and CI."
echo " -t Enable build time trace (clang only)."
echo " -a Enable AddressSanitizer."
echo " -m Enable MemorySanitizer."
echo " -s Enable ThreadSanitizer."
echo " -u Enable UndefinedBehaviorSanitizer."
}

# Parse CLI options
export_compile_commands="OFF"
enable_ccache="OFF"
enable_time_trace="OFF"
enable_asan="OFF"
enable_msan="OFF"
enable_tsan="OFF"
enable_ubsan="OFF"
build_type="Release"

while getopts "hectab:" opt; do
while getopts "hectamsub:" opt; do
case ${opt} in
h )
show_help
Expand All @@ -85,6 +91,15 @@ while getopts "hectab:" opt; do
a )
enable_asan="ON"
;;
m )
enable_msan="ON"
;;
s )
enable_tsan="ON"
;;
u )
enable_ubsan="ON"
;;
b )
build_type="$OPTARG"
;;
Expand All @@ -106,6 +121,9 @@ echo "Enable ccache: $enable_ccache"
echo "Build type: $build_type"
echo "Enable time trace: $enable_time_trace"
echo "Enable AddressSanitizer: $enable_asan"
echo "Enable MemorySanitizer: $enable_msan"
echo "Enable ThreadSanitizer: $enable_tsan"
echo "Enable UndefinedBehaviorSanitizer: $enable_ubsan"

# Create and link the build directory
mkdir -p build_$build_type
Expand All @@ -127,6 +145,18 @@ if [ "$enable_asan" = "ON" ]; then
cmake_args="$cmake_args -DENABLE_ASAN=ON"
fi

if [ "$enable_msan" = "ON" ]; then
cmake_args="$cmake_args -DENABLE_MSAN=ON"
fi

if [ "$enable_tsan" = "ON" ]; then
cmake_args="$cmake_args -DENABLE_TSAN=ON"
fi

if [ "$enable_ubsan" = "ON" ]; then
cmake_args="$cmake_args -DENABLE_UBSAN=ON"
fi

# Configure cmake
cmake $cmake_args

Expand Down

0 comments on commit fbf65c3

Please sign in to comment.