dberris created this revision.
Herald added a subscriber: mgorny.

This is a work-in-progress change that attempts to allow us to build
and use the XRay runtime in Darwin. Current state:

- Assembler files are broken due to assumptions about ELFisms.
- Test infrastructure is laid out, but we aren't able to test yet.

We also:

- Use a better preprocessor check for preinint array support.
- Only build for osx.
- Enable the use of -fxray-instrument (and other flags) in clang.

This patch uses the monorepo layout for the chnage.


https://reviews.llvm.org/D39114

Files:
  clang/lib/Driver/XRayArgs.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/xray/CMakeLists.txt
  compiler-rt/lib/xray/tests/CMakeLists.txt
  compiler-rt/lib/xray/xray_init.cc
  compiler-rt/test/xray/TestCases/Darwin/always-never-instrument.cc
  compiler-rt/test/xray/TestCases/Darwin/lit.local.cfg
  compiler-rt/test/xray/TestCases/Linux/lit.local.cfg
  compiler-rt/test/xray/lit.cfg

Index: compiler-rt/test/xray/lit.cfg
===================================================================
--- compiler-rt/test/xray/lit.cfg
+++ compiler-rt/test/xray/lit.cfg
@@ -40,7 +40,7 @@
 # Default test suffixes.
 config.suffixes = ['.c', '.cc', '.cpp']
 
-if config.host_os not in ['Linux']:
+if config.host_os not in ['Linux', 'Darwin']:
   config.unsupported = True
 elif '64' not in config.host_arch:
   if 'arm' in config.host_arch:
Index: compiler-rt/test/xray/TestCases/Linux/lit.local.cfg
===================================================================
--- /dev/null
+++ compiler-rt/test/xray/TestCases/Linux/lit.local.cfg
@@ -0,0 +1,9 @@
+def getRoot(config):
+  if not config.parent:
+    return config
+  return getRoot(config.parent)
+
+root = getRoot(config)
+
+if root.host_os not in ['Linux']:
+  config.unsupported = True
Index: compiler-rt/test/xray/TestCases/Darwin/lit.local.cfg
===================================================================
--- /dev/null
+++ compiler-rt/test/xray/TestCases/Darwin/lit.local.cfg
@@ -0,0 +1,9 @@
+def getRoot(config):
+  if not config.parent:
+    return config
+  return getRoot(config.parent)
+
+root = getRoot(config)
+
+if root.host_os not in ['Darwin']:
+  config.unsupported = True
Index: compiler-rt/test/xray/TestCases/Darwin/always-never-instrument.cc
===================================================================
--- /dev/null
+++ compiler-rt/test/xray/TestCases/Darwin/always-never-instrument.cc
@@ -0,0 +1,23 @@
+// Test that the always/never instrument lists apply.
+// RUN: echo "fun:main" > %tmp-always.txt
+// RUN: echo "fun:__xray*" > %tmp-never.txt
+// RUN: %clangxx_xray \
+// RUN:     -fxray-never-instrument=%tmp-never.txt \
+// RUN:     -fxray-always-instrument=%tmp-always.txt \
+// RUN:     %s -o %t
+// RUN: %llvm_xray extract -symbolize %t | \
+// RUN:    FileCheck %s --check-prefix NOINSTR
+// RUN: %llvm_xray extract -symbolize %t | \
+// RUN:    FileCheck %s --check-prefix ALWAYSINSTR
+// REQUIRES: x86_64-linux
+// REQUIRES: built-in-llvm-tree
+
+// NOINSTR-NOT: {{.*__xray_NeverInstrumented.*}}
+int __xray_NeverInstrumented() {
+  return 0;
+}
+
+// ALWAYSINSTR: {{.*function-name:.*main.*}}
+int main(int argc, char *argv[]) {
+  return __xray_NeverInstrumented();
+}
Index: compiler-rt/lib/xray/xray_init.cc
===================================================================
--- compiler-rt/lib/xray/xray_init.cc
+++ compiler-rt/lib/xray/xray_init.cc
@@ -88,7 +88,8 @@
 #endif
 }
 
-#ifndef XRAY_NO_PREINIT
+// Only add the preinit array initialization if the sanitizers can.
+#if !defined(XRAY_NO_PREINIT) && SANITIZER_CAN_USE_PREINIT_ARRAY
 __attribute__((section(".preinit_array"),
                used)) void (*__local_xray_preinit)(void) = __xray_init;
 #endif
Index: compiler-rt/lib/xray/tests/CMakeLists.txt
===================================================================
--- compiler-rt/lib/xray/tests/CMakeLists.txt
+++ compiler-rt/lib/xray/tests/CMakeLists.txt
@@ -12,9 +12,19 @@
   -I${COMPILER_RT_SOURCE_DIR}/lib)
 
 set(XRAY_TEST_ARCH ${XRAY_SUPPORTED_ARCH})
+set(XRAY_LINK_FLAGS)
+append_list_if(COMPILER_RT_HAS_LIBRT -lrt XRAY_LINK_FLAGS)
+append_list_if(COMPILER_RT_HAS_LIBM -lm XRAY_LINK_FLAGS)
+append_list_if(COMPILER_RT_HAS_LIBPTHREAD -lpthread XRAY_LINK_FLAGS)
+if (APPLE)
+  list(APPEND XRAY_LINK_FLAGS -lc++)
+else()
+  append_list_if(COMPILER_RT_HAS_LIBSTDCXX lstdc++ XRAY_LINK_FLAGS)
+endif()
+
 macro(add_xray_unittest testname)
   cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
-  if(UNIX AND NOT APPLE)
+  if(UNIX)
     foreach(arch ${XRAY_TEST_ARCH})
       set(TEST_OBJECTS)
       generate_compiler_rt_tests(TEST_OBJECTS
@@ -24,9 +34,8 @@
         CFLAGS ${XRAY_UNITTEST_CFLAGS}
         LINK_FLAGS -fxray-instrument
           ${TARGET_LINK_FLAGS}
-          -lstdc++ -lm ${CMAKE_THREAD_LIBS_INIT}
-          -lpthread
-          -ldl -lrt)
+          ${CMAKE_THREAD_LIBS_INIT}
+          ${XRAY_LINK_FLAGS})
       set_target_properties(XRayUnitTests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
     endforeach()
   endif()
Index: compiler-rt/lib/xray/CMakeLists.txt
===================================================================
--- compiler-rt/lib/xray/CMakeLists.txt
+++ compiler-rt/lib/xray/CMakeLists.txt
@@ -76,6 +76,18 @@
     RTSanitizerCommon
     RTSanitizerCommonLibc)
 
+if (APPLE)
+  # We only support running on osx for now.
+  add_compiler_rt_runtime(clang_rt.xray
+    STATIC
+    OS osx
+    ARCHS ${XRAY_SUPPORTED_ARCH}
+    OBJECT_LIBS ${XRAY_COMMON_RUNTIME_OBJECT_LIBS}
+    SOURCES ${x86_64_SOURCES}
+    CFLAGS ${XRAY_CFLAGS}
+    DEFS ${XRAY_COMMON_DEFINITIONS}
+    PARENT_TARGET xray)
+else()
 foreach(arch ${XRAY_SUPPORTED_ARCH})
   if(CAN_TARGET_${arch})
     add_compiler_rt_runtime(clang_rt.xray
@@ -88,6 +100,7 @@
      PARENT_TARGET xray)
   endif()
 endforeach()
+endif()
 
 if(COMPILER_RT_INCLUDE_TESTS)
   add_subdirectory(tests)
Index: compiler-rt/cmake/config-ix.cmake
===================================================================
--- compiler-rt/cmake/config-ix.cmake
+++ compiler-rt/cmake/config-ix.cmake
@@ -412,13 +412,17 @@
   list_intersect(SCUDO_SUPPORTED_ARCH
     ALL_SCUDO_SUPPORTED_ARCH
     SANITIZER_COMMON_SUPPORTED_ARCH)
-  list_intersect(XRAY_SUPPORTED_ARCH
-    ALL_XRAY_SUPPORTED_ARCH
-    SANITIZER_COMMON_SUPPORTED_ARCH)
   list_intersect(FUZZER_SUPPORTED_ARCH
     ALL_FUZZER_SUPPORTED_ARCH
     ALL_SANITIZER_COMMON_SUPPORTED_ARCH)
 
+  # For XRay, only support x86_64 in APPLE.
+  # list_intersect(XRAY_SUPPORTED_ARCH
+  #   ALL_XRAY_SUPPORTED_ARCH
+  #   SANITIZER_COMMON_SUPPORTED_ARCH)
+  set(XRAY_SUPPORTED_ARCH ${X86_64})
+
+
 else()
   # Architectures supported by compiler-rt libraries.
   filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
@@ -580,7 +584,7 @@
 endif()
 
 if (COMPILER_RT_HAS_SANITIZER_COMMON AND XRAY_SUPPORTED_ARCH AND
-    OS_NAME MATCHES "Linux")
+    OS_NAME MATCHES "Darwin|Linux")
   set(COMPILER_RT_HAS_XRAY TRUE)
 else()
   set(COMPILER_RT_HAS_XRAY FALSE)
Index: clang/lib/Driver/XRayArgs.cpp
===================================================================
--- clang/lib/Driver/XRayArgs.cpp
+++ clang/lib/Driver/XRayArgs.cpp
@@ -51,6 +51,15 @@
         D.Diag(diag::err_drv_clang_unsupported)
             << (std::string(XRayInstrumentOption) + " on " + Triple.str());
       }
+    else if (Triple.getOS() == llvm::Triple::Darwin)
+      // Experimental support for macos.
+      switch (Triple.getArch()) {
+      case llvm::Triple::x86_64:
+        break;
+      default:
+        D.Diag(diag::err_drv_clang_unsupported)
+            << (std::string(XRayInstrumentOption) + " on " + Triple.str());
+      }
     else
       D.Diag(diag::err_drv_clang_unsupported)
           << (std::string(XRayInstrumentOption) + " on non-Linux target OS");
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to