1# Build for the ThreadSanitizer runtime support library. 2 3include_directories(..) 4 5set(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. 8append_list_if(COMPILER_RT_HAS_FPIE_FLAG -fPIE TSAN_CFLAGS) 9append_no_rtti_flag(TSAN_CFLAGS) 10 11set(TSAN_RTL_CFLAGS ${TSAN_CFLAGS}) 12append_list_if(COMPILER_RT_HAS_MSSE3_FLAG -msse3 TSAN_RTL_CFLAGS) 13append_list_if(SANITIZER_LIMIT_FRAME_SIZE -Wframe-larger-than=512 14 TSAN_RTL_CFLAGS) 15append_list_if(COMPILER_RT_HAS_WGLOBAL_CONSTRUCTORS_FLAG -Wglobal-constructors 16 TSAN_RTL_CFLAGS) 17# FIXME: Add support for --sysroot=. compile flag: 18 19set(TSAN_SOURCES 20 rtl/tsan_clock.cc 21 rtl/tsan_flags.cc 22 rtl/tsan_fd.cc 23 rtl/tsan_ignoreset.cc 24 rtl/tsan_interceptors.cc 25 rtl/tsan_interface_ann.cc 26 rtl/tsan_interface_atomic.cc 27 rtl/tsan_interface.cc 28 rtl/tsan_interface_java.cc 29 rtl/tsan_md5.cc 30 rtl/tsan_mman.cc 31 rtl/tsan_mutex.cc 32 rtl/tsan_mutexset.cc 33 rtl/tsan_report.cc 34 rtl/tsan_rtl.cc 35 rtl/tsan_rtl_mutex.cc 36 rtl/tsan_rtl_report.cc 37 rtl/tsan_rtl_thread.cc 38 rtl/tsan_stack_trace.cc 39 rtl/tsan_stat.cc 40 rtl/tsan_suppressions.cc 41 rtl/tsan_symbolize.cc 42 rtl/tsan_sync.cc) 43 44if(APPLE) 45 list(APPEND TSAN_SOURCES rtl/tsan_platform_mac.cc) 46elseif(UNIX) 47 # Assume Linux 48 list(APPEND TSAN_SOURCES 49 rtl/tsan_platform_linux.cc) 50endif() 51 52set(TSAN_HEADERS 53 rtl/tsan_clock.h 54 rtl/tsan_defs.h 55 rtl/tsan_dense_alloc.h 56 rtl/tsan_fd.h 57 rtl/tsan_flags.h 58 rtl/tsan_flags.inc 59 rtl/tsan_ignoreset.h 60 rtl/tsan_interface_ann.h 61 rtl/tsan_interface.h 62 rtl/tsan_interface_inl.h 63 rtl/tsan_interface_java.h 64 rtl/tsan_mman.h 65 rtl/tsan_mutex.h 66 rtl/tsan_mutexset.h 67 rtl/tsan_platform.h 68 rtl/tsan_report.h 69 rtl/tsan_rtl.h 70 rtl/tsan_stack_trace.h 71 rtl/tsan_stat.h 72 rtl/tsan_suppressions.h 73 rtl/tsan_symbolize.h 74 rtl/tsan_sync.h 75 rtl/tsan_trace.h 76 rtl/tsan_update_shadow_word_inl.h 77 rtl/tsan_vector.h) 78 79set(TSAN_RUNTIME_LIBRARIES) 80add_custom_target(tsan) 81# TSan is currently supported on 64-bit Linux only. 82if(UNIX AND NOT APPLE) 83 foreach(arch ${TSAN_SUPPORTED_ARCH}) 84 if(arch STREQUAL "x86_64") 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 # Sanity check for Go runtime. 90 set(BUILDGO_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/go/buildgo.sh) 91 add_custom_target(GotsanRuntimeCheck 92 COMMAND env "CC=${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}" 93 IN_TMPDIR=1 SILENT=1 ${BUILDGO_SCRIPT} 94 DEPENDS clang_rt.tsan-${arch} ${BUILDGO_SCRIPT} 95 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/go 96 COMMENT "Checking TSan Go runtime..." 97 VERBATIM) 98 else() 99 set(TSAN_ASM_SOURCES) 100 endif() 101 add_compiler_rt_runtime(clang_rt.tsan-${arch} ${arch} STATIC 102 SOURCES ${TSAN_SOURCES} ${TSAN_ASM_SOURCES} 103 $<TARGET_OBJECTS:RTInterception.${arch}> 104 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}> 105 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}> 106 CFLAGS ${TSAN_RTL_CFLAGS} 107 DEFS ${TSAN_COMMON_DEFINITIONS}) 108 list(APPEND TSAN_RUNTIME_LIBRARIES clang_rt.tsan-${arch}) 109 add_sanitizer_rt_symbols(clang_rt.tsan-${arch} rtl/tsan.syms.extra) 110 add_dependencies(tsan clang_rt.tsan-${arch} 111 clang_rt.tsan-${arch}-symbols) 112 endforeach() 113endif() 114 115add_dependencies(compiler-rt tsan) 116 117 118# Build libcxx instrumented with TSan. 119if(TSAN_SUPPORTED_ARCH AND 120 COMPILER_RT_HAS_LIBCXX_SOURCES AND 121 COMPILER_RT_TEST_COMPILER_ID STREQUAL "Clang") 122 set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libcxx_tsan) 123 add_custom_libcxx(libcxx_tsan ${LIBCXX_PREFIX} 124 DEPS ${TSAN_RUNTIME_LIBRARIES} 125 CFLAGS -fsanitize=thread) 126endif() 127 128if(COMPILER_RT_INCLUDE_TESTS) 129 add_subdirectory(tests) 130endif() 131