Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_platform.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 | // Platform-specific code. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dmitry Vyukov | 5b266cb | 2012-07-16 13:02:40 +0000 | [diff] [blame] | 15 | #ifndef TSAN_PLATFORM_H |
| 16 | #define TSAN_PLATFORM_H |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 17 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 18 | #if !defined(__LP64__) && !defined(_WIN64) |
| 19 | # error "Only 64-bit is supported" |
| 20 | #endif |
| 21 | |
Dmitry Vyukov | 385542a | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 22 | #include "tsan_defs.h" |
| 23 | #include "tsan_trace.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 24 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 25 | namespace __tsan { |
| 26 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 27 | #if !defined(SANITIZER_GO) |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 28 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 29 | #if defined(__x86_64__) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 30 | /* |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 31 | C/C++ on linux/x86_64 and freebsd/x86_64 |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 32 | 0000 0000 1000 - 0100 0000 0000: main binary and/or MAP_32BIT mappings |
| 33 | 0100 0000 0000 - 0200 0000 0000: - |
| 34 | 0200 0000 0000 - 1000 0000 0000: shadow |
| 35 | 1000 0000 0000 - 3000 0000 0000: - |
| 36 | 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects) |
| 37 | 4000 0000 0000 - 6000 0000 0000: - |
| 38 | 6000 0000 0000 - 6200 0000 0000: traces |
| 39 | 6200 0000 0000 - 7d00 0000 0000: - |
| 40 | 7d00 0000 0000 - 7e00 0000 0000: heap |
| 41 | 7e00 0000 0000 - 7e80 0000 0000: - |
| 42 | 7e80 0000 0000 - 8000 0000 0000: modules and main thread stack |
| 43 | */ |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 44 | const uptr kMetaShadowBeg = 0x300000000000ull; |
| 45 | const uptr kMetaShadowEnd = 0x400000000000ull; |
| 46 | const uptr kTraceMemBeg = 0x600000000000ull; |
| 47 | const uptr kTraceMemEnd = 0x620000000000ull; |
| 48 | const uptr kShadowBeg = 0x020000000000ull; |
| 49 | const uptr kShadowEnd = 0x100000000000ull; |
| 50 | const uptr kHeapMemBeg = 0x7d0000000000ull; |
| 51 | const uptr kHeapMemEnd = 0x7e0000000000ull; |
| 52 | const uptr kLoAppMemBeg = 0x000000001000ull; |
| 53 | const uptr kLoAppMemEnd = 0x010000000000ull; |
| 54 | const uptr kHiAppMemBeg = 0x7e8000000000ull; |
| 55 | const uptr kHiAppMemEnd = 0x800000000000ull; |
| 56 | const uptr kAppMemMsk = 0x7c0000000000ull; |
| 57 | const uptr kAppMemXor = 0x020000000000ull; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 58 | const uptr kVdsoBeg = 0xf000000000000000ull; |
| 59 | #elif defined(__mips64) |
| 60 | /* |
| 61 | C/C++ on linux/mips64 |
| 62 | 0100 0000 00 - 0200 0000 00: main binary |
| 63 | 0200 0000 00 - 1400 0000 00: - |
| 64 | 1400 0000 00 - 2400 0000 00: shadow |
| 65 | 2400 0000 00 - 3000 0000 00: - |
| 66 | 3000 0000 00 - 4000 0000 00: metainfo (memory blocks and sync objects) |
| 67 | 4000 0000 00 - 6000 0000 00: - |
| 68 | 6000 0000 00 - 6200 0000 00: traces |
| 69 | 6200 0000 00 - fe00 0000 00: - |
| 70 | fe00 0000 00 - ff00 0000 00: heap |
| 71 | ff00 0000 00 - ff80 0000 00: - |
| 72 | ff80 0000 00 - ffff ffff ff: modules and main thread stack |
| 73 | */ |
| 74 | const uptr kMetaShadowBeg = 0x3000000000ull; |
| 75 | const uptr kMetaShadowEnd = 0x4000000000ull; |
| 76 | const uptr kTraceMemBeg = 0x6000000000ull; |
| 77 | const uptr kTraceMemEnd = 0x6200000000ull; |
| 78 | const uptr kShadowBeg = 0x1400000000ull; |
| 79 | const uptr kShadowEnd = 0x2400000000ull; |
| 80 | const uptr kHeapMemBeg = 0xfe00000000ull; |
| 81 | const uptr kHeapMemEnd = 0xff00000000ull; |
| 82 | const uptr kLoAppMemBeg = 0x0100000000ull; |
| 83 | const uptr kLoAppMemEnd = 0x0200000000ull; |
| 84 | const uptr kHiAppMemBeg = 0xff80000000ull; |
| 85 | const uptr kHiAppMemEnd = 0xffffffffffull; |
| 86 | const uptr kAppMemMsk = 0xfc00000000ull; |
| 87 | const uptr kAppMemXor = 0x0400000000ull; |
| 88 | const uptr kVdsoBeg = 0xfffff00000ull; |
| 89 | #endif |
Dmitry Vyukov | 385542a | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 90 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 91 | ALWAYS_INLINE |
| 92 | bool IsAppMem(uptr mem) { |
| 93 | return (mem >= kHeapMemBeg && mem < kHeapMemEnd) || |
| 94 | (mem >= kLoAppMemBeg && mem < kLoAppMemEnd) || |
| 95 | (mem >= kHiAppMemBeg && mem < kHiAppMemEnd); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 98 | ALWAYS_INLINE |
| 99 | bool IsShadowMem(uptr mem) { |
| 100 | return mem >= kShadowBeg && mem <= kShadowEnd; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 103 | ALWAYS_INLINE |
| 104 | bool IsMetaMem(uptr mem) { |
| 105 | return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd; |
| 106 | } |
| 107 | |
| 108 | ALWAYS_INLINE |
| 109 | uptr MemToShadow(uptr x) { |
| 110 | DCHECK(IsAppMem(x)); |
| 111 | return (((x) & ~(kAppMemMsk | (kShadowCell - 1))) |
| 112 | ^ kAppMemXor) * kShadowCnt; |
| 113 | } |
| 114 | |
| 115 | ALWAYS_INLINE |
| 116 | u32 *MemToMeta(uptr x) { |
| 117 | DCHECK(IsAppMem(x)); |
| 118 | return (u32*)(((((x) & ~(kAppMemMsk | (kMetaShadowCell - 1))) |
| 119 | ^ kAppMemXor) / kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg); |
| 120 | } |
| 121 | |
| 122 | ALWAYS_INLINE |
| 123 | uptr ShadowToMem(uptr s) { |
| 124 | CHECK(IsShadowMem(s)); |
| 125 | if (s >= MemToShadow(kLoAppMemBeg) && s <= MemToShadow(kLoAppMemEnd - 1)) |
| 126 | return (s / kShadowCnt) ^ kAppMemXor; |
| 127 | else |
| 128 | return ((s / kShadowCnt) ^ kAppMemXor) | kAppMemMsk; |
| 129 | } |
| 130 | |
| 131 | static USED uptr UserRegions[] = { |
| 132 | kLoAppMemBeg, kLoAppMemEnd, |
| 133 | kHiAppMemBeg, kHiAppMemEnd, |
| 134 | kHeapMemBeg, kHeapMemEnd, |
| 135 | }; |
| 136 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 137 | #elif defined(SANITIZER_GO) && !SANITIZER_WINDOWS |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 138 | |
| 139 | /* Go on linux, darwin and freebsd |
| 140 | 0000 0000 1000 - 0000 1000 0000: executable |
| 141 | 0000 1000 0000 - 00c0 0000 0000: - |
| 142 | 00c0 0000 0000 - 00e0 0000 0000: heap |
| 143 | 00e0 0000 0000 - 2000 0000 0000: - |
| 144 | 2000 0000 0000 - 2380 0000 0000: shadow |
| 145 | 2380 0000 0000 - 3000 0000 0000: - |
| 146 | 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects) |
| 147 | 4000 0000 0000 - 6000 0000 0000: - |
| 148 | 6000 0000 0000 - 6200 0000 0000: traces |
| 149 | 6200 0000 0000 - 8000 0000 0000: - |
| 150 | */ |
| 151 | |
| 152 | const uptr kMetaShadowBeg = 0x300000000000ull; |
| 153 | const uptr kMetaShadowEnd = 0x400000000000ull; |
| 154 | const uptr kTraceMemBeg = 0x600000000000ull; |
| 155 | const uptr kTraceMemEnd = 0x620000000000ull; |
| 156 | const uptr kShadowBeg = 0x200000000000ull; |
| 157 | const uptr kShadowEnd = 0x238000000000ull; |
| 158 | const uptr kAppMemBeg = 0x000000001000ull; |
| 159 | const uptr kAppMemEnd = 0x00e000000000ull; |
| 160 | |
| 161 | ALWAYS_INLINE |
| 162 | bool IsAppMem(uptr mem) { |
| 163 | return mem >= kAppMemBeg && mem < kAppMemEnd; |
| 164 | } |
| 165 | |
| 166 | ALWAYS_INLINE |
| 167 | bool IsShadowMem(uptr mem) { |
| 168 | return mem >= kShadowBeg && mem <= kShadowEnd; |
| 169 | } |
| 170 | |
| 171 | ALWAYS_INLINE |
| 172 | bool IsMetaMem(uptr mem) { |
| 173 | return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd; |
| 174 | } |
| 175 | |
| 176 | ALWAYS_INLINE |
| 177 | uptr MemToShadow(uptr x) { |
| 178 | DCHECK(IsAppMem(x)); |
| 179 | return ((x & ~(kShadowCell - 1)) * kShadowCnt) | kShadowBeg; |
| 180 | } |
| 181 | |
| 182 | ALWAYS_INLINE |
| 183 | u32 *MemToMeta(uptr x) { |
| 184 | DCHECK(IsAppMem(x)); |
| 185 | return (u32*)(((x & ~(kMetaShadowCell - 1)) / \ |
| 186 | kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg); |
| 187 | } |
| 188 | |
| 189 | ALWAYS_INLINE |
| 190 | uptr ShadowToMem(uptr s) { |
| 191 | CHECK(IsShadowMem(s)); |
| 192 | return (s & ~kShadowBeg) / kShadowCnt; |
| 193 | } |
| 194 | |
| 195 | static USED uptr UserRegions[] = { |
| 196 | kAppMemBeg, kAppMemEnd, |
| 197 | }; |
| 198 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 199 | #elif defined(SANITIZER_GO) && SANITIZER_WINDOWS |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 200 | |
| 201 | /* Go on windows |
| 202 | 0000 0000 1000 - 0000 1000 0000: executable |
| 203 | 0000 1000 0000 - 00f8 0000 0000: - |
| 204 | 00c0 0000 0000 - 00e0 0000 0000: heap |
| 205 | 00e0 0000 0000 - 0100 0000 0000: - |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 206 | 0100 0000 0000 - 0500 0000 0000: shadow |
| 207 | 0500 0000 0000 - 0560 0000 0000: - |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 208 | 0560 0000 0000 - 0760 0000 0000: traces |
| 209 | 0760 0000 0000 - 07d0 0000 0000: metainfo (memory blocks and sync objects) |
| 210 | 07d0 0000 0000 - 8000 0000 0000: - |
| 211 | */ |
| 212 | |
| 213 | const uptr kMetaShadowBeg = 0x076000000000ull; |
| 214 | const uptr kMetaShadowEnd = 0x07d000000000ull; |
| 215 | const uptr kTraceMemBeg = 0x056000000000ull; |
| 216 | const uptr kTraceMemEnd = 0x076000000000ull; |
| 217 | const uptr kShadowBeg = 0x010000000000ull; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 218 | const uptr kShadowEnd = 0x050000000000ull; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 219 | const uptr kAppMemBeg = 0x000000001000ull; |
| 220 | const uptr kAppMemEnd = 0x00e000000000ull; |
| 221 | |
| 222 | ALWAYS_INLINE |
| 223 | bool IsAppMem(uptr mem) { |
| 224 | return mem >= kAppMemBeg && mem < kAppMemEnd; |
| 225 | } |
| 226 | |
| 227 | ALWAYS_INLINE |
| 228 | bool IsShadowMem(uptr mem) { |
| 229 | return mem >= kShadowBeg && mem <= kShadowEnd; |
| 230 | } |
| 231 | |
| 232 | ALWAYS_INLINE |
| 233 | bool IsMetaMem(uptr mem) { |
| 234 | return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd; |
| 235 | } |
| 236 | |
| 237 | ALWAYS_INLINE |
| 238 | uptr MemToShadow(uptr x) { |
| 239 | DCHECK(IsAppMem(x)); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 240 | return ((x & ~(kShadowCell - 1)) * kShadowCnt) + kShadowBeg; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ALWAYS_INLINE |
| 244 | u32 *MemToMeta(uptr x) { |
| 245 | DCHECK(IsAppMem(x)); |
| 246 | return (u32*)(((x & ~(kMetaShadowCell - 1)) / \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 247 | kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | ALWAYS_INLINE |
| 251 | uptr ShadowToMem(uptr s) { |
| 252 | CHECK(IsShadowMem(s)); |
| 253 | // FIXME(dvyukov): this is most likely wrong as the mapping is not bijection. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 254 | return (s - kShadowBeg) / kShadowCnt; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static USED uptr UserRegions[] = { |
| 258 | kAppMemBeg, kAppMemEnd, |
| 259 | }; |
| 260 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 261 | #else |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 262 | # error "Unknown platform" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 263 | #endif |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 264 | |
| 265 | // The additional page is to catch shadow stack overflow as paging fault. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 266 | // Windows wants 64K alignment for mmaps. |
| 267 | const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace) |
| 268 | + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1); |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 269 | |
Timur Iskhodzhanov | abfdbdf | 2013-03-28 18:52:40 +0000 | [diff] [blame] | 270 | uptr ALWAYS_INLINE GetThreadTrace(int tid) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 271 | uptr p = kTraceMemBeg + (uptr)tid * kTotalTraceSize; |
| 272 | DCHECK_LT(p, kTraceMemEnd); |
Dmitry Vyukov | 9743d74 | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 273 | return p; |
| 274 | } |
| 275 | |
Timur Iskhodzhanov | abfdbdf | 2013-03-28 18:52:40 +0000 | [diff] [blame] | 276 | uptr ALWAYS_INLINE GetThreadTraceHeader(int tid) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 277 | uptr p = kTraceMemBeg + (uptr)tid * kTotalTraceSize |
Dmitry Vyukov | 01a7ce8 | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 278 | + kTraceSize * sizeof(Event); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 279 | DCHECK_LT(p, kTraceMemEnd); |
Dmitry Vyukov | 385542a | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 280 | return p; |
| 281 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 282 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 283 | void InitializePlatform(); |
| 284 | void FlushShadowMemory(); |
| 285 | void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 286 | |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 287 | // Says whether the addr relates to a global var. |
| 288 | // Guesses with high probability, may yield both false positives and negatives. |
| 289 | bool IsGlobalVar(uptr addr); |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 290 | int ExtractResolvFDs(void *state, int *fds, int nfd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 291 | int ExtractRecvmsgFDs(void *msg, int *fds, int nfd); |
| 292 | |
| 293 | int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m, |
| 294 | void *abstime), void *c, void *m, void *abstime, |
| 295 | void(*cleanup)(void *arg), void *arg); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 296 | |
| 297 | } // namespace __tsan |
| 298 | |
Dmitry Vyukov | 5b266cb | 2012-07-16 13:02:40 +0000 | [diff] [blame] | 299 | #endif // TSAN_PLATFORM_H |