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