1*490215a3Smrg //=-- ubsan_signals_standalone.cc
2*490215a3Smrg //------------------------------------------------===//
3*490215a3Smrg //
4*490215a3Smrg //                     The LLVM Compiler Infrastructure
5*490215a3Smrg //
6*490215a3Smrg // This file is distributed under the University of Illinois Open Source
7*490215a3Smrg // License. See LICENSE.TXT for details.
8*490215a3Smrg //
9*490215a3Smrg //===----------------------------------------------------------------------===//
10*490215a3Smrg //
11*490215a3Smrg // Installs signal handlers and related interceptors for UBSan standalone.
12*490215a3Smrg //
13*490215a3Smrg //===----------------------------------------------------------------------===//
14*490215a3Smrg 
15*490215a3Smrg #include "ubsan_platform.h"
16*490215a3Smrg #include "sanitizer_common/sanitizer_platform.h"
17*490215a3Smrg #if CAN_SANITIZE_UB
18*490215a3Smrg #include "interception/interception.h"
19*490215a3Smrg #include "sanitizer_common/sanitizer_stacktrace.h"
20*490215a3Smrg #include "ubsan_diag.h"
21*490215a3Smrg #include "ubsan_init.h"
22*490215a3Smrg 
23*490215a3Smrg // Interception of signals breaks too many things on Android.
24*490215a3Smrg // * It requires that ubsan is the first dependency of the main executable for
25*490215a3Smrg // the interceptors to work correctly. This complicates deployment, as it
26*490215a3Smrg // prevents us from enabling ubsan on random platform modules independently.
27*490215a3Smrg // * For this to work with ART VM, ubsan signal handler has to be set after the
28*490215a3Smrg // debuggerd handler, but before the ART handler.
29*490215a3Smrg // * Interceptors don't work at all when ubsan runtime is loaded late, ex. when
30*490215a3Smrg // it is part of an APK that does not use wrap.sh method.
31*490215a3Smrg #if SANITIZER_FUCHSIA || SANITIZER_ANDROID
32*490215a3Smrg 
33*490215a3Smrg namespace __ubsan {
InitializeDeadlySignals()34*490215a3Smrg void InitializeDeadlySignals() {}
35*490215a3Smrg }
36*490215a3Smrg 
37*490215a3Smrg #else
38*490215a3Smrg 
39*490215a3Smrg #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
40*490215a3Smrg #include "sanitizer_common/sanitizer_signal_interceptors.inc"
41*490215a3Smrg 
42*490215a3Smrg namespace __ubsan {
43*490215a3Smrg 
OnStackUnwind(const SignalContext & sig,const void *,BufferedStackTrace * stack)44*490215a3Smrg static void OnStackUnwind(const SignalContext &sig, const void *,
45*490215a3Smrg                           BufferedStackTrace *stack) {
46*490215a3Smrg   GetStackTrace(stack, kStackTraceMax, sig.pc, sig.bp, sig.context,
47*490215a3Smrg                 common_flags()->fast_unwind_on_fatal);
48*490215a3Smrg }
49*490215a3Smrg 
UBsanOnDeadlySignal(int signo,void * siginfo,void * context)50*490215a3Smrg static void UBsanOnDeadlySignal(int signo, void *siginfo, void *context) {
51*490215a3Smrg   HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr);
52*490215a3Smrg }
53*490215a3Smrg 
54*490215a3Smrg static bool is_initialized = false;
55*490215a3Smrg 
InitializeDeadlySignals()56*490215a3Smrg void InitializeDeadlySignals() {
57*490215a3Smrg   if (is_initialized)
58*490215a3Smrg     return;
59*490215a3Smrg   is_initialized = true;
60*490215a3Smrg   InitializeSignalInterceptors();
61*490215a3Smrg   InstallDeadlySignalHandlers(&UBsanOnDeadlySignal);
62*490215a3Smrg }
63*490215a3Smrg 
64*490215a3Smrg } // namespace __ubsan
65*490215a3Smrg 
66*490215a3Smrg #endif
67*490215a3Smrg 
68*490215a3Smrg #endif // CAN_SANITIZE_UB
69