diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -737,6 +737,12 @@ set(COMPILER_RT_HAS_TSAN FALSE) endif() +if (OS_NAME MATCHES "Linux|FreeBSD|Windows|NetBSD|SunOS") + set(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME TRUE) +else() + set(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME FALSE) +endif() + if (COMPILER_RT_HAS_SANITIZER_COMMON AND UBSAN_SUPPORTED_ARCH AND OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Windows|Android|Fuchsia|SunOS") set(COMPILER_RT_HAS_UBSAN TRUE) diff --git a/compiler-rt/lib/tsan/CMakeLists.txt b/compiler-rt/lib/tsan/CMakeLists.txt --- a/compiler-rt/lib/tsan/CMakeLists.txt +++ b/compiler-rt/lib/tsan/CMakeLists.txt @@ -24,6 +24,12 @@ append_list_if(COMPILER_RT_HAS_WGLOBAL_CONSTRUCTORS_FLAG -Wglobal-constructors TSAN_RTL_CFLAGS) +set(TSAN_RTL_DYNAMIC_CFLAGS ${TSAN_RTL_CFLAGS}) +list(REMOVE_ITEM TSAN_RTL_DYNAMIC_CFLAGS -fPIE) + +append_list_if(COMPILER_RT_HAS_LIBDL dl TSAN_DYNAMIC_LINK_LIBS) +append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread TSAN_DYNAMIC_LINK_LIBS) + set(TSAN_SOURCES rtl/tsan_clock.cpp rtl/tsan_debugging.cpp @@ -40,7 +46,6 @@ rtl/tsan_md5.cpp rtl/tsan_mman.cpp rtl/tsan_mutexset.cpp - rtl/tsan_preinit.cpp rtl/tsan_report.cpp rtl/tsan_rtl.cpp rtl/tsan_rtl_mutex.cpp @@ -58,6 +63,10 @@ rtl/tsan_new_delete.cpp ) +set(TSAN_PREINIT_SOURCES + rtl/tsan_preinit.cpp + ) + if(APPLE) list(APPEND TSAN_SOURCES rtl/tsan_interceptors_mac.cpp @@ -238,7 +247,7 @@ add_compiler_rt_runtime(clang_rt.tsan STATIC ARCHS ${arch} - SOURCES ${TSAN_SOURCES} ${TSAN_ASM_SOURCES} + SOURCES ${TSAN_SOURCES} ${TSAN_ASM_SOURCES} ${TSAN_PREINIT_SOURCE} $ $ $ @@ -258,6 +267,20 @@ PARENT_TARGET tsan) list(APPEND TSAN_RUNTIME_LIBRARIES clang_rt.tsan-${arch} clang_rt.tsan_cxx-${arch}) + add_compiler_rt_runtime(clang_rt.tsan + SHARED + ARCHS ${arch} + SOURCES ${TSAN_SOURCES} ${TSAN_ASM_SOURCES} + $ + $ + $ + $ + $ + $ + ADDITIONAL_HEADERS ${TSAN_HEADERS} + CFLAGS ${TSAN_RTL_DYNAMIC_CFLAGS} + LINK_LIBS ${TSAN_DYNAMIC_LINK_LIBS} + PARENT_TARGET tsan) add_sanitizer_rt_symbols(clang_rt.tsan ARCHS ${arch} EXTRA rtl/tsan.syms.extra) diff --git a/compiler-rt/lib/tsan/tests/CMakeLists.txt b/compiler-rt/lib/tsan/tests/CMakeLists.txt --- a/compiler-rt/lib/tsan/tests/CMakeLists.txt +++ b/compiler-rt/lib/tsan/tests/CMakeLists.txt @@ -4,6 +4,11 @@ set_target_properties(TsanUnitTests PROPERTIES FOLDER "Compiler-RT Tests") +# ThreadSanitizer unit tests with dynamic runtime (on platforms where it's +# not the default). +add_custom_target(TsanDynamicUnitTests) +set_target_properties(TsanDynamicUnitTests PROPERTIES FOLDER "Compiler-RT Tests") + set(TSAN_UNITTEST_CFLAGS ${COMPILER_RT_UNITTEST_CFLAGS} ${COMPILER_RT_GTEST_CFLAGS} @@ -57,6 +62,38 @@ list(APPEND LINK_FLAGS ${COMPILER_RT_TEST_LIBDISPATCH_CFLAGS}) endif() +function(add_tsan_tests arch test_runtime) + cmake_parse_arguments(TEST "" "KIND" "CFLAGS" ${ARGN}) + + # Closure to keep the values. + function(generate_tsan_tests test_objects test_suite testname) + generate_compiler_rt_tests(${test_objects} ${test_suite} ${testname} ${arch} + COMPILE_DEPS ${TSAN_UNITTEST_HEADERS} ${TSAN_IGNORELIST_FILE} + DEPS gtest tsan + KIND ${TEST_KIND} + ${ARGN} + ) + set("${test_objects}" "${${test_objects}}" PARENT_SCOPE) + endfunction() + set(TSAN_INST_TEST_OBJECTS) + generate_tsan_tests(TSAN_INST_TEST_OBJECTS TsanUnitTests + "Tsan-${arch}${TEST_KIND}-Test" + SUBDIR "default" + LINK_FLAGS ${TSAN_UNITTEST_INSTRUMENTED_LINK_FLAGS} + SOURCES ${TSAN_INST_TEST_SOURCES} + CFLAGS ${TSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${TEST_CFLAGS}) + + if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) + set(dynamic_test_name "Tsan-${arch}${TEST_KIND}-Dynamic-Test") + add_compiler_rt_test(TsanDynamicUnitTests "${dynamic_test_name}" "${arch}" + SUBDIR "dynamic" + OBJECTS ${TSAN_INST_TEST_OBJECTS} + DEPS tsan ${TSAN_INST_TEST_OBJECTS} + LINK_FLAGS ${TSAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS} + ) + endif() +endfunction() + set(TSAN_RTL_HEADERS) foreach (header ${TSAN_HEADERS}) list(APPEND TSAN_RTL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header}) @@ -91,4 +128,38 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID) add_subdirectory(rtl) add_subdirectory(unit) + foreach(arch ${TSAN_TEST_ARCH}) + + # Add static TSan runtime that will be linked with uninstrumented tests. + set(TSAN_TEST_RUNTIME RTTsanTest.${arch}) + if(APPLE) + set(TSAN_TEST_RUNTIME_OBJECTS + $ + $ + $ + $ + $ + $ + $ + $) + else() + set(TSAN_TEST_RUNTIME_OBJECTS + $ + $ + $ + $ + $ + $ + $ + $) + endif() + add_library(${TSAN_TEST_RUNTIME} STATIC ${TSAN_TEST_RUNTIME_OBJECTS}) + set_target_properties(${TSAN_TEST_RUNTIME} PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + FOLDER "Compiler-RT Runtime tests") + + add_tsan_tests(${arch} ${TSAN_TEST_RUNTIME} KIND "-inline") + add_tsan_tests(${arch} ${TSAN_TEST_RUNTIME} KIND "-calls" + CFLAGS -mllvm -tsan-instrumentation-with-call-threshold=0) + endforeach() endif() diff --git a/compiler-rt/test/tsan/CMakeLists.txt b/compiler-rt/test/tsan/CMakeLists.txt --- a/compiler-rt/test/tsan/CMakeLists.txt +++ b/compiler-rt/test/tsan/CMakeLists.txt @@ -17,7 +17,9 @@ set(TSAN_HAS_LIBCXX False) endif() +list(APPEND TSAN_DYNAMIC_TEST_DEPS TsanDynamicUnitTests) set(TSAN_TESTSUITES) +set(TSAN_DYNAMIC_TESTSUITES) if (NOT DEFINED TSAN_TEST_DEFLAKE_THRESHOLD) set(TSAN_TEST_DEFLAKE_THRESHOLD "10") @@ -39,6 +41,12 @@ string(REPLACE ";" " " LIBDISPATCH_CFLAGS_STRING " ${COMPILER_RT_TEST_LIBDISPATCH_CFLAGS}") string(APPEND TSAN_TEST_TARGET_CFLAGS ${LIBDISPATCH_CFLAGS_STRING}) + if(ANDROID OR APPLE) + set(TSAN_TEST_DYNAMIC True) + else() + set(TSAN_TEST_DYNAMIC False) + endif() + string(TOUPPER ${arch} ARCH_UPPER_CASE) set(CONFIG_NAME ${ARCH_UPPER_CASE}Config) @@ -49,6 +57,20 @@ ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py ) list(APPEND TSAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}) + + if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) + string(TOLOWER "-${arch}-${OS_NAME}-dynamic" TSAN_TEST_CONFIG_SUFFIX) + set(TSAN_TEST_DYNAMIC True) + set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}DynamicConfig) + configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in + ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py + MAIN_CONFIG + ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py + ) + list(APPEND TSAN_DYNAMIC_TESTSUITES + ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}) + endif() endforeach() # iOS and iOS simulator test suites @@ -98,11 +120,30 @@ configure_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py) + if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) + set(TSAN_TEST_DYNAMIC True) + configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in + ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic/lit.site.cfg.py) + endif() list(APPEND TSAN_TEST_DEPS TsanUnitTests) list(APPEND TSAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit) + if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) + list(APPEND TSAN_DYNAMIC_TEST_DEPS TsanDynamicUnitTests) + set(TSAN_DYNAMIC_TEST_DEPS ${TSAN_TEST_DEPS}) + list(APPEND TSAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic) + endif() endif() add_lit_testsuite(check-tsan "Running ThreadSanitizer tests" ${TSAN_TESTSUITES} DEPENDS ${TSAN_TEST_DEPS}) set_target_properties(check-tsan PROPERTIES FOLDER "Compiler-RT Tests") + +if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) + add_lit_testsuite(check-tsan-dynamic "Running the ThreadSanitizer tests with dynamic runtime" + ${TSAN_DYNAMIC_TESTSUITES} + EXCLUDE_FROM_CHECK_ALL + DEPENDS ${TSAN_DYNAMIC_TEST_DEPS}) + set_target_properties(check-tsan-dynamic PROPERTIES FOLDER "Compiler-RT Misc") +endif()