1 include(CompilerRTCompile) 2 3 set(SANITIZER_UNITTESTS 4 sanitizer_allocator_test.cc 5 sanitizer_atomic_test.cc 6 sanitizer_common_test.cc 7 sanitizer_flags_test.cc 8 sanitizer_ioctl_test.cc 9 sanitizer_libc_test.cc 10 sanitizer_linux_test.cc 11 sanitizer_list_test.cc 12 sanitizer_mutex_test.cc 13 sanitizer_nolibc_test.cc 14 sanitizer_printf_test.cc 15 sanitizer_scanf_interceptor_test.cc 16 sanitizer_stackdepot_test.cc 17 sanitizer_stacktrace_test.cc 18 sanitizer_stoptheworld_test.cc 19 sanitizer_suppressions_test.cc 20 sanitizer_test_main.cc 21 sanitizer_thread_registry_test.cc 22 ) 23 24 set(SANITIZER_TEST_HEADERS) 25 foreach(header ${SANITIZER_HEADERS}) 26 list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header}) 27 endforeach() 28 29 set(SANITIZER_TEST_CFLAGS_COMMON 30 ${COMPILER_RT_GTEST_INCLUDE_CFLAGS} 31 -I${COMPILER_RT_SOURCE_DIR}/include 32 -I${COMPILER_RT_SOURCE_DIR}/lib 33 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common 34 -DGTEST_HAS_RTTI=0 35 -O2 -g -fno-rtti 36 -Wall -Werror -Werror=sign-compare) 37 38 set(SANITIZER_TEST_LINK_FLAGS_COMMON 39 -lstdc++ -ldl) 40 41 include_directories(..) 42 include_directories(../..) 43 44 # Adds static library which contains sanitizer_common object file 45 # (universal binary on Mac and arch-specific object files on Linux). 46 macro(add_sanitizer_common_lib library) 47 add_library(${library} STATIC ${ARGN}) 48 set_target_properties(${library} PROPERTIES 49 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 50 endmacro() 51 52 function(get_sanitizer_common_lib_for_arch arch lib lib_name) 53 if(APPLE) 54 set(tgt_name "RTSanitizerCommon.test.osx") 55 else() 56 set(tgt_name "RTSanitizerCommon.test.${arch}") 57 endif() 58 set(${lib} "${tgt_name}" PARENT_SCOPE) 59 set(${lib_name} "lib${tgt_name}.a" PARENT_SCOPE) 60 endfunction() 61 62 # Sanitizer_common unit tests testsuite. 63 add_custom_target(SanitizerUnitTests) 64 set_target_properties(SanitizerUnitTests PROPERTIES 65 FOLDER "Sanitizer unittests") 66 67 # Adds sanitizer tests for architecture. 68 macro(add_sanitizer_tests_for_arch arch) 69 get_target_flags_for_arch(${arch} TARGET_FLAGS) 70 set(SANITIZER_TEST_SOURCES ${SANITIZER_UNITTESTS} 71 ${COMPILER_RT_GTEST_SOURCE}) 72 set(SANITIZER_TEST_OBJECTS) 73 foreach(source ${SANITIZER_TEST_SOURCES}) 74 get_filename_component(basename ${source} NAME) 75 set(output_obj "${basename}.${arch}.o") 76 clang_compile(${output_obj} ${source} 77 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS} 78 DEPS gtest ${SANITIZER_RUNTIME_LIBRARIES} 79 ${SANITIZER_TEST_HEADERS}) 80 list(APPEND SANITIZER_TEST_OBJECTS ${output_obj}) 81 endforeach() 82 get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB 83 SANITIZER_COMMON_LIB_NAME) 84 # Add unittest target. 85 set(SANITIZER_TEST_NAME "Sanitizer-${arch}-Test") 86 add_compiler_rt_test(SanitizerUnitTests ${SANITIZER_TEST_NAME} 87 OBJECTS ${SANITIZER_TEST_OBJECTS} 88 ${SANITIZER_COMMON_LIB_NAME} 89 DEPS ${SANITIZER_TEST_OBJECTS} ${SANITIZER_COMMON_LIB} 90 LINK_FLAGS ${SANITIZER_TEST_LINK_FLAGS_COMMON} 91 -lpthread ${TARGET_FLAGS}) 92 93 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND "${arch}" STREQUAL "x86_64") 94 # Test that the libc-independent part of sanitizer_common is indeed 95 # independent of libc, by linking this binary without libc (here) and 96 # executing it (unit test in sanitizer_nolibc_test.cc). 97 clang_compile(sanitizer_nolibc_test_main.${arch}.o 98 sanitizer_nolibc_test_main.cc 99 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS} 100 DEPS ${SANITIZER_RUNTIME_LIBRARIES} ${SANITIZER_TEST_HEADERS}) 101 add_compiler_rt_test(SanitizerUnitTests "Sanitizer-${arch}-Test-Nolibc" 102 OBJECTS sanitizer_nolibc_test_main.${arch}.o 103 -Wl,-whole-archive 104 libRTSanitizerCommon.test.nolibc.${arch}.a 105 -Wl,-no-whole-archive 106 DEPS sanitizer_nolibc_test_main.${arch}.o 107 RTSanitizerCommon.test.nolibc.${arch} 108 LINK_FLAGS -nostdlib ${TARGET_FLAGS}) 109 endif() 110 endmacro() 111 112 if(COMPILER_RT_CAN_EXECUTE_TESTS) 113 # We use just-built clang to build sanitizer_common unittests, so we must 114 # be sure that produced binaries would work. 115 if(APPLE) 116 add_sanitizer_common_lib("RTSanitizerCommon.test.osx" 117 $<TARGET_OBJECTS:RTSanitizerCommon.osx>) 118 else() 119 if(CAN_TARGET_x86_64) 120 add_sanitizer_common_lib("RTSanitizerCommon.test.x86_64" 121 $<TARGET_OBJECTS:RTSanitizerCommon.x86_64> 122 $<TARGET_OBJECTS:RTSanitizerCommonLibc.x86_64>) 123 add_sanitizer_common_lib("RTSanitizerCommon.test.nolibc.x86_64" 124 $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>) 125 endif() 126 if(CAN_TARGET_i386) 127 add_sanitizer_common_lib("RTSanitizerCommon.test.i386" 128 $<TARGET_OBJECTS:RTSanitizerCommon.i386> 129 $<TARGET_OBJECTS:RTSanitizerCommonLibc.i386>) 130 endif() 131 endif() 132 if(CAN_TARGET_x86_64) 133 add_sanitizer_tests_for_arch(x86_64) 134 endif() 135 if(CAN_TARGET_i386) 136 add_sanitizer_tests_for_arch(i386) 137 endif() 138 139 # Run unittests as a part of lit testsuite. 140 configure_lit_site_cfg( 141 ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in 142 ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg 143 ) 144 145 add_lit_testsuite(check-sanitizer "Running sanitizer library unittests" 146 ${CMAKE_CURRENT_BINARY_DIR} 147 DEPENDS SanitizerUnitTests 148 ) 149 set_target_properties(check-sanitizer PROPERTIES FOLDER "Sanitizer unittests") 150 endif() 151 152 if(ANDROID) 153 # We assume that unit tests on Android are built in a build 154 # tree with fresh Clang as a host compiler. 155 add_executable(SanitizerTest 156 ${SANITIZER_UNITTESTS} 157 ${COMPILER_RT_GTEST_SOURCE} 158 $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>) 159 set_target_compile_flags(SanitizerTest 160 ${SANITIZER_COMMON_CFLAGS} 161 ${SANITIZER_TEST_CFLAGS_COMMON}) 162 # Setup correct output directory and link flags. 163 set_target_properties(SanitizerTest PROPERTIES 164 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 165 set_target_link_flags(SanitizerTest ${SANITIZER_TEST_LINK_FLAGS_COMMON}) 166 # Add unit test to test suite. 167 add_dependencies(SanitizerUnitTests SanitizerTest) 168 endif() 169