1 //=-- ubsan_signals_standalone.cc
2 //------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 // Installs signal handlers and related interceptors for UBSan standalone.
12 //===----------------------------------------------------------------------===//
14 #include "ubsan_platform.h"
15 #include "sanitizer_common/sanitizer_platform.h"
17 #include "interception/interception.h"
18 #include "sanitizer_common/sanitizer_stacktrace.h"
19 #include "ubsan_diag.h"
20 #include "ubsan_init.h"
22 // Interception of signals breaks too many things on Android.
23 // * It requires that ubsan is the first dependency of the main executable for
24 // the interceptors to work correctly. This complicates deployment, as it
25 // prevents us from enabling ubsan on random platform modules independently.
26 // * For this to work with ART VM, ubsan signal handler has to be set after the
27 // debuggerd handler, but before the ART handler.
28 // * Interceptors don't work at all when ubsan runtime is loaded late, ex. when
29 // it is part of an APK that does not use wrap.sh method.
30 #if SANITIZER_FUCHSIA || SANITIZER_ANDROID
33 void InitializeDeadlySignals() {}
38 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
39 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
41 // TODO(yln): Temporary workaround. Will be removed.
42 void ubsan_GetStackTrace(BufferedStackTrace *stack, uptr max_depth,
43 uptr pc, uptr bp, void *context, bool fast);
47 static void OnStackUnwind(const SignalContext &sig, const void *,
48 BufferedStackTrace *stack) {
49 ubsan_GetStackTrace(stack, kStackTraceMax, sig.pc, sig.bp, sig.context,
50 common_flags()->fast_unwind_on_fatal);
53 static void UBsanOnDeadlySignal(int signo, void *siginfo, void *context) {
54 HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
57 static bool is_initialized = false;
59 void InitializeDeadlySignals() {
62 is_initialized = true;
63 InitializeSignalInterceptors();
64 InstallDeadlySignalHandlers(&UBsanOnDeadlySignal);
67 } // namespace __ubsan
71 #endif // CAN_SANITIZE_UB