]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_signals_standalone.cc
Unbreak DRM KMS build by adding the needed compatibility field in the LinuxKPI.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / ubsan / ubsan_signals_standalone.cc
1 //=-- ubsan_signals_standalone.cc
2 //------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Installs signal handlers and related interceptors for UBSan standalone.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ubsan_platform.h"
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if CAN_SANITIZE_UB
17 #include "interception/interception.h"
18 #include "sanitizer_common/sanitizer_stacktrace.h"
19 #include "ubsan_diag.h"
20 #include "ubsan_init.h"
21
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
31
32 namespace __ubsan {
33 void InitializeDeadlySignals() {}
34 }
35
36 #else
37
38 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
39 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
40
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);
44
45 namespace __ubsan {
46
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);
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