Alexey Samsonov | 3b2f9f4 | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_rtl_thread.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 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Samsonov | 8bd9098 | 2012-06-07 09:50:16 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 15 | #include "tsan_rtl.h" |
| 16 | #include "tsan_mman.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 17 | #include "tsan_platform.h" |
| 18 | #include "tsan_report.h" |
| 19 | #include "tsan_sync.h" |
| 20 | |
| 21 | namespace __tsan { |
| 22 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 23 | // ThreadContext implementation. |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 24 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 25 | ThreadContext::ThreadContext(int tid) |
| 26 | : ThreadContextBase(tid) |
| 27 | , thr() |
| 28 | , sync() |
| 29 | , epoch0() |
Dmitry Vyukov | 79915de | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 30 | , epoch1() { |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Dmitry Vyukov | 49e462f | 2013-03-18 10:10:15 +0000 | [diff] [blame] | 33 | #ifndef TSAN_GO |
| 34 | ThreadContext::~ThreadContext() { |
| 35 | } |
| 36 | #endif |
| 37 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 38 | void ThreadContext::OnDead() { |
| 39 | sync.Reset(); |
| 40 | } |
| 41 | |
| 42 | void ThreadContext::OnJoined(void *arg) { |
| 43 | ThreadState *caller_thr = static_cast<ThreadState *>(arg); |
Dmitry Vyukov | fbb194f | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 44 | AcquireImpl(caller_thr, 0, &sync); |
Dmitry Vyukov | 509dab3 | 2013-03-19 10:22:33 +0000 | [diff] [blame] | 45 | sync.Reset(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | struct OnCreatedArgs { |
| 49 | ThreadState *thr; |
| 50 | uptr pc; |
| 51 | }; |
| 52 | |
| 53 | void ThreadContext::OnCreated(void *arg) { |
| 54 | thr = 0; |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 55 | if (tid == 0) |
| 56 | return; |
| 57 | OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg); |
| 58 | args->thr->fast_state.IncrementEpoch(); |
| 59 | // Can't increment epoch w/o writing to the trace as well. |
| 60 | TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0); |
Dmitry Vyukov | fbb194f | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 61 | ReleaseImpl(args->thr, 0, &sync); |
Dmitry Vyukov | 7cd2025 | 2013-03-18 09:02:27 +0000 | [diff] [blame] | 62 | creation_stack_id = CurrentStackId(args->thr, args->pc); |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 63 | if (reuse_count == 0) |
| 64 | StatInc(args->thr, StatThreadMaxTid); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Dmitry Vyukov | 4ecfa69 | 2013-03-19 12:25:48 +0000 | [diff] [blame] | 67 | void ThreadContext::OnReset() { |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 68 | sync.Reset(); |
Dmitry Vyukov | 79915de | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 69 | FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event)); |
| 70 | //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace)); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | struct OnStartedArgs { |
| 74 | ThreadState *thr; |
| 75 | uptr stk_addr; |
| 76 | uptr stk_size; |
| 77 | uptr tls_addr; |
| 78 | uptr tls_size; |
| 79 | }; |
| 80 | |
| 81 | void ThreadContext::OnStarted(void *arg) { |
| 82 | OnStartedArgs *args = static_cast<OnStartedArgs*>(arg); |
Dmitry Vyukov | 5016003 | 2013-03-18 08:52:46 +0000 | [diff] [blame] | 83 | thr = args->thr; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 84 | // RoundUp so that one trace part does not contain events |
| 85 | // from different threads. |
| 86 | epoch0 = RoundUp(epoch1 + 1, kTracePartSize); |
| 87 | epoch1 = (u64)-1; |
Dmitry Vyukov | 6cfab72 | 2014-02-28 10:48:13 +0000 | [diff] [blame] | 88 | new(thr) ThreadState(ctx, tid, unique_id, |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 89 | epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size); |
Dmitry Vyukov | 464ebbd | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 90 | #ifndef TSAN_GO |
| 91 | thr->shadow_stack = &ThreadTrace(thr->tid)->shadow_stack[0]; |
| 92 | thr->shadow_stack_pos = thr->shadow_stack; |
| 93 | thr->shadow_stack_end = thr->shadow_stack + kShadowStackSize; |
| 94 | #else |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 95 | // Setup dynamic shadow stack. |
| 96 | const int kInitStackSize = 8; |
Dmitry Vyukov | 464ebbd | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 97 | thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack, |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 98 | kInitStackSize * sizeof(uptr)); |
Dmitry Vyukov | 464ebbd | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 99 | thr->shadow_stack_pos = thr->shadow_stack; |
| 100 | thr->shadow_stack_end = thr->shadow_stack + kInitStackSize; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 101 | #endif |
| 102 | #ifndef TSAN_GO |
Dmitry Vyukov | 464ebbd | 2013-10-16 15:35:12 +0000 | [diff] [blame] | 103 | AllocatorThreadStart(thr); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 104 | #endif |
Dmitry Vyukov | 6cfab72 | 2014-02-28 10:48:13 +0000 | [diff] [blame] | 105 | if (flags()->detect_deadlocks) { |
| 106 | thr->dd_pt = ctx->dd->CreatePhysicalThread(); |
| 107 | thr->dd_lt = ctx->dd->CreateLogicalThread(unique_id); |
| 108 | } |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 109 | thr->fast_synch_epoch = epoch0; |
Dmitry Vyukov | fbb194f | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 110 | AcquireImpl(thr, 0, &sync); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 111 | thr->fast_state.SetHistorySize(flags()->history_size); |
| 112 | const uptr trace = (epoch0 / kTracePartSize) % TraceParts(); |
Dmitry Vyukov | 79915de | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 113 | Trace *thr_trace = ThreadTrace(thr->tid); |
| 114 | thr_trace->headers[trace].epoch0 = epoch0; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 115 | StatInc(thr, StatSyncAcquire); |
Dmitry Vyukov | 79915de | 2013-03-20 10:31:53 +0000 | [diff] [blame] | 116 | sync.Reset(); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 117 | DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx " |
| 118 | "tls_addr=%zx tls_size=%zx\n", |
Alexey Samsonov | b5d10f6 | 2013-03-18 09:45:22 +0000 | [diff] [blame] | 119 | tid, (uptr)epoch0, args->stk_addr, args->stk_size, |
| 120 | args->tls_addr, args->tls_size); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 121 | thr->is_alive = true; |
| 122 | } |
| 123 | |
| 124 | void ThreadContext::OnFinished() { |
| 125 | if (!detached) { |
| 126 | thr->fast_state.IncrementEpoch(); |
| 127 | // Can't increment epoch w/o writing to the trace as well. |
| 128 | TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0); |
Dmitry Vyukov | fbb194f | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 129 | ReleaseImpl(thr, 0, &sync); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 130 | } |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 131 | epoch1 = thr->fast_state.epoch(); |
| 132 | |
Dmitry Vyukov | 6cfab72 | 2014-02-28 10:48:13 +0000 | [diff] [blame] | 133 | if (flags()->detect_deadlocks) { |
| 134 | ctx->dd->DestroyPhysicalThread(thr->dd_pt); |
| 135 | ctx->dd->DestroyLogicalThread(thr->dd_lt); |
| 136 | } |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 137 | #ifndef TSAN_GO |
| 138 | AllocatorThreadFinish(thr); |
| 139 | #endif |
| 140 | thr->~ThreadState(); |
Dmitry Vyukov | 6cfab72 | 2014-02-28 10:48:13 +0000 | [diff] [blame] | 141 | StatAggregate(ctx->stat, thr->stat); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 142 | thr = 0; |
| 143 | } |
| 144 | |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 145 | #ifndef TSAN_GO |
| 146 | struct ThreadLeak { |
| 147 | ThreadContext *tctx; |
| 148 | int count; |
| 149 | }; |
| 150 | |
| 151 | static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) { |
| 152 | Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 153 | ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base); |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 154 | if (tctx->detached || tctx->status != ThreadStatusFinished) |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 155 | return; |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 156 | for (uptr i = 0; i < leaks.Size(); i++) { |
| 157 | if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) { |
| 158 | leaks[i].count++; |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | ThreadLeak leak = {tctx, 1}; |
| 163 | leaks.PushBack(leak); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 164 | } |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 165 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 166 | |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 167 | #ifndef TSAN_GO |
| 168 | static void ReportIgnoresEnabled(ThreadContext *tctx, IgnoreSet *set) { |
| 169 | if (tctx->tid == 0) { |
| 170 | Printf("ThreadSanitizer: main thread finished with ignores enabled\n"); |
| 171 | } else { |
| 172 | Printf("ThreadSanitizer: thread T%d %s finished with ignores enabled," |
| 173 | " created at:\n", tctx->tid, tctx->name); |
| 174 | PrintStack(SymbolizeStackId(tctx->creation_stack_id)); |
Dmitry Vyukov | 536bff3 | 2013-05-21 08:12:35 +0000 | [diff] [blame] | 175 | } |
Dmitry Vyukov | c038686 | 2013-11-27 18:23:52 +0000 | [diff] [blame] | 176 | Printf(" One of the following ignores was not ended" |
| 177 | " (in order of probability)\n"); |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 178 | for (uptr i = 0; i < set->Size(); i++) { |
| 179 | Printf(" Ignore was enabled at:\n"); |
| 180 | PrintStack(SymbolizeStackId(set->At(i))); |
Dmitry Vyukov | fbb194f | 2013-10-10 15:58:12 +0000 | [diff] [blame] | 181 | } |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 182 | Die(); |
Dmitry Vyukov | 536bff3 | 2013-05-21 08:12:35 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 185 | static void ThreadCheckIgnore(ThreadState *thr) { |
Dmitry Vyukov | c9e12aa | 2014-03-20 10:36:20 +0000 | [diff] [blame^] | 186 | if (ctx->after_multithreaded_fork) |
Dmitry Vyukov | 16e7a75 | 2014-01-24 12:33:35 +0000 | [diff] [blame] | 187 | return; |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 188 | if (thr->ignore_reads_and_writes) |
| 189 | ReportIgnoresEnabled(thr->tctx, &thr->mop_ignore_set); |
| 190 | if (thr->ignore_sync) |
| 191 | ReportIgnoresEnabled(thr->tctx, &thr->sync_ignore_set); |
| 192 | } |
| 193 | #else |
| 194 | static void ThreadCheckIgnore(ThreadState *thr) {} |
| 195 | #endif |
| 196 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 197 | void ThreadFinalize(ThreadState *thr) { |
Dmitry Vyukov | 536bff3 | 2013-05-21 08:12:35 +0000 | [diff] [blame] | 198 | ThreadCheckIgnore(thr); |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 199 | #ifndef TSAN_GO |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 200 | if (!flags()->report_thread_leaks) |
| 201 | return; |
Dmitry Vyukov | c9e12aa | 2014-03-20 10:36:20 +0000 | [diff] [blame^] | 202 | ThreadRegistryLock l(ctx->thread_registry); |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 203 | Vector<ThreadLeak> leaks(MBlockScopedBuf); |
Dmitry Vyukov | c9e12aa | 2014-03-20 10:36:20 +0000 | [diff] [blame^] | 204 | ctx->thread_registry->RunCallbackForEachThreadLocked( |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 205 | MaybeReportThreadLeak, &leaks); |
| 206 | for (uptr i = 0; i < leaks.Size(); i++) { |
| 207 | ScopedReport rep(ReportTypeThreadLeak); |
| 208 | rep.AddThread(leaks[i].tctx); |
| 209 | rep.SetCount(leaks[i].count); |
Dmitry Vyukov | c9e12aa | 2014-03-20 10:36:20 +0000 | [diff] [blame^] | 210 | OutputReport(ctx, rep); |
Dmitry Vyukov | ebf63d0 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 211 | } |
| 212 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Dmitry Vyukov | 67dc570 | 2012-11-07 16:41:57 +0000 | [diff] [blame] | 215 | int ThreadCount(ThreadState *thr) { |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 216 | uptr result; |
| 217 | ctx->thread_registry->GetNumberOfThreads(0, 0, &result); |
| 218 | return (int)result; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 222 | StatInc(thr, StatThreadCreate); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 223 | OnCreatedArgs args = { thr, pc }; |
| 224 | int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args); |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 225 | DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 226 | StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads()); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 227 | return tid; |
| 228 | } |
| 229 | |
Dmitry Vyukov | 56faa55 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 230 | void ThreadStart(ThreadState *thr, int tid, uptr os_id) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 231 | uptr stk_addr = 0; |
| 232 | uptr stk_size = 0; |
| 233 | uptr tls_addr = 0; |
| 234 | uptr tls_size = 0; |
Dmitry Vyukov | 7339eb1 | 2012-05-25 11:15:04 +0000 | [diff] [blame] | 235 | GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 236 | |
Dmitry Vyukov | 2d4e3c1 | 2012-05-28 07:44:34 +0000 | [diff] [blame] | 237 | if (tid) { |
Dmitry Vyukov | 2e7f29f | 2013-03-18 15:49:07 +0000 | [diff] [blame] | 238 | if (stk_addr && stk_size) |
Dmitry Vyukov | ce26a0a | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 239 | MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 240 | |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 241 | if (tls_addr && tls_size) { |
| 242 | // Check that the thr object is in tls; |
| 243 | const uptr thr_beg = (uptr)thr; |
| 244 | const uptr thr_end = (uptr)thr + sizeof(*thr); |
| 245 | CHECK_GE(thr_beg, tls_addr); |
| 246 | CHECK_LE(thr_beg, tls_addr + tls_size); |
| 247 | CHECK_GE(thr_end, tls_addr); |
| 248 | CHECK_LE(thr_end, tls_addr + tls_size); |
| 249 | // Since the thr object is huge, skip it. |
Dmitry Vyukov | ce26a0a | 2013-03-18 16:56:48 +0000 | [diff] [blame] | 250 | MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr); |
| 251 | MemoryRangeImitateWrite(thr, /*pc=*/ 2, |
| 252 | thr_end, tls_addr + tls_size - thr_end); |
Dmitry Vyukov | 03d32ec | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 253 | } |
Dmitry Vyukov | 2d4e3c1 | 2012-05-28 07:44:34 +0000 | [diff] [blame] | 254 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 255 | |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 256 | ThreadRegistry *tr = ctx->thread_registry; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 257 | OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size }; |
Dmitry Vyukov | 3238e1c | 2013-11-27 11:30:28 +0000 | [diff] [blame] | 258 | tr->StartThread(tid, os_id, &args); |
| 259 | |
| 260 | tr->Lock(); |
| 261 | thr->tctx = (ThreadContext*)tr->GetThreadLocked(tid); |
| 262 | tr->Unlock(); |
Dmitry Vyukov | 16e7a75 | 2014-01-24 12:33:35 +0000 | [diff] [blame] | 263 | |
| 264 | #ifndef TSAN_GO |
| 265 | if (ctx->after_multithreaded_fork) { |
| 266 | thr->ignore_interceptors++; |
| 267 | ThreadIgnoreBegin(thr, 0); |
| 268 | ThreadIgnoreSyncBegin(thr, 0); |
| 269 | } |
| 270 | #endif |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void ThreadFinish(ThreadState *thr) { |
Dmitry Vyukov | 536bff3 | 2013-05-21 08:12:35 +0000 | [diff] [blame] | 274 | ThreadCheckIgnore(thr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 275 | StatInc(thr, StatThreadFinish); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 276 | if (thr->stk_addr && thr->stk_size) |
Dmitry Vyukov | 2e7f29f | 2013-03-18 15:49:07 +0000 | [diff] [blame] | 277 | DontNeedShadowFor(thr->stk_addr, thr->stk_size); |
| 278 | if (thr->tls_addr && thr->tls_size) |
| 279 | DontNeedShadowFor(thr->tls_addr, thr->tls_size); |
Dmitry Vyukov | fa985a0 | 2012-06-28 18:07:46 +0000 | [diff] [blame] | 280 | thr->is_alive = false; |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 281 | ctx->thread_registry->FinishThread(thr->tid); |
| 282 | } |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 283 | |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 284 | static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) { |
| 285 | uptr uid = (uptr)arg; |
| 286 | if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) { |
| 287 | tctx->user_id = 0; |
| 288 | return true; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 289 | } |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 290 | return false; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int ThreadTid(ThreadState *thr, uptr pc, uptr uid) { |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 294 | int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid); |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 295 | DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res); |
Dmitry Vyukov | 880bb66 | 2012-05-28 17:32:50 +0000 | [diff] [blame] | 296 | return res; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void ThreadJoin(ThreadState *thr, uptr pc, int tid) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 300 | CHECK_GT(tid, 0); |
| 301 | CHECK_LT(tid, kMaxTid); |
| 302 | DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 303 | ctx->thread_registry->JoinThread(tid, thr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void ThreadDetach(ThreadState *thr, uptr pc, int tid) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 307 | CHECK_GT(tid, 0); |
| 308 | CHECK_LT(tid, kMaxTid); |
Alexey Samsonov | 9aecdfe | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 309 | ctx->thread_registry->DetachThread(tid); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Dmitry Vyukov | 1b46993 | 2012-12-04 15:46:05 +0000 | [diff] [blame] | 312 | void ThreadSetName(ThreadState *thr, const char *name) { |
Dmitry Vyukov | c9e12aa | 2014-03-20 10:36:20 +0000 | [diff] [blame^] | 313 | ctx->thread_registry->SetThreadName(thr->tid, name); |
Dmitry Vyukov | 1b46993 | 2012-12-04 15:46:05 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 316 | void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr, |
| 317 | uptr size, bool is_write) { |
| 318 | if (size == 0) |
| 319 | return; |
| 320 | |
| 321 | u64 *shadow_mem = (u64*)MemToShadow(addr); |
| 322 | DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n", |
| 323 | thr->tid, (void*)pc, (void*)addr, |
| 324 | (int)size, is_write); |
| 325 | |
| 326 | #if TSAN_DEBUG |
| 327 | if (!IsAppMem(addr)) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 328 | Printf("Access to non app mem %zx\n", addr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 329 | DCHECK(IsAppMem(addr)); |
| 330 | } |
| 331 | if (!IsAppMem(addr + size - 1)) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 332 | Printf("Access to non app mem %zx\n", addr + size - 1); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 333 | DCHECK(IsAppMem(addr + size - 1)); |
| 334 | } |
| 335 | if (!IsShadowMem((uptr)shadow_mem)) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 336 | Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 337 | DCHECK(IsShadowMem((uptr)shadow_mem)); |
| 338 | } |
| 339 | if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) { |
Alexey Samsonov | ad9d65f | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 340 | Printf("Bad shadow addr %p (%zx)\n", |
Alexey Samsonov | 51ae983 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 341 | shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 342 | DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))); |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | StatInc(thr, StatMopRange); |
| 347 | |
Dmitry Vyukov | b62c158 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 348 | if (*shadow_mem == kShadowRodata) { |
| 349 | // Access to .rodata section, no races here. |
| 350 | // Measurements show that it can be 10-20% of all memory accesses. |
| 351 | StatInc(thr, StatMopRangeRodata); |
| 352 | return; |
| 353 | } |
| 354 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 355 | FastState fast_state = thr->fast_state; |
| 356 | if (fast_state.GetIgnoreBit()) |
| 357 | return; |
| 358 | |
| 359 | fast_state.IncrementEpoch(); |
| 360 | thr->fast_state = fast_state; |
Dmitry Vyukov | 2429b02 | 2012-11-28 10:35:31 +0000 | [diff] [blame] | 361 | TraceAddEvent(thr, fast_state, EventTypeMop, pc); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 362 | |
| 363 | bool unaligned = (addr % kShadowCell) != 0; |
| 364 | |
| 365 | // Handle unaligned beginning, if any. |
| 366 | for (; addr % kShadowCell && size; addr++, size--) { |
| 367 | int const kAccessSizeLog = 0; |
| 368 | Shadow cur(fast_state); |
| 369 | cur.SetWrite(is_write); |
| 370 | cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog); |
Dmitry Vyukov | ba42914 | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 371 | MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 372 | shadow_mem, cur); |
| 373 | } |
| 374 | if (unaligned) |
| 375 | shadow_mem += kShadowCnt; |
| 376 | // Handle middle part, if any. |
| 377 | for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) { |
| 378 | int const kAccessSizeLog = 3; |
| 379 | Shadow cur(fast_state); |
| 380 | cur.SetWrite(is_write); |
| 381 | cur.SetAddr0AndSizeLog(0, kAccessSizeLog); |
Dmitry Vyukov | ba42914 | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 382 | MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 383 | shadow_mem, cur); |
| 384 | shadow_mem += kShadowCnt; |
| 385 | } |
| 386 | // Handle ending, if any. |
| 387 | for (; size; addr++, size--) { |
| 388 | int const kAccessSizeLog = 0; |
| 389 | Shadow cur(fast_state); |
| 390 | cur.SetWrite(is_write); |
| 391 | cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog); |
Dmitry Vyukov | ba42914 | 2013-02-01 09:42:06 +0000 | [diff] [blame] | 392 | MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false, |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 393 | shadow_mem, cur); |
| 394 | } |
| 395 | } |
Dmitry Vyukov | 3c2489e | 2013-02-13 13:05:36 +0000 | [diff] [blame] | 396 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 397 | } // namespace __tsan |