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 | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_internal_defs.h" |
Alexey Samsonov | 91e1a7e | 2012-06-07 11:54:08 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 19 | #include "tsan_stat.h" |
| 20 | |
| 21 | #ifndef TSAN_DEBUG |
| 22 | #define TSAN_DEBUG 0 |
| 23 | #endif // TSAN_DEBUG |
| 24 | |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 25 | namespace __tsan { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 26 | |
Dmitry Vyukov | 9952b67 | 2012-11-08 11:32:40 +0000 | [diff] [blame] | 27 | #ifdef TSAN_GO |
| 28 | const char *const kTsanOptionsEnv = "GORACE"; |
| 29 | #else |
| 30 | const char *const kTsanOptionsEnv = "TSAN_OPTIONS"; |
| 31 | #endif |
| 32 | |
Dmitry Vyukov | f6985e3 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 33 | const int kTidBits = 13; |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 34 | const unsigned kMaxTid = 1 << kTidBits; |
Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 35 | const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit. |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 36 | const int kClkBits = 43; |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 37 | #ifndef TSAN_GO |
Dmitry Vyukov | 23ecb4a | 2012-09-06 16:11:30 +0000 | [diff] [blame] | 38 | const int kShadowStackSize = 4 * 1024; |
Dmitry Vyukov | c87e728 | 2012-09-06 15:18:14 +0000 | [diff] [blame] | 39 | const int kTraceStackSize = 256; |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 40 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef TSAN_SHADOW_COUNT |
| 43 | # if TSAN_SHADOW_COUNT == 2 \ |
| 44 | || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8 |
Dmitry Vyukov | 1d4120b | 2012-11-06 13:21:06 +0000 | [diff] [blame] | 45 | const uptr kShadowCnt = TSAN_SHADOW_COUNT; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 46 | # else |
| 47 | # error "TSAN_SHADOW_COUNT must be one of 2,4,8" |
| 48 | # endif |
| 49 | #else |
| 50 | // Count of shadow values in a shadow cell. |
Dmitry Vyukov | f34db58 | 2012-11-15 18:44:22 +0000 | [diff] [blame^] | 51 | const uptr kShadowCnt = 4; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | // That many user bytes are mapped onto a single shadow cell. |
Dmitry Vyukov | 1d4120b | 2012-11-06 13:21:06 +0000 | [diff] [blame] | 55 | const uptr kShadowCell = 8; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 56 | |
| 57 | // Size of a single shadow value (u64). |
Dmitry Vyukov | 1d4120b | 2012-11-06 13:21:06 +0000 | [diff] [blame] | 58 | const uptr kShadowSize = 8; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 59 | |
Dmitry Vyukov | c015712 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 60 | // Shadow memory is kShadowMultiplier times larger than user memory. |
| 61 | const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell; |
| 62 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 63 | #if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS |
| 64 | const bool kCollectStats = true; |
| 65 | #else |
| 66 | const bool kCollectStats = false; |
| 67 | #endif |
| 68 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 69 | // The following "build consistency" machinery ensures that all source files |
| 70 | // are built in the same configuration. Inconsistent builds lead to |
| 71 | // hard to debug crashes. |
| 72 | #if TSAN_DEBUG |
| 73 | void build_consistency_debug(); |
| 74 | #else |
| 75 | void build_consistency_release(); |
| 76 | #endif |
| 77 | |
| 78 | #if TSAN_COLLECT_STATS |
| 79 | void build_consistency_stats(); |
| 80 | #else |
| 81 | void build_consistency_nostats(); |
| 82 | #endif |
| 83 | |
| 84 | #if TSAN_SHADOW_COUNT == 1 |
| 85 | void build_consistency_shadow1(); |
| 86 | #elif TSAN_SHADOW_COUNT == 2 |
| 87 | void build_consistency_shadow2(); |
| 88 | #elif TSAN_SHADOW_COUNT == 4 |
| 89 | void build_consistency_shadow4(); |
| 90 | #else |
| 91 | void build_consistency_shadow8(); |
| 92 | #endif |
| 93 | |
| 94 | static inline void USED build_consistency() { |
| 95 | #if TSAN_DEBUG |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 96 | build_consistency_debug(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 97 | #else |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 98 | build_consistency_release(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 99 | #endif |
| 100 | #if TSAN_COLLECT_STATS |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 101 | build_consistency_stats(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 102 | #else |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 103 | build_consistency_nostats(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #if TSAN_SHADOW_COUNT == 1 |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 106 | build_consistency_shadow1(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 107 | #elif TSAN_SHADOW_COUNT == 2 |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 108 | build_consistency_shadow2(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 109 | #elif TSAN_SHADOW_COUNT == 4 |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 110 | build_consistency_shadow4(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 111 | #else |
Dmitry Vyukov | 30c32a8 | 2012-05-24 14:50:33 +0000 | [diff] [blame] | 112 | build_consistency_shadow8(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 113 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | template<typename T> |
| 117 | T min(T a, T b) { |
| 118 | return a < b ? a : b; |
| 119 | } |
| 120 | |
| 121 | template<typename T> |
| 122 | T max(T a, T b) { |
| 123 | return a > b ? a : b; |
| 124 | } |
| 125 | |
| 126 | template<typename T> |
| 127 | T RoundUp(T p, int align) { |
| 128 | DCHECK_EQ(align & (align - 1), 0); |
| 129 | return (T)(((u64)p + align - 1) & ~(align - 1)); |
| 130 | } |
| 131 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 132 | struct MD5Hash { |
| 133 | u64 hash[2]; |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 134 | bool operator==(const MD5Hash &other) const; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | MD5Hash md5_hash(const void *data, uptr size); |
| 138 | |
| 139 | struct ThreadState; |
| 140 | struct ThreadContext; |
| 141 | struct Context; |
| 142 | struct ReportStack; |
| 143 | class ReportDesc; |
| 144 | class RegionAlloc; |
| 145 | class StackTrace; |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 146 | struct MBlock; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 147 | |
| 148 | } // namespace __tsan |
| 149 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 150 | #endif // TSAN_DEFS_H |