sanitizer_internal_defs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer.
  9. // It contains macro used in run-time libraries code.
  10. //===----------------------------------------------------------------------===//
  11. #ifndef SANITIZER_DEFS_H
  12. #define SANITIZER_DEFS_H
  13. #include "sanitizer_platform.h"
  14. // Only use SANITIZER_*ATTRIBUTE* before the function return type!
  15. #if SANITIZER_WINDOWS
  16. # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
  17. // FIXME find out what we need on Windows, if anything.
  18. # define SANITIZER_WEAK_ATTRIBUTE
  19. #elif defined(SANITIZER_GO)
  20. # define SANITIZER_INTERFACE_ATTRIBUTE
  21. # define SANITIZER_WEAK_ATTRIBUTE
  22. #else
  23. # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
  24. # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
  25. #endif
  26. #if SANITIZER_LINUX && !defined(SANITIZER_GO)
  27. # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
  28. #else
  29. # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
  30. #endif
  31. // We can use .preinit_array section on Linux to call sanitizer initialization
  32. // functions very early in the process startup (unless PIC macro is defined).
  33. // FIXME: do we have anything like this on Mac?
  34. #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
  35. # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
  36. #else
  37. # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
  38. #endif
  39. // GCC does not understand __has_feature
  40. #if !defined(__has_feature)
  41. # define __has_feature(x) 0
  42. #endif
  43. // For portability reasons we do not include stddef.h, stdint.h or any other
  44. // system header, but we do need some basic types that are not defined
  45. // in a portable way by the language itself.
  46. namespace __sanitizer {
  47. #if defined(_WIN64)
  48. // 64-bit Windows uses LLP64 data model.
  49. typedef unsigned long long uptr; // NOLINT
  50. typedef signed long long sptr; // NOLINT
  51. #else
  52. typedef unsigned long uptr; // NOLINT
  53. typedef signed long sptr; // NOLINT
  54. #endif // defined(_WIN64)
  55. #if defined(__x86_64__)
  56. // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
  57. // 64-bit pointer to unwind stack frame.
  58. typedef unsigned long long uhwptr; // NOLINT
  59. #else
  60. typedef uptr uhwptr; // NOLINT
  61. #endif
  62. typedef unsigned char u8;
  63. typedef unsigned short u16; // NOLINT
  64. typedef unsigned int u32;
  65. typedef unsigned long long u64; // NOLINT
  66. typedef signed char s8;
  67. typedef signed short s16; // NOLINT
  68. typedef signed int s32;
  69. typedef signed long long s64; // NOLINT
  70. typedef int fd_t;
  71. // WARNING: OFF_T may be different from OS type off_t, depending on the value of
  72. // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
  73. // like pread and mmap, as opposed to pread64 and mmap64.
  74. // Mac and Linux/x86-64 are special.
  75. #if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__))
  76. typedef u64 OFF_T;
  77. #else
  78. typedef uptr OFF_T;
  79. #endif
  80. typedef u64 OFF64_T;
  81. #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
  82. typedef uptr operator_new_size_type;
  83. #else
  84. typedef u32 operator_new_size_type;
  85. #endif
  86. } // namespace __sanitizer
  87. extern "C" {
  88. // Tell the tools to write their reports to "path.<pid>" instead of stderr.
  89. // The special values are "stdout" and "stderr".
  90. SANITIZER_INTERFACE_ATTRIBUTE
  91. void __sanitizer_set_report_path(const char *path);
  92. typedef struct {
  93. int coverage_sandboxed;
  94. __sanitizer::sptr coverage_fd;
  95. unsigned int coverage_max_block_size;
  96. } __sanitizer_sandbox_arguments;
  97. // Notify the tools that the sandbox is going to be turned on.
  98. SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void
  99. __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
  100. // This function is called by the tool when it has just finished reporting
  101. // an error. 'error_summary' is a one-line string that summarizes
  102. // the error message. This function can be overridden by the client.
  103. SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
  104. void __sanitizer_report_error_summary(const char *error_summary);
  105. SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump();
  106. SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init();
  107. SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov();
  108. SANITIZER_INTERFACE_ATTRIBUTE
  109. void __sanitizer_annotate_contiguous_container(const void *beg,
  110. const void *end,
  111. const void *old_mid,
  112. const void *new_mid);
  113. SANITIZER_INTERFACE_ATTRIBUTE
  114. int __sanitizer_verify_contiguous_container(const void *beg, const void *mid,
  115. const void *end);
  116. } // extern "C"
  117. using namespace __sanitizer; // NOLINT
  118. // ----------- ATTENTION -------------
  119. // This header should NOT include any other headers to avoid portability issues.
  120. // Common defs.
  121. #define INLINE inline
  122. #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
  123. #define WEAK SANITIZER_WEAK_ATTRIBUTE
  124. // Platform-specific defs.
  125. #if defined(_MSC_VER)
  126. # define ALWAYS_INLINE __forceinline
  127. // FIXME(timurrrr): do we need this on Windows?
  128. # define ALIAS(x)
  129. # define ALIGNED(x) __declspec(align(x))
  130. # define FORMAT(f, a)
  131. # define NOINLINE __declspec(noinline)
  132. # define NORETURN __declspec(noreturn)
  133. # define THREADLOCAL __declspec(thread)
  134. # define NOTHROW
  135. # define LIKELY(x) (x)
  136. # define UNLIKELY(x) (x)
  137. # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
  138. #else // _MSC_VER
  139. # define ALWAYS_INLINE inline __attribute__((always_inline))
  140. # define ALIAS(x) __attribute__((alias(x)))
  141. // Please only use the ALIGNED macro before the type.
  142. // Using ALIGNED after the variable declaration is not portable!
  143. # define ALIGNED(x) __attribute__((aligned(x)))
  144. # define FORMAT(f, a) __attribute__((format(printf, f, a)))
  145. # define NOINLINE __attribute__((noinline))
  146. # define NORETURN __attribute__((noreturn))
  147. # define THREADLOCAL __thread
  148. # define NOTHROW throw()
  149. # define LIKELY(x) __builtin_expect(!!(x), 1)
  150. # define UNLIKELY(x) __builtin_expect(!!(x), 0)
  151. # if defined(__i386__) || defined(__x86_64__)
  152. // __builtin_prefetch(x) generates prefetchnt0 on x86
  153. # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
  154. # else
  155. # define PREFETCH(x) __builtin_prefetch(x)
  156. # endif
  157. #endif // _MSC_VER
  158. #if !defined(_MSC_VER) || defined(__clang__)
  159. # define UNUSED __attribute__((unused))
  160. # define USED __attribute__((used))
  161. #else
  162. # define UNUSED
  163. # define USED
  164. #endif
  165. // Unaligned versions of basic types.
  166. typedef ALIGNED(1) u16 uu16;
  167. typedef ALIGNED(1) u32 uu32;
  168. typedef ALIGNED(1) u64 uu64;
  169. typedef ALIGNED(1) s16 us16;
  170. typedef ALIGNED(1) s32 us32;
  171. typedef ALIGNED(1) s64 us64;
  172. #if SANITIZER_WINDOWS
  173. typedef unsigned long DWORD; // NOLINT
  174. typedef DWORD thread_return_t;
  175. # define THREAD_CALLING_CONV __stdcall
  176. #else // _WIN32
  177. typedef void* thread_return_t;
  178. # define THREAD_CALLING_CONV
  179. #endif // _WIN32
  180. typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
  181. // NOTE: Functions below must be defined in each run-time.
  182. namespace __sanitizer {
  183. void NORETURN Die();
  184. // FIXME: No, this shouldn't be in the sanitizer interface.
  185. SANITIZER_INTERFACE_ATTRIBUTE
  186. void NORETURN CheckFailed(const char *file, int line, const char *cond,
  187. u64 v1, u64 v2);
  188. } // namespace __sanitizer
  189. // Check macro
  190. #define RAW_CHECK_MSG(expr, msg) do { \
  191. if (UNLIKELY(!(expr))) { \
  192. RawWrite(msg); \
  193. Die(); \
  194. } \
  195. } while (0)
  196. #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
  197. #define CHECK_IMPL(c1, op, c2) \
  198. do { \
  199. __sanitizer::u64 v1 = (u64)(c1); \
  200. __sanitizer::u64 v2 = (u64)(c2); \
  201. if (UNLIKELY(!(v1 op v2))) \
  202. __sanitizer::CheckFailed(__FILE__, __LINE__, \
  203. "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
  204. } while (false) \
  205. /**/
  206. #define CHECK(a) CHECK_IMPL((a), !=, 0)
  207. #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
  208. #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
  209. #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
  210. #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
  211. #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
  212. #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
  213. #if TSAN_DEBUG
  214. #define DCHECK(a) CHECK(a)
  215. #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
  216. #define DCHECK_NE(a, b) CHECK_NE(a, b)
  217. #define DCHECK_LT(a, b) CHECK_LT(a, b)
  218. #define DCHECK_LE(a, b) CHECK_LE(a, b)
  219. #define DCHECK_GT(a, b) CHECK_GT(a, b)
  220. #define DCHECK_GE(a, b) CHECK_GE(a, b)
  221. #else
  222. #define DCHECK(a)
  223. #define DCHECK_EQ(a, b)
  224. #define DCHECK_NE(a, b)
  225. #define DCHECK_LT(a, b)
  226. #define DCHECK_LE(a, b)
  227. #define DCHECK_GT(a, b)
  228. #define DCHECK_GE(a, b)
  229. #endif
  230. #define UNREACHABLE(msg) do { \
  231. CHECK(0 && msg); \
  232. Die(); \
  233. } while (0)
  234. #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
  235. #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
  236. #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
  237. #define IMPL_PASTE(a, b) a##b
  238. #define IMPL_COMPILER_ASSERT(pred, line) \
  239. typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
  240. // Limits for integral types. We have to redefine it in case we don't
  241. // have stdint.h (like in Visual Studio 9).
  242. #undef __INT64_C
  243. #undef __UINT64_C
  244. #if SANITIZER_WORDSIZE == 64
  245. # define __INT64_C(c) c ## L
  246. # define __UINT64_C(c) c ## UL
  247. #else
  248. # define __INT64_C(c) c ## LL
  249. # define __UINT64_C(c) c ## ULL
  250. #endif // SANITIZER_WORDSIZE == 64
  251. #undef INT32_MIN
  252. #define INT32_MIN (-2147483647-1)
  253. #undef INT32_MAX
  254. #define INT32_MAX (2147483647)
  255. #undef UINT32_MAX
  256. #define UINT32_MAX (4294967295U)
  257. #undef INT64_MIN
  258. #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
  259. #undef INT64_MAX
  260. #define INT64_MAX (__INT64_C(9223372036854775807))
  261. #undef UINT64_MAX
  262. #define UINT64_MAX (__UINT64_C(18446744073709551615))
  263. enum LinkerInitialized { LINKER_INITIALIZED = 0 };
  264. #if !defined(_MSC_VER) || defined(__clang__)
  265. # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
  266. # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
  267. #else
  268. extern "C" void* _ReturnAddress(void);
  269. # pragma intrinsic(_ReturnAddress)
  270. # define GET_CALLER_PC() (uptr)_ReturnAddress()
  271. // CaptureStackBackTrace doesn't need to know BP on Windows.
  272. // FIXME: This macro is still used when printing error reports though it's not
  273. // clear if the BP value is needed in the ASan reports on Windows.
  274. # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
  275. #endif
  276. #define HANDLE_EINTR(res, f) \
  277. { \
  278. int rverrno; \
  279. do { \
  280. res = (f); \
  281. } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
  282. }
  283. #endif // SANITIZER_DEFS_H