sanitizer_signal_interceptors.inc 3.6 KB

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