sanitizer_internal_defs.h 15 KB

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