]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_signal_interceptors.inc
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_signal_interceptors.inc
1 //===-- sanitizer_signal_interceptors.inc -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Signal interceptors for sanitizers.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "interception/interception.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_internal_defs.h"
17 #include "sanitizer_platform_interceptors.h"
18
19 using namespace __sanitizer;
20
21 #if SANITIZER_NETBSD
22 #define sigaction_symname __sigaction14
23 #else
24 #define sigaction_symname sigaction
25 #endif
26
27 #ifndef SIGNAL_INTERCEPTOR_SIGNAL_IMPL
28 #define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signum, handler) \
29   { return REAL(func)(signum, handler); }
30 #endif
31
32 #ifndef SIGNAL_INTERCEPTOR_SIGACTION_IMPL
33 #define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signum, act, oldact) \
34   { return REAL(sigaction_symname)(signum, act, oldact); }
35 #endif
36
37 #if SANITIZER_INTERCEPT_BSD_SIGNAL
38 INTERCEPTOR(uptr, bsd_signal, int signum, uptr handler) {
39   if (GetHandleSignalMode(signum) == kHandleSignalExclusive) return 0;
40   SIGNAL_INTERCEPTOR_SIGNAL_IMPL(bsd_signal, signum, handler);
41 }
42 #define INIT_BSD_SIGNAL COMMON_INTERCEPT_FUNCTION(bsd_signal)
43 #else  // SANITIZER_INTERCEPT_BSD_SIGNAL
44 #define INIT_BSD_SIGNAL
45 #endif  // SANITIZER_INTERCEPT_BSD_SIGNAL
46
47 #if SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
48 INTERCEPTOR(uptr, signal, int signum, uptr handler) {
49   if (GetHandleSignalMode(signum) == kHandleSignalExclusive)
50     return (uptr) nullptr;
51   SIGNAL_INTERCEPTOR_SIGNAL_IMPL(signal, signum, handler);
52 }
53 #define INIT_SIGNAL COMMON_INTERCEPT_FUNCTION(signal)
54
55 INTERCEPTOR(int, sigaction_symname, int signum,
56             const __sanitizer_sigaction *act, __sanitizer_sigaction *oldact) {
57   if (GetHandleSignalMode(signum) == kHandleSignalExclusive) return 0;
58   SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signum, act, oldact);
59 }
60 #define INIT_SIGACTION COMMON_INTERCEPT_FUNCTION(sigaction_symname)
61
62 namespace __sanitizer {
63 int real_sigaction(int signum, const void *act, void *oldact) {
64   return REAL(sigaction_symname)(signum, (const __sanitizer_sigaction *)act,
65                          (__sanitizer_sigaction *)oldact);
66 }
67 }  // namespace __sanitizer
68 #else  // SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
69 #define INIT_SIGNAL
70 #define INIT_SIGACTION
71 // We need to have defined REAL(sigaction) on other systems.
72 namespace __sanitizer {
73 struct __sanitizer_sigaction;
74 }
75 DEFINE_REAL(int, sigaction, int signum, const __sanitizer_sigaction *act,
76             __sanitizer_sigaction *oldact)
77 #endif  // SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
78
79 static void InitializeSignalInterceptors() {
80   static bool was_called_once;
81   CHECK(!was_called_once);
82   was_called_once = true;
83
84   INIT_BSD_SIGNAL;
85   INIT_SIGNAL;
86   INIT_SIGACTION;
87 }