Alexey Samsonov | 3b2f9f4 | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_rtl.cc -------------------------------------------------------===// |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 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 | // Main file (entry points) for the TSan run-time. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dmitry Vyukov | 6fa46f7 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_atomic.h" |
Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_common.h" |
Kostya Serebryany | c5bea20 | 2012-05-31 13:42:53 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 8bd9098 | 2012-06-07 09:50:16 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 19 | #include "tsan_defs.h" |
| 20 | #include "tsan_platform.h" |
| 21 | #include "tsan_rtl.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 22 | #include "tsan_mman.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 23 | #include "tsan_suppressions.h" |
| 24 | |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 25 | volatile int __tsan_resumed = 0; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 26 | |
| 27 | extern "C" void __tsan_resume() { |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 28 | __tsan_resumed = 1; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | namespace __tsan { |
| 32 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 33 | #ifndef TSAN_GO |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 34 | THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64); |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 35 | char allocator_placeholder[sizeof(Allocator)] ALIGNED(64); |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 36 | #endif |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 37 | static char ctx_placeholder[sizeof(Context)] ALIGNED(64); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 38 | |
| 39 | static Context *ctx; |
| 40 | Context *CTX() { |
| 41 | return ctx; |
| 42 | } |
| 43 | |
| 44 | Context::Context() |
| 45 | : initialized() |
| 46 | , report_mtx(MutexTypeReport, StatMtxReport) |
| 47 | , nreported() |
| 48 | , nmissed_expected() |
| 49 | , thread_mtx(MutexTypeThreads, StatMtxThreads) |
| 50 | , racy_stacks(MBlockRacyStacks) |
| 51 | , racy_addresses(MBlockRacyAddresses) { |
| 52 | } |
| 53 | |
| 54 | // The objects are allocated in TLS, so one may rely on zero-initialization. |
| 55 | ThreadState::ThreadState(Context *ctx, int tid, u64 epoch, |
| 56 | uptr stk_addr, uptr stk_size, |
| 57 | uptr tls_addr, uptr tls_size) |
| 58 | : fast_state(tid, epoch) |
| 59 | // Do not touch these, rely on zero initialization, |
| 60 | // they may be accessed before the ctor. |
| 61 | // , fast_ignore_reads() |
| 62 | // , fast_ignore_writes() |
| 63 | // , in_rtl() |
| 64 | , shadow_stack_pos(&shadow_stack[0]) |
| 65 | , tid(tid) |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 66 | , stk_addr(stk_addr) |
| 67 | , stk_size(stk_size) |
| 68 | , tls_addr(tls_addr) |
| 69 | , tls_size(tls_size) { |
| 70 | } |
| 71 | |
| 72 | ThreadContext::ThreadContext(int tid) |
| 73 | : tid(tid) |
| 74 | , unique_id() |
| 75 | , user_id() |
| 76 | , thr() |
| 77 | , status(ThreadStatusInvalid) |
| 78 | , detached() |
| 79 | , reuse_count() |
| 80 | , epoch0() |
| 81 | , epoch1() |
Dmitry Vyukov | f6985e3 | 2012-05-22 14:34:43 +0000 | [diff] [blame] | 82 | , dead_info() |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 83 | , dead_next() { |
| 84 | } |
| 85 | |
Dmitry Vyukov | 15710c9 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 86 | static void WriteMemoryProfile(char *buf, uptr buf_size, int num) { |
| 87 | uptr shadow = GetShadowMemoryConsumption(); |
| 88 | |
| 89 | int nthread = 0; |
| 90 | int nlivethread = 0; |
| 91 | uptr threadmem = 0; |
| 92 | { |
| 93 | Lock l(&ctx->thread_mtx); |
| 94 | for (unsigned i = 0; i < kMaxTid; i++) { |
| 95 | ThreadContext *tctx = ctx->threads[i]; |
| 96 | if (tctx == 0) |
| 97 | continue; |
| 98 | nthread += 1; |
| 99 | threadmem += sizeof(ThreadContext); |
| 100 | if (tctx->status != ThreadStatusRunning) |
| 101 | continue; |
| 102 | nlivethread += 1; |
| 103 | threadmem += sizeof(ThreadState); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | uptr nsync = 0; |
| 108 | uptr syncmem = CTX()->synctab.GetMemoryConsumption(&nsync); |
| 109 | |
Alexey Samsonov | e1cb524 | 2012-06-19 09:21:57 +0000 | [diff] [blame] | 110 | internal_snprintf(buf, buf_size, "%d: shadow=%zuMB" |
| 111 | " thread=%zuMB(total=%d/live=%d)" |
| 112 | " sync=%zuMB(cnt=%zu)\n", |
Dmitry Vyukov | 15710c9 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 113 | num, |
| 114 | shadow >> 20, |
| 115 | threadmem >> 20, nthread, nlivethread, |
| 116 | syncmem >> 20, nsync); |
| 117 | } |
| 118 | |
| 119 | static void MemoryProfileThread(void *arg) { |
| 120 | ScopedInRtl in_rtl; |
| 121 | fd_t fd = (fd_t)(uptr)arg; |
| 122 | for (int i = 0; ; i++) { |
| 123 | InternalScopedBuf<char> buf(4096); |
| 124 | WriteMemoryProfile(buf.Ptr(), buf.Size(), i); |
| 125 | internal_write(fd, buf.Ptr(), internal_strlen(buf.Ptr())); |
Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 126 | SleepForSeconds(1); |
Dmitry Vyukov | 15710c9 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | static void InitializeMemoryProfile() { |
| 131 | if (flags()->profile_memory == 0 || flags()->profile_memory[0] == 0) |
| 132 | return; |
| 133 | InternalScopedBuf<char> filename(4096); |
Alexey Samsonov | e1cb524 | 2012-06-19 09:21:57 +0000 | [diff] [blame] | 134 | internal_snprintf(filename.Ptr(), filename.Size(), "%s.%d", |
Dmitry Vyukov | 15710c9 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 135 | flags()->profile_memory, GetPid()); |
| 136 | fd_t fd = internal_open(filename.Ptr(), true); |
| 137 | if (fd == kInvalidFd) { |
Alexey Samsonov | ac4c290 | 2012-06-06 10:13:27 +0000 | [diff] [blame] | 138 | TsanPrintf("Failed to open memory profile file '%s'\n", &filename[0]); |
Dmitry Vyukov | 15710c9 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 139 | Die(); |
| 140 | } |
| 141 | internal_start_thread(&MemoryProfileThread, (void*)(uptr)fd); |
| 142 | } |
| 143 | |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 144 | static void MemoryFlushThread(void *arg) { |
| 145 | ScopedInRtl in_rtl; |
| 146 | for (int i = 0; ; i++) { |
Alexey Samsonov | 58a3c58 | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 147 | SleepForMillis(flags()->flush_memory_ms); |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 148 | FlushShadowMemory(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static void InitializeMemoryFlush() { |
| 153 | if (flags()->flush_memory_ms == 0) |
| 154 | return; |
| 155 | if (flags()->flush_memory_ms < 100) |
| 156 | flags()->flush_memory_ms = 100; |
| 157 | internal_start_thread(&MemoryFlushThread, 0); |
| 158 | } |
| 159 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 160 | void Initialize(ThreadState *thr) { |
| 161 | // Thread safe because done before all threads exist. |
| 162 | static bool is_initialized = false; |
| 163 | if (is_initialized) |
| 164 | return; |
| 165 | is_initialized = true; |
| 166 | ScopedInRtl in_rtl; |
Dmitry Vyukov | 9f143c5 | 2012-08-16 19:36:45 +0000 | [diff] [blame^] | 167 | #ifndef TSAN_GO |
Dmitry Vyukov | 954fc8c | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 168 | InitializeAllocator(); |
Dmitry Vyukov | 9f143c5 | 2012-08-16 19:36:45 +0000 | [diff] [blame^] | 169 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 170 | InitializeInterceptors(); |
| 171 | const char *env = InitializePlatform(); |
| 172 | InitializeMutex(); |
| 173 | InitializeDynamicAnnotations(); |
| 174 | ctx = new(ctx_placeholder) Context; |
| 175 | InitializeShadowMemory(); |
| 176 | ctx->dead_list_size = 0; |
| 177 | ctx->dead_list_head = 0; |
| 178 | ctx->dead_list_tail = 0; |
| 179 | InitializeFlags(&ctx->flags, env); |
| 180 | InitializeSuppressions(); |
Dmitry Vyukov | 15710c9 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 181 | InitializeMemoryProfile(); |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 182 | InitializeMemoryFlush(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 183 | |
| 184 | if (ctx->flags.verbosity) |
Alexey Samsonov | ac4c290 | 2012-06-06 10:13:27 +0000 | [diff] [blame] | 185 | TsanPrintf("***** Running under ThreadSanitizer v2 (pid %d) *****\n", |
| 186 | GetPid()); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 187 | |
| 188 | // Initialize thread 0. |
| 189 | ctx->thread_seq = 0; |
| 190 | int tid = ThreadCreate(thr, 0, 0, true); |
| 191 | CHECK_EQ(tid, 0); |
| 192 | ThreadStart(thr, tid); |
| 193 | CHECK_EQ(thr->in_rtl, 1); |
| 194 | ctx->initialized = true; |
| 195 | |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 196 | if (flags()->stop_on_start) { |
Alexey Samsonov | ac4c290 | 2012-06-06 10:13:27 +0000 | [diff] [blame] | 197 | TsanPrintf("ThreadSanitizer is suspended at startup (pid %d)." |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 198 | " Call __tsan_resume().\n", |
| 199 | GetPid()); |
| 200 | while (__tsan_resumed == 0); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | int Finalize(ThreadState *thr) { |
| 205 | ScopedInRtl in_rtl; |
| 206 | Context *ctx = __tsan::ctx; |
| 207 | bool failed = false; |
| 208 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 209 | ThreadFinalize(thr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 210 | |
| 211 | if (ctx->nreported) { |
| 212 | failed = true; |
Alexey Samsonov | ac4c290 | 2012-06-06 10:13:27 +0000 | [diff] [blame] | 213 | TsanPrintf("ThreadSanitizer: reported %d warnings\n", ctx->nreported); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | if (ctx->nmissed_expected) { |
| 217 | failed = true; |
Alexey Samsonov | ac4c290 | 2012-06-06 10:13:27 +0000 | [diff] [blame] | 218 | TsanPrintf("ThreadSanitizer: missed %d expected races\n", |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 219 | ctx->nmissed_expected); |
| 220 | } |
| 221 | |
| 222 | StatOutput(ctx->stat); |
Dmitry Vyukov | 19b855f | 2012-05-17 15:00:27 +0000 | [diff] [blame] | 223 | return failed ? flags()->exitcode : 0; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 226 | void TraceSwitch(ThreadState *thr) { |
Dmitry Vyukov | de1fd1c | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 227 | thr->nomalloc++; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 228 | ScopedInRtl in_rtl; |
| 229 | Lock l(&thr->trace.mtx); |
| 230 | unsigned trace = (thr->fast_state.epoch() / kTracePartSize) % kTraceParts; |
| 231 | TraceHeader *hdr = &thr->trace.headers[trace]; |
| 232 | hdr->epoch0 = thr->fast_state.epoch(); |
| 233 | hdr->stack0.ObtainCurrent(thr, 0); |
Dmitry Vyukov | de1fd1c | 2012-06-22 11:08:55 +0000 | [diff] [blame] | 234 | thr->nomalloc--; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 237 | #ifndef TSAN_GO |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 238 | extern "C" void __tsan_trace_switch() { |
| 239 | TraceSwitch(cur_thread()); |
| 240 | } |
| 241 | |
| 242 | extern "C" void __tsan_report_race() { |
| 243 | ReportRace(cur_thread()); |
| 244 | } |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 245 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 246 | |
| 247 | ALWAYS_INLINE |
| 248 | static Shadow LoadShadow(u64 *p) { |
| 249 | u64 raw = atomic_load((atomic_uint64_t*)p, memory_order_relaxed); |
| 250 | return Shadow(raw); |
| 251 | } |
| 252 | |
| 253 | ALWAYS_INLINE |
| 254 | static void StoreShadow(u64 *sp, u64 s) { |
| 255 | atomic_store((atomic_uint64_t*)sp, s, memory_order_relaxed); |
| 256 | } |
| 257 | |
| 258 | ALWAYS_INLINE |
| 259 | static void StoreIfNotYetStored(u64 *sp, u64 *s) { |
| 260 | StoreShadow(sp, *s); |
| 261 | *s = 0; |
| 262 | } |
| 263 | |
| 264 | static inline void HandleRace(ThreadState *thr, u64 *shadow_mem, |
| 265 | Shadow cur, Shadow old) { |
| 266 | thr->racy_state[0] = cur.raw(); |
| 267 | thr->racy_state[1] = old.raw(); |
| 268 | thr->racy_shadow_addr = shadow_mem; |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 269 | #ifndef TSAN_GO |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 270 | HACKY_CALL(__tsan_report_race); |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 271 | #else |
| 272 | ReportRace(thr); |
| 273 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static inline bool BothReads(Shadow s, int kAccessIsWrite) { |
| 277 | return !kAccessIsWrite && !s.is_write(); |
| 278 | } |
| 279 | |
| 280 | static inline bool OldIsRWStronger(Shadow old, int kAccessIsWrite) { |
| 281 | return old.is_write() || !kAccessIsWrite; |
| 282 | } |
| 283 | |
| 284 | static inline bool OldIsRWWeaker(Shadow old, int kAccessIsWrite) { |
| 285 | return !old.is_write() || kAccessIsWrite; |
| 286 | } |
| 287 | |
| 288 | static inline bool OldIsInSameSynchEpoch(Shadow old, ThreadState *thr) { |
| 289 | return old.epoch() >= thr->fast_synch_epoch; |
| 290 | } |
| 291 | |
| 292 | static inline bool HappensBefore(Shadow old, ThreadState *thr) { |
| 293 | return thr->clock.get(old.tid()) >= old.epoch(); |
| 294 | } |
| 295 | |
| 296 | ALWAYS_INLINE |
| 297 | void MemoryAccessImpl(ThreadState *thr, uptr addr, |
| 298 | int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state, |
| 299 | u64 *shadow_mem, Shadow cur) { |
| 300 | StatInc(thr, StatMop); |
| 301 | StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead); |
| 302 | StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog)); |
| 303 | |
| 304 | // This potentially can live in an MMX/SSE scratch register. |
| 305 | // The required intrinsics are: |
| 306 | // __m128i _mm_move_epi64(__m128i*); |
| 307 | // _mm_storel_epi64(u64*, __m128i); |
| 308 | u64 store_word = cur.raw(); |
| 309 | |
| 310 | // scan all the shadow values and dispatch to 4 categories: |
| 311 | // same, replace, candidate and race (see comments below). |
| 312 | // we consider only 3 cases regarding access sizes: |
| 313 | // equal, intersect and not intersect. initially I considered |
| 314 | // larger and smaller as well, it allowed to replace some |
| 315 | // 'candidates' with 'same' or 'replace', but I think |
| 316 | // it's just not worth it (performance- and complexity-wise). |
| 317 | |
| 318 | Shadow old(0); |
| 319 | if (kShadowCnt == 1) { |
| 320 | int idx = 0; |
| 321 | #include "tsan_update_shadow_word_inl.h" |
| 322 | } else if (kShadowCnt == 2) { |
| 323 | int idx = 0; |
| 324 | #include "tsan_update_shadow_word_inl.h" |
| 325 | idx = 1; |
| 326 | #include "tsan_update_shadow_word_inl.h" |
| 327 | } else if (kShadowCnt == 4) { |
| 328 | int idx = 0; |
| 329 | #include "tsan_update_shadow_word_inl.h" |
| 330 | idx = 1; |
| 331 | #include "tsan_update_shadow_word_inl.h" |
| 332 | idx = 2; |
| 333 | #include "tsan_update_shadow_word_inl.h" |
| 334 | idx = 3; |
| 335 | #include "tsan_update_shadow_word_inl.h" |
| 336 | } else if (kShadowCnt == 8) { |
| 337 | int idx = 0; |
| 338 | #include "tsan_update_shadow_word_inl.h" |
| 339 | idx = 1; |
| 340 | #include "tsan_update_shadow_word_inl.h" |
| 341 | idx = 2; |
| 342 | #include "tsan_update_shadow_word_inl.h" |
| 343 | idx = 3; |
| 344 | #include "tsan_update_shadow_word_inl.h" |
| 345 | idx = 4; |
| 346 | #include "tsan_update_shadow_word_inl.h" |
| 347 | idx = 5; |
| 348 | #include "tsan_update_shadow_word_inl.h" |
| 349 | idx = 6; |
| 350 | #include "tsan_update_shadow_word_inl.h" |
| 351 | idx = 7; |
| 352 | #include "tsan_update_shadow_word_inl.h" |
| 353 | } else { |
| 354 | CHECK(false); |
| 355 | } |
| 356 | |
| 357 | // we did not find any races and had already stored |
| 358 | // the current access info, so we are done |
| 359 | if (LIKELY(store_word == 0)) |
| 360 | return; |
| 361 | // choose a random candidate slot and replace it |
| 362 | StoreShadow(shadow_mem + (cur.epoch() % kShadowCnt), store_word); |
| 363 | StatInc(thr, StatShadowReplace); |
| 364 | return; |
| 365 | RACE: |
| 366 | HandleRace(thr, shadow_mem, cur, old); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | ALWAYS_INLINE |
| 371 | void MemoryAccess(ThreadState *thr, uptr pc, uptr addr, |
| 372 | int kAccessSizeLog, bool kAccessIsWrite) { |
| 373 | u64 *shadow_mem = (u64*)MemToShadow(addr); |
| 374 | DPrintf2("#%d: tsan::OnMemoryAccess: @%p %p size=%d" |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 375 | " is_write=%d shadow_mem=%p {%zx, %zx, %zx, %zx}\n", |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 376 | (int)thr->fast_state.tid(), (void*)pc, (void*)addr, |
| 377 | (int)(1 << kAccessSizeLog), kAccessIsWrite, shadow_mem, |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 378 | (uptr)shadow_mem[0], (uptr)shadow_mem[1], |
| 379 | (uptr)shadow_mem[2], (uptr)shadow_mem[3]); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 380 | #if TSAN_DEBUG |
| 381 | if (!IsAppMem(addr)) { |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 382 | TsanPrintf("Access to non app mem %zx\n", addr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 383 | DCHECK(IsAppMem(addr)); |
| 384 | } |
| 385 | if (!IsShadowMem((uptr)shadow_mem)) { |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 386 | TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 387 | DCHECK(IsShadowMem((uptr)shadow_mem)); |
| 388 | } |
| 389 | #endif |
| 390 | |
| 391 | FastState fast_state = thr->fast_state; |
| 392 | if (fast_state.GetIgnoreBit()) |
| 393 | return; |
| 394 | fast_state.IncrementEpoch(); |
| 395 | thr->fast_state = fast_state; |
| 396 | Shadow cur(fast_state); |
| 397 | cur.SetAddr0AndSizeLog(addr & 7, kAccessSizeLog); |
| 398 | cur.SetWrite(kAccessIsWrite); |
| 399 | |
| 400 | // We must not store to the trace if we do not store to the shadow. |
| 401 | // That is, this call must be moved somewhere below. |
| 402 | TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc); |
| 403 | |
| 404 | MemoryAccessImpl(thr, addr, kAccessSizeLog, kAccessIsWrite, fast_state, |
| 405 | shadow_mem, cur); |
| 406 | } |
| 407 | |
| 408 | static void MemoryRangeSet(ThreadState *thr, uptr pc, uptr addr, uptr size, |
| 409 | u64 val) { |
| 410 | if (size == 0) |
| 411 | return; |
| 412 | // FIXME: fix me. |
| 413 | uptr offset = addr % kShadowCell; |
| 414 | if (offset) { |
| 415 | offset = kShadowCell - offset; |
| 416 | if (size <= offset) |
| 417 | return; |
| 418 | addr += offset; |
| 419 | size -= offset; |
| 420 | } |
| 421 | CHECK_EQ(addr % 8, 0); |
| 422 | CHECK(IsAppMem(addr)); |
| 423 | CHECK(IsAppMem(addr + size - 1)); |
| 424 | (void)thr; |
| 425 | (void)pc; |
| 426 | // Some programs mmap like hundreds of GBs but actually used a small part. |
| 427 | // So, it's better to report a false positive on the memory |
| 428 | // then to hang here senselessly. |
| 429 | const uptr kMaxResetSize = 1024*1024*1024; |
| 430 | if (size > kMaxResetSize) |
| 431 | size = kMaxResetSize; |
Dmitry Vyukov | 9f1509f | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 432 | size = (size + (kShadowCell - 1)) & ~(kShadowCell - 1); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 433 | u64 *p = (u64*)MemToShadow(addr); |
| 434 | CHECK(IsShadowMem((uptr)p)); |
| 435 | CHECK(IsShadowMem((uptr)(p + size * kShadowCnt / kShadowCell - 1))); |
| 436 | // FIXME: may overwrite a part outside the region |
Dmitry Vyukov | 9f1509f | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 437 | for (uptr i = 0; i < size * kShadowCnt / kShadowCell;) { |
| 438 | p[i++] = val; |
| 439 | for (uptr j = 1; j < kShadowCnt; j++) |
| 440 | p[i++] = 0; |
| 441 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size) { |
| 445 | MemoryRangeSet(thr, pc, addr, size, 0); |
| 446 | } |
| 447 | |
| 448 | void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size) { |
| 449 | MemoryAccessRange(thr, pc, addr, size, true); |
Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame] | 450 | Shadow s(thr->fast_state); |
| 451 | s.MarkAsFreed(); |
| 452 | s.SetWrite(true); |
| 453 | s.SetAddr0AndSizeLog(0, 3); |
| 454 | MemoryRangeSet(thr, pc, addr, size, s.raw()); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Dmitry Vyukov | 9f1509f | 2012-08-15 16:52:19 +0000 | [diff] [blame] | 457 | void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size) { |
| 458 | Shadow s(thr->fast_state); |
| 459 | s.SetWrite(true); |
| 460 | s.SetAddr0AndSizeLog(0, 3); |
| 461 | MemoryRangeSet(thr, pc, addr, size, s.raw()); |
| 462 | } |
| 463 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 464 | void FuncEntry(ThreadState *thr, uptr pc) { |
| 465 | DCHECK_EQ(thr->in_rtl, 0); |
| 466 | StatInc(thr, StatFuncEnter); |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 467 | DPrintf2("#%d: FuncEntry %p\n", (int)thr->fast_state.tid(), (void*)pc); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 468 | thr->fast_state.IncrementEpoch(); |
| 469 | TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncEnter, pc); |
| 470 | |
| 471 | // Shadow stack maintenance can be replaced with |
| 472 | // stack unwinding during trace switch (which presumably must be faster). |
Dmitry Vyukov | 3de9ca0 | 2012-05-28 07:45:35 +0000 | [diff] [blame] | 473 | DCHECK_GE(thr->shadow_stack_pos, &thr->shadow_stack[0]); |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 474 | #ifndef TSAN_GO |
Dmitry Vyukov | 3de9ca0 | 2012-05-28 07:45:35 +0000 | [diff] [blame] | 475 | DCHECK_LT(thr->shadow_stack_pos, &thr->shadow_stack[kShadowStackSize]); |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 476 | #else |
| 477 | if (thr->shadow_stack_pos == thr->shadow_stack_end) { |
| 478 | const int sz = thr->shadow_stack_end - thr->shadow_stack; |
| 479 | const int newsz = 2 * sz; |
| 480 | uptr *newstack = (uptr*)internal_alloc(MBlockShadowStack, |
| 481 | newsz * sizeof(uptr)); |
| 482 | internal_memcpy(newstack, thr->shadow_stack, sz * sizeof(uptr)); |
| 483 | internal_free(thr->shadow_stack); |
| 484 | thr->shadow_stack = newstack; |
| 485 | thr->shadow_stack_pos = newstack + sz; |
| 486 | thr->shadow_stack_end = newstack + newsz; |
| 487 | } |
| 488 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 489 | thr->shadow_stack_pos[0] = pc; |
| 490 | thr->shadow_stack_pos++; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void FuncExit(ThreadState *thr) { |
| 494 | DCHECK_EQ(thr->in_rtl, 0); |
| 495 | StatInc(thr, StatFuncExit); |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 496 | DPrintf2("#%d: FuncExit\n", (int)thr->fast_state.tid()); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 497 | thr->fast_state.IncrementEpoch(); |
| 498 | TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncExit, 0); |
| 499 | |
Dmitry Vyukov | 3de9ca0 | 2012-05-28 07:45:35 +0000 | [diff] [blame] | 500 | DCHECK_GT(thr->shadow_stack_pos, &thr->shadow_stack[0]); |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 501 | #ifndef TSAN_GO |
Dmitry Vyukov | 3de9ca0 | 2012-05-28 07:45:35 +0000 | [diff] [blame] | 502 | DCHECK_LT(thr->shadow_stack_pos, &thr->shadow_stack[kShadowStackSize]); |
Dmitry Vyukov | 5bfac97 | 2012-07-16 16:44:47 +0000 | [diff] [blame] | 503 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 504 | thr->shadow_stack_pos--; |
| 505 | } |
| 506 | |
| 507 | void IgnoreCtl(ThreadState *thr, bool write, bool begin) { |
| 508 | DPrintf("#%d: IgnoreCtl(%d, %d)\n", thr->tid, write, begin); |
| 509 | thr->ignore_reads_and_writes += begin ? 1 : -1; |
| 510 | CHECK_GE(thr->ignore_reads_and_writes, 0); |
| 511 | if (thr->ignore_reads_and_writes) |
| 512 | thr->fast_state.SetIgnoreBit(); |
| 513 | else |
| 514 | thr->fast_state.ClearIgnoreBit(); |
| 515 | } |
| 516 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 517 | bool MD5Hash::operator==(const MD5Hash &other) const { |
| 518 | return hash[0] == other.hash[0] && hash[1] == other.hash[1]; |
| 519 | } |
| 520 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 521 | #if TSAN_DEBUG |
| 522 | void build_consistency_debug() {} |
| 523 | #else |
| 524 | void build_consistency_release() {} |
| 525 | #endif |
| 526 | |
| 527 | #if TSAN_COLLECT_STATS |
| 528 | void build_consistency_stats() {} |
| 529 | #else |
| 530 | void build_consistency_nostats() {} |
| 531 | #endif |
| 532 | |
| 533 | #if TSAN_SHADOW_COUNT == 1 |
| 534 | void build_consistency_shadow1() {} |
| 535 | #elif TSAN_SHADOW_COUNT == 2 |
| 536 | void build_consistency_shadow2() {} |
| 537 | #elif TSAN_SHADOW_COUNT == 4 |
| 538 | void build_consistency_shadow4() {} |
| 539 | #else |
| 540 | void build_consistency_shadow8() {} |
| 541 | #endif |
| 542 | |
| 543 | } // namespace __tsan |
| 544 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 545 | #ifndef TSAN_GO |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 546 | // Must be included in this file to make sure everything is inlined. |
| 547 | #include "tsan_interface_inl.h" |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 548 | #endif |