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