forked from OSchip/llvm-project
[sanitizer] Support compiler-rt builtins
This change adds support for compiler-rt builtins as an alternative compiler runtime to libgcc. Differential Revision: https://reviews.llvm.org/D35165 llvm-svn: 309060
This commit is contained in:
parent
fc0e245464
commit
b1305ce038
|
@ -78,6 +78,7 @@ option(COMPILER_RT_EXTERNALIZE_DEBUGINFO
|
|||
# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
|
||||
pythonize_bool(COMPILER_RT_DEBUG)
|
||||
|
||||
include(HandleCompilerRT)
|
||||
include(config-ix)
|
||||
|
||||
if(APPLE AND SANITIZER_MIN_OSX_VERSION AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9")
|
||||
|
@ -93,6 +94,8 @@ endif()
|
|||
option(SANITIZER_CAN_USE_CXXABI "Sanitizers can use cxxabi" ${use_cxxabi_default})
|
||||
pythonize_bool(SANITIZER_CAN_USE_CXXABI)
|
||||
|
||||
option(SANITIZER_USE_COMPILER_RT "Use compiler-rt builtins instead of libgcc" OFF)
|
||||
|
||||
#================================
|
||||
# Setup Compiler Flags
|
||||
#================================
|
||||
|
@ -228,6 +231,19 @@ append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS)
|
|||
append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS)
|
||||
append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS)
|
||||
|
||||
# Set common link flags.
|
||||
append_list_if(COMPILER_RT_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS)
|
||||
|
||||
if (SANITIZER_USE_COMPILER_RT)
|
||||
list(APPEND SANITIZER_COMMON_LINK_FLAGS -rtlib=compiler-rt)
|
||||
find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY)
|
||||
list(APPEND SANITIZER_COMMON_LINK_LIBS ${COMPILER_RT_BUILTINS_LIBRARY})
|
||||
else()
|
||||
append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
|
||||
endif()
|
||||
|
||||
append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
|
||||
|
||||
# Warnings to turn off for all libraries, not just sanitizers.
|
||||
append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
function(find_compiler_rt_library name dest)
|
||||
set(dest "" PARENT_SCOPE)
|
||||
set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_CFLAGS}
|
||||
"--rtlib=compiler-rt" "--print-libgcc-file-name")
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND CMAKE_CXX_COMPILER_TARGET)
|
||||
list(APPEND CLANG_COMMAND "--target=${CMAKE_CXX_COMPILER_TARGET}")
|
||||
endif()
|
||||
execute_process(
|
||||
COMMAND ${CLANG_COMMAND}
|
||||
RESULT_VARIABLE HAD_ERROR
|
||||
OUTPUT_VARIABLE LIBRARY_FILE
|
||||
)
|
||||
string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
|
||||
string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
|
||||
if (NOT HAD_ERROR AND EXISTS "${LIBRARY_FILE}")
|
||||
message(STATUS "Found compiler-rt ${name} library: ${LIBRARY_FILE}")
|
||||
set(${dest} "${LIBRARY_FILE}" PARENT_SCOPE)
|
||||
else()
|
||||
message(STATUS "Failed to find compiler-rt ${name} library")
|
||||
endif()
|
||||
endfunction()
|
|
@ -1,4 +1,5 @@
|
|||
include(CMakePushCheckState)
|
||||
include(CheckCCompilerFlag)
|
||||
include(CheckCXXCompilerFlag)
|
||||
include(CheckLibraryExists)
|
||||
include(CheckSymbolExists)
|
||||
|
@ -11,6 +12,26 @@ function(check_linker_flag flag out_var)
|
|||
cmake_pop_check_state()
|
||||
endfunction()
|
||||
|
||||
check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)
|
||||
if (NOT SANITIZER_USE_COMPILER_RT)
|
||||
check_library_exists(gcc_s __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_S_LIB)
|
||||
endif()
|
||||
|
||||
check_c_compiler_flag(-nodefaultlibs COMPILER_RT_HAS_NODEFAULTLIBS_FLAG)
|
||||
if (COMPILER_RT_HAS_NODEFAULTLIBS_FLAG)
|
||||
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
|
||||
if (COMPILER_RT_HAS_LIBC)
|
||||
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
|
||||
endif ()
|
||||
if (SANITIZER_USE_COMPILER_RT)
|
||||
list(APPEND CMAKE_REQUIRED_FLAGS -rtlib=compiler-rt)
|
||||
find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY)
|
||||
list(APPEND CMAKE_REQUIRED_LIBRARIES "${COMPILER_RT_BUILTINS_LIBRARY}")
|
||||
elseif (COMPILER_RT_HAS_GCC_S_LIB)
|
||||
list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
# CodeGen options.
|
||||
check_cxx_compiler_flag(-fPIC COMPILER_RT_HAS_FPIC_FLAG)
|
||||
check_cxx_compiler_flag(-fPIE COMPILER_RT_HAS_FPIE_FLAG)
|
||||
|
@ -73,7 +94,6 @@ check_cxx_compiler_flag(/wd4800 COMPILER_RT_HAS_WD4800_FLAG)
|
|||
check_symbol_exists(__func__ "" COMPILER_RT_HAS_FUNC_SYMBOL)
|
||||
|
||||
# Libraries.
|
||||
check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)
|
||||
check_library_exists(dl dlopen "" COMPILER_RT_HAS_LIBDL)
|
||||
check_library_exists(rt shm_open "" COMPILER_RT_HAS_LIBRT)
|
||||
check_library_exists(m pow "" COMPILER_RT_HAS_LIBM)
|
||||
|
|
|
@ -40,7 +40,7 @@ set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
|
|||
|
||||
append_rtti_flag(OFF ASAN_CFLAGS)
|
||||
|
||||
set(ASAN_DYNAMIC_LINK_FLAGS)
|
||||
set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
|
||||
|
||||
if(ANDROID)
|
||||
# On Android, -z global does not do what it is documented to do.
|
||||
|
@ -65,7 +65,8 @@ append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
|
|||
-ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
|
||||
append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
|
||||
|
||||
append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
|
||||
set(ASAN_DYNAMIC_LIBS ${SANITIZER_COMMON_LINK_LIBS})
|
||||
|
||||
append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
|
||||
|
|
|
@ -29,6 +29,8 @@ add_compiler_rt_object_libraries(RTLSanCommon
|
|||
if(COMPILER_RT_HAS_LSAN)
|
||||
add_compiler_rt_component(lsan)
|
||||
if(APPLE)
|
||||
set(LSAN_LINK_LIBS ${SANITIZER_COMMON_LINK_LIBS})
|
||||
|
||||
add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
|
||||
add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
|
||||
|
||||
|
@ -42,7 +44,8 @@ if(COMPILER_RT_HAS_LSAN)
|
|||
RTSanitizerCommon
|
||||
RTSanitizerCommonLibc
|
||||
CFLAGS ${LSAN_CFLAGS}
|
||||
LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
|
||||
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS}
|
||||
LINK_LIBS ${LSAN_LINK_LIBS}
|
||||
PARENT_TARGET lsan)
|
||||
else()
|
||||
foreach(arch ${LSAN_SUPPORTED_ARCH})
|
||||
|
|
|
@ -6,6 +6,8 @@ set_target_properties(stats PROPERTIES FOLDER "Compiler-RT Misc")
|
|||
if(APPLE)
|
||||
set(STATS_LIB_FLAVOR SHARED)
|
||||
|
||||
set(STATS_LINK_LIBS ${SANITIZER_COMMON_LINK_LIBS})
|
||||
|
||||
add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
|
||||
add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
|
||||
add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
|
||||
|
@ -23,7 +25,8 @@ add_compiler_rt_runtime(clang_rt.stats
|
|||
OBJECT_LIBS RTSanitizerCommon
|
||||
RTSanitizerCommonLibc
|
||||
CFLAGS ${SANITIZER_COMMON_CFLAGS}
|
||||
LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
|
||||
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS}
|
||||
LINK_LIBS ${STATS_LINK_LIBS}
|
||||
PARENT_TARGET stats)
|
||||
|
||||
add_compiler_rt_runtime(clang_rt.stats_client
|
||||
|
|
|
@ -109,6 +109,8 @@ if(APPLE)
|
|||
set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES LANGUAGE C)
|
||||
endif()
|
||||
|
||||
set(TSAN_LINK_LIBS ${SANITIZER_COMMON_LINK_LIBS})
|
||||
|
||||
add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
|
||||
add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
|
||||
|
||||
|
@ -122,7 +124,8 @@ if(APPLE)
|
|||
RTSanitizerCommonLibc
|
||||
RTUbsan
|
||||
CFLAGS ${TSAN_RTL_CFLAGS}
|
||||
LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
|
||||
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS}
|
||||
LINK_LIBS ${TSAN_LINK_LIBS}
|
||||
PARENT_TARGET tsan)
|
||||
add_compiler_rt_object_libraries(RTTsan_dynamic
|
||||
OS ${TSAN_SUPPORTED_OS}
|
||||
|
|
|
@ -10,10 +10,12 @@ set(DD_SOURCES
|
|||
dd_interceptors.cc
|
||||
)
|
||||
|
||||
set(DD_LINKLIBS)
|
||||
set(DD_LINKLIBS ${SANITIZER_COMMON_LINK_LIBS})
|
||||
|
||||
append_list_if(COMPILER_RT_HAS_LIBDL dl DD_LINKLIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBRT rt DD_LINKLIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread DD_LINKLIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ DD_LINKLIBS)
|
||||
|
||||
add_custom_target(dd)
|
||||
# Deadlock detector is currently supported on 64-bit Linux only.
|
||||
|
@ -40,6 +42,7 @@ if(CAN_TARGET_x86_64 AND UNIX AND NOT APPLE AND NOT ANDROID)
|
|||
$<TARGET_OBJECTS:RTInterception.${arch}>
|
||||
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
|
||||
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
|
||||
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS}
|
||||
LINK_LIBS ${DD_LINKLIBS}
|
||||
PARENT_TARGET dd)
|
||||
endif()
|
||||
|
|
|
@ -34,9 +34,12 @@ set(UBSAN_CXXFLAGS ${SANITIZER_COMMON_CFLAGS})
|
|||
append_rtti_flag(ON UBSAN_CXXFLAGS)
|
||||
append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_CXXFLAGS)
|
||||
|
||||
set(UBSAN_DYNAMIC_LIBS ${SANITIZER_COMMON_LINK_LIBS})
|
||||
|
||||
append_list_if(COMPILER_RT_HAS_LIBDL dl UBSAN_DYNAMIC_LIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBRT rt UBSAN_DYNAMIC_LIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread UBSAN_DYNAMIC_LIBS)
|
||||
append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ UBSAN_DYNAMIC_LIBS)
|
||||
|
||||
add_compiler_rt_component(ubsan)
|
||||
|
||||
|
@ -155,6 +158,7 @@ else()
|
|||
RTSanitizerCommonLibc
|
||||
RTUbsan
|
||||
CFLAGS ${UBSAN_CFLAGS}
|
||||
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS}
|
||||
LINK_LIBS ${UBSAN_DYNAMIC_LIBS}
|
||||
PARENT_TARGET ubsan)
|
||||
|
||||
|
@ -166,6 +170,7 @@ else()
|
|||
RTUbsan
|
||||
RTUbsan_cxx
|
||||
CFLAGS ${UBSAN_CXXFLAGS}
|
||||
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS}
|
||||
LINK_LIBS ${UBSAN_DYNAMIC_LIBS}
|
||||
PARENT_TARGET ubsan)
|
||||
|
||||
|
|
Loading…
Reference in New Issue