1 # Testing rules for AddressSanitizer.
3 # These are broken into two buckets. One set of tests directly interacts with
4 # the runtime library and checks its functionality. These are the
5 # no-instrumentation tests.
7 # Another group of tests relies upon the ability to compile the test with
8 # address sanitizer instrumentation pass. These tests form "integration" tests
9 # and have some elements of version skew -- they test the *host* compiler's
10 # instrumentation against the just-built runtime library.
12 include(CheckCXXCompilerFlag)
13 include(CompilerRTCompile)
15 include_directories(..)
16 include_directories(../..)
18 set(ASAN_UNITTEST_HEADERS
23 set(ASAN_UNITTEST_COMMON_CFLAGS
24 ${COMPILER_RT_UNITTEST_CFLAGS}
25 ${COMPILER_RT_GTEST_CFLAGS}
26 ${SANITIZER_TEST_CXX_CFLAGS}
27 -I${COMPILER_RT_SOURCE_DIR}/include
28 -I${COMPILER_RT_SOURCE_DIR}/lib
29 -I${COMPILER_RT_SOURCE_DIR}/lib/asan
30 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/tests
31 -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS
36 append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros ASAN_UNITTEST_COMMON_CFLAGS)
38 # This will ensure the target linker is used
39 # during cross compilation
40 set(ASAN_UNITTEST_COMMON_LINK_FLAGS
41 ${COMPILER_RT_UNITTEST_LINK_FLAGS}
42 ${COMPILER_RT_UNWINDER_LINK_LIBS}
43 ${SANITIZER_TEST_CXX_LIBRARIES})
45 # -gline-tables-only must be enough for ASan, so use it if possible.
46 if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
47 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gline-tables-only)
49 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -g)
52 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gcodeview)
54 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -g)
56 # Use -D instead of definitions to please custom compile command.
57 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
58 -DASAN_HAS_IGNORELIST=1
59 -DASAN_HAS_EXCEPTIONS=1
64 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS ${DARWIN_osx_CFLAGS})
65 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS ${DARWIN_osx_LINK_FLAGS})
67 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
68 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
69 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
70 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS})
73 set(ASAN_IGNORELIST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore")
74 set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
75 ${ASAN_UNITTEST_COMMON_CFLAGS}
77 "-fsanitize-ignorelist=${ASAN_IGNORELIST_FILE}"
80 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS --driver-mode=g++)
83 # x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests.
84 if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
85 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS "-lc++")
88 # Unit tests on Mac depend on Foundation.
90 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -framework Foundation)
93 list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -pie)
96 set(ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS
97 ${ASAN_UNITTEST_COMMON_LINK_FLAGS})
98 list(APPEND ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS -fsanitize=address)
100 set(ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS
101 ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS}
104 set(ASAN_UNITTEST_INSTRUMENTED_LIBS)
106 set(ASAN_UNITTEST_NOINST_LINK_FLAGS ${ASAN_UNITTEST_COMMON_LINK_FLAGS})
108 append_list_if(COMPILER_RT_HAS_LIBM -lm ASAN_UNITTEST_NOINST_LINK_FLAGS)
109 append_list_if(COMPILER_RT_HAS_LIBDL -ldl ASAN_UNITTEST_NOINST_LINK_FLAGS)
110 append_list_if(COMPILER_RT_HAS_LIBRT -lrt ASAN_UNITTEST_NOINST_LINK_FLAGS)
111 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ASAN_UNITTEST_NOINST_LINK_FLAGS)
112 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS)
115 # TODO(eugenis): move all -l flags above to _LIBS?
116 set(ASAN_UNITTEST_NOINST_LIBS)
117 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_UNITTEST_NOINST_LIBS)
119 # Main AddressSanitizer unit tests.
120 add_custom_target(AsanUnitTests)
121 set_target_properties(AsanUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
123 # AddressSanitizer unit tests with dynamic runtime (on platforms where it's
125 add_custom_target(AsanDynamicUnitTests)
126 set_target_properties(AsanDynamicUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
127 # ASan benchmarks (not actively used now).
128 add_custom_target(AsanBenchmarks)
129 set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Compiler-RT Tests")
131 set(ASAN_NOINST_TEST_SOURCES
132 ${COMPILER_RT_GTEST_SOURCE}
133 asan_fake_stack_test.cpp
138 set(ASAN_INST_TEST_SOURCES
139 ${COMPILER_RT_GTEST_SOURCE}
140 asan_globals_test.cpp
141 asan_interface_test.cpp
142 asan_internal_interface_test.cpp
150 list(APPEND ASAN_INST_TEST_SOURCES
152 asan_mac_test_helpers.mm
156 set(ASAN_BENCHMARKS_SOURCES
157 ${COMPILER_RT_GTEST_SOURCE}
158 asan_benchmarks_test.cpp
161 function(add_asan_tests arch test_runtime)
162 cmake_parse_arguments(TEST "" "KIND" "CFLAGS" ${ARGN})
164 # The Lit files are configured once per architecture and static/dynamic
165 # selection. Each configuration expects the test binaries in a corresponding
166 # subdirectory. Generate subdirectory names based on the architecture name.
167 string(TOUPPER ${arch} ARCH_UPPER_CASE)
168 set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
169 set(CONFIG_NAME_DYNAMIC ${ARCH_UPPER_CASE}${OS_NAME}DynamicConfig)
171 # Closure to keep the values.
172 function(generate_asan_tests test_objects test_suite testname)
173 generate_compiler_rt_tests(${test_objects} ${test_suite} ${testname} ${arch}
174 COMPILE_DEPS ${ASAN_UNITTEST_HEADERS} ${ASAN_IGNORELIST_FILE}
179 set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
182 set(TARGET_LINK_FLAGS)
183 get_target_link_flags_for_arch(${arch} TARGET_LINK_FLAGS)
185 set(ASAN_INST_TEST_OBJECTS)
186 generate_asan_tests(ASAN_INST_TEST_OBJECTS AsanUnitTests
187 "Asan-${arch}${TEST_KIND}-Test"
188 SUBDIR "${CONFIG_NAME}"
189 LINK_FLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS} ${TARGET_LINK_FLAGS}
190 SOURCES ${ASAN_INST_TEST_SOURCES}
191 CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${TEST_CFLAGS})
193 if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
194 set(dynamic_test_name "Asan-${arch}${TEST_KIND}-Dynamic-Test")
197 # With the MSVC CRT, the choice between static and dynamic CRT is made at
198 # compile time with a macro. Simulate the effect of passing /MD to clang-cl.
199 set(ASAN_DYNAMIC_TEST_OBJECTS)
200 generate_asan_tests(ASAN_DYNAMIC_TEST_OBJECTS
201 AsanDynamicUnitTests "${dynamic_test_name}"
202 SUBDIR "${CONFIG_NAME_DYNAMIC}"
203 CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -D_MT -D_DLL
204 SOURCES ${ASAN_INST_TEST_SOURCES}
205 LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS}
206 -Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames
210 # Otherwise, reuse ASAN_INST_TEST_OBJECTS.
211 add_compiler_rt_test(AsanDynamicUnitTests "${dynamic_test_name}" "${arch}"
212 SUBDIR "${CONFIG_NAME_DYNAMIC}"
213 OBJECTS ${ASAN_INST_TEST_OBJECTS}
214 DEPS asan ${ASAN_INST_TEST_OBJECTS}
215 LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS} ${TARGET_LINK_FLAGS}
220 # Uninstrumented tests.
221 set(ASAN_NOINST_TEST_OBJECTS)
222 generate_asan_tests(ASAN_NOINST_TEST_OBJECTS
223 AsanUnitTests "Asan-${arch}${TEST_KIND}-Noinst-Test"
224 SUBDIR "${CONFIG_NAME}"
225 CFLAGS ${ASAN_UNITTEST_COMMON_CFLAGS}
226 LINK_FLAGS ${ASAN_UNITTEST_NOINST_LINK_FLAGS} ${TARGET_LINK_FLAGS}
227 SOURCES ${ASAN_NOINST_TEST_SOURCES}
228 RUNTIME ${test_runtime})
230 set(ASAN_BENCHMARK_OBJECTS)
231 generate_asan_tests(ASAN_BENCHMARK_OBJECTS
232 AsanBenchmarks "Asan-${arch}${TEST_KIND}-Benchmark"
233 SUBDIR "${CONFIG_NAME}"
234 CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
235 SOURCES ${ASAN_BENCHMARKS_SOURCES}
236 LINK_FLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS} ${TARGET_LINK_FLAGS})
239 if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID)
240 set(ASAN_TEST_ARCH ${ASAN_SUPPORTED_ARCH})
242 darwin_filter_host_archs(ASAN_SUPPORTED_ARCH ASAN_TEST_ARCH)
244 list(REMOVE_ITEM ASAN_TEST_ARCH sparc sparcv9)
245 if(OS_NAME MATCHES "SunOS")
246 list(REMOVE_ITEM ASAN_TEST_ARCH x86_64)
249 foreach(arch ${ASAN_TEST_ARCH})
251 # Add static ASan runtime that will be linked with uninstrumented tests.
252 set(ASAN_TEST_RUNTIME RTAsanTest.${arch})
254 set(ASAN_TEST_RUNTIME_OBJECTS
255 $<TARGET_OBJECTS:RTAsan_dynamic.osx>
256 $<TARGET_OBJECTS:RTInterception.osx>
257 $<TARGET_OBJECTS:RTSanitizerCommon.osx>
258 $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>
259 $<TARGET_OBJECTS:RTSanitizerCommonCoverage.osx>
260 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.osx>
261 $<TARGET_OBJECTS:RTLSanCommon.osx>
262 $<TARGET_OBJECTS:RTUbsan.osx>)
264 set(ASAN_TEST_RUNTIME_OBJECTS
265 $<TARGET_OBJECTS:RTAsan.${arch}>
266 $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
267 $<TARGET_OBJECTS:RTAsan_static.${arch}>
268 $<TARGET_OBJECTS:RTInterception.${arch}>
269 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
270 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
271 $<TARGET_OBJECTS:RTSanitizerCommonCoverage.${arch}>
272 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
273 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizerInternal.${arch}>
274 $<TARGET_OBJECTS:RTLSanCommon.${arch}>
275 $<TARGET_OBJECTS:RTUbsan.${arch}>
276 $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>)
278 add_library(${ASAN_TEST_RUNTIME} STATIC ${ASAN_TEST_RUNTIME_OBJECTS})
279 set_target_properties(${ASAN_TEST_RUNTIME} PROPERTIES
280 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
281 FOLDER "Compiler-RT Runtime tests")
283 add_asan_tests(${arch} ${ASAN_TEST_RUNTIME} KIND "-inline")
284 add_asan_tests(${arch} ${ASAN_TEST_RUNTIME} KIND "-calls"
285 CFLAGS -mllvm -asan-instrumentation-with-call-threshold=0)
290 foreach(arch ${ASAN_SUPPORTED_ARCH})
291 # Test w/o ASan instrumentation. Link it with ASan statically.
292 add_executable(AsanNoinstTest # FIXME: .arch?
293 $<TARGET_OBJECTS:RTAsan.${arch}>
294 $<TARGET_OBJECTS:RTAsan_static.${arch}>
295 $<TARGET_OBJECTS:RTInterception.${arch}>
296 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
297 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
298 $<TARGET_OBJECTS:RTSanitizerCommonCoverage.${arch}>
299 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
300 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizerInternal.${arch}>
301 $<TARGET_OBJECTS:RTLSanCommon.${arch}>
302 $<TARGET_OBJECTS:RTUbsan.${arch}>
303 $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
304 ${COMPILER_RT_GTEST_SOURCE}
305 ${ASAN_NOINST_TEST_SOURCES})
306 set_target_compile_flags(AsanNoinstTest ${ASAN_UNITTEST_COMMON_CFLAGS})
307 set_target_link_flags(AsanNoinstTest ${ASAN_UNITTEST_NOINST_LINK_FLAGS})
308 target_link_libraries(AsanNoinstTest ${ASAN_UNITTEST_NOINST_LIBS})
310 # Test with ASan instrumentation. Link with ASan dynamic runtime.
311 add_executable(AsanTest
312 ${COMPILER_RT_GTEST_SOURCE}
313 ${ASAN_INST_TEST_SOURCES})
314 set_target_compile_flags(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
315 set_target_link_flags(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS})
316 target_link_libraries(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_LIBS})
318 # Setup correct output directory and link flags.
319 set_target_properties(AsanNoinstTest AsanTest PROPERTIES
320 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
321 # Add unit tests to the test suite.
322 add_dependencies(AsanUnitTests AsanNoinstTest AsanTest)