summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-02-27 18:00:15 +0100
committerMinijackson <minijackson@riseup.net>2018-02-27 18:00:15 +0100
commit42d0b10e7c22bfbcae00cb6f9176775de1c561ad (patch)
tree75ec94a0689ea16a6ad27c156bcba06e91149d72
parent8a8729a371373d5c841e058ab21f4fdd0169b40a (diff)
downloadkube-42d0b10e7c22bfbcae00cb6f9176775de1c561ad.tar.gz
kube-42d0b10e7c22bfbcae00cb6f9176775de1c561ad.zip
Add support in CMake for Sanitizers
-rw-r--r--.gitignore2
-rw-r--r--CMakeLists.txt3
-rw-r--r--applications/kube/CMakeLists.txt1
-rw-r--r--cmake/modules/FindASan.cmake59
-rw-r--r--cmake/modules/FindMSan.cmake57
-rw-r--r--cmake/modules/FindSanitizers.cmake87
-rw-r--r--cmake/modules/FindTSan.cmake65
-rw-r--r--cmake/modules/FindUBSan.cmake46
-rwxr-xr-xcmake/modules/asan-wrapper55
-rw-r--r--cmake/modules/sanitize-helpers.cmake170
-rw-r--r--framework/qml/tests/CMakeLists.txt1
-rw-r--r--framework/src/domain/mime/mimetreeparser/autotests/CMakeLists.txt1
-rw-r--r--framework/src/domain/mime/mimetreeparser/tests/CMakeLists.txt1
-rw-r--r--framework/src/domain/mime/tests/CMakeLists.txt1
-rw-r--r--framework/src/domain/settings/tests/CMakeLists.txt1
-rw-r--r--framework/src/tests/CMakeLists.txt2
-rw-r--r--tests/CMakeLists.txt1
17 files changed, 552 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 7eaaf531..3274b0fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,5 +5,5 @@
5*.patch 5*.patch
6*.swp 6*.swp
7*.qmlc 7*.qmlc
8build/ 8build*/
9site 9site
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7cc67c1b..3ec85570 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,9 @@ find_package(PkgConfig REQUIRED)
18find_package(ECM 5.29.0 REQUIRED NO_MODULE) 18find_package(ECM 5.29.0 REQUIRED NO_MODULE)
19 19
20set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR}) 20set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
21set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules")
22
23find_package(Sanitizers)
21 24
22include(KDEInstallDirs) 25include(KDEInstallDirs)
23include(KDECompilerSettings) 26include(KDECompilerSettings)
diff --git a/applications/kube/CMakeLists.txt b/applications/kube/CMakeLists.txt
index 53556345..adb138f6 100644
--- a/applications/kube/CMakeLists.txt
+++ b/applications/kube/CMakeLists.txt
@@ -14,6 +14,7 @@ if(APPLE OR WIN32)
14endif() 14endif()
15 15
16add_executable(${PROJECT_NAME} ${SRCS}) 16add_executable(${PROJECT_NAME} ${SRCS})
17add_sanitizers(${PROJECT_NAME})
17target_link_libraries(${PROJECT_NAME} 18target_link_libraries(${PROJECT_NAME}
18 Qt5::Quick 19 Qt5::Quick
19 ${CMAKE_DL_LIBS} 20 ${CMAKE_DL_LIBS}
diff --git a/cmake/modules/FindASan.cmake b/cmake/modules/FindASan.cmake
new file mode 100644
index 00000000..98ea7cb3
--- /dev/null
+++ b/cmake/modules/FindASan.cmake
@@ -0,0 +1,59 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_ADDRESS "Enable AddressSanitizer for sanitized targets." Off)
26
27set(FLAG_CANDIDATES
28 # Clang 3.2+ use this version. The no-omit-frame-pointer option is optional.
29 "-g -fsanitize=address -fno-omit-frame-pointer"
30 "-g -fsanitize=address"
31
32 # Older deprecated flag for ASan
33 "-g -faddress-sanitizer"
34)
35
36
37if (SANITIZE_ADDRESS AND (SANITIZE_THREAD OR SANITIZE_MEMORY))
38 message(FATAL_ERROR "AddressSanitizer is not compatible with "
39 "ThreadSanitizer or MemorySanitizer.")
40endif ()
41
42
43include(sanitize-helpers)
44
45if (SANITIZE_ADDRESS)
46 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}" "AddressSanitizer"
47 "ASan")
48
49 find_program(ASan_WRAPPER "asan-wrapper" PATHS ${CMAKE_MODULE_PATH})
50 mark_as_advanced(ASan_WRAPPER)
51endif ()
52
53function (add_sanitize_address TARGET)
54 if (NOT SANITIZE_ADDRESS)
55 return()
56 endif ()
57
58 sanitizer_add_flags(${TARGET} "AddressSanitizer" "ASan")
59endfunction ()
diff --git a/cmake/modules/FindMSan.cmake b/cmake/modules/FindMSan.cmake
new file mode 100644
index 00000000..22d0050e
--- /dev/null
+++ b/cmake/modules/FindMSan.cmake
@@ -0,0 +1,57 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_MEMORY "Enable MemorySanitizer for sanitized targets." Off)
26
27set(FLAG_CANDIDATES
28 "-g -fsanitize=memory"
29)
30
31
32include(sanitize-helpers)
33
34if (SANITIZE_MEMORY)
35 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
36 message(WARNING "MemorySanitizer disabled for target ${TARGET} because "
37 "MemorySanitizer is supported for Linux systems only.")
38 set(SANITIZE_MEMORY Off CACHE BOOL
39 "Enable MemorySanitizer for sanitized targets." FORCE)
40 elseif (NOT ${CMAKE_SIZEOF_VOID_P} EQUAL 8)
41 message(WARNING "MemorySanitizer disabled for target ${TARGET} because "
42 "MemorySanitizer is supported for 64bit systems only.")
43 set(SANITIZE_MEMORY Off CACHE BOOL
44 "Enable MemorySanitizer for sanitized targets." FORCE)
45 else ()
46 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}" "MemorySanitizer"
47 "MSan")
48 endif ()
49endif ()
50
51function (add_sanitize_memory TARGET)
52 if (NOT SANITIZE_MEMORY)
53 return()
54 endif ()
55
56 sanitizer_add_flags(${TARGET} "MemorySanitizer" "MSan")
57endfunction ()
diff --git a/cmake/modules/FindSanitizers.cmake b/cmake/modules/FindSanitizers.cmake
new file mode 100644
index 00000000..4f586a34
--- /dev/null
+++ b/cmake/modules/FindSanitizers.cmake
@@ -0,0 +1,87 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25# If any of the used compiler is a GNU compiler, add a second option to static
26# link against the sanitizers.
27option(SANITIZE_LINK_STATIC "Try to link static against sanitizers." Off)
28
29
30
31
32set(FIND_QUIETLY_FLAG "")
33if (DEFINED Sanitizers_FIND_QUIETLY)
34 set(FIND_QUIETLY_FLAG "QUIET")
35endif ()
36
37find_package(ASan ${FIND_QUIETLY_FLAG})
38find_package(TSan ${FIND_QUIETLY_FLAG})
39find_package(MSan ${FIND_QUIETLY_FLAG})
40find_package(UBSan ${FIND_QUIETLY_FLAG})
41
42
43
44
45function(sanitizer_add_blacklist_file FILE)
46 if(NOT IS_ABSOLUTE ${FILE})
47 set(FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}")
48 endif()
49 get_filename_component(FILE "${FILE}" REALPATH)
50
51 sanitizer_check_compiler_flags("-fsanitize-blacklist=${FILE}"
52 "SanitizerBlacklist" "SanBlist")
53endfunction()
54
55function(add_sanitizers ...)
56 # If no sanitizer is enabled, return immediately.
57 if (NOT (SANITIZE_ADDRESS OR SANITIZE_MEMORY OR SANITIZE_THREAD OR
58 SANITIZE_UNDEFINED))
59 return()
60 endif ()
61
62 foreach (TARGET ${ARGV})
63 # Check if this target will be compiled by exactly one compiler. Other-
64 # wise sanitizers can't be used and a warning should be printed once.
65 sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
66 list(LENGTH TARGET_COMPILER NUM_COMPILERS)
67 if (NUM_COMPILERS GREATER 1)
68 message(WARNING "Can't use any sanitizers for target ${TARGET}, "
69 "because it will be compiled by incompatible compilers. "
70 "Target will be compiled without sanitizers.")
71 return()
72
73 # If the target is compiled by no known compiler, ignore it.
74 elseif (NUM_COMPILERS EQUAL 0)
75 message(WARNING "Can't use any sanitizers for target ${TARGET}, "
76 "because it uses an unknown compiler. Target will be "
77 "compiled without sanitizers.")
78 return()
79 endif ()
80
81 # Add sanitizers for target.
82 add_sanitize_address(${TARGET})
83 add_sanitize_thread(${TARGET})
84 add_sanitize_memory(${TARGET})
85 add_sanitize_undefined(${TARGET})
86 endforeach ()
87endfunction(add_sanitizers)
diff --git a/cmake/modules/FindTSan.cmake b/cmake/modules/FindTSan.cmake
new file mode 100644
index 00000000..3cba3c03
--- /dev/null
+++ b/cmake/modules/FindTSan.cmake
@@ -0,0 +1,65 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_THREAD "Enable ThreadSanitizer for sanitized targets." Off)
26
27set(FLAG_CANDIDATES
28 "-g -fsanitize=thread"
29)
30
31
32# ThreadSanitizer is not compatible with MemorySanitizer.
33if (SANITIZE_THREAD AND SANITIZE_MEMORY)
34 message(FATAL_ERROR "ThreadSanitizer is not compatible with "
35 "MemorySanitizer.")
36endif ()
37
38
39include(sanitize-helpers)
40
41if (SANITIZE_THREAD)
42 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND
43 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
44 message(WARNING "ThreadSanitizer disabled for target ${TARGET} because "
45 "ThreadSanitizer is supported for Linux systems and macOS only.")
46 set(SANITIZE_THREAD Off CACHE BOOL
47 "Enable ThreadSanitizer for sanitized targets." FORCE)
48 elseif (NOT ${CMAKE_SIZEOF_VOID_P} EQUAL 8)
49 message(WARNING "ThreadSanitizer disabled for target ${TARGET} because "
50 "ThreadSanitizer is supported for 64bit systems only.")
51 set(SANITIZE_THREAD Off CACHE BOOL
52 "Enable ThreadSanitizer for sanitized targets." FORCE)
53 else ()
54 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}" "ThreadSanitizer"
55 "TSan")
56 endif ()
57endif ()
58
59function (add_sanitize_thread TARGET)
60 if (NOT SANITIZE_THREAD)
61 return()
62 endif ()
63
64 sanitizer_add_flags(${TARGET} "ThreadSanitizer" "TSan")
65endfunction ()
diff --git a/cmake/modules/FindUBSan.cmake b/cmake/modules/FindUBSan.cmake
new file mode 100644
index 00000000..ae103f71
--- /dev/null
+++ b/cmake/modules/FindUBSan.cmake
@@ -0,0 +1,46 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_UNDEFINED
26 "Enable UndefinedBehaviorSanitizer for sanitized targets." Off)
27
28set(FLAG_CANDIDATES
29 "-g -fsanitize=undefined"
30)
31
32
33include(sanitize-helpers)
34
35if (SANITIZE_UNDEFINED)
36 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}"
37 "UndefinedBehaviorSanitizer" "UBSan")
38endif ()
39
40function (add_sanitize_undefined TARGET)
41 if (NOT SANITIZE_UNDEFINED)
42 return()
43 endif ()
44
45 sanitizer_add_flags(${TARGET} "UndefinedBehaviorSanitizer" "UBSan")
46endfunction ()
diff --git a/cmake/modules/asan-wrapper b/cmake/modules/asan-wrapper
new file mode 100755
index 00000000..5d541033
--- /dev/null
+++ b/cmake/modules/asan-wrapper
@@ -0,0 +1,55 @@
1#!/bin/sh
2
3# The MIT License (MIT)
4#
5# Copyright (c)
6# 2013 Matthew Arsenault
7# 2015-2016 RWTH Aachen University, Federal Republic of Germany
8#
9# Permission is hereby granted, free of charge, to any person obtaining a copy
10# of this software and associated documentation files (the "Software"), to deal
11# in the Software without restriction, including without limitation the rights
12# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13# copies of the Software, and to permit persons to whom the Software is
14# furnished to do so, subject to the following conditions:
15#
16# The above copyright notice and this permission notice shall be included in all
17# copies or substantial portions of the Software.
18#
19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25# SOFTWARE.
26
27# This script is a wrapper for AddressSanitizer. In some special cases you need
28# to preload AddressSanitizer to avoid error messages - e.g. if you're
29# preloading another library to your application. At the moment this script will
30# only do something, if we're running on a Linux platform. OSX might not be
31# affected.
32
33
34# Exit immediately, if platform is not Linux.
35if [ "$(uname)" != "Linux" ]
36then
37 exec $@
38fi
39
40
41# Get the used libasan of the application ($1). If a libasan was found, it will
42# be prepended to LD_PRELOAD.
43libasan=$(ldd $1 | grep libasan | sed "s/^[[:space:]]//" | cut -d' ' -f1)
44if [ -n "$libasan" ]
45then
46 if [ -n "$LD_PRELOAD" ]
47 then
48 export LD_PRELOAD="$libasan:$LD_PRELOAD"
49 else
50 export LD_PRELOAD="$libasan"
51 fi
52fi
53
54# Execute the application.
55exec $@
diff --git a/cmake/modules/sanitize-helpers.cmake b/cmake/modules/sanitize-helpers.cmake
new file mode 100644
index 00000000..c51ee6af
--- /dev/null
+++ b/cmake/modules/sanitize-helpers.cmake
@@ -0,0 +1,170 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25# Helper function to get the language of a source file.
26function (sanitizer_lang_of_source FILE RETURN_VAR)
27 get_filename_component(FILE_EXT "${FILE}" EXT)
28 string(TOLOWER "${FILE_EXT}" FILE_EXT)
29 string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
30
31 get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
32 foreach (LANG ${ENABLED_LANGUAGES})
33 list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
34 if (NOT ${TEMP} EQUAL -1)
35 set(${RETURN_VAR} "${LANG}" PARENT_SCOPE)
36 return()
37 endif ()
38 endforeach()
39
40 set(${RETURN_VAR} "" PARENT_SCOPE)
41endfunction ()
42
43
44# Helper function to get compilers used by a target.
45function (sanitizer_target_compilers TARGET RETURN_VAR)
46 # Check if all sources for target use the same compiler. If a target uses
47 # e.g. C and Fortran mixed and uses different compilers (e.g. clang and
48 # gfortran) this can trigger huge problems, because different compilers may
49 # use different implementations for sanitizers.
50 set(BUFFER "")
51 get_target_property(TSOURCES ${TARGET} SOURCES)
52 foreach (FILE ${TSOURCES})
53 # If expression was found, FILE is a generator-expression for an object
54 # library. Object libraries will be ignored.
55 string(REGEX MATCH "TARGET_OBJECTS:([^ >]+)" _file ${FILE})
56 if ("${_file}" STREQUAL "")
57 sanitizer_lang_of_source(${FILE} LANG)
58 if (LANG)
59 list(APPEND BUFFER ${CMAKE_${LANG}_COMPILER_ID})
60 endif ()
61 endif ()
62 endforeach ()
63
64 list(REMOVE_DUPLICATES BUFFER)
65 set(${RETURN_VAR} "${BUFFER}" PARENT_SCOPE)
66endfunction ()
67
68
69# Helper function to check compiler flags for language compiler.
70function (sanitizer_check_compiler_flag FLAG LANG VARIABLE)
71 if (${LANG} STREQUAL "C")
72 include(CheckCCompilerFlag)
73 check_c_compiler_flag("${FLAG}" ${VARIABLE})
74
75 elseif (${LANG} STREQUAL "CXX")
76 include(CheckCXXCompilerFlag)
77 check_cxx_compiler_flag("${FLAG}" ${VARIABLE})
78
79 elseif (${LANG} STREQUAL "Fortran")
80 # CheckFortranCompilerFlag was introduced in CMake 3.x. To be compatible
81 # with older Cmake versions, we will check if this module is present
82 # before we use it. Otherwise we will define Fortran coverage support as
83 # not available.
84 include(CheckFortranCompilerFlag OPTIONAL RESULT_VARIABLE INCLUDED)
85 if (INCLUDED)
86 check_fortran_compiler_flag("${FLAG}" ${VARIABLE})
87 elseif (NOT CMAKE_REQUIRED_QUIET)
88 message(STATUS "Performing Test ${VARIABLE}")
89 message(STATUS "Performing Test ${VARIABLE}"
90 " - Failed (Check not supported)")
91 endif ()
92 endif()
93endfunction ()
94
95
96# Helper function to test compiler flags.
97function (sanitizer_check_compiler_flags FLAG_CANDIDATES NAME PREFIX)
98 set(CMAKE_REQUIRED_QUIET ${${PREFIX}_FIND_QUIETLY})
99
100 get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
101 foreach (LANG ${ENABLED_LANGUAGES})
102 # Sanitizer flags are not dependend on language, but the used compiler.
103 # So instead of searching flags foreach language, search flags foreach
104 # compiler used.
105 set(COMPILER ${CMAKE_${LANG}_COMPILER_ID})
106 if (NOT DEFINED ${PREFIX}_${COMPILER}_FLAGS)
107 foreach (FLAG ${FLAG_CANDIDATES})
108 if(NOT CMAKE_REQUIRED_QUIET)
109 message(STATUS "Try ${COMPILER} ${NAME} flag = [${FLAG}]")
110 endif()
111
112 set(CMAKE_REQUIRED_FLAGS "${FLAG}")
113 unset(${PREFIX}_FLAG_DETECTED CACHE)
114 sanitizer_check_compiler_flag("${FLAG}" ${LANG}
115 ${PREFIX}_FLAG_DETECTED)
116
117 if (${PREFIX}_FLAG_DETECTED)
118 # If compiler is a GNU compiler, search for static flag, if
119 # SANITIZE_LINK_STATIC is enabled.
120 if (SANITIZE_LINK_STATIC AND (${COMPILER} STREQUAL "GNU"))
121 string(TOLOWER ${PREFIX} PREFIX_lower)
122 sanitizer_check_compiler_flag(
123 "-static-lib${PREFIX_lower}" ${LANG}
124 ${PREFIX}_STATIC_FLAG_DETECTED)
125
126 if (${PREFIX}_STATIC_FLAG_DETECTED)
127 set(FLAG "-static-lib${PREFIX_lower} ${FLAG}")
128 endif ()
129 endif ()
130
131 set(${PREFIX}_${COMPILER}_FLAGS "${FLAG}" CACHE STRING
132 "${NAME} flags for ${COMPILER} compiler.")
133 mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
134 break()
135 endif ()
136 endforeach ()
137
138 if (NOT ${PREFIX}_FLAG_DETECTED)
139 set(${PREFIX}_${COMPILER}_FLAGS "" CACHE STRING
140 "${NAME} flags for ${COMPILER} compiler.")
141 mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
142
143 message(WARNING "${NAME} is not available for ${COMPILER} "
144 "compiler. Targets using this compiler will be "
145 "compiled without ${NAME}.")
146 endif ()
147 endif ()
148 endforeach ()
149endfunction ()
150
151
152# Helper to assign sanitizer flags for TARGET.
153function (sanitizer_add_flags TARGET NAME PREFIX)
154 # Get list of compilers used by target and check, if sanitizer is available
155 # for this target. Other compiler checks like check for conflicting
156 # compilers will be done in add_sanitizers function.
157 sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
158 list(LENGTH TARGET_COMPILER NUM_COMPILERS)
159 if ("${${PREFIX}_${TARGET_COMPILER}_FLAGS}" STREQUAL "")
160 return()
161 endif()
162
163 # Set compile- and link-flags for target.
164 set_property(TARGET ${TARGET} APPEND_STRING
165 PROPERTY COMPILE_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
166 set_property(TARGET ${TARGET} APPEND_STRING
167 PROPERTY COMPILE_FLAGS " ${SanBlist_${TARGET_COMPILER}_FLAGS}")
168 set_property(TARGET ${TARGET} APPEND_STRING
169 PROPERTY LINK_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
170endfunction ()
diff --git a/framework/qml/tests/CMakeLists.txt b/framework/qml/tests/CMakeLists.txt
index bc34855c..a0a37aa8 100644
--- a/framework/qml/tests/CMakeLists.txt
+++ b/framework/qml/tests/CMakeLists.txt
@@ -2,6 +2,7 @@
2find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Quick QuickTest) 2find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Quick QuickTest)
3 3
4add_executable(testrunner testrunner.cpp) 4add_executable(testrunner testrunner.cpp)
5add_sanitizers(testrunner)
5target_link_libraries(testrunner 6target_link_libraries(testrunner
6 Qt5::Quick 7 Qt5::Quick
7 Qt5::QuickTest 8 Qt5::QuickTest
diff --git a/framework/src/domain/mime/mimetreeparser/autotests/CMakeLists.txt b/framework/src/domain/mime/mimetreeparser/autotests/CMakeLists.txt
index f0b1f5f5..97bd3152 100644
--- a/framework/src/domain/mime/mimetreeparser/autotests/CMakeLists.txt
+++ b/framework/src/domain/mime/mimetreeparser/autotests/CMakeLists.txt
@@ -32,6 +32,7 @@ macro(add_mimetreeparser_crypto_unittest _source)
32 set(_test ${_source} util.cpp) 32 set(_test ${_source} util.cpp)
33 get_filename_component(_name ${_source} NAME_WE) 33 get_filename_component(_name ${_source} NAME_WE)
34 add_executable( ${_name} ${_test} setupenv.cpp) 34 add_executable( ${_name} ${_test} setupenv.cpp)
35 add_sanitizers(${_name})
35 ecm_mark_as_test(${_name}) 36 ecm_mark_as_test(${_name})
36 target_link_libraries( ${_name} 37 target_link_libraries( ${_name}
37 kube_otp 38 kube_otp
diff --git a/framework/src/domain/mime/mimetreeparser/tests/CMakeLists.txt b/framework/src/domain/mime/mimetreeparser/tests/CMakeLists.txt
index aa58cc71..aa6e2b07 100644
--- a/framework/src/domain/mime/mimetreeparser/tests/CMakeLists.txt
+++ b/framework/src/domain/mime/mimetreeparser/tests/CMakeLists.txt
@@ -9,6 +9,7 @@ include_directories(
9include(ECMAddTests) 9include(ECMAddTests)
10 10
11add_executable(mimetreeparsertest interfacetest.cpp) 11add_executable(mimetreeparsertest interfacetest.cpp)
12add_sanitizers(mimetreeparsertest)
12add_gpg_crypto_test(mimetreeparsertest mimetreeparsertest) 13add_gpg_crypto_test(mimetreeparsertest mimetreeparsertest)
13target_link_libraries(mimetreeparsertest 14target_link_libraries(mimetreeparsertest
14 kube_otp 15 kube_otp
diff --git a/framework/src/domain/mime/tests/CMakeLists.txt b/framework/src/domain/mime/tests/CMakeLists.txt
index 2844d7ed..270e2ea0 100644
--- a/framework/src/domain/mime/tests/CMakeLists.txt
+++ b/framework/src/domain/mime/tests/CMakeLists.txt
@@ -10,6 +10,7 @@ find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test WebEngine)
10include( ${CMAKE_SOURCE_DIR}/cmake/modules/add_gpg_crypto_test.cmake ) 10include( ${CMAKE_SOURCE_DIR}/cmake/modules/add_gpg_crypto_test.cmake )
11 11
12add_executable(mailtemplatetest mailtemplatetest.cpp) 12add_executable(mailtemplatetest mailtemplatetest.cpp)
13add_sanitizers(mailtemplatetest)
13add_gpg_crypto_test(mailtemplatetest mailtemplatetest) 14add_gpg_crypto_test(mailtemplatetest mailtemplatetest)
14target_link_libraries(mailtemplatetest 15target_link_libraries(mailtemplatetest
15 Qt5::Core 16 Qt5::Core
diff --git a/framework/src/domain/settings/tests/CMakeLists.txt b/framework/src/domain/settings/tests/CMakeLists.txt
index 9cb3f37c..0dab8b67 100644
--- a/framework/src/domain/settings/tests/CMakeLists.txt
+++ b/framework/src/domain/settings/tests/CMakeLists.txt
@@ -1,5 +1,6 @@
1include_directories(../) 1include_directories(../)
2add_executable(settingstest settingstest.cpp) 2add_executable(settingstest settingstest.cpp)
3add_sanitizers(settingstest)
3add_test(settingstest settingstest) 4add_test(settingstest settingstest)
4target_link_libraries(settingstest 5target_link_libraries(settingstest
5 frameworkplugin 6 frameworkplugin
diff --git a/framework/src/tests/CMakeLists.txt b/framework/src/tests/CMakeLists.txt
index a98bed83..c810c652 100644
--- a/framework/src/tests/CMakeLists.txt
+++ b/framework/src/tests/CMakeLists.txt
@@ -6,6 +6,7 @@ include_directories(
6find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test Gui) 6find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test Gui)
7 7
8add_executable(folderlistmodeltest folderlistmodeltest.cpp) 8add_executable(folderlistmodeltest folderlistmodeltest.cpp)
9add_sanitizers(folderlistmodeltest)
9add_test(folderlistmodeltest folderlistmodeltest) 10add_test(folderlistmodeltest folderlistmodeltest)
10target_link_libraries(folderlistmodeltest 11target_link_libraries(folderlistmodeltest
11 Qt5::Core 12 Qt5::Core
@@ -15,6 +16,7 @@ target_link_libraries(folderlistmodeltest
15) 16)
16 17
17add_executable(maillistmodeltest maillistmodeltest.cpp) 18add_executable(maillistmodeltest maillistmodeltest.cpp)
19add_sanitizers(maillistmodeltest)
18add_test(maillistmodeltest maillistmodeltest) 20add_test(maillistmodeltest maillistmodeltest)
19target_link_libraries(maillistmodeltest 21target_link_libraries(maillistmodeltest
20 Qt5::Core 22 Qt5::Core
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 35b28559..f569b802 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -3,6 +3,7 @@ find_package(Sink CONFIG REQUIRED)
3find_package(KAsync CONFIG REQUIRED) 3find_package(KAsync CONFIG REQUIRED)
4 4
5add_executable(kubetestrunner kubetestrunner.cpp) 5add_executable(kubetestrunner kubetestrunner.cpp)
6add_sanitizers(kubetestrunner)
6target_link_libraries(kubetestrunner 7target_link_libraries(kubetestrunner
7 Qt5::QuickTest 8 Qt5::QuickTest
8 Qt5::Quick 9 Qt5::Quick