tsan_defs.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===-- tsan_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 a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef TSAN_DEFS_H
  13. #define TSAN_DEFS_H
  14. #include "sanitizer_common/sanitizer_internal_defs.h"
  15. #include "sanitizer_common/sanitizer_libc.h"
  16. #include "sanitizer_common/sanitizer_mutex.h"
  17. #include "ubsan/ubsan_platform.h"
  18. #ifndef TSAN_VECTORIZE
  19. # define TSAN_VECTORIZE __SSE4_2__
  20. #endif
  21. #if TSAN_VECTORIZE
  22. // <emmintrin.h> transitively includes <stdlib.h>,
  23. // and it's prohibited to include std headers into tsan runtime.
  24. // So we do this dirty trick.
  25. # define _MM_MALLOC_H_INCLUDED
  26. # define __MM_MALLOC_H
  27. # include <emmintrin.h>
  28. # include <smmintrin.h>
  29. # define VECTOR_ALIGNED ALIGNED(16)
  30. typedef __m128i m128;
  31. #else
  32. # define VECTOR_ALIGNED
  33. #endif
  34. // Setup defaults for compile definitions.
  35. #ifndef TSAN_NO_HISTORY
  36. # define TSAN_NO_HISTORY 0
  37. #endif
  38. #ifndef TSAN_CONTAINS_UBSAN
  39. # if CAN_SANITIZE_UB && !SANITIZER_GO
  40. # define TSAN_CONTAINS_UBSAN 1
  41. # else
  42. # define TSAN_CONTAINS_UBSAN 0
  43. # endif
  44. #endif
  45. namespace __tsan {
  46. constexpr uptr kByteBits = 8;
  47. // Thread slot ID.
  48. enum class Sid : u8 {};
  49. constexpr uptr kThreadSlotCount = 256;
  50. constexpr Sid kFreeSid = static_cast<Sid>(255);
  51. // Abstract time unit, vector clock element.
  52. enum class Epoch : u16 {};
  53. constexpr uptr kEpochBits = 14;
  54. constexpr Epoch kEpochZero = static_cast<Epoch>(0);
  55. constexpr Epoch kEpochOver = static_cast<Epoch>(1 << kEpochBits);
  56. constexpr Epoch kEpochLast = static_cast<Epoch>((1 << kEpochBits) - 1);
  57. inline Epoch EpochInc(Epoch epoch) {
  58. return static_cast<Epoch>(static_cast<u16>(epoch) + 1);
  59. }
  60. inline bool EpochOverflow(Epoch epoch) { return epoch == kEpochOver; }
  61. const uptr kShadowStackSize = 64 * 1024;
  62. // Count of shadow values in a shadow cell.
  63. const uptr kShadowCnt = 4;
  64. // That many user bytes are mapped onto a single shadow cell.
  65. const uptr kShadowCell = 8;
  66. // Single shadow value.
  67. enum class RawShadow : u32 {};
  68. const uptr kShadowSize = sizeof(RawShadow);
  69. // Shadow memory is kShadowMultiplier times larger than user memory.
  70. const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
  71. // That many user bytes are mapped onto a single meta shadow cell.
  72. // Must be less or equal to minimal memory allocator alignment.
  73. const uptr kMetaShadowCell = 8;
  74. // Size of a single meta shadow value (u32).
  75. const uptr kMetaShadowSize = 4;
  76. // All addresses and PCs are assumed to be compressable to that many bits.
  77. const uptr kCompressedAddrBits = 44;
  78. #if TSAN_NO_HISTORY
  79. const bool kCollectHistory = false;
  80. #else
  81. const bool kCollectHistory = true;
  82. #endif
  83. // The following "build consistency" machinery ensures that all source files
  84. // are built in the same configuration. Inconsistent builds lead to
  85. // hard to debug crashes.
  86. #if SANITIZER_DEBUG
  87. void build_consistency_debug();
  88. #else
  89. void build_consistency_release();
  90. #endif
  91. static inline void USED build_consistency() {
  92. #if SANITIZER_DEBUG
  93. build_consistency_debug();
  94. #else
  95. build_consistency_release();
  96. #endif
  97. }
  98. template<typename T>
  99. T min(T a, T b) {
  100. return a < b ? a : b;
  101. }
  102. template<typename T>
  103. T max(T a, T b) {
  104. return a > b ? a : b;
  105. }
  106. template<typename T>
  107. T RoundUp(T p, u64 align) {
  108. DCHECK_EQ(align & (align - 1), 0);
  109. return (T)(((u64)p + align - 1) & ~(align - 1));
  110. }
  111. template<typename T>
  112. T RoundDown(T p, u64 align) {
  113. DCHECK_EQ(align & (align - 1), 0);
  114. return (T)((u64)p & ~(align - 1));
  115. }
  116. // Zeroizes high part, returns 'bits' lsb bits.
  117. template<typename T>
  118. T GetLsb(T v, int bits) {
  119. return (T)((u64)v & ((1ull << bits) - 1));
  120. }
  121. struct MD5Hash {
  122. u64 hash[2];
  123. bool operator==(const MD5Hash &other) const;
  124. };
  125. MD5Hash md5_hash(const void *data, uptr size);
  126. struct Processor;
  127. struct ThreadState;
  128. class ThreadContext;
  129. struct TidSlot;
  130. struct Context;
  131. struct ReportStack;
  132. class ReportDesc;
  133. class RegionAlloc;
  134. struct Trace;
  135. struct TracePart;
  136. typedef uptr AccessType;
  137. enum : AccessType {
  138. kAccessWrite = 0,
  139. kAccessRead = 1 << 0,
  140. kAccessAtomic = 1 << 1,
  141. kAccessVptr = 1 << 2, // read or write of an object virtual table pointer
  142. kAccessFree = 1 << 3, // synthetic memory access during memory freeing
  143. kAccessExternalPC = 1 << 4, // access PC can have kExternalPCBit set
  144. kAccessCheckOnly = 1 << 5, // check for races, but don't store
  145. kAccessNoRodata = 1 << 6, // don't check for .rodata marker
  146. };
  147. // Descriptor of user's memory block.
  148. struct MBlock {
  149. u64 siz : 48;
  150. u64 tag : 16;
  151. StackID stk;
  152. Tid tid;
  153. };
  154. COMPILER_CHECK(sizeof(MBlock) == 16);
  155. enum ExternalTag : uptr {
  156. kExternalTagNone = 0,
  157. kExternalTagSwiftModifyingAccess = 1,
  158. kExternalTagFirstUserAvailable = 2,
  159. kExternalTagMax = 1024,
  160. // Don't set kExternalTagMax over 65,536, since MBlock only stores tags
  161. // as 16-bit values, see tsan_defs.h.
  162. };
  163. enum {
  164. MutexTypeReport = MutexLastCommon,
  165. MutexTypeSyncVar,
  166. MutexTypeAnnotations,
  167. MutexTypeAtExit,
  168. MutexTypeFired,
  169. MutexTypeRacy,
  170. MutexTypeGlobalProc,
  171. MutexTypeInternalAlloc,
  172. MutexTypeTrace,
  173. MutexTypeSlot,
  174. MutexTypeSlots,
  175. };
  176. } // namespace __tsan
  177. #endif // TSAN_DEFS_H