Debian Codegraph @

tests/RuntimeCheck.cmake

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 # Configuration for code analysis tools: runtime checking and coverage.
21
22 # Any test that needs runtime checks should use TEST_EXE_PREFIX and TEST_ENV.
23 # Normally they are set as a result of the RUNTIME_CHECK value,
24 # but can be set directly for unsupported tools or unusual flags
25 # e.g. -DTEST_EXE_PREFIX=rr or -DTEST_EXE_PREFIX="valgrind --tool=massif"
26
27 set(TEST_EXE_PREFIX "" CACHE STRING "Prefix for test executable command line")
28 set(TEST_WRAP_PREFIX "" CACHE STRING "Prefix for interpreter tests (e.g. python, ruby) that load proton as an extension")
29 set(TEST_ENV "" CACHE STRING "Extra environment for tests: name1=value1;name2=value2")
30 mark_as_advanced(TEST_EXE_PREFIX TEST_WRAP_PREFIX TEST_ENV)
31
32 # Check for valgrind
33 find_program(VALGRIND_EXECUTABLE valgrind DOC "location of valgrind program")
34 set(VALGRIND_SUPPRESSIONS "${CMAKE_SOURCE_DIR}/tests/valgrind.supp" CACHE STRING "Suppressions file for valgrind")
35 set(VALGRIND_COMMON_ARGS "--error-exitcode=42 --quiet --suppressions=${VALGRIND_SUPPRESSIONS}")
36 mark_as_advanced(VALGRIND_EXECUTABLE VALGRIND_SUPPRESSIONS VALGRIND_COMMON_ARGS)
37
38 # Check for compiler sanitizers
39 if((CMAKE_C_COMPILER_ID MATCHES "GNU"
40 AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.8)
41 OR (CMAKE_C_COMPILER_ID MATCHES "Clang"
42 AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.1))
43 set(HAS_SANITIZERS TRUE)
44 endif()
45
46 # Valid values for RUNTIME_CHECK
47 set(runtime_checks OFF asan tsan memcheck helgrind)
48
49 # Set the default
50 if(NOT CMAKE_BUILD_TYPE MATCHES "Coverage" AND VALGRIND_EXECUTABLE)
51 set(RUNTIME_CHECK_DEFAULT "memcheck")
52 endif()
53
54 # Deprecated options to enable runtime checks
55 macro(deprecated_enable_check old new doc)
56 if (${old})
57 message("WARNING: option ${old} is deprecated, use RUNTIME_CHECK=${new} instead")
58 set(RUNTIME_CHECK_DEFAULT ${new})
59 endif()
60 unset(${old} CACHE)
61 endmacro()
62 deprecated_enable_check(ENABLE_VALGRIND memcheck "Use valgrind to detect run-time problems")
63 deprecated_enable_check(ENABLE_SANITIZERS asan "Compile with memory sanitizers (asan, ubsan)")
64 deprecated_enable_check(ENABLE_TSAN tsan "Compile with thread sanitizer (tsan)")
65
66 set(RUNTIME_CHECK ${RUNTIME_CHECK_DEFAULT} CACHE STRING "Enable runtime checks. Valid values: ${runtime_checks}")
67
68 if(CMAKE_BUILD_TYPE MATCHES "Coverage" AND RUNTIME_CHECK)
69 message(FATAL_ERROR "Cannot set RUNTIME_CHECK with CMAKE_BUILD_TYPE=Coverage")
70 endif()
71
72 macro(assert_has_sanitizers)
73 if(NOT HAS_SANITIZERS)
74 message(FATAL_ERROR "compiler sanitizers are not available")
75 endif()
76 endmacro()
77
78 macro(assert_has_valgrind)
79 if(NOT VALGRIND_EXECUTABLE)
80 message(FATAL_ERROR "valgrind is not available")
81 endif()
82 endmacro()
83
84 if(RUNTIME_CHECK STREQUAL "memcheck")
85 assert_has_valgrind()
86 message(STATUS "Runtime memory checker: valgrind memcheck")
87 set(TEST_EXE_PREFIX "${VALGRIND_EXECUTABLE} --tool=memcheck --leak-check=full ${VALGRIND_COMMON_ARGS}")
88 # TODO aconway 2018-09-06: NO TEST_WRAP_PREFIX, need --trace-children + many suppressions
89
90 elseif(RUNTIME_CHECK STREQUAL "helgrind")
91 assert_has_valgrind()
92 message(STATUS "Runtime race checker: valgrind helgrind")
93 set(TEST_EXE_PREFIX "${VALGRIND_EXECUTABLE} --tool=helgrind ${VALGRIND_COMMON_ARGS}")
94 # TODO aconway 2018-09-06: NO TEST_WRAP_PREFIX, need --trace-children + many suppressions
95
96 elseif(RUNTIME_CHECK STREQUAL "asan")
97 assert_has_sanitizers()
98 message(STATUS "Runtime memory checker: gcc/clang memory sanitizers")
99 # clang defaults to static sanitizer libs (which is preferred), but then we cannot LD_PRELOAD
100 if (CMAKE_C_COMPILER_ID MATCHES "Clang")
101 set(CLANG_ASAN_FLAG "-shared-libasan")
102 endif()
103
104 set(SANITIZE_FLAGS "-g -fno-omit-frame-pointer ${CLANG_ASAN_FLAG} -fsanitize=address,undefined -fsanitize-recover=vptr")
105 set(TEST_WRAP_PREFIX "${CMAKE_SOURCE_DIR}/tests/preload_asan.sh $<TARGET_FILE:qpid-proton-core>")
106 list(APPEND TEST_ENV "UBSAN_OPTIONS=suppressions=${CMAKE_SOURCE_DIR}/tests/ubsan.supp")
107 list(APPEND TEST_ENV "LSAN_OPTIONS=suppressions=${CMAKE_SOURCE_DIR}/tests/lsan.supp")
108
109 elseif(RUNTIME_CHECK STREQUAL "tsan")
110 assert_has_sanitizers()
111 message(STATUS "Runtime race checker: gcc/clang thread sanitizer")
112 set(SANITIZE_FLAGS "-g -fno-omit-frame-pointer -fsanitize=thread")
113 set(TEST_WRAP_PREFIX "${CMAKE_SOURCE_DIR}/tests/preload_tsan.sh $<TARGET_FILE:qpid-proton-core>")
114 list(APPEND TEST_ENV "TSAN_OPTIONS=second_deadlock_stack=1 suppressions=${CMAKE_SOURCE_DIR}/tests/tsan.supp")
115
116 elseif(RUNTIME_CHECK)
117 message(FATAL_ERROR "'RUNTIME_CHECK=${RUNTIME_CHECK}' is invalid, valid values: ${runtime_checks}")
118 endif()
119
120 if(SANITIZE_FLAGS)
121 set(ENABLE_UNDEFINED_ERROR OFF CACHE BOOL "Disabled for sanitizers" FORCE)
122 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZE_FLAGS}")
123 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZE_FLAGS}")
124 endif()
125
126 if(TEST_EXE_PREFIX)
127 # Add TEST_EXE_PREFIX to TEST_ENV so test runner scripts can use it.
128 list(APPEND TEST_ENV "TEST_EXE_PREFIX=${TEST_EXE_PREFIX}")
129 # Make a CMake-list form of TEST_EXE_PREFIX for (pn_)add_test() commands
130 separate_arguments(TEST_EXE_PREFIX_CMD UNIX_COMMAND "${TEST_EXE_PREFIX}")
131 endif()
132 separate_arguments(TEST_WRAP_PREFIX_CMD UNIX_COMMAND "${TEST_WRAP_PREFIX}")