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