Skip to content

Commit d1bac4c

Browse files
committed
[libcxx] Add support for sanitizers on OS X.
Summary: This patch adds special configuration logic to find the compiler_rt libraries required by sanitizers on OS X. The supported sanitizers are Address and Undefined. Reviewers: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D11381 llvm-svn: 242858
1 parent f179e36 commit d1bac4c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

libcxx/lib/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ if (LIBCXX_COVERAGE_LIBRARY)
6868
endif()
6969
target_link_libraries(cxx ${libraries})
7070

71+
if (APPLE AND LLVM_USE_SANITIZER)
72+
if ("${LLVM_USE_SANITIZER}" STREQUAL "Address")
73+
set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
74+
elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
75+
set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
76+
else()
77+
message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
78+
endif()
79+
if (LIBFILE)
80+
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=lib OUTPUT_VARIABLE LIBDIR RESULT_VARIABLE Result)
81+
if (NOT ${Result} EQUAL "0")
82+
message(FATAL "Failed to find library resource directory")
83+
endif()
84+
string(STRIP "${LIBDIR}" LIBDIR)
85+
set(LIBDIR "${LIBDIR}/darwin/")
86+
if (NOT IS_DIRECTORY "${LIBDIR}")
87+
message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
88+
endif()
89+
set(LIBCXX_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
90+
set(LIBCXX_SANITIZER_LIBRARY "${LIBCXX_SANITIZER_LIBRARY}" PARENT_SCOPE)
91+
message(STATUS "Manually linking compiler-rt library: ${LIBCXX_SANITIZER_LIBRARY}")
92+
target_link_libraries(cxx "${LIBCXX_SANITIZER_LIBRARY}")
93+
target_link_libraries(cxx "-Wl,-rpath,${LIBDIR}")
94+
endif()
95+
endif()
96+
7197

7298
# Setup flags.
7399
append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_FPIC_FLAG -fPIC)

libcxx/test/libcxx/test/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,19 @@ def configure_sanitizer(self):
623623
'-fno-sanitize-recover']
624624
self.cxx.compile_flags += ['-O3']
625625
self.config.available_features.add('ubsan')
626+
if self.target_info.platform() == 'darwin':
627+
self.config.available_features.add('sanitizer-new-delete')
626628
elif san == 'Thread':
627629
self.cxx.flags += ['-fsanitize=thread']
628630
self.config.available_features.add('tsan')
629631
self.config.available_features.add('sanitizer-new-delete')
630632
else:
631633
self.lit_config.fatal('unsupported value for '
632634
'use_sanitizer: {0}'.format(san))
635+
san_lib = self.get_lit_conf('sanitizer_library')
636+
if san_lib:
637+
self.cxx.link_flags += [
638+
san_lib, '-Wl,-rpath,%s' % os.path.dirname(san_lib)]
633639

634640
def configure_coverage(self):
635641
self.generate_coverage = self.get_lit_bool('generate_coverage', False)

libcxx/test/lit.site.cfg.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ config.enable_thread_unsafe_c_functions = "@LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTI
1616
config.enable_monotonic_clock = "@LIBCXX_ENABLE_MONOTONIC_CLOCK@"
1717
config.cxx_abi = "@LIBCXX_CXX_ABI_LIBNAME@"
1818
config.use_sanitizer = "@LLVM_USE_SANITIZER@"
19+
config.sanitizer_library = "@LIBCXX_SANITIZER_LIBRARY@"
1920
config.abi_library_path = "@LIBCXX_CXX_ABI_LIBRARY_PATH@"
2021
config.configuration_variant = "@LIBCXX_LIT_VARIANT@"
2122
config.target_triple = "@LIBCXX_TARGET_TRIPLE@"

0 commit comments

Comments
 (0)