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_GTEST_CFLAGS}
25 -I${COMPILER_RT_SOURCE_DIR}/include
26 -I${COMPILER_RT_SOURCE_DIR}/lib
27 -I${COMPILER_RT_SOURCE_DIR}/lib/asan
28 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/tests
32 append_if(COMPILER_RT_HAS_G_FLAG -g ASAN_UNITTEST_COMMON_CFLAGS)
33 append_if(COMPILER_RT_HAS_Zi_FLAG -Zi ASAN_UNITTEST_COMMON_CFLAGS)
34 append_if(COMPILER_RT_HAS_WNO_VARIADIC_MACROS_FLAG -Wno-variadic-macros ASAN_UNITTEST_COMMON_CFLAGS)
37 # MSVC system headers and gtest use a lot of deprecated stuff.
38 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
39 -Wno-deprecated-declarations)
41 # clang-cl doesn't support exceptions yet.
42 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
46 # We should teach clang-cl to understand more pragmas.
47 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
49 -Wno-undefined-inline)
52 # Use -D instead of definitions to please custom compile command.
53 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
54 -DASAN_HAS_BLACKLIST=1
55 -DASAN_HAS_EXCEPTIONS=1
58 set(ASAN_BLACKLIST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore")
59 set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
60 ${ASAN_UNITTEST_COMMON_CFLAGS}
62 "-fsanitize-blacklist=${ASAN_BLACKLIST_FILE}"
65 # Unit tests require libstdc++ on POSIX.
67 list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS --driver-mode=g++ -lstdc++)
70 # x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests.
71 if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
72 list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS "-lc++")
75 # Unit tests on Mac depend on Foundation.
77 list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS -framework Foundation)
80 list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS -pie)
83 set(ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS
84 ${ASAN_UNITTEST_COMMON_LINKFLAGS})
85 # On Android, we link with ASan runtime manually. On other platforms we depend
86 # on Clang driver behavior, passing -fsanitize=address flag.
88 list(APPEND ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS -fsanitize=address)
91 set(ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINKFLAGS
92 ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS}
95 set(ASAN_UNITTEST_NOINST_LINKFLAGS ${ASAN_UNITTEST_COMMON_LINKFLAGS})
96 append_if(COMPILER_RT_HAS_LIBM -lm ASAN_UNITTEST_NOINST_LINKFLAGS)
97 append_if(COMPILER_RT_HAS_LIBDL -ldl ASAN_UNITTEST_NOINST_LINKFLAGS)
98 append_if(COMPILER_RT_HAS_LIBPTHREAD -lpthread ASAN_UNITTEST_NOINST_LINKFLAGS)
99 append_if(COMPILER_RT_HAS_LIBPTHREAD -lpthread
100 ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINKFLAGS)
102 # Compile source for the given architecture, using compiler
103 # options in ${ARGN}, and add it to the object list.
104 macro(asan_compile obj_list source arch kind)
105 get_filename_component(basename ${source} NAME)
106 set(output_obj "${obj_list}.${basename}.${arch}${kind}.o")
107 get_target_flags_for_arch(${arch} TARGET_CFLAGS)
108 set(COMPILE_DEPS ${ASAN_UNITTEST_HEADERS} ${ASAN_BLACKLIST_FILE})
109 if(NOT COMPILER_RT_STANDALONE_BUILD)
110 list(APPEND COMPILE_DEPS gtest asan)
112 clang_compile(${output_obj} ${source}
113 CFLAGS ${ARGN} ${TARGET_CFLAGS}
114 DEPS ${COMPILE_DEPS})
115 list(APPEND ${obj_list} ${output_obj})
118 # Link ASan unit test for a given architecture from a set
119 # of objects in with given linker flags.
120 macro(add_asan_test test_suite test_name arch kind)
121 parse_arguments(TEST "OBJECTS;LINKFLAGS" "WITH_TEST_RUNTIME" ${ARGN})
122 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
123 set(TEST_DEPS ${TEST_OBJECTS})
124 if(NOT COMPILER_RT_STANDALONE_BUILD)
125 list(APPEND TEST_DEPS asan)
127 if(TEST_WITH_TEST_RUNTIME)
128 list(APPEND TEST_DEPS ${ASAN_TEST_RUNTIME})
130 list(APPEND TEST_OBJECTS lib${ASAN_TEST_RUNTIME}.a)
132 list(APPEND TEST_OBJECTS ${ASAN_TEST_RUNTIME}.lib)
135 add_compiler_rt_test(${test_suite} ${test_name}
136 OBJECTS ${TEST_OBJECTS}
138 LINK_FLAGS ${TEST_LINKFLAGS}
139 ${TARGET_LINK_FLAGS})
142 # Main AddressSanitizer unit tests.
143 add_custom_target(AsanUnitTests)
144 set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests")
145 # ASan benchmarks (not actively used now).
146 add_custom_target(AsanBenchmarks)
147 set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks")
149 set(ASAN_NOINST_TEST_SOURCES
150 ${COMPILER_RT_GTEST_SOURCE}
151 asan_fake_stack_test.cc
155 set(ASAN_INST_TEST_SOURCES
156 ${COMPILER_RT_GTEST_SOURCE}
159 asan_interface_test.cc
166 list(APPEND ASAN_INST_TEST_SOURCES asan_mac_test.cc)
169 set(ASAN_BENCHMARKS_SOURCES
170 ${COMPILER_RT_GTEST_SOURCE}
171 asan_benchmarks_test.cc)
173 # Adds ASan unit tests and benchmarks for architecture.
174 macro(add_asan_tests_for_arch_and_kind arch kind)
175 # Instrumented tests.
176 set(ASAN_INST_TEST_OBJECTS)
177 foreach(src ${ASAN_INST_TEST_SOURCES})
178 asan_compile(ASAN_INST_TEST_OBJECTS ${src} ${arch} ${kind}
179 ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ARGN})
182 # Add Mac-specific helper.
183 asan_compile(ASAN_INST_TEST_OBJECTS asan_mac_test_helpers.mm ${arch} ${kind}
184 ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -ObjC ${ARGN})
186 add_asan_test(AsanUnitTests "Asan-${arch}${kind}-Test" ${arch} ${kind}
187 OBJECTS ${ASAN_INST_TEST_OBJECTS}
188 LINKFLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS})
189 if(COMPILER_RT_BUILD_SHARED_ASAN)
190 add_asan_test(AsanUnitTests "Asan-${arch}${kind}-Dynamic-Test" ${arch} ${kind}
191 OBJECTS ${ASAN_INST_TEST_OBJECTS}
192 LINKFLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINKFLAGS})
195 # Add static ASan runtime that will be linked with uninstrumented tests.
196 set(ASAN_TEST_RUNTIME RTAsanTest.${arch}${kind})
198 set(ASAN_TEST_RUNTIME_OBJECTS
199 $<TARGET_OBJECTS:RTAsan.osx>
200 $<TARGET_OBJECTS:RTInterception.osx>
201 $<TARGET_OBJECTS:RTSanitizerCommon.osx>
202 $<TARGET_OBJECTS:RTLSanCommon.osx>)
204 set(ASAN_TEST_RUNTIME_OBJECTS
205 $<TARGET_OBJECTS:RTAsan.${arch}>
206 $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
207 $<TARGET_OBJECTS:RTInterception.${arch}>
208 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
209 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
211 list(APPEND ASAN_TEST_RUNTIME_OBJECTS
212 $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
215 add_library(${ASAN_TEST_RUNTIME} STATIC ${ASAN_TEST_RUNTIME_OBJECTS})
216 set_target_properties(${ASAN_TEST_RUNTIME} PROPERTIES
217 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
218 # Uninstrumented tests.
219 set(ASAN_NOINST_TEST_OBJECTS)
220 foreach(src ${ASAN_NOINST_TEST_SOURCES})
221 asan_compile(ASAN_NOINST_TEST_OBJECTS ${src} ${arch} ${kind}
222 ${ASAN_UNITTEST_COMMON_CFLAGS} ${ARGN})
224 add_asan_test(AsanUnitTests "Asan-${arch}${kind}-Noinst-Test" ${arch} ${kind}
225 OBJECTS ${ASAN_NOINST_TEST_OBJECTS}
226 LINKFLAGS ${ASAN_UNITTEST_NOINST_LINKFLAGS}
230 set(ASAN_BENCHMARKS_OBJECTS)
231 foreach(src ${ASAN_BENCHMARKS_SOURCES})
232 asan_compile(ASAN_BENCHMARKS_OBJECTS ${src} ${arch} ${kind}
233 ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ARGN})
235 add_asan_test(AsanBenchmarks "Asan-${arch}${kind}-Benchmark" ${arch} ${kind}
236 OBJECTS ${ASAN_BENCHMARKS_OBJECTS}
237 LINKFLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS})
238 if(COMPILER_RT_BUILD_SHARED_ASAN)
239 add_asan_test(AsanBenchmarks "Asan-${arch}${kind}-Dynamic-Benchmark" ${arch} ${kind}
240 OBJECTS ${ASAN_BENCHMARKS_OBJECTS}
241 LINKFLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINKFLAGS})
245 if(COMPILER_RT_CAN_EXECUTE_TESTS)
246 foreach(arch ${ASAN_SUPPORTED_ARCH})
247 add_asan_tests_for_arch_and_kind(${arch} "-inline")
248 add_asan_tests_for_arch_and_kind(${arch} "-with-calls"
249 -mllvm -asan-instrumentation-with-call-threshold=0)
254 # We assume that unit tests on Android are built in a build
255 # tree with fresh Clang as a host compiler.
257 # Test w/o ASan instrumentation. Link it with ASan statically.
258 add_executable(AsanNoinstTest
259 $<TARGET_OBJECTS:RTAsan.arm.android>
260 $<TARGET_OBJECTS:RTInterception.arm.android>
261 $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>
262 ${COMPILER_RT_GTEST_SOURCE}
263 ${ASAN_NOINST_TEST_SOURCES})
264 set_target_compile_flags(AsanNoinstTest ${ASAN_UNITTEST_COMMON_CFLAGS})
265 set_target_link_flags(AsanNoinstTest ${ASAN_UNITTEST_NOINST_LINKFLAGS})
266 target_link_libraries(AsanNoinstTest log)
268 # Test with ASan instrumentation. Link with ASan dynamic runtime.
269 add_executable(AsanTest
270 ${COMPILER_RT_GTEST_SOURCE}
271 ${ASAN_INST_TEST_SOURCES})
272 set_target_compile_flags(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
273 set_target_link_flags(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS})
274 target_link_libraries(AsanTest clang_rt.asan-arm-android)
276 # Setup correct output directory and link flags.
277 set_target_properties(AsanNoinstTest AsanTest PROPERTIES
278 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
279 # Add unit test to test suite.
280 add_dependencies(AsanUnitTests AsanNoinstTest AsanTest)