CMakeLists.txt revision 0f7d4a4ad69ad20053d8b78c611853e778bd465a
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  # Use -D instead of definitions to please custom compile command.
19  -DASAN_HAS_BLACKLIST=1
20  -DASAN_HAS_EXCEPTIONS=1
21  -DASAN_NEEDS_SEGV=1
22  -DASAN_UAR=0
23  -Wall
24  -Wno-format
25  -fvisibility=hidden
26  -g
27  -O2
28)
29# Support 64-bit and 32-bit builds.
30if(LLVM_BUILD_32_BITS)
31  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m32)
32else()
33  list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m64)
34endif()
35
36set(ASAN_GTEST_INCLUDE_CFLAGS
37  -I${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include
38  -I${LLVM_MAIN_SRC_DIR}/include
39  -I${LLVM_BINARY_DIR}/include
40  -D__STDC_CONSTANT_MACROS
41  -D__STDC_LIMIT_MACROS
42)
43
44set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS
45  ${ASAN_UNITTEST_COMMON_CFLAGS}
46  ${ASAN_GTEST_INCLUDE_CFLAGS}
47  -faddress-sanitizer
48  -mllvm "-asan-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore"
49  -mllvm -asan-stack=1
50  -mllvm -asan-globals=1
51  -mllvm -asan-mapping-scale=0        # default will be used
52  -mllvm -asan-mapping-offset-log=-1  # default will be used
53  -mllvm -asan-use-after-return=0
54)
55
56function(add_asan_test testsuite testname)
57  add_unittest(${testsuite} ${testname} ${ARGN})
58  if (APPLE)
59    # Darwin-specific linker flags.
60    set_property(TARGET ${testname} APPEND PROPERTY
61                 LINK_FLAGS "-framework Foundation")
62    target_link_libraries(${testname} clang_rt.asan_osx)
63  elseif (UNIX)
64    # Linux-specific linker flags.
65    set_property(TARGET ${testname} APPEND PROPERTY
66                 LINK_FLAGS "-lpthread -ldl -rdynamic")
67    if(LLVM_BUILD_32_BITS)
68      target_link_libraries(${testname} clang_rt.asan-i386)
69    else()
70      target_link_libraries(${testname} clang_rt.asan-x86_64)
71    endif()
72  endif()
73  set(add_compile_flags "")
74  get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS)
75  foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
76    set(add_compile_flags "${add_compile_flags} ${arg}")
77  endforeach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
78  set_property(TARGET ${testname} PROPERTY COMPILE_FLAGS
79               "${compile_flags} ${add_compile_flags}")
80endfunction()
81
82set(ASAN_NOINST_TEST_SOURCES
83  asan_noinst_test.cc
84  asan_break_optimization.cc
85)
86
87set(ASAN_INST_TEST_OBJECTS)
88
89# We only support building instrumented tests when we're not cross compiling
90# and targeting a unix-like system where we can predict viable compilation and
91# linking strategies.
92if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX)
93
94  # This function is a custom routine to manage manually compiling source files
95  # for unit tests with the just-built Clang binary, using the ASan
96  # instrumentation, and linking them into a test executable.
97  function(add_asan_compile_command source extra_cflags)
98    set(output_obj "${source}.asan.o")
99    add_custom_command(
100      OUTPUT ${output_obj}
101      COMMAND clang
102              ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
103              ${extra_cflags}
104              -c -o "${output_obj}"
105              ${CMAKE_CURRENT_SOURCE_DIR}/${source}
106      MAIN_DEPENDENCY ${source}
107      DEPENDS clang ${ASAN_RUNTIME_LIBRARIES} ${ARGN}
108      )
109  endfunction()
110
111  add_asan_compile_command(asan_globals_test.cc "")
112  add_asan_compile_command(asan_test.cc "")
113  list(APPEND ASAN_INST_TEST_OBJECTS asan_globals_test.cc.asan.o
114                                     asan_test.cc.asan.o)
115  if (APPLE)
116    add_asan_compile_command(asan_mac_test.mm "-ObjC")
117    list(APPEND ASAN_INST_TEST_OBJECTS asan_mac_test.mm.asan.o)
118  endif()
119
120  # Build benchmarks test instrumented with AddressSanitizer.
121  add_asan_compile_command(asan_benchmarks_test.cc "")
122  add_custom_target(AsanBenchmarks)
123  set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks")
124  add_asan_test(AsanBenchmarks AsanBenchmark asan_break_optimization.cc
125                                             asan_benchmarks_test.cc.asan.o)
126endif()
127
128# Main AddressSanitizer unit tests.
129add_custom_target(AsanUnitTests)
130set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests")
131add_asan_test(AsanUnitTests AsanTest ${ASAN_NOINST_TEST_SOURCES}
132                                     ${ASAN_INST_TEST_OBJECTS})
133