1//===-- sanitizer_signal_interceptors.inc -----------------------*- C++ -*-===//
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// Signal interceptors for sanitizers.
10//
11//===----------------------------------------------------------------------===//
12
13#include "interception/interception.h"
14#include "sanitizer_common.h"
15#include "sanitizer_internal_defs.h"
16#include "sanitizer_platform_interceptors.h"
17
18using namespace __sanitizer;
19
20#if SANITIZER_NETBSD
21#define sigaction_symname __sigaction14
22#else
23#define sigaction_symname sigaction
24#endif
25
26#ifndef SIGNAL_INTERCEPTOR_SIGNAL_IMPL
27#define SIGNAL_INTERCEPTOR_SIGNAL_IMPL(func, signum, handler) \
28  { return REAL(func)(signum, handler); }
29#endif
30
31#ifndef SIGNAL_INTERCEPTOR_SIGACTION_IMPL
32#define SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signum, act, oldact) \
33  { return REAL(sigaction_symname)(signum, act, oldact); }
34#endif
35
36#if SANITIZER_INTERCEPT_BSD_SIGNAL
37INTERCEPTOR(uptr, bsd_signal, int signum, uptr handler) {
38  if (GetHandleSignalMode(signum) == kHandleSignalExclusive) return 0;
39  SIGNAL_INTERCEPTOR_SIGNAL_IMPL(bsd_signal, signum, handler);
40}
41#define INIT_BSD_SIGNAL COMMON_INTERCEPT_FUNCTION(bsd_signal)
42#else  // SANITIZER_INTERCEPT_BSD_SIGNAL
43#define INIT_BSD_SIGNAL
44#endif  // SANITIZER_INTERCEPT_BSD_SIGNAL
45
46#if SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
47INTERCEPTOR(uptr, signal, int signum, uptr handler) {
48  if (GetHandleSignalMode(signum) == kHandleSignalExclusive)
49    return (uptr) nullptr;
50  SIGNAL_INTERCEPTOR_SIGNAL_IMPL(signal, signum, handler);
51}
52#define INIT_SIGNAL COMMON_INTERCEPT_FUNCTION(signal)
53
54INTERCEPTOR(int, sigaction_symname, int signum,
55            const __sanitizer_sigaction *act, __sanitizer_sigaction *oldact) {
56  if (GetHandleSignalMode(signum) == kHandleSignalExclusive) {
57    if (!oldact) return 0;
58    act = nullptr;
59  }
60  SIGNAL_INTERCEPTOR_SIGACTION_IMPL(signum, act, oldact);
61}
62#define INIT_SIGACTION COMMON_INTERCEPT_FUNCTION(sigaction_symname)
63
64namespace __sanitizer {
65int real_sigaction(int signum, const void *act, void *oldact) {
66  return REAL(sigaction_symname)(signum, (const __sanitizer_sigaction *)act,
67                         (__sanitizer_sigaction *)oldact);
68}
69}  // namespace __sanitizer
70#else  // SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
71#define INIT_SIGNAL
72#define INIT_SIGACTION
73// We need to have defined REAL(sigaction) on other systems.
74namespace __sanitizer {
75struct __sanitizer_sigaction;
76}
77DEFINE_REAL(int, sigaction, int signum, const __sanitizer_sigaction *act,
78            __sanitizer_sigaction *oldact)
79#endif  // SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
80
81static void InitializeSignalInterceptors() {
82  static bool was_called_once;
83  CHECK(!was_called_once);
84  was_called_once = true;
85
86  INIT_BSD_SIGNAL;
87  INIT_SIGNAL;
88  INIT_SIGACTION;
89}
90