CMakeLists.txt revision 7a89732b47eba572d5c27bd2ad846d99c87f574c
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_BLACKLIST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore") 65 66set(ASAN_UNITTEST_INSTRUMENTED_CFLAGS 67 ${ASAN_UNITTEST_COMMON_CFLAGS} 68 ${ASAN_GTEST_INCLUDE_CFLAGS} 69 -faddress-sanitizer 70 -mllvm "-asan-blacklist=${ASAN_BLACKLIST_FILE}" 71 -mllvm -asan-stack=1 72 -mllvm -asan-globals=1 73 -mllvm -asan-mapping-scale=0 # default will be used 74 -mllvm -asan-mapping-offset-log=-1 # default will be used 75 -mllvm -asan-use-after-return=0 76) 77 78function(add_asan_test testsuite testname) 79 add_unittest(${testsuite} ${testname} ${ARGN}) 80 if (APPLE) 81 # Darwin-specific linker flags. 82 set_property(TARGET ${testname} APPEND PROPERTY 83 LINK_FLAGS "-framework Foundation") 84 target_link_libraries(${testname} clang_rt.asan_osx) 85 elseif (ANDROID) 86 target_link_libraries(${testname} clang_rt.asan-arm-android) 87 elseif (UNIX) 88 # Linux-specific linker flags. 89 set_property(TARGET ${testname} APPEND PROPERTY 90 LINK_FLAGS "-lpthread -ldl -rdynamic") 91 if(LLVM_BUILD_32_BITS) 92 target_link_libraries(${testname} clang_rt.asan-i386) 93 else() 94 target_link_libraries(${testname} clang_rt.asan-x86_64) 95 endif() 96 endif() 97 set(add_compile_flags "") 98 get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS) 99 foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS}) 100 set(add_compile_flags "${add_compile_flags} ${arg}") 101 endforeach(arg ${ASAN_UNITTEST_COMMON_CFLAGS}) 102 set_property(TARGET ${testname} PROPERTY COMPILE_FLAGS 103 "${compile_flags} ${add_compile_flags}") 104endfunction() 105 106set(ASAN_NOINST_TEST_SOURCES 107 asan_noinst_test.cc 108 asan_break_optimization.cc 109 asan_test_main.cc 110) 111 112set(ASAN_INST_TEST_OBJECTS) 113 114# We only support building instrumented tests when we're not cross compiling 115# and targeting a unix-like system where we can predict viable compilation and 116# linking strategies. 117# We use a different approach to build these tests for Android. See below. 118if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID) 119 120 # This function is a custom routine to manage manually compiling source files 121 # for unit tests with the just-built Clang binary, using the ASan 122 # instrumentation, and linking them into a test executable. 123 function(add_asan_compile_command source extra_cflags) 124 set(output_obj "${source}.asan.o") 125 add_custom_command( 126 OUTPUT ${output_obj} 127 COMMAND clang 128 ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} 129 ${extra_cflags} 130 -c -o "${output_obj}" 131 ${CMAKE_CURRENT_SOURCE_DIR}/${source} 132 MAIN_DEPENDENCY ${source} 133 DEPENDS clang ${ASAN_RUNTIME_LIBRARIES} ${ASAN_BLACKLIST_FILE} ${ARGN} 134 ) 135 endfunction() 136 137 add_asan_compile_command(asan_globals_test.cc "") 138 add_asan_compile_command(asan_test.cc "") 139 list(APPEND ASAN_INST_TEST_OBJECTS asan_globals_test.cc.asan.o 140 asan_test.cc.asan.o) 141 if (APPLE) 142 add_asan_compile_command(asan_mac_test.mm "-ObjC") 143 list(APPEND ASAN_INST_TEST_OBJECTS asan_mac_test.mm.asan.o) 144 endif() 145 146 # Build benchmarks test instrumented with AddressSanitizer. 147 add_asan_compile_command(asan_benchmarks_test.cc "") 148 add_custom_target(AsanBenchmarks) 149 set_target_properties(AsanBenchmarks PROPERTIES FOLDER "Asan benchmarks") 150 add_asan_test(AsanBenchmarks AsanBenchmark asan_break_optimization.cc 151 asan_benchmarks_test.cc.asan.o) 152endif() 153 154# Main AddressSanitizer unit tests. 155add_custom_target(AsanUnitTests) 156set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests") 157 158if(ANDROID) 159 set(ASAN_INST_TEST_SOURCES asan_globals_test.cc asan_test.cc) 160 add_library(asan_noinst_test OBJECT 161 ${ASAN_NOINST_TEST_SOURCES} 162 ) 163 set_target_compile_flags(asan_noinst_test 164 ${ASAN_UNITTEST_COMMON_CFLAGS} ${ASAN_GTEST_INCLUDE_CFLAGS} 165 ) 166 add_asan_test(AsanUnitTests AsanTest 167 ${ASAN_INST_TEST_SOURCES} 168 $<TARGET_OBJECTS:asan_noinst_test> 169 ) 170 set_target_compile_flags(AsanTest 171 ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${ASAN_GTEST_INCLUDE_CFLAGS} 172 ) 173 set_target_link_flags(AsanTest 174 -pie 175 ) 176else() 177 add_asan_test(AsanUnitTests AsanTest ${ASAN_NOINST_TEST_SOURCES} 178 ${ASAN_INST_TEST_OBJECTS}) 179endif() 180