[CMake] Test for libdl and libpthread presence
[blocksruntime.git] / lib / asan / tests / CMakeLists.txt
blob15864c4bef9e576e14d46728113c29f6e93b5491
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
19   asan_mac_test.h
20   asan_test_config.h
21   asan_test_utils.h)
23 set(ASAN_UNITTEST_COMMON_CFLAGS
24   ${COMPILER_RT_GTEST_INCLUDE_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
29   -Wall
30   -Wno-format
31   -Werror
32   -Werror=sign-compare
33   -g
34   -O2)
35 append_if(ASAN_UNITTEST_COMMON_CFLAGS COMPILER_RT_HAS_WNO_VARIADIC_MACROS_FLAG -Wno-variadic-macros)
37 # Use -D instead of definitions to please custom compile command.
38 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
39   -DASAN_HAS_BLACKLIST=1
40   -DASAN_HAS_EXCEPTIONS=1
41   -DASAN_NEEDS_SEGV=1
42   -DASAN_UAR=0)
44 set(ASAN_BLACKLIST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore")
45 set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
46   ${ASAN_UNITTEST_COMMON_CFLAGS}
47   -fsanitize=address
48   "-fsanitize-blacklist=${ASAN_BLACKLIST_FILE}"
49   -mllvm -asan-stack=1
50   -mllvm -asan-globals=1
53 # Unit tests require libstdc++.
54 set(ASAN_UNITTEST_COMMON_LINKFLAGS -lstdc++)
55 # Unit tests on Mac depend on Foundation.
56 if(APPLE)
57   list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS -framework Foundation)
58 endif()
59 if(ANDROID)
60   list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS -pie)
61 endif()
63 set(ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS
64   ${ASAN_UNITTEST_COMMON_LINKFLAGS})
65 # On Android, we link with ASan runtime manually. On other platforms we depend
66 # on Clang driver behavior, passing -fsanitize=address flag.
67 if(NOT ANDROID)
68   list(APPEND ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS -fsanitize=address)
69 endif()
71 set(ASAN_UNITTEST_NOINST_LINKFLAGS
72   ${ASAN_UNITTEST_COMMON_LINKFLAGS} -lm)
73 append_if(ASAN_UNITTEST_NOINST_LINKFLAGS COMPILER_RT_HAS_LIBDL -ldl)
74 append_if(ASAN_UNITTEST_NOINST_LINKFLAGS COMPILER_RT_HAS_LIBPTHREAD -lpthread)
76 # Compile source for the given architecture, using compiler
77 # options in ${ARGN}, and add it to the object list.
78 macro(asan_compile obj_list source arch)
79   get_filename_component(basename ${source} NAME)
80   set(output_obj "${obj_list}.${basename}.${arch}.o")
81   get_target_flags_for_arch(${arch} TARGET_CFLAGS)
82   set(COMPILE_DEPS ${ASAN_UNITTEST_HEADERS} ${ASAN_BLACKLIST_FILE})
83   if(NOT COMPILER_RT_STANDALONE_BUILD)
84     list(APPEND COMPILE_DEPS gtest asan)
85   endif()
86   clang_compile(${output_obj} ${source}
87                 CFLAGS ${ARGN} ${TARGET_CFLAGS}
88                 DEPS ${COMPILE_DEPS})
89   list(APPEND ${obj_list} ${output_obj})
90 endmacro()
92 # Link ASan unit test for a given architecture from a set
93 # of objects in with given linker flags.
94 macro(add_asan_test test_suite test_name arch)
95   parse_arguments(TEST "OBJECTS;LINKFLAGS" "WITH_TEST_RUNTIME" ${ARGN})
96   get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
97   set(TEST_DEPS ${TEST_OBJECTS})
98   if(NOT COMPILER_RT_STANDALONE_BUILD)
99     list(APPEND TEST_DEPS asan)
100   endif()
101   if(TEST_WITH_TEST_RUNTIME)
102     list(APPEND TEST_DEPS ${ASAN_TEST_RUNTIME})
103     list(APPEND TEST_OBJECTS lib${ASAN_TEST_RUNTIME}.a)
104   endif()
105   add_compiler_rt_test(${test_suite} ${test_name}
106                        OBJECTS ${TEST_OBJECTS}
107                        DEPS ${TEST_DEPS}
108                        LINK_FLAGS ${TEST_LINKFLAGS}
109                                   ${TARGET_LINK_FLAGS})
110 endmacro()
112 # Main AddressSanitizer unit tests.
113 add_custom_target(AsanUnitTests)
114 set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests")
115 # ASan benchmarks (not actively used now).
116 add_custom_target(AsanBenchmarks)
117 set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks")
119 set(ASAN_NOINST_TEST_SOURCES
120   ${COMPILER_RT_GTEST_SOURCE}
121   asan_fake_stack_test.cc
122   asan_noinst_test.cc
123   asan_test_main.cc)
125 set(ASAN_INST_TEST_SOURCES
126   ${COMPILER_RT_GTEST_SOURCE}
127   asan_asm_test.cc
128   asan_globals_test.cc
129   asan_interface_test.cc
130   asan_test.cc
131   asan_oob_test.cc
132   asan_mem_test.cc
133   asan_str_test.cc
134   asan_test_main.cc)
135 if(APPLE)
136   list(APPEND ASAN_INST_TEST_SOURCES asan_mac_test.cc)
137 endif()
139 set(ASAN_BENCHMARKS_SOURCES
140   ${COMPILER_RT_GTEST_SOURCE}
141   asan_benchmarks_test.cc)  
143 # Adds ASan unit tests and benchmarks for architecture.
144 macro(add_asan_tests_for_arch arch)
145   # Instrumented tests.
146   set(ASAN_INST_TEST_OBJECTS)
147   foreach(src ${ASAN_INST_TEST_SOURCES})
148     asan_compile(ASAN_INST_TEST_OBJECTS ${src} ${arch}
149                  ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
150   endforeach()
151   if (APPLE)
152     # Add Mac-specific helper.
153     asan_compile(ASAN_INST_TEST_OBJECTS asan_mac_test_helpers.mm ${arch}
154                  ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -ObjC)
155   endif()
156   add_asan_test(AsanUnitTests "Asan-${arch}-Test" ${arch}
157                 OBJECTS ${ASAN_INST_TEST_OBJECTS}
158                 LINKFLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS})
160   # Add static ASan runtime that will be linked with uninstrumented tests.
161   set(ASAN_TEST_RUNTIME RTAsanTest.${arch})
162   if(APPLE)
163     set(ASAN_TEST_RUNTIME_OBJECTS
164       $<TARGET_OBJECTS:RTAsan.osx>
165       $<TARGET_OBJECTS:RTInterception.osx>
166       $<TARGET_OBJECTS:RTSanitizerCommon.osx>
167       $<TARGET_OBJECTS:RTLSanCommon.osx>)
168   else()
169     set(ASAN_TEST_RUNTIME_OBJECTS
170       $<TARGET_OBJECTS:RTAsan.${arch}>
171       $<TARGET_OBJECTS:RTInterception.${arch}>
172       $<TARGET_OBJECTS:RTLSanCommon.${arch}>
173       $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
174       $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
175   endif()
176   add_library(${ASAN_TEST_RUNTIME} STATIC ${ASAN_TEST_RUNTIME_OBJECTS})
177   set_target_properties(${ASAN_TEST_RUNTIME} PROPERTIES
178     ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
179   # Uninstrumented tests.
180   set(ASAN_NOINST_TEST_OBJECTS)
181   foreach(src ${ASAN_NOINST_TEST_SOURCES})
182     asan_compile(ASAN_NOINST_TEST_OBJECTS ${src} ${arch}
183                  ${ASAN_UNITTEST_COMMON_CFLAGS})
184   endforeach()
185   add_asan_test(AsanUnitTests "Asan-${arch}-Noinst-Test" ${arch}
186                 OBJECTS ${ASAN_NOINST_TEST_OBJECTS}
187                 LINKFLAGS ${ASAN_UNITTEST_NOINST_LINKFLAGS}
188                 WITH_TEST_RUNTIME)
190   # Benchmarks.
191   set(ASAN_BENCHMARKS_OBJECTS)
192   foreach(src ${ASAN_BENCHMARKS_SOURCES})
193     asan_compile(ASAN_BENCHMARKS_OBJECTS ${src} ${arch}
194                  ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
195   endforeach()
196   add_asan_test(AsanBenchmarks "Asan-${arch}-Benchmark" ${arch}
197                 OBJECTS ${ASAN_BENCHMARKS_OBJECTS}
198                 LINKFLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS})
199 endmacro()
201 if(COMPILER_RT_CAN_EXECUTE_TESTS)
202   foreach(arch ${ASAN_SUPPORTED_ARCH})
203     add_asan_tests_for_arch(${arch})
204   endforeach()
205 endif()
207 if(ANDROID)
208   # We assume that unit tests on Android are built in a build
209   # tree with fresh Clang as a host compiler.
210   
211   # Test w/o ASan instrumentation. Link it with ASan statically.
212   add_executable(AsanNoinstTest
213     $<TARGET_OBJECTS:RTAsan.arm.android>
214     $<TARGET_OBJECTS:RTInterception.arm.android>
215     $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>
216     ${COMPILER_RT_GTEST_SOURCE}
217     ${ASAN_NOINST_TEST_SOURCES})
218   set_target_compile_flags(AsanNoinstTest ${ASAN_UNITTEST_COMMON_CFLAGS})
219   set_target_link_flags(AsanNoinstTest ${ASAN_UNITTEST_NOINST_LINKFLAGS})
220   target_link_libraries(AsanNoinstTest log)
222   # Test with ASan instrumentation. Link with ASan dynamic runtime.
223   add_executable(AsanTest
224     ${COMPILER_RT_GTEST_SOURCE}
225     ${ASAN_INST_TEST_SOURCES})
226   set_target_compile_flags(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS})
227   set_target_link_flags(AsanTest ${ASAN_UNITTEST_INSTRUMENTED_LINKFLAGS})
228   target_link_libraries(AsanTest clang_rt.asan-arm-android)
230   # Setup correct output directory and link flags.
231   set_target_properties(AsanNoinstTest AsanTest PROPERTIES
232     RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
233   # Add unit test to test suite.
234   add_dependencies(AsanUnitTests AsanNoinstTest AsanTest)
235 endif()