ECMEnableSanitizers.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # SPDX-FileCopyrightText: 2014 Mathieu Tarral <mathieu.tarral@gmail.com>
  2. #
  3. # SPDX-License-Identifier: BSD-3-Clause
  4. #[=======================================================================[.rst:
  5. ECMEnableSanitizers
  6. -------------------
  7. Enable compiler sanitizer flags.
  8. The following sanitizers are supported:
  9. - Address Sanitizer
  10. - Memory Sanitizer
  11. - Thread Sanitizer
  12. - Leak Sanitizer
  13. - Undefined Behaviour Sanitizer
  14. All of them are implemented in Clang, depending on your version, and
  15. there is an work in progress in GCC, where some of them are currently
  16. implemented.
  17. This module will check your current compiler version to see if it
  18. supports the sanitizers that you want to enable
  19. Usage
  20. =====
  21. Simply add::
  22. include(ECMEnableSanitizers)
  23. to your ``CMakeLists.txt``. Note that this module is included in
  24. KDECompilerSettings, so projects using that module do not need to also
  25. include this one.
  26. The sanitizers are not enabled by default. Instead, you must set
  27. ``ECM_ENABLE_SANITIZERS`` (either in your ``CMakeLists.txt`` or on the
  28. command line) to a semicolon-separated list of sanitizers you wish to enable.
  29. The options are:
  30. - address
  31. - memory
  32. - thread
  33. - leak
  34. - undefined
  35. - fuzzer
  36. The sanitizers "address", "memory" and "thread" are mutually exclusive. You
  37. cannot enable two of them in the same build.
  38. "leak" requires the "address" sanitizer.
  39. .. note::
  40. To reduce the overhead induced by the instrumentation of the sanitizers, it
  41. is advised to enable compiler optimizations (``-O1`` or higher).
  42. Example
  43. =======
  44. This is an example of usage::
  45. mkdir build
  46. cd build
  47. cmake -DECM_ENABLE_SANITIZERS='address;leak;undefined' ..
  48. .. note::
  49. Most of the sanitizers will require Clang. To enable it, use::
  50. -DCMAKE_CXX_COMPILER=clang++
  51. Since 1.3.0.
  52. #]=======================================================================]
  53. # MACRO check_compiler_version
  54. #-----------------------------
  55. macro (check_compiler_version gcc_required_version clang_required_version msvc_required_version)
  56. if (
  57. (
  58. CMAKE_CXX_COMPILER_ID MATCHES "GNU"
  59. AND
  60. CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${gcc_required_version}
  61. )
  62. OR
  63. (
  64. CMAKE_CXX_COMPILER_ID MATCHES "Clang"
  65. AND
  66. CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${clang_required_version}
  67. )
  68. OR
  69. (
  70. CMAKE_CXX_COMPILER_ID MATCHES "MSVC"
  71. AND
  72. CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${msvc_required_version}
  73. )
  74. )
  75. # error !
  76. message(FATAL_ERROR "You ask to enable the sanitizer ${CUR_SANITIZER},
  77. but your compiler ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION}
  78. does not support it !
  79. You should use at least GCC ${gcc_required_version}, Clang ${clang_required_version}
  80. or MSVC ${msvc_required_version}
  81. (99.99 means not implemented yet)")
  82. endif ()
  83. endmacro ()
  84. # MACRO check_compiler_support
  85. #------------------------------
  86. macro (enable_sanitizer_flags sanitize_option)
  87. if (${sanitize_option} MATCHES "address")
  88. check_compiler_version("4.8" "3.1" "19.28")
  89. if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  90. set(XSAN_COMPILE_FLAGS "-fsanitize=address")
  91. else()
  92. set(XSAN_COMPILE_FLAGS "-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  93. set(XSAN_LINKER_FLAGS "-fsanitize=address")
  94. endif()
  95. elseif (${sanitize_option} MATCHES "thread")
  96. check_compiler_version("4.8" "3.1" "99.99")
  97. set(XSAN_COMPILE_FLAGS "-fsanitize=thread")
  98. set(XSAN_LINKER_FLAGS "tsan")
  99. elseif (${sanitize_option} MATCHES "memory")
  100. check_compiler_version("99.99" "3.1" "99.99")
  101. set(XSAN_COMPILE_FLAGS "-fsanitize=memory")
  102. elseif (${sanitize_option} MATCHES "leak")
  103. check_compiler_version("4.9" "3.4" "99.99")
  104. set(XSAN_COMPILE_FLAGS "-fsanitize=leak")
  105. set(XSAN_LINKER_FLAGS "lsan")
  106. elseif (${sanitize_option} MATCHES "undefined")
  107. check_compiler_version("4.9" "3.1" "99.99")
  108. set(XSAN_COMPILE_FLAGS "-fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  109. elseif (${sanitize_option} MATCHES "fuzzer")
  110. check_compiler_version("99.99" "6.0" "99.99")
  111. set(XSAN_COMPILE_FLAGS "-fsanitize=fuzzer")
  112. else ()
  113. message(FATAL_ERROR "Compiler sanitizer option \"${sanitize_option}\" not supported.")
  114. endif ()
  115. endmacro ()
  116. if (ECM_ENABLE_SANITIZERS)
  117. if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  118. # for each element of the ECM_ENABLE_SANITIZERS list
  119. foreach ( CUR_SANITIZER ${ECM_ENABLE_SANITIZERS} )
  120. # lowercase filter
  121. string(TOLOWER ${CUR_SANITIZER} CUR_SANITIZER)
  122. # check option and enable appropriate flags
  123. enable_sanitizer_flags ( ${CUR_SANITIZER} )
  124. # TODO: GCC will not link pthread library if enabled ASan
  125. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  126. set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${XSAN_COMPILE_FLAGS}" )
  127. link_libraries(${XSAN_LINKER_FLAGS})
  128. endif()
  129. set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XSAN_COMPILE_FLAGS}" )
  130. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  131. link_libraries(${XSAN_LINKER_FLAGS})
  132. endif()
  133. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  134. string(REPLACE "-Wl,--no-undefined" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
  135. string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")
  136. link_libraries(${XSAN_LINKER_FLAGS})
  137. endif ()
  138. endforeach()
  139. else()
  140. message(STATUS "Tried to enable sanitizers (-DECM_ENABLE_SANITIZERS=${ECM_ENABLE_SANITIZERS}), \
  141. but compiler (${CMAKE_CXX_COMPILER_ID}) does not have sanitizer support")
  142. endif()
  143. endif()