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 TSAN_GO
~ThreadContext()34 ThreadContext::~ThreadContext() {
35 }
36 #endif
37
OnDead()38 void ThreadContext::OnDead() {
39 sync.Reset();
40 }
41
OnJoined(void * arg)42 void ThreadContext::OnJoined(void *arg) {
43 ThreadState *caller_thr = static_cast<ThreadState *>(arg);
44 caller_thr->clock.acquire(&sync);
45 StatInc(caller_thr, StatSyncAcquire);
46 sync.Reset();
47 }
48
49 struct OnCreatedArgs {
50 ThreadState *thr;
51 uptr pc;
52 };
53
OnCreated(void * arg)54 void ThreadContext::OnCreated(void *arg) {
55 thr = 0;
56 if (tid == 0)
57 return;
58 OnCreatedArgs *args = static_cast<OnCreatedArgs *>(arg);
59 args->thr->fast_state.IncrementEpoch();
60 // Can't increment epoch w/o writing to the trace as well.
61 TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
62 args->thr->clock.set(args->thr->tid, args->thr->fast_state.epoch());
63 args->thr->fast_synch_epoch = args->thr->fast_state.epoch();
64 args->thr->clock.release(&sync);
65 StatInc(args->thr, StatSyncRelease);
66 #ifdef TSAN_GO
67 creation_stack.ObtainCurrent(args->thr, args->pc);
68 #else
69 creation_stack_id = CurrentStackId(args->thr, args->pc);
70 #endif
71 if (reuse_count == 0)
72 StatInc(args->thr, StatThreadMaxTid);
73 }
74
OnReset()75 void ThreadContext::OnReset() {
76 sync.Reset();
77 FlushUnneededShadowMemory(GetThreadTrace(tid), TraceSize() * sizeof(Event));
78 //!!! FlushUnneededShadowMemory(GetThreadTraceHeader(tid), sizeof(Trace));
79 }
80
81 struct OnStartedArgs {
82 ThreadState *thr;
83 uptr stk_addr;
84 uptr stk_size;
85 uptr tls_addr;
86 uptr tls_size;
87 };
88
OnStarted(void * arg)89 void ThreadContext::OnStarted(void *arg) {
90 OnStartedArgs *args = static_cast<OnStartedArgs*>(arg);
91 thr = args->thr;
92 // RoundUp so that one trace part does not contain events
93 // from different threads.
94 epoch0 = RoundUp(epoch1 + 1, kTracePartSize);
95 epoch1 = (u64)-1;
96 new(thr) ThreadState(CTX(), tid, unique_id,
97 epoch0, args->stk_addr, args->stk_size, args->tls_addr, args->tls_size);
98 #ifdef TSAN_GO
99 // Setup dynamic shadow stack.
100 const int kInitStackSize = 8;
101 args->thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
102 kInitStackSize * sizeof(uptr));
103 args->thr->shadow_stack_pos = thr->shadow_stack;
104 args->thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
105 #endif
106 #ifndef TSAN_GO
107 AllocatorThreadStart(args->thr);
108 #endif
109 thr = args->thr;
110 thr->fast_synch_epoch = epoch0;
111 thr->clock.set(tid, epoch0);
112 thr->clock.acquire(&sync);
113 thr->fast_state.SetHistorySize(flags()->history_size);
114 const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
115 Trace *thr_trace = ThreadTrace(thr->tid);
116 thr_trace->headers[trace].epoch0 = epoch0;
117 StatInc(thr, StatSyncAcquire);
118 sync.Reset();
119 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
120 "tls_addr=%zx tls_size=%zx\n",
121 tid, (uptr)epoch0, args->stk_addr, args->stk_size,
122 args->tls_addr, args->tls_size);
123 thr->is_alive = true;
124 }
125
OnFinished()126 void ThreadContext::OnFinished() {
127 if (!detached) {
128 thr->fast_state.IncrementEpoch();
129 // Can't increment epoch w/o writing to the trace as well.
130 TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
131 thr->clock.set(thr->tid, thr->fast_state.epoch());
132 thr->fast_synch_epoch = thr->fast_state.epoch();
133 thr->clock.release(&sync);
134 StatInc(thr, StatSyncRelease);
135 }
136 epoch1 = thr->fast_state.epoch();
137
138 #ifndef TSAN_GO
139 AllocatorThreadFinish(thr);
140 #endif
141 thr->~ThreadState();
142 StatAggregate(CTX()->stat, thr->stat);
143 thr = 0;
144 }
145
146 #ifndef TSAN_GO
147 struct ThreadLeak {
148 ThreadContext *tctx;
149 int count;
150 };
151
MaybeReportThreadLeak(ThreadContextBase * tctx_base,void * arg)152 static void MaybeReportThreadLeak(ThreadContextBase *tctx_base, void *arg) {
153 Vector<ThreadLeak> &leaks = *(Vector<ThreadLeak>*)arg;
154 ThreadContext *tctx = static_cast<ThreadContext*>(tctx_base);
155 if (tctx->detached || tctx->status != ThreadStatusFinished)
156 return;
157 for (uptr i = 0; i < leaks.Size(); i++) {
158 if (leaks[i].tctx->creation_stack_id == tctx->creation_stack_id) {
159 leaks[i].count++;
160 return;
161 }
162 }
163 ThreadLeak leak = {tctx, 1};
164 leaks.PushBack(leak);
165 }
166 #endif
167
ThreadCheckIgnore(ThreadState * thr)168 static void ThreadCheckIgnore(ThreadState *thr) {
169 if (thr->ignore_reads_and_writes) {
170 Printf("ThreadSanitizer: thread T%d finished with ignores enabled.\n",
171 thr->tid);
172 }
173 }
174
ThreadFinalize(ThreadState * thr)175 void ThreadFinalize(ThreadState *thr) {
176 CHECK_GT(thr->in_rtl, 0);
177 ThreadCheckIgnore(thr);
178 #ifndef TSAN_GO
179 if (!flags()->report_thread_leaks)
180 return;
181 ThreadRegistryLock l(CTX()->thread_registry);
182 Vector<ThreadLeak> leaks(MBlockScopedBuf);
183 CTX()->thread_registry->RunCallbackForEachThreadLocked(
184 MaybeReportThreadLeak, &leaks);
185 for (uptr i = 0; i < leaks.Size(); i++) {
186 ScopedReport rep(ReportTypeThreadLeak);
187 rep.AddThread(leaks[i].tctx);
188 rep.SetCount(leaks[i].count);
189 OutputReport(CTX(), rep);
190 }
191 #endif
192 }
193
ThreadCount(ThreadState * thr)194 int ThreadCount(ThreadState *thr) {
195 CHECK_GT(thr->in_rtl, 0);
196 Context *ctx = CTX();
197 uptr result;
198 ctx->thread_registry->GetNumberOfThreads(0, 0, &result);
199 return (int)result;
200 }
201
ThreadCreate(ThreadState * thr,uptr pc,uptr uid,bool detached)202 int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
203 CHECK_GT(thr->in_rtl, 0);
204 StatInc(thr, StatThreadCreate);
205 Context *ctx = CTX();
206 OnCreatedArgs args = { thr, pc };
207 int tid = ctx->thread_registry->CreateThread(uid, detached, thr->tid, &args);
208 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
209 StatSet(thr, StatThreadMaxAlive, ctx->thread_registry->GetMaxAliveThreads());
210 return tid;
211 }
212
ThreadStart(ThreadState * thr,int tid,uptr os_id)213 void ThreadStart(ThreadState *thr, int tid, uptr os_id) {
214 CHECK_GT(thr->in_rtl, 0);
215 uptr stk_addr = 0;
216 uptr stk_size = 0;
217 uptr tls_addr = 0;
218 uptr tls_size = 0;
219 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
220
221 if (tid) {
222 if (stk_addr && stk_size)
223 MemoryRangeImitateWrite(thr, /*pc=*/ 1, stk_addr, stk_size);
224
225 if (tls_addr && tls_size) {
226 // Check that the thr object is in tls;
227 const uptr thr_beg = (uptr)thr;
228 const uptr thr_end = (uptr)thr + sizeof(*thr);
229 CHECK_GE(thr_beg, tls_addr);
230 CHECK_LE(thr_beg, tls_addr + tls_size);
231 CHECK_GE(thr_end, tls_addr);
232 CHECK_LE(thr_end, tls_addr + tls_size);
233 // Since the thr object is huge, skip it.
234 MemoryRangeImitateWrite(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
235 MemoryRangeImitateWrite(thr, /*pc=*/ 2,
236 thr_end, tls_addr + tls_size - thr_end);
237 }
238 }
239
240 OnStartedArgs args = { thr, stk_addr, stk_size, tls_addr, tls_size };
241 CTX()->thread_registry->StartThread(tid, os_id, &args);
242 }
243
ThreadFinish(ThreadState * thr)244 void ThreadFinish(ThreadState *thr) {
245 CHECK_GT(thr->in_rtl, 0);
246 ThreadCheckIgnore(thr);
247 StatInc(thr, StatThreadFinish);
248 if (thr->stk_addr && thr->stk_size)
249 DontNeedShadowFor(thr->stk_addr, thr->stk_size);
250 if (thr->tls_addr && thr->tls_size)
251 DontNeedShadowFor(thr->tls_addr, thr->tls_size);
252 thr->is_alive = false;
253 Context *ctx = CTX();
254 ctx->thread_registry->FinishThread(thr->tid);
255 }
256
FindThreadByUid(ThreadContextBase * tctx,void * arg)257 static bool FindThreadByUid(ThreadContextBase *tctx, void *arg) {
258 uptr uid = (uptr)arg;
259 if (tctx->user_id == uid && tctx->status != ThreadStatusInvalid) {
260 tctx->user_id = 0;
261 return true;
262 }
263 return false;
264 }
265
ThreadTid(ThreadState * thr,uptr pc,uptr uid)266 int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
267 CHECK_GT(thr->in_rtl, 0);
268 Context *ctx = CTX();
269 int res = ctx->thread_registry->FindThread(FindThreadByUid, (void*)uid);
270 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
271 return res;
272 }
273
ThreadJoin(ThreadState * thr,uptr pc,int tid)274 void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
275 CHECK_GT(thr->in_rtl, 0);
276 CHECK_GT(tid, 0);
277 CHECK_LT(tid, kMaxTid);
278 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
279 Context *ctx = CTX();
280 ctx->thread_registry->JoinThread(tid, thr);
281 }
282
ThreadDetach(ThreadState * thr,uptr pc,int tid)283 void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
284 CHECK_GT(thr->in_rtl, 0);
285 CHECK_GT(tid, 0);
286 CHECK_LT(tid, kMaxTid);
287 Context *ctx = CTX();
288 ctx->thread_registry->DetachThread(tid);
289 }
290
ThreadSetName(ThreadState * thr,const char * name)291 void ThreadSetName(ThreadState *thr, const char *name) {
292 CHECK_GT(thr->in_rtl, 0);
293 CTX()->thread_registry->SetThreadName(thr->tid, name);
294 }
295
MemoryAccessRange(ThreadState * thr,uptr pc,uptr addr,uptr size,bool is_write)296 void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
297 uptr size, bool is_write) {
298 if (size == 0)
299 return;
300
301 u64 *shadow_mem = (u64*)MemToShadow(addr);
302 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
303 thr->tid, (void*)pc, (void*)addr,
304 (int)size, is_write);
305
306 #if TSAN_DEBUG
307 if (!IsAppMem(addr)) {
308 Printf("Access to non app mem %zx\n", addr);
309 DCHECK(IsAppMem(addr));
310 }
311 if (!IsAppMem(addr + size - 1)) {
312 Printf("Access to non app mem %zx\n", addr + size - 1);
313 DCHECK(IsAppMem(addr + size - 1));
314 }
315 if (!IsShadowMem((uptr)shadow_mem)) {
316 Printf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
317 DCHECK(IsShadowMem((uptr)shadow_mem));
318 }
319 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
320 Printf("Bad shadow addr %p (%zx)\n",
321 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
322 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
323 }
324 #endif
325
326 StatInc(thr, StatMopRange);
327
328 if (*shadow_mem == kShadowRodata) {
329 // Access to .rodata section, no races here.
330 // Measurements show that it can be 10-20% of all memory accesses.
331 StatInc(thr, StatMopRangeRodata);
332 return;
333 }
334
335 FastState fast_state = thr->fast_state;
336 if (fast_state.GetIgnoreBit())
337 return;
338
339 fast_state.IncrementEpoch();
340 thr->fast_state = fast_state;
341 TraceAddEvent(thr, fast_state, EventTypeMop, pc);
342
343 bool unaligned = (addr % kShadowCell) != 0;
344
345 // Handle unaligned beginning, if any.
346 for (; addr % kShadowCell && size; addr++, size--) {
347 int const kAccessSizeLog = 0;
348 Shadow cur(fast_state);
349 cur.SetWrite(is_write);
350 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
351 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
352 shadow_mem, cur);
353 }
354 if (unaligned)
355 shadow_mem += kShadowCnt;
356 // Handle middle part, if any.
357 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
358 int const kAccessSizeLog = 3;
359 Shadow cur(fast_state);
360 cur.SetWrite(is_write);
361 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
362 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
363 shadow_mem, cur);
364 shadow_mem += kShadowCnt;
365 }
366 // Handle ending, if any.
367 for (; size; addr++, size--) {
368 int const kAccessSizeLog = 0;
369 Shadow cur(fast_state);
370 cur.SetWrite(is_write);
371 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
372 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, false,
373 shadow_mem, cur);
374 }
375 }
376
377 } // namespace __tsan
378