asan_interceptors.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //===-- asan_interceptors.h -------------------------------------*- 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. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // ASan-private header for asan_interceptors.cpp
  12. //===----------------------------------------------------------------------===//
  13. #ifndef ASAN_INTERCEPTORS_H
  14. #define ASAN_INTERCEPTORS_H
  15. #include "asan_interceptors_memintrinsics.h"
  16. #include "asan_internal.h"
  17. #include "interception/interception.h"
  18. #include "sanitizer_common/sanitizer_platform.h"
  19. #include "sanitizer_common/sanitizer_platform_interceptors.h"
  20. namespace __asan {
  21. void InitializeAsanInterceptors();
  22. void InitializePlatformInterceptors();
  23. #define ENSURE_ASAN_INITED() \
  24. do { \
  25. CHECK(!asan_init_is_running); \
  26. if (UNLIKELY(!asan_inited)) { \
  27. AsanInitFromRtl(); \
  28. } \
  29. } while (0)
  30. } // namespace __asan
  31. // There is no general interception at all on Fuchsia.
  32. // Only the functions in asan_interceptors_memintrinsics.h are
  33. // really defined to replace libc functions.
  34. #if !SANITIZER_FUCHSIA
  35. // Use macro to describe if specific function should be
  36. // intercepted on a given platform.
  37. #if !SANITIZER_WINDOWS
  38. # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
  39. # define ASAN_INTERCEPT__LONGJMP 1
  40. # define ASAN_INTERCEPT_INDEX 1
  41. # define ASAN_INTERCEPT_PTHREAD_CREATE 1
  42. #else
  43. # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
  44. # define ASAN_INTERCEPT__LONGJMP 0
  45. # define ASAN_INTERCEPT_INDEX 0
  46. # define ASAN_INTERCEPT_PTHREAD_CREATE 0
  47. #endif
  48. #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
  49. SANITIZER_SOLARIS
  50. # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
  51. #else
  52. # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
  53. #endif
  54. #if SANITIZER_GLIBC || SANITIZER_SOLARIS
  55. # define ASAN_INTERCEPT_SWAPCONTEXT 1
  56. #else
  57. # define ASAN_INTERCEPT_SWAPCONTEXT 0
  58. #endif
  59. #if !SANITIZER_WINDOWS
  60. # define ASAN_INTERCEPT_SIGLONGJMP 1
  61. #else
  62. # define ASAN_INTERCEPT_SIGLONGJMP 0
  63. #endif
  64. #if SANITIZER_GLIBC
  65. # define ASAN_INTERCEPT___LONGJMP_CHK 1
  66. #else
  67. # define ASAN_INTERCEPT___LONGJMP_CHK 0
  68. #endif
  69. #if ASAN_HAS_EXCEPTIONS && !SANITIZER_WINDOWS && !SANITIZER_SOLARIS && \
  70. !SANITIZER_NETBSD
  71. # define ASAN_INTERCEPT___CXA_THROW 1
  72. # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 1
  73. # if defined(_GLIBCXX_SJLJ_EXCEPTIONS) || (SANITIZER_IOS && defined(__arm__))
  74. # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 1
  75. # else
  76. # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 1
  77. # endif
  78. #else
  79. # define ASAN_INTERCEPT___CXA_THROW 0
  80. # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 0
  81. # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 0
  82. # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 0
  83. #endif
  84. #if !SANITIZER_WINDOWS
  85. # define ASAN_INTERCEPT___CXA_ATEXIT 1
  86. #else
  87. # define ASAN_INTERCEPT___CXA_ATEXIT 0
  88. #endif
  89. #if SANITIZER_NETBSD
  90. # define ASAN_INTERCEPT_ATEXIT 1
  91. #else
  92. # define ASAN_INTERCEPT_ATEXIT 0
  93. #endif
  94. #if SANITIZER_GLIBC
  95. # define ASAN_INTERCEPT___STRDUP 1
  96. #else
  97. # define ASAN_INTERCEPT___STRDUP 0
  98. #endif
  99. #if SANITIZER_LINUX && \
  100. (defined(__arm__) || defined(__aarch64__) || defined(__i386__) || \
  101. defined(__x86_64__) || SANITIZER_RISCV64)
  102. # define ASAN_INTERCEPT_VFORK 1
  103. #else
  104. # define ASAN_INTERCEPT_VFORK 0
  105. #endif
  106. #if SANITIZER_NETBSD
  107. # define ASAN_INTERCEPT_PTHREAD_ATFORK 1
  108. #else
  109. # define ASAN_INTERCEPT_PTHREAD_ATFORK 0
  110. #endif
  111. DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size)
  112. DECLARE_REAL(char*, strchr, const char *str, int c)
  113. DECLARE_REAL(SIZE_T, strlen, const char *s)
  114. DECLARE_REAL(char*, strncpy, char *to, const char *from, uptr size)
  115. DECLARE_REAL(uptr, strnlen, const char *s, uptr maxlen)
  116. DECLARE_REAL(char*, strstr, const char *s1, const char *s2)
  117. # if !SANITIZER_MAC
  118. # define ASAN_INTERCEPT_FUNC(name) \
  119. do { \
  120. if (!INTERCEPT_FUNCTION(name)) \
  121. VReport(1, "AddressSanitizer: failed to intercept '%s'\n", #name); \
  122. } while (0)
  123. # define ASAN_INTERCEPT_FUNC_VER(name, ver) \
  124. do { \
  125. if (!INTERCEPT_FUNCTION_VER(name, ver)) \
  126. VReport(1, "AddressSanitizer: failed to intercept '%s@@%s'\n", \
  127. #name, ver); \
  128. } while (0)
  129. # define ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \
  130. do { \
  131. if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \
  132. VReport(1, \
  133. "AddressSanitizer: failed to intercept '%s@@%s' or '%s'\n", \
  134. #name, ver, #name); \
  135. } while (0)
  136. # else
  137. // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
  138. # define ASAN_INTERCEPT_FUNC(name)
  139. # endif // SANITIZER_MAC
  140. #endif // !SANITIZER_FUCHSIA
  141. #endif // ASAN_INTERCEPTORS_H