File: Sanitizers.cmake

package info (click to toggle)
mongo-c-driver 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,088 kB
  • sloc: ansic: 193,670; python: 7,780; cpp: 1,493; sh: 659; makefile: 78
file content (60 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download | duplicates (2)
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
include (CheckCSourceCompiles)
include (CMakePushCheckState)
include (MongoSettings)

mongo_setting (
   MONGO_SANITIZE "Semicolon/comma-separated list of sanitizers to apply when building"
   DEFAULT
      DEVEL EVAL [[
         if(NOT MSVC)
            set(DEFAULT "address,undefined")
         endif()
      ]])

mongo_bool_setting(
    MONGO_FUZZ "Enable libFuzzer integration (Requires a C++ compiler)"
    DEFAULT VALUE OFF
    VALIDATE CODE [[
        if (MONGO_FUZZ AND NOT ENABLE_STATIC)
            message (FATAL_ERROR "MONGO_FUZZ requires ENABLE_STATIC=ON or ENABLE_STATIC=BUILD_ONLY")
        endif ()
    ]]
)

if (MONGO_FUZZ)
    set(mongo_fuzz_options "address,undefined,fuzzer-no-link")
    if (MONGO_SANITIZE AND NOT "${MONGO_SANITIZE}" STREQUAL "${mongo_fuzz_options}")
        message(WARNING "Overriding user-provided MONGO_SANITIZE options due to MONGO_FUZZ=ON")
    endif ()
    set_property (CACHE MONGO_SANITIZE PROPERTY VALUE "${mongo_fuzz_options}")
endif ()

# Replace commas with semicolons for the genex
string(REPLACE ";" "," _sanitize "${MONGO_SANITIZE}")

if (_sanitize)
    string (MAKE_C_IDENTIFIER "HAVE_SANITIZE_${_sanitize}" ident)
    string (TOUPPER "${ident}" varname)
    set (flag -fsanitize=${_sanitize} -fno-sanitize-recover=all)

    cmake_push_check_state ()
        set (CMAKE_REQUIRED_FLAGS "${flag}")
        set (CMAKE_REQUIRED_LIBRARIES "${flag}")
        check_c_source_compiles ([[
            #include <stdio.h>

            int main (void) {
                puts ("Hello, world!");
                return 0;
            }
        ]] "${varname}")
    cmake_pop_check_state ()

    if (NOT "${${varname}}")
        message (SEND_ERROR "Requested sanitizer option '${flag}' is not supported by the compiler+linker")
    else ()
        message (STATUS "Enabling sanitizers: ${flag}")
        mongo_platform_compile_options ($<BUILD_INTERFACE:${flag}>)
        mongo_platform_link_options (${flag})
    endif ()
endif ()