CMakeLists.txt revision de55be3899b994731ba2d9e168281d608dab3048
1# Testing rules for AddressSanitizer.
2#
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.
6#
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.
11
12include(CheckCXXCompilerFlag)
13
14include_directories(..)
15include_directories(../..)
16
17set(ASAN_UNITTEST_COMMON_CFLAGS
18  -Wall
19  -Wno-format
20  -Werror
21  -fvisibility=hidden
22  -g
23  -O2
24)
25
26if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
27  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -Wno-variadic-macros)
28endif()
29
30# Use -D instead of definitions to please custom compile command.
31if(ANDROID)
32  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
33    -DASAN_LOW_MEMORY=1
34    -DASAN_HAS_BLACKLIST=1
35    -DASAN_HAS_EXCEPTIONS=1
36    -DASAN_NEEDS_SEGV=0
37    -DASAN_UAR=0
38    -fPIE
39  )
40else()
41  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS
42    -DASAN_HAS_BLACKLIST=1
43    -DASAN_HAS_EXCEPTIONS=1
44    -DASAN_NEEDS_SEGV=1
45    -DASAN_UAR=0
46  )
47endif()
48
49# Support 64-bit and 32-bit builds.
50if(LLVM_BUILD_32_BITS)
51  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m32)
52else()
53  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m64)
54endif()
55
56set(ASAN_GTEST_INCLUDE_CFLAGS
57  -I${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include
58  -I${LLVM_MAIN_SRC_DIR}/include
59  -I${LLVM_BINARY_DIR}/include
60  -D__STDC_CONSTANT_MACROS
61  -D__STDC_LIMIT_MACROS
62)
63
64set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
65  ${ASAN_UNITTEST_COMMON_CFLAGS}
66  ${ASAN_GTEST_INCLUDE_CFLAGS}
67  -faddress-sanitizer
68  -mllvm "-asan-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore"
69  -mllvm -asan-stack=1
70  -mllvm -asan-globals=1
71  -mllvm -asan-mapping-scale=0        # default will be used
72  -mllvm -asan-mapping-offset-log=-1  # default will be used
73  -mllvm -asan-use-after-return=0
74)
75
76function(add_asan_test testsuite testname)
77  add_unittest(${testsuite} ${testname} ${ARGN})
78  if (APPLE)
79    # Darwin-specific linker flags.
80    set_property(TARGET ${testname} APPEND PROPERTY
81                 LINK_FLAGS "-framework Foundation")
82    target_link_libraries(${testname} clang_rt.asan_osx)
83  elseif (ANDROID)
84    target_link_libraries(${testname} clang_rt.asan-arm-android)
85  elseif (UNIX)
86    # Linux-specific linker flags.
87    set_property(TARGET ${testname} APPEND PROPERTY
88                 LINK_FLAGS "-lpthread -ldl -rdynamic")
89    if(LLVM_BUILD_32_BITS)
90      target_link_libraries(${testname} clang_rt.asan-i386)
91    else()
92      target_link_libraries(${testname} clang_rt.asan-x86_64)
93    endif()
94  endif()
95  set(add_compile_flags "")
96  get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS)
97  foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
98    set(add_compile_flags "${add_compile_flags} ${arg}")
99  endforeach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
100  set_property(TARGET ${testname} PROPERTY COMPILE_FLAGS
101               "${compile_flags} ${add_compile_flags}")
102endfunction()
103
104set(ASAN_NOINST_TEST_SOURCES
105  asan_noinst_test.cc
106  asan_break_optimization.cc
107  asan_test_main.cc
108)
109
110set(ASAN_INST_TEST_OBJECTS)
111
112# We only support building instrumented tests when we're not cross compiling
113# and targeting a unix-like system where we can predict viable compilation and
114# linking strategies.
115# We use a different approach to build these tests for Android. See below.
116if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
117
118  # This function is a custom routine to manage manually compiling source files
119  # for unit tests with the just-built Clang binary, using the ASan
120  # instrumentation, and linking them into a test executable.
121  function(add_asan_compile_command source extra_cflags)
122    set(output_obj "${source}.asan.o")
123    add_custom_command(
124      OUTPUT ${output_obj}
125      COMMAND clang
126              ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
127              ${extra_cflags}
128              -c -o "${output_obj}"
129              ${CMAKE_CURRENT_SOURCE_DIR}/${source}
130      MAIN_DEPENDENCY ${source}
131      DEPENDS clang ${ASAN_RUNTIME_LIBRARIES} ${ARGN}
132      )
133  endfunction()
134
135  add_asan_compile_command(asan_globals_test.cc "")
136  add_asan_compile_command(asan_test.cc "")
137  list(APPEND ASAN_INST_TEST_OBJECTS asan_globals_test.cc.asan.o
138                                     asan_test.cc.asan.o)
139  if (APPLE)
140    add_asan_compile_command(asan_mac_test.mm "-ObjC")
141    list(APPEND ASAN_INST_TEST_OBJECTS asan_mac_test.mm.asan.o)
142  endif()
143
144  # Build benchmarks test instrumented with AddressSanitizer.
145  add_asan_compile_command(asan_benchmarks_test.cc "")
146  add_custom_target(AsanBenchmarks)
147  set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks")
148  add_asan_test(AsanBenchmarks AsanBenchmark asan_break_optimization.cc
149                                             asan_benchmarks_test.cc.asan.o)
150endif()
151
152# Main AddressSanitizer unit tests.
153add_custom_target(AsanUnitTests)
154set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests")
155
156if(ANDROID)
157  set(ASAN_INST_TEST_SOURCES asan_globals_test.cc asan_test.cc)
158  add_library(asan_noinst_test OBJECT
159    ${ASAN_NOINST_TEST_SOURCES}
160    )
161  set_target_compile_flags(asan_noinst_test
162    ${ASAN_UNITTEST_COMMON_CFLAGS} ${ASAN_GTEST_INCLUDE_CFLAGS}
163    )
164  add_asan_test(AsanUnitTests AsanTest
165    ${ASAN_INST_TEST_SOURCES}
166    $<TARGET_OBJECTS:asan_noinst_test>
167    )
168  set_target_compile_flags(AsanTest
169    ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ASAN_GTEST_INCLUDE_CFLAGS}
170    )
171  set_target_link_flags(AsanTest
172    -pie
173    )
174else()
175  add_asan_test(AsanUnitTests AsanTest ${ASAN_NOINST_TEST_SOURCES}
176    ${ASAN_INST_TEST_OBJECTS})
177endif()
178