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