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