[llvm] 55978f9 - [gn build] add build file for tsan runtime

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 2 09:59:41 PDT 2021


Author: Nico Weber
Date: 2021-04-02T12:59:14-04:00
New Revision: 55978f914e88dc12bcc62666128c175c86adfd22

URL: https://github.com/llvm/llvm-project/commit/55978f914e88dc12bcc62666128c175c86adfd22
DIFF: https://github.com/llvm/llvm-project/commit/55978f914e88dc12bcc62666128c175c86adfd22.diff

LOG: [gn build] add build file for tsan runtime

Linux-only for now. Some mac bits stubbed out, but not tested.

Good enough for the tiny_race.c example at
https://clang.llvm.org/docs/ThreadSanitizer.html :

   $ out/gn/bin/clang -fsanitize=address -g -O1 tiny_race.c
   $ while true; do ./a.out || echo $? ; done

While here, also make `-fsanitize=address` work for .c files.

Differential Revision: https://reviews.llvm.org/D99795

Added: 
    llvm/utils/gn/secondary/compiler-rt/lib/tsan/BUILD.gn

Modified: 
    compiler-rt/lib/tsan/CMakeLists.txt
    llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn
    llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/tsan/CMakeLists.txt b/compiler-rt/lib/tsan/CMakeLists.txt
index 54b156f29d0fe..b77137633171f 100644
--- a/compiler-rt/lib/tsan/CMakeLists.txt
+++ b/compiler-rt/lib/tsan/CMakeLists.txt
@@ -108,7 +108,8 @@ set(TSAN_HEADERS
   rtl/tsan_symbolize.h
   rtl/tsan_sync.h
   rtl/tsan_trace.h
-  rtl/tsan_update_shadow_word_inl.h)
+  rtl/tsan_update_shadow_word_inl.h
+  )
 
 set(TSAN_RUNTIME_LIBRARIES)
 add_compiler_rt_component(tsan)
@@ -126,7 +127,10 @@ if(APPLE)
     message(FATAL_ERROR "Building the TSan runtime requires at least macOS SDK 10.12 (or aligned SDK on other platforms)")
   endif()
 
-  add_asm_sources(TSAN_ASM_SOURCES rtl/tsan_rtl_amd64.S rtl/tsan_rtl_aarch64.S)
+  add_asm_sources(TSAN_ASM_SOURCES
+    rtl/tsan_rtl_amd64.S
+    rtl/tsan_rtl_aarch64.S
+    )
 
   set(TSAN_LINK_LIBS ${SANITIZER_COMMON_LINK_LIBS})
 
@@ -170,7 +174,9 @@ if(APPLE)
 else()
   foreach(arch ${TSAN_SUPPORTED_ARCH})
     if(arch STREQUAL "x86_64")
-      add_asm_sources(TSAN_ASM_SOURCES rtl/tsan_rtl_amd64.S)
+      add_asm_sources(TSAN_ASM_SOURCES
+        rtl/tsan_rtl_amd64.S
+        )
       # Sanity check for Go runtime.
       set(BUILDGO_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/go/buildgo.sh)
       add_custom_target(GotsanRuntimeCheck
@@ -182,7 +188,9 @@ else()
         COMMENT "Checking TSan Go runtime..."
         VERBATIM)
     elseif(arch STREQUAL "aarch64")
-      add_asm_sources(TSAN_ASM_SOURCES rtl/tsan_rtl_aarch64.S)
+      add_asm_sources(TSAN_ASM_SOURCES
+        rtl/tsan_rtl_aarch64.S
+        )
       # Sanity check for Go runtime.
       set(BUILDGO_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/go/buildgo.sh)
       add_custom_target(GotsanRuntimeCheck
@@ -194,7 +202,9 @@ else()
 	COMMENT "Checking TSan Go runtime..."
 	VERBATIM)
     elseif(arch MATCHES "powerpc64|powerpc64le")
-      add_asm_sources(TSAN_ASM_SOURCES rtl/tsan_rtl_ppc64.S)
+      add_asm_sources(TSAN_ASM_SOURCES
+        rtl/tsan_rtl_ppc64.S
+        )
       # Sanity check for Go runtime.
       set(BUILDGO_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/go/buildgo.sh)
       add_custom_target(GotsanRuntimeCheck
@@ -206,7 +216,9 @@ else()
 	COMMENT "Checking TSan Go runtime..."
 	VERBATIM)
     elseif(arch MATCHES "mips64|mips64le")
-      add_asm_sources(TSAN_ASM_SOURCES rtl/tsan_rtl_mips64.S)
+      add_asm_sources(TSAN_ASM_SOURCES
+        rtl/tsan_rtl_mips64.S
+        )
     else()
       set(TSAN_ASM_SOURCES)
     endif()

diff  --git a/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn
index 7133d9db18188..a99f8ad1470b0 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn
@@ -4,4 +4,8 @@ group("lib") {
     "//compiler-rt/lib/builtins",
     "//compiler-rt/lib/profile",
   ]
+  if (target_os == "linux") {
+    # FIXME: Make work on macOS.
+    deps += [ "//compiler-rt/lib/tsan" ]
+  }
 }

diff  --git a/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
index 1f754d07db374..3593d1fcba53c 100644
--- a/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/asan/BUILD.gn
@@ -28,7 +28,6 @@ target(asan_target_type, "asan") {
     "//compiler-rt/lib/interception:sources",
     "//compiler-rt/lib/lsan:common_sources",
     "//compiler-rt/lib/sanitizer_common:sources",
-    "//compiler-rt/lib/ubsan:cxx_sources",
     "//compiler-rt/lib/ubsan:sources",
   ]
 
@@ -37,7 +36,10 @@ target(asan_target_type, "asan") {
     configs -= [ "//llvm/utils/gn/build:thin_archive" ]
     deps += [ ":asan_cxx" ]
   } else {
-    deps += [ ":cxx_sources" ]
+    deps += [
+      ":cxx_sources",
+      "//compiler-rt/lib/ubsan:cxx_sources",
+    ]
     defines = [ "ASAN_DYNAMIC" ]
   }
 
@@ -183,7 +185,10 @@ if (asan_target_type == "static_library") {
     output_name = "clang_rt.asan_cxx$crt_current_target_suffix"
     complete_static_lib = true
     configs -= [ "//llvm/utils/gn/build:thin_archive" ]
-    deps = [ ":cxx_sources" ]
+    deps = [
+      ":cxx_sources",
+      "//compiler-rt/lib/ubsan:cxx_sources",
+    ]
   }
 }
 

diff  --git a/llvm/utils/gn/secondary/compiler-rt/lib/tsan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/tsan/BUILD.gn
new file mode 100644
index 0000000000000..675a0a91d8791
--- /dev/null
+++ b/llvm/utils/gn/secondary/compiler-rt/lib/tsan/BUILD.gn
@@ -0,0 +1,187 @@
+import("//compiler-rt/target.gni")
+
+source_set("cxx_sources") {
+  configs -= [ "//llvm/utils/gn/build:llvm_code" ]
+  configs += [ "//llvm/utils/gn/build:crt_code" ]
+  sources = [ "rtl/tsan_new_delete.cpp" ]
+}
+
+if (current_os == "mac") {
+  tsan_target_type = "shared_library"
+} else {
+  tsan_target_type = "static_library"
+}
+
+target(tsan_target_type, "tsan") {
+  configs -= [ "//llvm/utils/gn/build:llvm_code" ]
+  configs += [ "//llvm/utils/gn/build:crt_code" ]
+
+  output_dir = crt_current_out_dir
+  if (current_os == "mac") {
+    output_name = "clang_rt.tsan_osx_dynamic"
+  } else {
+    assert(current_os != "win", "Tsan does not work on Windows")
+    output_name = "clang_rt.tsan$crt_current_target_suffix"
+  }
+
+  deps = [
+    "//compiler-rt/lib/interception:sources",
+    "//compiler-rt/lib/sanitizer_common:sources",
+    "//compiler-rt/lib/ubsan:sources",
+  ]
+
+  if (tsan_target_type == "static_library") {
+    complete_static_lib = true
+    configs -= [ "//llvm/utils/gn/build:thin_archive" ]
+    deps += [ ":tsan_cxx" ]
+  } else {
+    deps += [
+      ":cxx_sources",
+      "//compiler-rt/lib/ubsan:cxx_sources",
+    ]
+  }
+
+  # It's performance-critical for TSan runtime to be built with -fPIE to reduce
+  # the number of register spills.
+  cflags = [ "-fPIE" ]
+
+  sources = [
+    "rtl/tsan_clock.cpp",
+    "rtl/tsan_clock.h",
+    "rtl/tsan_debugging.cpp",
+    "rtl/tsan_defs.h",
+    "rtl/tsan_dense_alloc.h",
+    "rtl/tsan_external.cpp",
+    "rtl/tsan_fd.cpp",
+    "rtl/tsan_fd.h",
+    "rtl/tsan_flags.cpp",
+    "rtl/tsan_flags.h",
+    "rtl/tsan_flags.inc",
+    "rtl/tsan_ignoreset.cpp",
+    "rtl/tsan_ignoreset.h",
+    "rtl/tsan_interceptors.h",
+    "rtl/tsan_interceptors_posix.cpp",
+    "rtl/tsan_interface.cpp",
+    "rtl/tsan_interface.h",
+    "rtl/tsan_interface_ann.cpp",
+    "rtl/tsan_interface_ann.h",
+    "rtl/tsan_interface_atomic.cpp",
+    "rtl/tsan_interface_inl.h",
+    "rtl/tsan_interface_java.cpp",
+    "rtl/tsan_interface_java.h",
+    "rtl/tsan_malloc_mac.cpp",
+    "rtl/tsan_md5.cpp",
+    "rtl/tsan_mman.cpp",
+    "rtl/tsan_mman.h",
+    "rtl/tsan_mutex.cpp",
+    "rtl/tsan_mutex.h",
+    "rtl/tsan_mutexset.cpp",
+    "rtl/tsan_mutexset.h",
+    "rtl/tsan_platform.h",
+    "rtl/tsan_ppc_regs.h",
+    "rtl/tsan_preinit.cpp",
+    "rtl/tsan_report.cpp",
+    "rtl/tsan_report.h",
+    "rtl/tsan_rtl.cpp",
+    "rtl/tsan_rtl.h",
+    "rtl/tsan_rtl_mutex.cpp",
+    "rtl/tsan_rtl_proc.cpp",
+    "rtl/tsan_rtl_report.cpp",
+    "rtl/tsan_rtl_thread.cpp",
+    "rtl/tsan_stack_trace.cpp",
+    "rtl/tsan_stack_trace.h",
+    "rtl/tsan_stat.cpp",
+    "rtl/tsan_stat.h",
+    "rtl/tsan_suppressions.cpp",
+    "rtl/tsan_suppressions.h",
+    "rtl/tsan_symbolize.cpp",
+    "rtl/tsan_symbolize.h",
+    "rtl/tsan_sync.cpp",
+    "rtl/tsan_sync.h",
+    "rtl/tsan_trace.h",
+    "rtl/tsan_update_shadow_word_inl.h",
+  ]
+  if (target_os == "mac") {
+    sources += [
+      "rtl/tsan_interceptors_libdispatch.cpp",
+      "rtl/tsan_interceptors_mac.cpp",
+      "rtl/tsan_interceptors_mach_vm.cpp",
+      "rtl/tsan_platform_mac.cpp",
+      "rtl/tsan_platform_posix.cpp",
+    ]
+    cflags += [ "-fblocks" ]
+  } else {
+    # Assume Linux
+    sources += [
+      "rtl/tsan_platform_linux.cpp",
+      "rtl/tsan_platform_posix.cpp",
+    ]
+  }
+  if (target_cpu == "x64") {
+    sources += [ "rtl/tsan_rtl_amd64.S" ]
+  } else if (target_cpu == "arm64") {
+    sources += [ "rtl/tsan_rtl_aarch64.S" ]
+  } else if (target_cpu == "powerpc64") {
+    sources += [ "rtl/tsan_rtl_ppc64.S" ]
+  } else if (target_cpu == "mips64") {
+    sources += [ "rtl/tsan_rtl_mips64.S" ]
+  }
+
+  # To be able to include sanitizer_common.
+  include_dirs = [ ".." ]
+
+  # FIXME: have SANITIZER_COMMON_CFLAGS thingy? should fno-builtin be in
+  # crt_code?
+  cflags += [ "-fno-builtin" ]
+
+  # FIXME: link rt dl m pthread log
+  # FIXME: dep on libcxx-headers?
+  # FIXME: add_sanitizer_rt_version_list (cf hwasan)
+  # FIXME: need libclang_rt.tsan*.a.syms?
+  # FIXME: tsan_blacklist.txt
+
+  if (target_os == "mac") {
+    # The -U flags below correspond to the add_weak_symbols() calls in CMake.
+    ldflags = [
+      "-lc++",
+      "-lc++abi",
+
+      # tsan
+      # FIXME
+
+      # ubsan
+      "-Wl,-U,___ubsan_default_options",
+
+      # sanitizer_common
+      "-Wl,-U,___sanitizer_free_hook",
+      "-Wl,-U,___sanitizer_malloc_hook",
+      "-Wl,-U,___sanitizer_report_error_summary",
+      "-Wl,-U,___sanitizer_sandbox_on_notify",
+      "-Wl,-U,___sanitizer_symbolize_code",
+      "-Wl,-U,___sanitizer_symbolize_data",
+      "-Wl,-U,___sanitizer_symbolize_demangle",
+      "-Wl,-U,___sanitizer_symbolize_flush",
+
+      # FIXME: better
+      "-Wl,-install_name, at rpath/libclang_rt.tsan_osx_dynamic.dylib",
+    ]
+    # FIXME: -Wl,-rpath
+    # FIXME: codesign (??)
+  }
+}
+
+if (tsan_target_type == "static_library") {
+  static_library("tsan_cxx") {
+    assert(current_os != "win", "FIXME")
+    output_dir = crt_current_out_dir
+    output_name = "clang_rt.tsan_cxx$crt_current_target_suffix"
+    complete_static_lib = true
+    configs -= [ "//llvm/utils/gn/build:thin_archive" ]
+    deps = [
+      ":cxx_sources",
+      "//compiler-rt/lib/ubsan:cxx_sources",
+    ]
+  }
+}
+# FIXME:
+# Build libcxx instrumented with TSan.


        


More information about the llvm-commits mailing list