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