sanitizer_internal_defs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //===-- sanitizer_internal_defs.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 shared between AddressSanitizer and ThreadSanitizer.
  10. // It contains macro used in run-time libraries code.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_DEFS_H
  13. #define SANITIZER_DEFS_H
  14. #include "sanitizer_platform.h"
  15. #include "sanitizer_redefine_builtins.h"
  16. // GCC does not understand __has_feature.
  17. #if !defined(__has_feature)
  18. #define __has_feature(x) 0
  19. #endif
  20. #ifndef SANITIZER_DEBUG
  21. # define SANITIZER_DEBUG 0
  22. #endif
  23. #define SANITIZER_STRINGIFY_(S) #S
  24. #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)
  25. // Only use SANITIZER_*ATTRIBUTE* before the function return type!
  26. #if SANITIZER_WINDOWS
  27. #if SANITIZER_IMPORT_INTERFACE
  28. # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
  29. #else
  30. # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
  31. #endif
  32. # define SANITIZER_WEAK_ATTRIBUTE
  33. # define SANITIZER_WEAK_IMPORT
  34. #elif SANITIZER_GO
  35. # define SANITIZER_INTERFACE_ATTRIBUTE
  36. # define SANITIZER_WEAK_ATTRIBUTE
  37. # define SANITIZER_WEAK_IMPORT
  38. #else
  39. # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
  40. # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
  41. # if SANITIZER_APPLE
  42. # define SANITIZER_WEAK_IMPORT extern "C" __attribute((weak_import))
  43. # else
  44. # define SANITIZER_WEAK_IMPORT extern "C" SANITIZER_WEAK_ATTRIBUTE
  45. # endif // SANITIZER_APPLE
  46. #endif // SANITIZER_WINDOWS
  47. //--------------------------- WEAK FUNCTIONS ---------------------------------//
  48. // When working with weak functions, to simplify the code and make it more
  49. // portable, when possible define a default implementation using this macro:
  50. //
  51. // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
  52. //
  53. // For example:
  54. // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
  55. //
  56. #if SANITIZER_WINDOWS
  57. #include "sanitizer_win_defs.h"
  58. # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
  59. WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
  60. #else
  61. # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
  62. extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
  63. ReturnType Name(__VA_ARGS__)
  64. #endif
  65. // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
  66. // will evaluate to a null pointer when not defined.
  67. #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
  68. #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
  69. # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
  70. // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
  71. // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
  72. // by Xcode >= 4.5.
  73. #elif SANITIZER_APPLE && \
  74. __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
  75. # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
  76. #else
  77. # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
  78. #endif
  79. #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
  80. // For some weak hooks that will be called very often and we want to avoid the
  81. // overhead of executing the default implementation when it is not necessary,
  82. // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
  83. // implementation for platforms that doesn't support weak symbols. For example:
  84. //
  85. // #if !SANITIZER_SUPPORT_WEAK_HOOKS
  86. // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
  87. // return a > b;
  88. // }
  89. // #endif
  90. //
  91. // And then use it as: if (compare_hook) compare_hook(a, b);
  92. //----------------------------------------------------------------------------//
  93. // We can use .preinit_array section on Linux to call sanitizer initialization
  94. // functions very early in the process startup (unless PIC macro is defined).
  95. //
  96. // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
  97. // lock held. It will lead to dead lock if unresolved PLT functions (which helds
  98. // rtld_bind_lock reader lock) are called inside .preinit_array functions.
  99. //
  100. // FIXME: do we have anything like this on Mac?
  101. #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
  102. #if (SANITIZER_LINUX || SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)
  103. #define SANITIZER_CAN_USE_PREINIT_ARRAY 1
  104. // Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
  105. // FIXME: Check for those conditions.
  106. #elif SANITIZER_SOLARIS && !defined(PIC)
  107. # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
  108. #else
  109. # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
  110. #endif
  111. #endif // SANITIZER_CAN_USE_PREINIT_ARRAY
  112. // GCC does not understand __has_feature
  113. #if !defined(__has_feature)
  114. # define __has_feature(x) 0
  115. #endif
  116. // Older GCCs do not understand __has_attribute.
  117. #if !defined(__has_attribute)
  118. # define __has_attribute(x) 0
  119. #endif
  120. #if !defined(__has_cpp_attribute)
  121. # define __has_cpp_attribute(x) 0
  122. #endif
  123. // For portability reasons we do not include stddef.h, stdint.h or any other
  124. // system header, but we do need some basic types that are not defined
  125. // in a portable way by the language itself.
  126. namespace __sanitizer {
  127. #if defined(_WIN64)
  128. // 64-bit Windows uses LLP64 data model.
  129. typedef unsigned long long uptr;
  130. typedef signed long long sptr;
  131. #else
  132. # if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE || SANITIZER_WINDOWS
  133. typedef unsigned long uptr;
  134. typedef signed long sptr;
  135. # else
  136. typedef unsigned int uptr;
  137. typedef signed int sptr;
  138. # endif
  139. #endif // defined(_WIN64)
  140. #if defined(__x86_64__)
  141. // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
  142. // 64-bit pointer to unwind stack frame.
  143. typedef unsigned long long uhwptr;
  144. #else
  145. typedef uptr uhwptr;
  146. #endif
  147. typedef unsigned char u8;
  148. typedef unsigned short u16;
  149. typedef unsigned int u32;
  150. typedef unsigned long long u64;
  151. typedef signed char s8;
  152. typedef signed short s16;
  153. typedef signed int s32;
  154. typedef signed long long s64;
  155. #if SANITIZER_WINDOWS
  156. // On Windows, files are HANDLE, which is a synonim of void*.
  157. // Use void* to avoid including <windows.h> everywhere.
  158. typedef void* fd_t;
  159. typedef unsigned error_t;
  160. #else
  161. typedef int fd_t;
  162. typedef int error_t;
  163. #endif
  164. #if SANITIZER_SOLARIS && !defined(_LP64)
  165. typedef long pid_t;
  166. #else
  167. typedef int pid_t;
  168. #endif
  169. #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE || \
  170. (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
  171. (SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID) || \
  172. (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__)))
  173. typedef u64 OFF_T;
  174. #else
  175. typedef uptr OFF_T;
  176. #endif
  177. typedef u64 OFF64_T;
  178. #if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE
  179. typedef uptr operator_new_size_type;
  180. #else
  181. # if defined(__s390__) && !defined(__s390x__)
  182. // Special case: 31-bit s390 has unsigned long as size_t.
  183. typedef unsigned long operator_new_size_type;
  184. # else
  185. typedef u32 operator_new_size_type;
  186. # endif
  187. #endif
  188. typedef u64 tid_t;
  189. // ----------- ATTENTION -------------
  190. // This header should NOT include any other headers to avoid portability issues.
  191. // Common defs.
  192. #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
  193. #define SANITIZER_WEAK_DEFAULT_IMPL \
  194. extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
  195. #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
  196. extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
  197. // Platform-specific defs.
  198. #if defined(_MSC_VER)
  199. # define ALWAYS_INLINE __forceinline
  200. // FIXME(timurrrr): do we need this on Windows?
  201. # define ALIAS(x)
  202. # define ALIGNED(x) __declspec(align(x))
  203. # define FORMAT(f, a)
  204. # define NOINLINE __declspec(noinline)
  205. # define NORETURN __declspec(noreturn)
  206. # define THREADLOCAL __declspec(thread)
  207. # define LIKELY(x) (x)
  208. # define UNLIKELY(x) (x)
  209. # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
  210. # define WARN_UNUSED_RESULT
  211. #else // _MSC_VER
  212. # define ALWAYS_INLINE inline __attribute__((always_inline))
  213. # define ALIAS(x) __attribute__((alias(SANITIZER_STRINGIFY(x))))
  214. // Please only use the ALIGNED macro before the type.
  215. // Using ALIGNED after the variable declaration is not portable!
  216. # define ALIGNED(x) __attribute__((aligned(x)))
  217. # define FORMAT(f, a) __attribute__((format(printf, f, a)))
  218. # define NOINLINE __attribute__((noinline))
  219. # define NORETURN __attribute__((noreturn))
  220. # define THREADLOCAL __thread
  221. # define LIKELY(x) __builtin_expect(!!(x), 1)
  222. # define UNLIKELY(x) __builtin_expect(!!(x), 0)
  223. # if defined(__i386__) || defined(__x86_64__)
  224. // __builtin_prefetch(x) generates prefetchnt0 on x86
  225. # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
  226. # else
  227. # define PREFETCH(x) __builtin_prefetch(x)
  228. # endif
  229. # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  230. #endif // _MSC_VER
  231. #if !defined(_MSC_VER) || defined(__clang__)
  232. # define UNUSED __attribute__((unused))
  233. # define USED __attribute__((used))
  234. #else
  235. # define UNUSED
  236. # define USED
  237. #endif
  238. #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
  239. # define NOEXCEPT noexcept
  240. #else
  241. # define NOEXCEPT throw()
  242. #endif
  243. #if __has_cpp_attribute(clang::fallthrough)
  244. # define FALLTHROUGH [[clang::fallthrough]]
  245. #elif __has_cpp_attribute(fallthrough)
  246. # define FALLTHROUGH [[fallthrough]]
  247. #else
  248. # define FALLTHROUGH
  249. #endif
  250. #if __has_attribute(uninitialized)
  251. # define UNINITIALIZED __attribute__((uninitialized))
  252. #else
  253. # define UNINITIALIZED
  254. #endif
  255. // Unaligned versions of basic types.
  256. typedef ALIGNED(1) u16 uu16;
  257. typedef ALIGNED(1) u32 uu32;
  258. typedef ALIGNED(1) u64 uu64;
  259. typedef ALIGNED(1) s16 us16;
  260. typedef ALIGNED(1) s32 us32;
  261. typedef ALIGNED(1) s64 us64;
  262. #if SANITIZER_WINDOWS
  263. } // namespace __sanitizer
  264. typedef unsigned long DWORD;
  265. namespace __sanitizer {
  266. typedef DWORD thread_return_t;
  267. # define THREAD_CALLING_CONV __stdcall
  268. #else // _WIN32
  269. typedef void* thread_return_t;
  270. # define THREAD_CALLING_CONV
  271. #endif // _WIN32
  272. typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
  273. // NOTE: Functions below must be defined in each run-time.
  274. void NORETURN Die();
  275. void NORETURN CheckFailed(const char *file, int line, const char *cond,
  276. u64 v1, u64 v2);
  277. // Check macro
  278. #define RAW_CHECK_MSG(expr, msg, ...) \
  279. do { \
  280. if (UNLIKELY(!(expr))) { \
  281. const char* msgs[] = {msg, __VA_ARGS__}; \
  282. for (const char* m : msgs) RawWrite(m); \
  283. Die(); \
  284. } \
  285. } while (0)
  286. #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr "\n", )
  287. #define RAW_CHECK_VA(expr, ...) RAW_CHECK_MSG(expr, #expr "\n", __VA_ARGS__)
  288. #define CHECK_IMPL(c1, op, c2) \
  289. do { \
  290. __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
  291. __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
  292. if (UNLIKELY(!(v1 op v2))) \
  293. __sanitizer::CheckFailed(__FILE__, __LINE__, \
  294. "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
  295. } while (false) \
  296. /**/
  297. #define CHECK(a) CHECK_IMPL((a), !=, 0)
  298. #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
  299. #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
  300. #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
  301. #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
  302. #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
  303. #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
  304. #if SANITIZER_DEBUG
  305. #define DCHECK(a) CHECK(a)
  306. #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
  307. #define DCHECK_NE(a, b) CHECK_NE(a, b)
  308. #define DCHECK_LT(a, b) CHECK_LT(a, b)
  309. #define DCHECK_LE(a, b) CHECK_LE(a, b)
  310. #define DCHECK_GT(a, b) CHECK_GT(a, b)
  311. #define DCHECK_GE(a, b) CHECK_GE(a, b)
  312. #else
  313. #define DCHECK(a)
  314. #define DCHECK_EQ(a, b)
  315. #define DCHECK_NE(a, b)
  316. #define DCHECK_LT(a, b)
  317. #define DCHECK_LE(a, b)
  318. #define DCHECK_GT(a, b)
  319. #define DCHECK_GE(a, b)
  320. #endif
  321. #define UNREACHABLE(msg) do { \
  322. CHECK(0 && msg); \
  323. Die(); \
  324. } while (0)
  325. #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
  326. #define COMPILER_CHECK(pred) static_assert(pred, "")
  327. #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
  328. // Limits for integral types. We have to redefine it in case we don't
  329. // have stdint.h (like in Visual Studio 9).
  330. #undef __INT64_C
  331. #undef __UINT64_C
  332. #if SANITIZER_WORDSIZE == 64
  333. # define __INT64_C(c) c ## L
  334. # define __UINT64_C(c) c ## UL
  335. #else
  336. # define __INT64_C(c) c ## LL
  337. # define __UINT64_C(c) c ## ULL
  338. #endif // SANITIZER_WORDSIZE == 64
  339. #undef INT32_MIN
  340. #define INT32_MIN (-2147483647-1)
  341. #undef INT32_MAX
  342. #define INT32_MAX (2147483647)
  343. #undef UINT32_MAX
  344. #define UINT32_MAX (4294967295U)
  345. #undef INT64_MIN
  346. #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
  347. #undef INT64_MAX
  348. #define INT64_MAX (__INT64_C(9223372036854775807))
  349. #undef UINT64_MAX
  350. #define UINT64_MAX (__UINT64_C(18446744073709551615))
  351. #undef UINTPTR_MAX
  352. #if SANITIZER_WORDSIZE == 64
  353. # define UINTPTR_MAX (18446744073709551615UL)
  354. #else
  355. # define UINTPTR_MAX (4294967295U)
  356. #endif // SANITIZER_WORDSIZE == 64
  357. enum LinkerInitialized { LINKER_INITIALIZED = 0 };
  358. #if !defined(_MSC_VER) || defined(__clang__)
  359. # define GET_CALLER_PC() \
  360. ((__sanitizer::uptr)__builtin_extract_return_addr( \
  361. __builtin_return_address(0)))
  362. # define GET_CURRENT_FRAME() ((__sanitizer::uptr)__builtin_frame_address(0))
  363. inline void Trap() {
  364. __builtin_trap();
  365. }
  366. #else
  367. extern "C" void* _ReturnAddress(void);
  368. extern "C" void* _AddressOfReturnAddress(void);
  369. # pragma intrinsic(_ReturnAddress)
  370. # pragma intrinsic(_AddressOfReturnAddress)
  371. # define GET_CALLER_PC() ((__sanitizer::uptr)_ReturnAddress())
  372. // CaptureStackBackTrace doesn't need to know BP on Windows.
  373. # define GET_CURRENT_FRAME() \
  374. (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
  375. extern "C" void __ud2(void);
  376. # pragma intrinsic(__ud2)
  377. inline void Trap() {
  378. __ud2();
  379. }
  380. #endif
  381. #define HANDLE_EINTR(res, f) \
  382. { \
  383. int rverrno; \
  384. do { \
  385. res = (f); \
  386. } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
  387. }
  388. // Forces the compiler to generate a frame pointer in the function.
  389. #define ENABLE_FRAME_POINTER \
  390. do { \
  391. volatile __sanitizer::uptr enable_fp; \
  392. enable_fp = GET_CURRENT_FRAME(); \
  393. (void)enable_fp; \
  394. } while (0)
  395. // Internal thread identifier allocated by ThreadRegistry.
  396. typedef u32 Tid;
  397. constexpr Tid kInvalidTid = -1;
  398. constexpr Tid kMainTid = 0;
  399. // Stack depot stack identifier.
  400. typedef u32 StackID;
  401. const StackID kInvalidStackID = 0;
  402. } // namespace __sanitizer
  403. namespace __asan {
  404. using namespace __sanitizer;
  405. }
  406. namespace __dsan {
  407. using namespace __sanitizer;
  408. }
  409. namespace __dfsan {
  410. using namespace __sanitizer;
  411. }
  412. namespace __lsan {
  413. using namespace __sanitizer;
  414. }
  415. namespace __msan {
  416. using namespace __sanitizer;
  417. }
  418. namespace __hwasan {
  419. using namespace __sanitizer;
  420. }
  421. namespace __tsan {
  422. using namespace __sanitizer;
  423. }
  424. namespace __scudo {
  425. using namespace __sanitizer;
  426. }
  427. namespace __ubsan {
  428. using namespace __sanitizer;
  429. }
  430. namespace __xray {
  431. using namespace __sanitizer;
  432. }
  433. namespace __interception {
  434. using namespace __sanitizer;
  435. }
  436. namespace __hwasan {
  437. using namespace __sanitizer;
  438. }
  439. namespace __memprof {
  440. using namespace __sanitizer;
  441. }
  442. #endif // SANITIZER_DEFS_H