CMakeLists.txt revision 79753b2c945b590e9a60a2ac2d96c088f920a905
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 17add_custom_target(AsanTests) 18set_target_properties(AsanTests PROPERTIES FOLDER "ASan tests") 19function(add_asan_test testname) 20 add_unittest(AsanTests ${testname} ${ARGN}) 21 if(CMAKE_SIZEOF_VOID_P EQUAL 4) 22 target_link_libraries(${testname} clang_rt.asan-i386) 23 else() 24 target_link_libraries(${testname} clang_rt.asan-x86_64) 25 endif() 26endfunction() 27 28add_asan_test(AsanNoInstrumentationTests 29 asan_noinst_test.cc 30 asan_break_optimization.cc 31 ) 32 33set(ASAN_EXTRA_CFLAGS 34 -I${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include 35 -I${LLVM_MAIN_SRC_DIR}/include 36 -I${LLVM_BINARY_DIR}/include 37 -D__STDC_CONSTANT_MACROS 38 -D__STDC_LIMIT_MACROS 39) 40 41# We only support building instrumented tests when we're not cross compiling 42# and targeting a unix-like system where we can predict viable compilation and 43# linking strategies. 44if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX) 45 46 # This function is a custom routine to manage manually compiling source files 47 # for unit tests with the just-built Clang binary, using the ASan 48 # instrumentation, and linking them into a test executable. 49 function(add_asan_compile_command source) 50 add_custom_command( 51 OUTPUT "${source}.asan.o" 52 COMMAND clang 53 -faddress-sanitizer ${ASAN_CFLAGS} ${ASAN_EXTRA_CFLAGS} 54 -mllvm "-asan-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/asan_test.ignore" 55 -DASAN_HAS_BLACKLIST=1 56 -DASAN_HAS_EXCEPTIONS=1 57 -DASAN_NEEDS_SEGV=1 58 -DASAN_UAR=0 59 -c -o "${source}.asan.o" 60 ${CMAKE_CURRENT_SOURCE_DIR}/${source} 61 MAIN_DEPENDENCY ${source} 62 DEPENDS clang ${ARGN} 63 ) 64 endfunction() 65 66 add_asan_compile_command(asan_globals_test.cc) 67 add_asan_compile_command(asan_test.cc) 68 69 add_asan_test(AsanInstrumentationTests 70 asan_globals_test.cc.asan.o 71 asan_test.cc.asan.o 72 asan_break_optimization.cc 73 ) 74 75endif() 76