summaryrefslogtreecommitdiffstats
path: root/cmake/Sanitizer.cmake
blob: c2587066ff92b9f0b6bbbabb8d733a296af1263d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Ported from Clickhouse: https://github.com/ClickHouse/ClickHouse/blob/master/cmake/sanitize.cmake

set (SAN_FLAGS "${SAN_FLAGS} -g -fno-omit-frame-pointer -DSANITIZER")
# O1 is normally set by clang, and -Og by gcc
if (COMPILER_GCC)
    if (ENABLE_FULL_DEBUG MATCHES "ON")
        set (SAN_FLAGS "${SAN_FLAGS} -O0")
    else()
        set (SAN_FLAGS "${SAN_FLAGS} -Og")
    endif()
else ()
    if (ENABLE_FULL_DEBUG MATCHES "ON")
        set (SAN_FLAGS "${SAN_FLAGS} -O0")
    else()
        set (SAN_FLAGS "${SAN_FLAGS} -O1")
    endif()
endif ()
if (SANITIZE)
    if (ENABLE_JEMALLOC MATCHES "ON")
        message (STATUS "Jemalloc support is useless in case of build with sanitizers")
        set (ENABLE_JEMALLOC "OFF")
    endif ()

    string(REPLACE "," ";" SANITIZE_LIST ${SANITIZE})
    foreach(SANITIZE ${SANITIZE_LIST})
        if (SANITIZE STREQUAL "address")
            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope")
            if (COMPILER_GCC)
                set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libasan")
                set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libasan")
                set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libasan")
            endif ()

        elseif (SANITIZE STREQUAL "leak")
            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=leak")
            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=leak")
            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=leak")

        elseif (SANITIZE STREQUAL "memory")
            set (MSAN_FLAGS "-fsanitize=memory -fsanitize-memory-track-origins -fno-optimize-sibling-calls")

            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MSAN_FLAGS}")
            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MSAN_FLAGS}")
            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=memory")

            if (COMPILER_GCC)
                set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libmsan")
                set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libmsan")
                set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libmsan")
            endif ()

        elseif (SANITIZE STREQUAL "undefined")
            set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
            set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all")
            set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")

            if (COMPILER_GCC)
                set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libubsan")
                set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libubsan")
                set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libubsan")
            endif ()
        else ()
            message (FATAL_ERROR "Unknown sanitizer type: ${SANITIZE}")
        endif ()
    endforeach ()
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_FLAGS}")
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_FLAGS}")
    message (STATUS "Add sanitizer: ${SANITIZE}")
    # Disable sanitizing on make stage e.g. for snowball compiler
    set (ENV{ASAN_OPTIONS} "detect_leaks=0")
    message (STATUS "Sanitizer CFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
    message (STATUS "Sanitizer CXXFLAGS: ${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
endif()