From c05282c38ce811005715618ab549135382f9fadb Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sun, 9 Jan 2022 17:47:55 +1100 Subject: [PATCH] [host] cmake: add ubsan and asan to CMake (needs clang64 on windows) Note that this still is a pain to make work as you require the MS runtime DLLs, and to build with clang64. --- host/CMakeLists.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 01daae38..09fd76a2 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -29,6 +29,12 @@ include(UninstallTarget) option(ENABLE_BACKTRACE "Enable backtrace support on crash" ON) add_feature_info(ENABLE_BACKTRACE ENABLE_BACKTRACE "Backtrace support.") +option(ENABLE_ASAN "Build with AddressSanitizer" OFF) +add_feature_info(ENABLE_ASAN ENABLE_ASAN "AddressSanitizer support.") + +option(ENABLE_UBSAN "Build with UndefinedBehaviorSanitizer" OFF) +add_feature_info(ENABLE_UBSAN ENABLE_UBSAN "UndefinedBehaviorSanitizer support.") + add_compile_options( "-Wall" "-Werror" @@ -44,6 +50,16 @@ if(WIN32) add_compile_options("-fdebug-prefix-map=${PROJECT_TOP}/=") endif() +if(ENABLE_ASAN) + add_compile_options("-fno-omit-frame-pointer" "-fsanitize=address") + set(EXE_FLAGS "${EXE_FLAGS} -fno-omit-frame-pointer -fsanitize=address") +endif() + +if(ENABLE_UBSAN) + add_compile_options("-fsanitize=undefined") + set(EXE_FLAGS "${EXE_FLAGS} -fsanitize=undefined") +endif() + add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/version.c ${CMAKE_BINARY_DIR}/include/version.h @@ -60,6 +76,12 @@ include_directories( #link_libraries( #) +if (ENABLE_ASAN OR ENABLE_UBSAN) + link_directories("c:/msys64/clang64/lib/clang/12.0.0/lib/windows") + link_libraries(clang_rt.asan_dynamic-x86_64 clang_rt.asan_dynamic_runtime_thunk-x86_64) +endif() + + set(SOURCES ${CMAKE_BINARY_DIR}/version.c src/app.c