Index: compiler-rt/lib/ubsan/CMakeLists.txt =================================================================== --- compiler-rt/lib/ubsan/CMakeLists.txt +++ compiler-rt/lib/ubsan/CMakeLists.txt @@ -1,6 +1,6 @@ # Build for the undefined behavior sanitizer runtime support library. -set(UBSAN_SOURCES +set(UBSAN_COMMON_SOURCES ubsan_diag.cc ubsan_init.cc ubsan_flags.cc @@ -11,6 +11,7 @@ set(UBSAN_STANDALONE_SOURCES ubsan_diag_standalone.cc ubsan_init_standalone.cc + ubsan_signals_standalone.cc ) set(UBSAN_CXXABI_SOURCES @@ -45,19 +46,30 @@ add_compiler_rt_component(ubsan) -if(APPLE) - set(UBSAN_COMMON_SOURCES ${UBSAN_SOURCES}) - if(SANITIZER_CAN_USE_CXXABI) - list(APPEND UBSAN_COMMON_SOURCES ${UBSAN_CXXABI_SOURCES}) - endif() +if(APPLE AND SANITIZER_CAN_USE_CXXABI) + list(APPEND UBSAN_COMMON_SOURCES ${UBSAN_CXXABI_SOURCES}) +endif() +set(UBSAN_PLUGIN_SOURCES + ${UBSAN_COMMON_SOURCES} + ubsan_signals_plugin.cc + ) + +if(APPLE) # Common parts of UBSan runtime. - add_compiler_rt_object_libraries(RTUbsan + add_compiler_rt_object_libraries(RTUbsan_common OS ${SANITIZER_COMMON_SUPPORTED_OS} ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH} SOURCES ${UBSAN_COMMON_SOURCES} CFLAGS ${UBSAN_CXXFLAGS}) + # UBSan plugging used by other tools. + add_compiler_rt_object_libraries(RTUbsan + OS ${SANITIZER_COMMON_SUPPORTED_OS} + ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH} + SOURCES ${UBSAN_PLUGIN_SOURCES} + CFLAGS ${UBSAN_CXXFLAGS}) + if(COMPILER_RT_HAS_UBSAN) # Initializer of standalone UBSan runtime. add_compiler_rt_object_libraries(RTUbsan_standalone @@ -73,7 +85,7 @@ SHARED OS ${SANITIZER_COMMON_SUPPORTED_OS} ARCHS ${UBSAN_SUPPORTED_ARCH} - OBJECT_LIBS RTUbsan + OBJECT_LIBS RTUbsan_common RTUbsan_standalone RTSanitizerCommon RTSanitizerCommonLibc @@ -83,9 +95,14 @@ else() # Common parts of UBSan runtime. + add_compiler_rt_object_libraries(RTUbsan_common + ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH} + SOURCES ${UBSAN_COMMON_SOURCES} CFLAGS ${UBSAN_CFLAGS}) + + # UBSan plugging used by other tools. add_compiler_rt_object_libraries(RTUbsan ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH} - SOURCES ${UBSAN_SOURCES} CFLAGS ${UBSAN_CFLAGS}) + SOURCES ${UBSAN_PLUGIN_SOURCES} CFLAGS ${UBSAN_CFLAGS}) if(SANITIZER_CAN_USE_CXXABI) # C++-specific parts of UBSan runtime. Requires a C++ ABI library. @@ -133,7 +150,8 @@ # Initializer of standalone UBSan runtime. add_compiler_rt_object_libraries(RTUbsan_standalone ARCHS ${UBSAN_SUPPORTED_ARCH} - SOURCES ${UBSAN_STANDALONE_SOURCES} CFLAGS ${UBSAN_STANDALONE_CFLAGS}) + SOURCES ${UBSAN_STANDALONE_SOURCES} + CFLAGS ${UBSAN_STANDALONE_CFLAGS}) # Standalone UBSan runtimes. add_compiler_rt_runtime(clang_rt.ubsan_standalone @@ -141,8 +159,9 @@ ARCHS ${UBSAN_SUPPORTED_ARCH} OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc - RTUbsan + RTUbsan_common RTUbsan_standalone + RTInterception CFLAGS ${UBSAN_CFLAGS} PARENT_TARGET ubsan) @@ -159,7 +178,9 @@ ARCHS ${UBSAN_SUPPORTED_ARCH} OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc - RTUbsan + RTUbsan_common + RTUbsan_standalone + RTInterception CFLAGS ${UBSAN_CFLAGS} LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} LINK_LIBS ${UBSAN_DYNAMIC_LIBS} Index: compiler-rt/lib/ubsan/ubsan_init.h =================================================================== --- compiler-rt/lib/ubsan/ubsan_init.h +++ compiler-rt/lib/ubsan/ubsan_init.h @@ -29,6 +29,9 @@ // from "parent tool" (e.g. ASan) initialization. void InitAsPlugin(); +// Initializes signal handlers and interceptors. +void InitializeDeadlySignals(); + } // namespace __ubsan #endif // UBSAN_INIT_H Index: compiler-rt/lib/ubsan/ubsan_init.cc =================================================================== --- compiler-rt/lib/ubsan/ubsan_init.cc +++ compiler-rt/lib/ubsan/ubsan_init.cc @@ -43,6 +43,7 @@ CacheBinaryName(); InitializeFlags(); __sanitizer_set_report_path(common_flags()->log_path); + InitializeDeadlySignals(); AndroidLogInit(); InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); CommonInit(); Index: compiler-rt/lib/ubsan/ubsan_signals_plugin.cc =================================================================== --- /dev/null +++ compiler-rt/lib/ubsan/ubsan_signals_plugin.cc @@ -0,0 +1,26 @@ +//=-- ubsan_interceptors_plugin.cc +//------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Plugin version of InitializeDeadlySignals which does nothing as parent tools +// suppose to handle signals. +// +//===----------------------------------------------------------------------===// + +#include "ubsan_platform.h" +#if CAN_SANITIZE_UB +#include "ubsan_init.h" + +namespace __ubsan { + +void InitializeDeadlySignals() {} + +} // namespace __ubsan + +#endif // CAN_SANITIZE_UB Index: compiler-rt/lib/ubsan/ubsan_signals_standalone.cc =================================================================== --- /dev/null +++ compiler-rt/lib/ubsan/ubsan_signals_standalone.cc @@ -0,0 +1,45 @@ +//=-- ubsan_interceptors_standalone.cc +//------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Installs signal handlers and related interceptors for UBSan standalone. +// +//===----------------------------------------------------------------------===// + +#include "ubsan_platform.h" +#if CAN_SANITIZE_UB +#include "interception/interception.h" +#include "sanitizer_common/sanitizer_stacktrace.h" +#include "ubsan_diag.h" +#include "ubsan_init.h" + +#define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name) +#include "sanitizer_common/sanitizer_signal_interceptors.inc" + +namespace __ubsan { + +static void OnStackUnwind(const SignalContext &sig, const void *, + BufferedStackTrace *stack) { + GetStackTraceWithPcBpAndContext(stack, kStackTraceMax, sig.pc, sig.bp, + sig.context, + common_flags()->fast_unwind_on_fatal); +} + +static void UBsanOnDeadlySignal(int signo, void *siginfo, void *context) { + HandleReportDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr); +} + +void InitializeDeadlySignals() { + InitializeSignalInterceptors(); + InstallDeadlySignalHandlers(&UBsanOnDeadlySignal); +} + +} // namespace __ubsan + +#endif // CAN_SANITIZE_UB Index: compiler-rt/test/sanitizer_common/TestCases/Linux/allow_user_segv.cc =================================================================== --- compiler-rt/test/sanitizer_common/TestCases/Linux/allow_user_segv.cc +++ compiler-rt/test/sanitizer_common/TestCases/Linux/allow_user_segv.cc @@ -20,7 +20,6 @@ // Remove when fixed: https://github.com/google/sanitizers/issues/637 // XFAIL: msan // XFAIL: tsan -// XFAIL: ubsan #include #include Index: compiler-rt/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc =================================================================== --- compiler-rt/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc +++ compiler-rt/test/sanitizer_common/TestCases/Posix/dedup_token_length_test.cc @@ -7,10 +7,9 @@ // RUN: env %tool_options='abort_on_error=0, dedup_token_length=3' not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK3 --match-full-lines // REQUIRES: stable-runtime -// FIXME: implement SEGV handler in other sanitizers, not just asan. +// FIXME: implement SEGV handler in other sanitizers. // XFAIL: msan // XFAIL: tsan -// XFAIL: ubsan volatile int *null = 0; Index: compiler-rt/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc =================================================================== --- compiler-rt/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc +++ compiler-rt/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc @@ -11,7 +11,6 @@ // FIXME: implement in other sanitizers. // XFAIL: msan // XFAIL: tsan -// XFAIL: ubsan int main() { #if defined(__x86_64__) Index: compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc =================================================================== --- compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc +++ compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_fd_test.cc @@ -9,7 +9,6 @@ // FIXME: implement SEGV handler in other sanitizers, not just asan. // XFAIL: msan // XFAIL: tsan -// XFAIL: ubsan #include #include