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