1 # Build for the ThreadSanitizer runtime support library. 2 3 include_directories(..) 4 5 set(TSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS}) 6 # SANITIZER_COMMON_CFLAGS contains -fPIC, but it's performance-critical for 7 # TSan runtime to be built with -fPIE to reduce the number of register spills. 8 append_if(COMPILER_RT_HAS_FPIE_FLAG -fPIE TSAN_CFLAGS) 9 append_no_rtti_flag(TSAN_CFLAGS) 10 11 set(TSAN_RTL_CFLAGS ${TSAN_CFLAGS}) 12 append_if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG -Wframe-larger-than=512 TSAN_RTL_CFLAGS) 13 append_if(COMPILER_RT_HAS_WGLOBAL_CONSTRUCTORS_FLAG -Wglobal-constructors TSAN_RTL_CFLAGS) 14 # FIXME: Add support for --sysroot=. compile flag: 15 16 if("${CMAKE_BUILD_TYPE}" EQUAL "Release") 17 set(TSAN_COMMON_DEFINITIONS DEBUG=0) 18 else() 19 set(TSAN_COMMON_DEFINITIONS DEBUG=1) 20 endif() 21 22 set(TSAN_SOURCES 23 rtl/tsan_clock.cc 24 rtl/tsan_flags.cc 25 rtl/tsan_fd.cc 26 rtl/tsan_ignoreset.cc 27 rtl/tsan_interceptors.cc 28 rtl/tsan_interface_ann.cc 29 rtl/tsan_interface_atomic.cc 30 rtl/tsan_interface.cc 31 rtl/tsan_interface_java.cc 32 rtl/tsan_md5.cc 33 rtl/tsan_mman.cc 34 rtl/tsan_mutex.cc 35 rtl/tsan_mutexset.cc 36 rtl/tsan_report.cc 37 rtl/tsan_rtl.cc 38 rtl/tsan_rtl_mutex.cc 39 rtl/tsan_rtl_report.cc 40 rtl/tsan_rtl_thread.cc 41 rtl/tsan_stack_trace.cc 42 rtl/tsan_stat.cc 43 rtl/tsan_suppressions.cc 44 rtl/tsan_symbolize.cc 45 rtl/tsan_sync.cc) 46 47 if(APPLE) 48 list(APPEND TSAN_SOURCES rtl/tsan_platform_mac.cc) 49 elseif(UNIX) 50 # Assume Linux 51 list(APPEND TSAN_SOURCES 52 rtl/tsan_platform_linux.cc) 53 endif() 54 55 set(TSAN_HEADERS 56 rtl/tsan_clock.h 57 rtl/tsan_defs.h 58 rtl/tsan_dense_alloc.h 59 rtl/tsan_fd.h 60 rtl/tsan_flags.h 61 rtl/tsan_ignoreset.h 62 rtl/tsan_interface_ann.h 63 rtl/tsan_interface.h 64 rtl/tsan_interface_inl.h 65 rtl/tsan_interface_java.h 66 rtl/tsan_mman.h 67 rtl/tsan_mutex.h 68 rtl/tsan_mutexset.h 69 rtl/tsan_platform.h 70 rtl/tsan_report.h 71 rtl/tsan_rtl.h 72 rtl/tsan_stack_trace.h 73 rtl/tsan_stat.h 74 rtl/tsan_suppressions.h 75 rtl/tsan_symbolize.h 76 rtl/tsan_sync.h 77 rtl/tsan_trace.h 78 rtl/tsan_update_shadow_word_inl.h 79 rtl/tsan_vector.h) 80 81 set(TSAN_RUNTIME_LIBRARIES) 82 add_custom_target(tsan) 83 # TSan is currently supported on 64-bit Linux only. 84 if(CAN_TARGET_x86_64 AND UNIX AND NOT APPLE) 85 set(TSAN_ASM_SOURCES rtl/tsan_rtl_amd64.S) 86 # Pass ASM file directly to the C++ compiler. 87 set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES 88 LANGUAGE C) 89 set(arch "x86_64") 90 add_compiler_rt_runtime(clang_rt.tsan-${arch} ${arch} STATIC 91 SOURCES ${TSAN_SOURCES} ${TSAN_ASM_SOURCES} 92 $<TARGET_OBJECTS:RTInterception.${arch}> 93 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}> 94 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}> 95 CFLAGS ${TSAN_RTL_CFLAGS} 96 DEFS ${TSAN_COMMON_DEFINITIONS}) 97 list(APPEND TSAN_RUNTIME_LIBRARIES clang_rt.tsan-${arch}) 98 add_sanitizer_rt_symbols(clang_rt.tsan-${arch} rtl/tsan.syms.extra) 99 add_dependencies(tsan clang_rt.tsan-${arch} 100 clang_rt.tsan-${arch}-symbols) 101 endif() 102 103 add_dependencies(compiler-rt tsan) 104 105 # Build libcxx instrumented with TSan. 106 if(COMPILER_RT_HAS_LIBCXX_SOURCES AND 107 COMPILER_RT_TEST_COMPILER_ID STREQUAL "Clang") 108 set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libcxx_tsan) 109 add_custom_libcxx(libcxx_tsan ${LIBCXX_PREFIX} 110 DEPS ${TSAN_RUNTIME_LIBRARIES} 111 CFLAGS -fsanitize=thread) 112 endif() 113 114 if(COMPILER_RT_INCLUDE_TESTS) 115 add_subdirectory(tests) 116 endif() 117