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