-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.cmake
303 lines (277 loc) · 10.5 KB
/
test.cmake
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
include_guard()
include(CTest)
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
include(${CMAKE_CURRENT_LIST_DIR}/code_coverage.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/util.cmake)
set(valgrind_suppression_dir $ENV{HOME}/opt/cli_tool_configs/valgrind_supp)
set(sanitizer_suppression_dir $ENV{HOME}/opt/cli_tool_configs/sanitizer_supp)
option(DISABLE_RUNTIME_ANALYSIS "Disable all runtime analysis" OFF)
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT isMultiConfig)
set(LIB_DIRECTORY "${CMAKE_BINARY_DIR}")
else()
set(LIB_DIRECTORY $<PATH:APPEND,${CMAKE_BINARY_DIR},$<CONFIG>>)
endif()
function(__add_fuzzing_test target target_command)
set_target_properties(${target} PROPERTIES INTERPROCEDURAL_OPTIMIZATION FALSE)
if(NOT DEFINED ENV{MAX_FUZZING_TIME})
set(ENV{MAX_FUZZING_TIME} 60)
endif()
if(NOT DEFINED ENV{FUZZING_TIMEOUT})
set(ENV{FUZZING_TIMEOUT} 60)
endif()
if(NOT DEFINED ENV{FUZZING_JOBS})
set(ENV{FUZZING_JOBS} 1)
endif()
if(NOT DEFINED ENV{FUZZING_RSS_LIMIT})
set(ENV{FUZZING_RSS_LIMIT} 4096)
endif()
if(NOT DEFINED ENV{FUZZING_MAX_LEN})
set(ENV{FUZZING_MAX_LEN} 4096)
endif()
find_package(libFuzzer REQUIRED)
target_link_libraries(${target} PRIVATE libFuzzer::libFuzzer)
add_custom_target(
__working_dir_for_${target}
COMMAND ${CMAKE_COMMAND} -E make_directory
${CMAKE_CURRENT_BINARY_DIR}/fuzz_test/__${target}
# COMMAND ${CMAKE_COMMAND} -E copy_directory "${LIB_DIRECTORY}"
# ${CMAKE_CURRENT_BINARY_DIR}/fuzz_test/__${target}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
add_test(
NAME ${target}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/fuzz_test/__${target}
COMMAND
${target_command} -jobs=$ENV{FUZZING_JOBS}
-max_total_time=$ENV{MAX_FUZZING_TIME} -timeout=$ENV{FUZZING_TIMEOUT}
-rss_limit_mb=$ENV{FUZZING_RSS_LIMIT} -max_len=$ENV{FUZZING_MAX_LEN})
add_dependencies(${target} __working_dir_for_${target})
endfunction()
function(__test_impl)
set(cpu_analysis_tools MEMCHECK UBSAN HELGRIND ASAN)
set(gpu_analysis_tools CUDA-MEMCHECK CUDA-SYNCCHECK CUDA-INITCHECK
CUDA-RACECHECK)
set(oneValueArgs TARGET WITH_CPU_ANALYSIS WITH_GPU_ANALYSIS FUZZING
${cpu_analysis_tools} ${gpu_analysis_tools})
cmake_parse_arguments(this "" "${oneValueArgs}" "ARGS" ${ARGN})
separate_arguments(this_ARGS)
if("${this_TARGET}" STREQUAL "")
message(FATAL_ERROR "no target specified")
return()
endif()
if(NOT BUILD_TESTING)
set_target_properties("${this_TARGET}" PROPERTIES EXCLUDE_FROM_ALL ON)
return()
endif()
if(NOT TARGET "${this_TARGET}")
message(FATAL_ERROR "${this_TARGET} is not a target")
return()
endif()
if("${this_WITH_CPU_ANALYSIS}" STREQUAL "")
set(this_WITH_CPU_ANALYSIS TRUE)
endif()
if("${this_WITH_GPU_ANALYSIS}" STREQUAL "")
if(CUDA IN_LIST languages)
set(this_WITH_GPU_ANALYSIS TRUE)
else()
set(this_WITH_GPU_ANALYSIS FALSE)
endif()
endif()
if(DISABLE_RUNTIME_ANALYSIS)
set(this_WITH_CPU_ANALYSIS FALSE)
set(this_WITH_GPU_ANALYSIS FALSE)
endif()
if(NOT this_WITH_CPU_ANALYSIS)
foreach(tool IN LISTS cpu_analysis_tools)
set(this_${tool} FALSE)
endforeach()
endif()
if(NOT this_WITH_GPU_ANALYSIS)
foreach(tool IN LISTS gpu_analysis_tools)
set(this_${tool} FALSE)
endforeach()
endif()
find_package(valgrind)
if(TARGET valgrind::valgrind)
file(GLOB valgrind_suppression_files ${valgrind_suppression_dir}/*.supp)
foreach(tool IN ITEMS MEMCHECK HELGRIND)
if("${this_${tool}}" STREQUAL "")
set(this_${tool} TRUE)
endif()
endforeach()
else()
foreach(tool IN ITEMS MEMCHECK HELGRIND)
set(this_${tool} FALSE)
endforeach()
endif()
find_package(Sanitizer REQUIRED)
if(this_FUZZING)
if(NOT TARGET Sanitizer::address)
message(FATAL_ERROR "Can't find asan")
endif()
endif()
foreach(sanitizer_name IN ITEMS ASAN TSAN UBSAN MSAN)
if(sanitizer_name STREQUAL ASAN)
set(sanitizer_target Sanitizer::address)
elseif(sanitizer_name STREQUAL MSAN)
set(sanitizer_target Sanitizer::memory)
elseif(sanitizer_name STREQUAL UBSAN)
set(sanitizer_target Sanitizer::undefined)
elseif(sanitizer_name STREQUAL TSAN)
set(sanitizer_target Sanitizer::thread)
endif()
if(TARGET ${sanitizer_target})
if("${this_${sanitizer_name}}" STREQUAL "" OR "${this_${sanitizer_name}}")
set(this_${sanitizer_name} TRUE)
endif()
else()
set(this_${sanitizer_name} FALSE)
endif()
endforeach()
if(this_WITH_GPU_ANALYSIS)
find_package(CUDA-MEMCHECK REQUIRED)
if(TARGET CUDA-MEMCHECK::cuda-memcheck)
if("${this_CUDA-MEMCHECK}" STREQUAL "")
set(this_CUDA-MEMCHECK TRUE)
endif()
if("${this_CUDA-SYNCCHECK}" STREQUAL "")
set(this_CUDA-SYNCCHECK TRUE)
endif()
if("${this_CUDA-INITCHECK}" STREQUAL "")
set(this_CUDA-INITCHECK TRUE)
endif()
if("${this_CUDA-RACECHECK}" STREQUAL "")
set(this_CUDA-RACECHECK TRUE)
endif()
else()
message(WARNING "no cuda-memcheck")
set(this_CUDA-MEMCHECK FALSE)
set(this_CUDA-SYNCCHECK FALSE)
set(this_CUDA-INITCHECK FALSE)
set(this_CUDA-RACECHECK FALSE)
endif()
endif()
get_test_property(new_env ${this_TARGET} ENVIRONMENT)
list(APPEND new_env "DEBUGINFOD_URLS=$ENV{DEBUGINFOD_URLS}")
if(ENABLE_LLVM_CODE_COVERAGE)
list(APPEND new_env
"LLVM_PROFILE_FILE=${CMAKE_BINARY_DIR}/profraw_dir/%p.profraw")
endif()
set(ASAN_OPTIONS
"ASAN_OPTIONS=check_initialization_order=true:detect_stack_use_after_return=true:strict_init_order=true:detect_container_overflow=0"
)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(ASAN_OPTIONS "${ASAN_OPTIONS}:protect_shadow_gap=0")
endif()
list(APPEND new_env "${ASAN_OPTIONS}")
set(LSAN_OPTIONS "LSAN_OPTIONS=fast_unwind_on_malloc=0")
if(EXISTS "${sanitizer_suppression_dir}/lsan.supp")
set(LSAN_OPTIONS
"${LSAN_OPTIONS}:suppressions=${sanitizer_suppression_dir}/lsan.supp")
endif()
set(TSAN_OPTIONS
"TSAN_OPTIONS=ignore_noninstrumented_modules=1:force_seq_cst_atomics=1:history_size=7:second_deadlock_stack=1"
)
if(EXISTS "${sanitizer_suppression_dir}/tsan.supp")
set(TSAN_OPTIONS
"${TSAN_OPTIONS}:suppressions=${sanitizer_suppression_dir}/tsan.supp")
endif()
list(APPEND new_env "${TSAN_OPTIONS}")
set(has_test FALSE)
if(this_FUZZING)
find_package(libFuzzer REQUIRED)
target_link_libraries(${this_TARGET} PRIVATE libFuzzer::libFuzzer)
endif()
foreach(tool IN LISTS cpu_analysis_tools gpu_analysis_tools)
if(NOT ${this_${tool}})
continue()
endif()
set(has_test TRUE)
set(new_target ${tool}_${this_TARGET})
clone_executable(${this_TARGET} ${new_target})
set(new_target_command $<TARGET_FILE:${new_target}>)
if(tool STREQUAL ASAN)
target_link_libraries(${new_target} PRIVATE Sanitizer::address)
elseif(tool STREQUAL MSAN)
target_link_libraries(${new_target} PRIVATE Sanitizer::memory)
if(EXISTS "${sanitizer_suppression_dir}/msan.supp")
target_compile_options(
${new_target}
PRIVATE -fsanitize-ignorelist=${sanitizer_suppression_dir}/msan.supp)
target_link_options(
${new_target} PRIVATE
-fsanitize-ignorelist=${sanitizer_suppression_dir}/msan.supp)
endif()
elseif(tool STREQUAL UBSAN)
target_link_libraries(${new_target} PRIVATE Sanitizer::undefined)
elseif(tool STREQUAL TSAN)
target_link_libraries(${new_target} PRIVATE Sanitizer::thread)
elseif(tool STREQUAL MEMCHECK)
set(memcheck_command
$<TARGET_FILE:valgrind::valgrind> --tool=memcheck --error-exitcode=1
--max-threads=10000 --trace-children=yes --gen-suppressions=all
--track-fds=no --leak-check=full)
foreach(suppression_file ${valgrind_suppression_files})
set(memcheck_command
"${memcheck_command} --suppressions=${suppression_file}")
endforeach()
separate_arguments(memcheck_command)
set(new_target_command "${memcheck_command};$<TARGET_FILE:${new_target}>")
elseif(tool STREQUAL HELGRIND)
set(helgrind_command
$<TARGET_FILE:valgrind::valgrind> --tool=helgrind --error-exitcode=1
--trace-children=yes --gen-suppressions=all --max-threads=5000)
foreach(suppression_file ${valgrind_suppression_files})
set(helgrind_command
"${helgrind_command} --suppressions=${suppression_file}")
endforeach()
separate_arguments(helgrind_command)
set(new_target_command "${helgrind_command};$<TARGET_FILE:${new_target}>")
elseif(tool STREQUAL CUDA-MEMCHECK)
set(memcheck_command
$<TARGET_FILE:CUDA-MEMCHECK::cuda-memcheck> --tool memcheck
--leak-check full --error-exitcode 1 --flush-to-disk yes)
set(new_target_command "${memcheck_command};$<TARGET_FILE:${new_target}>")
elseif(tool STREQUAL CUDA-SYNCCHECK)
set(synccheck_command
$<TARGET_FILE:CUDA-MEMCHECK::cuda-memcheck> --tool synccheck
--leak-check full --error-exitcode 1 --flush-to-disk yes)
set(new_target_command
"${synccheck_command};$<TARGET_FILE:${new_target}>")
elseif(tool STREQUAL CUDA-INITCHECK)
set(initcheck_command
$<TARGET_FILE:CUDA-MEMCHECK::cuda-memcheck> --tool initcheck
--leak-check full --error-exitcode 1 --flush-to-disk yes)
set(new_target_command
"${initcheck_command};$<TARGET_FILE:${new_target}>")
elseif(tool STREQUAL CUDA-RACECHECK)
set(racecheck_command
$<TARGET_FILE:CUDA-MEMCHECK::cuda-memcheck> --tool racecheck
--leak-check full --error-exitcode 1 --flush-to-disk yes)
set(new_target_command
"${racecheck_command};$<TARGET_FILE:${new_target}>")
endif()
if(this_FUZZING)
__add_fuzzing_test(${new_target} ${new_target_command})
else()
add_test(
NAME ${new_target}
COMMAND ${new_target_command} ${this_ARGS}
WORKING_DIRECTORY "${LIB_DIRECTORY}")
endif()
set_tests_properties(${new_target} PROPERTIES ENVIRONMENT "${new_env}")
endforeach()
if(NOT has_test)
if(this_FUZZING)
__add_fuzzing_test(${this_TARGET} ${this_TARGET})
else()
add_test(
NAME ${this_TARGET}
COMMAND ${this_TARGET} ${this_ARGS}
WORKING_DIRECTORY "${LIB_DIRECTORY}")
endif()
endif()
endfunction()
function(add_test_with_runtime_analysis)
__test_impl("${ARGV};FUZZING;OFF")
endfunction()