sanitizer_signal_interceptors.inc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if (GetHandleSignalMode(signum) == kHandleSignalExclusive) return 0;
  41. SIGNAL_INTERCEPTOR_SIGNAL_IMPL(bsd_signal, signum, handler);
  42. }
  43. #define INIT_BSD_SIGNAL COMMON_INTERCEPT_FUNCTION(bsd_signal)
  44. #else // SANITIZER_INTERCEPT_BSD_SIGNAL
  45. #define INIT_BSD_SIGNAL
  46. #endif // SANITIZER_INTERCEPT_BSD_SIGNAL
  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. INTERCEPTOR(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. namespace __sanitizer {
  64. int real_sigaction(int signum, const void *act, void *oldact) {
  65. return REAL(sigaction_symname)(signum, (const __sanitizer_sigaction *)act,
  66. (__sanitizer_sigaction *)oldact);
  67. }
  68. } // namespace __sanitizer
  69. #else // SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
  70. #define INIT_SIGNAL
  71. #define INIT_SIGACTION
  72. // We need to have defined REAL(sigaction) on other systems.
  73. namespace __sanitizer {
  74. struct __sanitizer_sigaction;
  75. }
  76. DEFINE_REAL(int, sigaction, int signum, const __sanitizer_sigaction *act,
  77. __sanitizer_sigaction *oldact)
  78. #endif // SANITIZER_INTERCEPT_SIGNAL_AND_SIGACTION
  79. static void InitializeSignalInterceptors() {
  80. static bool was_called_once;
  81. CHECK(!was_called_once);
  82. was_called_once = true;
  83. INIT_BSD_SIGNAL;
  84. INIT_SIGNAL;
  85. INIT_SIGACTION;
  86. }