Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef TSAN_DEFS_H |
| 15 | #define TSAN_DEFS_H |
| 16 | |
Alexey Samsonov | 3efd6fc | 2012-06-15 12:24:07 +0000 | [diff] [blame] | 17 | #include "interception/interception.h" |
Alexey Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Alexey Samsonov | 91e1a7e | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 20 | #include "tsan_stat.h" |
| 21 | |
| 22 | #ifndef TSAN_DEBUG |
| 23 | #define TSAN_DEBUG 0 |
| 24 | #endif // TSAN_DEBUG |
| 25 | |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 26 | namespace __tsan { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 27 | |
Dmitry Vyukov | f6985e3 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 28 | const int kTidBits = 13; |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 29 | const unsigned kMaxTid = 1 << kTidBits; |
Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 30 | const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit. |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 31 | const int kClkBits = 43; |
Dmitry Vyukov | de1fd1c | 2012-06-22 11:08:55 +0000 | [diff] [blame^] | 32 | const int kShadowStackSize = 1024; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 33 | |
| 34 | #ifdef TSAN_SHADOW_COUNT |
| 35 | # if TSAN_SHADOW_COUNT == 2 \ |
| 36 | || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8 |
| 37 | const unsigned kShadowCnt = TSAN_SHADOW_COUNT; |
| 38 | # else |
| 39 | # error "TSAN_SHADOW_COUNT must be one of 2,4,8" |
| 40 | # endif |
| 41 | #else |
| 42 | // Count of shadow values in a shadow cell. |
| 43 | const unsigned kShadowCnt = 8; |
| 44 | #endif |
| 45 | |
| 46 | // That many user bytes are mapped onto a single shadow cell. |
| 47 | const unsigned kShadowCell = 8; |
| 48 | |
| 49 | // Size of a single shadow value (u64). |
| 50 | const unsigned kShadowSize = 8; |
| 51 | |
| 52 | #if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS |
| 53 | const bool kCollectStats = true; |
| 54 | #else |
| 55 | const bool kCollectStats = false; |
| 56 | #endif |
| 57 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | #if TSAN_DEBUG |
| 59 | #define DCHECK(a) CHECK(a) |
| 60 | #define DCHECK_EQ(a, b) CHECK_EQ(a, b) |
| 61 | #define DCHECK_NE(a, b) CHECK_NE(a, b) |
| 62 | #define DCHECK_LT(a, b) CHECK_LT(a, b) |
| 63 | #define DCHECK_LE(a, b) CHECK_LE(a, b) |
| 64 | #define DCHECK_GT(a, b) CHECK_GT(a, b) |
| 65 | #define DCHECK_GE(a, b) CHECK_GE(a, b) |
| 66 | #else |
| 67 | #define DCHECK(a) |
| 68 | #define DCHECK_EQ(a, b) |
| 69 | #define DCHECK_NE(a, b) |
| 70 | #define DCHECK_LT(a, b) |
| 71 | #define DCHECK_LE(a, b) |
| 72 | #define DCHECK_GT(a, b) |
| 73 | #define DCHECK_GE(a, b) |
| 74 | #endif |
| 75 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 76 | // The following "build consistency" machinery ensures that all source files |
| 77 | // are built in the same configuration. Inconsistent builds lead to |
| 78 | // hard to debug crashes. |
| 79 | #if TSAN_DEBUG |
| 80 | void build_consistency_debug(); |
| 81 | #else |
| 82 | void build_consistency_release(); |
| 83 | #endif |
| 84 | |
| 85 | #if TSAN_COLLECT_STATS |
| 86 | void build_consistency_stats(); |
| 87 | #else |
| 88 | void build_consistency_nostats(); |
| 89 | #endif |
| 90 | |
| 91 | #if TSAN_SHADOW_COUNT == 1 |
| 92 | void build_consistency_shadow1(); |
| 93 | #elif TSAN_SHADOW_COUNT == 2 |
| 94 | void build_consistency_shadow2(); |
| 95 | #elif TSAN_SHADOW_COUNT == 4 |
| 96 | void build_consistency_shadow4(); |
| 97 | #else |
| 98 | void build_consistency_shadow8(); |
| 99 | #endif |
| 100 | |
| 101 | static inline void USED build_consistency() { |
| 102 | #if TSAN_DEBUG |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 103 | build_consistency_debug(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 104 | #else |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 105 | build_consistency_release(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 106 | #endif |
| 107 | #if TSAN_COLLECT_STATS |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 108 | build_consistency_stats(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 109 | #else |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 110 | build_consistency_nostats(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 111 | #endif |
| 112 | #if TSAN_SHADOW_COUNT == 1 |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 113 | build_consistency_shadow1(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 114 | #elif TSAN_SHADOW_COUNT == 2 |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 115 | build_consistency_shadow2(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 116 | #elif TSAN_SHADOW_COUNT == 4 |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 117 | build_consistency_shadow4(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 118 | #else |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 119 | build_consistency_shadow8(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 120 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | template<typename T> |
| 124 | T min(T a, T b) { |
| 125 | return a < b ? a : b; |
| 126 | } |
| 127 | |
| 128 | template<typename T> |
| 129 | T max(T a, T b) { |
| 130 | return a > b ? a : b; |
| 131 | } |
| 132 | |
| 133 | template<typename T> |
| 134 | T RoundUp(T p, int align) { |
| 135 | DCHECK_EQ(align & (align - 1), 0); |
| 136 | return (T)(((u64)p + align - 1) & ~(align - 1)); |
| 137 | } |
| 138 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 139 | struct MD5Hash { |
| 140 | u64 hash[2]; |
| 141 | bool operator==(const MD5Hash &other) const { |
| 142 | return hash[0] == other.hash[0] && hash[1] == other.hash[1]; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | MD5Hash md5_hash(const void *data, uptr size); |
| 147 | |
| 148 | struct ThreadState; |
| 149 | struct ThreadContext; |
| 150 | struct Context; |
| 151 | struct ReportStack; |
| 152 | class ReportDesc; |
| 153 | class RegionAlloc; |
| 154 | class StackTrace; |
| 155 | |
| 156 | } // namespace __tsan |
| 157 | |
Alexey Samsonov | 3efd6fc | 2012-06-15 12:24:07 +0000 | [diff] [blame] | 158 | DECLARE_REAL(void*, memset, void *ptr, int v, uptr size); |
| 159 | DECLARE_REAL(void*, memcpy, void *dst, const void *src, uptr size); |
| 160 | DECLARE_REAL(int, strncmp, const char *s1, const char *s2, uptr n); |
| 161 | DECLARE_REAL(const char*, strstr, const char *s1, const char *s2); |
| 162 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 163 | #endif // TSAN_DEFS_H |