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 #ifndef TSAN_GO
24 const int kThreadQuarantineSize = 16;
25 #else
26 const int kThreadQuarantineSize = 64;
27 #endif
28
MaybeReportThreadLeak(ThreadContext * tctx)29 static void MaybeReportThreadLeak(ThreadContext *tctx) {
30 if (tctx->detached)
31 return;
32 if (tctx->status != ThreadStatusCreated
33 && tctx->status != ThreadStatusRunning
34 && tctx->status != ThreadStatusFinished)
35 return;
36 ScopedReport rep(ReportTypeThreadLeak);
37 rep.AddThread(tctx);
38 OutputReport(rep);
39 }
40
ThreadFinalize(ThreadState * thr)41 void ThreadFinalize(ThreadState *thr) {
42 CHECK_GT(thr->in_rtl, 0);
43 if (!flags()->report_thread_leaks)
44 return;
45 Context *ctx = CTX();
46 Lock l(&ctx->thread_mtx);
47 for (unsigned i = 0; i < kMaxTid; i++) {
48 ThreadContext *tctx = ctx->threads[i];
49 if (tctx == 0)
50 continue;
51 MaybeReportThreadLeak(tctx);
52 }
53 }
54
ThreadDead(ThreadState * thr,ThreadContext * tctx)55 static void ThreadDead(ThreadState *thr, ThreadContext *tctx) {
56 Context *ctx = CTX();
57 CHECK_GT(thr->in_rtl, 0);
58 CHECK(tctx->status == ThreadStatusRunning
59 || tctx->status == ThreadStatusFinished);
60 DPrintf("#%d: ThreadDead uid=%zu\n", thr->tid, tctx->user_id);
61 tctx->status = ThreadStatusDead;
62 tctx->user_id = 0;
63 tctx->sync.Reset();
64
65 // Put to dead list.
66 tctx->dead_next = 0;
67 if (ctx->dead_list_size == 0)
68 ctx->dead_list_head = tctx;
69 else
70 ctx->dead_list_tail->dead_next = tctx;
71 ctx->dead_list_tail = tctx;
72 ctx->dead_list_size++;
73 }
74
ThreadCreate(ThreadState * thr,uptr pc,uptr uid,bool detached)75 int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached) {
76 CHECK_GT(thr->in_rtl, 0);
77 Context *ctx = CTX();
78 Lock l(&ctx->thread_mtx);
79 StatInc(thr, StatThreadCreate);
80 int tid = -1;
81 ThreadContext *tctx = 0;
82 if (ctx->dead_list_size > kThreadQuarantineSize
83 || ctx->thread_seq >= kMaxTid) {
84 if (ctx->dead_list_size == 0) {
85 TsanPrintf("ThreadSanitizer: %d thread limit exceeded. Dying.\n",
86 kMaxTid);
87 Die();
88 }
89 StatInc(thr, StatThreadReuse);
90 tctx = ctx->dead_list_head;
91 ctx->dead_list_head = tctx->dead_next;
92 ctx->dead_list_size--;
93 if (ctx->dead_list_size == 0) {
94 CHECK_EQ(tctx->dead_next, 0);
95 ctx->dead_list_head = 0;
96 }
97 CHECK_EQ(tctx->status, ThreadStatusDead);
98 tctx->status = ThreadStatusInvalid;
99 tctx->reuse_count++;
100 tctx->sync.Reset();
101 tid = tctx->tid;
102 DestroyAndFree(tctx->dead_info);
103 } else {
104 StatInc(thr, StatThreadMaxTid);
105 tid = ctx->thread_seq++;
106 void *mem = internal_alloc(MBlockThreadContex, sizeof(ThreadContext));
107 tctx = new(mem) ThreadContext(tid);
108 ctx->threads[tid] = tctx;
109 }
110 CHECK_NE(tctx, 0);
111 CHECK_GE(tid, 0);
112 CHECK_LT(tid, kMaxTid);
113 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
114 CHECK_EQ(tctx->status, ThreadStatusInvalid);
115 ctx->alive_threads++;
116 if (ctx->max_alive_threads < ctx->alive_threads) {
117 ctx->max_alive_threads++;
118 CHECK_EQ(ctx->max_alive_threads, ctx->alive_threads);
119 StatInc(thr, StatThreadMaxAlive);
120 }
121 tctx->status = ThreadStatusCreated;
122 tctx->thr = 0;
123 tctx->user_id = uid;
124 tctx->unique_id = ctx->unique_thread_seq++;
125 tctx->detached = detached;
126 if (tid) {
127 thr->fast_state.IncrementEpoch();
128 // Can't increment epoch w/o writing to the trace as well.
129 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
130 thr->clock.set(thr->tid, thr->fast_state.epoch());
131 thr->fast_synch_epoch = thr->fast_state.epoch();
132 thr->clock.release(&tctx->sync);
133 StatInc(thr, StatSyncRelease);
134
135 tctx->creation_stack.ObtainCurrent(thr, pc);
136 }
137 return tid;
138 }
139
ThreadStart(ThreadState * thr,int tid)140 void ThreadStart(ThreadState *thr, int tid) {
141 CHECK_GT(thr->in_rtl, 0);
142 uptr stk_addr = 0;
143 uptr stk_size = 0;
144 uptr tls_addr = 0;
145 uptr tls_size = 0;
146 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
147
148 if (tid) {
149 if (stk_addr && stk_size) {
150 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
151 }
152
153 if (tls_addr && tls_size) {
154 // Check that the thr object is in tls;
155 const uptr thr_beg = (uptr)thr;
156 const uptr thr_end = (uptr)thr + sizeof(*thr);
157 CHECK_GE(thr_beg, tls_addr);
158 CHECK_LE(thr_beg, tls_addr + tls_size);
159 CHECK_GE(thr_end, tls_addr);
160 CHECK_LE(thr_end, tls_addr + tls_size);
161 // Since the thr object is huge, skip it.
162 MemoryResetRange(thr, /*pc=*/ 2, tls_addr, thr_beg - tls_addr);
163 MemoryResetRange(thr, /*pc=*/ 2, thr_end, tls_addr + tls_size - thr_end);
164 }
165 }
166
167 Lock l(&CTX()->thread_mtx);
168 ThreadContext *tctx = CTX()->threads[tid];
169 CHECK_NE(tctx, 0);
170 CHECK_EQ(tctx->status, ThreadStatusCreated);
171 tctx->status = ThreadStatusRunning;
172 tctx->epoch0 = tctx->epoch1 + 1;
173 tctx->epoch1 = (u64)-1;
174 new(thr) ThreadState(CTX(), tid, tctx->unique_id,
175 tctx->epoch0, stk_addr, stk_size,
176 tls_addr, tls_size);
177 #ifdef TSAN_GO
178 // Setup dynamic shadow stack.
179 const int kInitStackSize = 8;
180 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
181 kInitStackSize * sizeof(uptr));
182 thr->shadow_stack_pos = thr->shadow_stack;
183 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
184 #endif
185 tctx->thr = thr;
186 thr->fast_synch_epoch = tctx->epoch0;
187 thr->clock.set(tid, tctx->epoch0);
188 thr->clock.acquire(&tctx->sync);
189 StatInc(thr, StatSyncAcquire);
190 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
191 "tls_addr=%zx tls_size=%zx\n",
192 tid, (uptr)tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
193 thr->is_alive = true;
194 }
195
ThreadFinish(ThreadState * thr)196 void ThreadFinish(ThreadState *thr) {
197 CHECK_GT(thr->in_rtl, 0);
198 StatInc(thr, StatThreadFinish);
199 // FIXME: Treat it as write.
200 if (thr->stk_addr && thr->stk_size)
201 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
202 if (thr->tls_addr && thr->tls_size) {
203 const uptr thr_beg = (uptr)thr;
204 const uptr thr_end = (uptr)thr + sizeof(*thr);
205 // Since the thr object is huge, skip it.
206 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
207 MemoryResetRange(thr, /*pc=*/ 5,
208 thr_end, thr->tls_addr + thr->tls_size - thr_end);
209 }
210 thr->is_alive = false;
211 Context *ctx = CTX();
212 Lock l(&ctx->thread_mtx);
213 ThreadContext *tctx = ctx->threads[thr->tid];
214 CHECK_NE(tctx, 0);
215 CHECK_EQ(tctx->status, ThreadStatusRunning);
216 CHECK_GT(ctx->alive_threads, 0);
217 ctx->alive_threads--;
218 if (tctx->detached) {
219 ThreadDead(thr, tctx);
220 } else {
221 thr->fast_state.IncrementEpoch();
222 // Can't increment epoch w/o writing to the trace as well.
223 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
224 thr->clock.set(thr->tid, thr->fast_state.epoch());
225 thr->fast_synch_epoch = thr->fast_state.epoch();
226 thr->clock.release(&tctx->sync);
227 StatInc(thr, StatSyncRelease);
228 tctx->status = ThreadStatusFinished;
229 }
230
231 // Save from info about the thread.
232 tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
233 ThreadDeadInfo();
234 internal_memcpy(&tctx->dead_info->trace.events[0],
235 &thr->trace.events[0], sizeof(thr->trace.events));
236 for (int i = 0; i < kTraceParts; i++) {
237 tctx->dead_info->trace.headers[i].stack0.CopyFrom(
238 thr->trace.headers[i].stack0);
239 }
240 tctx->epoch1 = thr->fast_state.epoch();
241
242 #ifndef TSAN_GO
243 AlloctorThreadFinish(thr);
244 #endif
245 thr->~ThreadState();
246 StatAggregate(ctx->stat, thr->stat);
247 tctx->thr = 0;
248 }
249
ThreadTid(ThreadState * thr,uptr pc,uptr uid)250 int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
251 CHECK_GT(thr->in_rtl, 0);
252 Context *ctx = CTX();
253 Lock l(&ctx->thread_mtx);
254 int res = -1;
255 for (unsigned tid = 0; tid < kMaxTid; tid++) {
256 ThreadContext *tctx = ctx->threads[tid];
257 if (tctx != 0 && tctx->user_id == uid
258 && tctx->status != ThreadStatusInvalid) {
259 tctx->user_id = 0;
260 res = tid;
261 break;
262 }
263 }
264 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
265 return res;
266 }
267
ThreadJoin(ThreadState * thr,uptr pc,int tid)268 void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
269 CHECK_GT(thr->in_rtl, 0);
270 CHECK_GT(tid, 0);
271 CHECK_LT(tid, kMaxTid);
272 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
273 Context *ctx = CTX();
274 Lock l(&ctx->thread_mtx);
275 ThreadContext *tctx = ctx->threads[tid];
276 if (tctx->status == ThreadStatusInvalid) {
277 TsanPrintf("ThreadSanitizer: join of non-existent thread\n");
278 return;
279 }
280 CHECK_EQ(tctx->detached, false);
281 CHECK_EQ(tctx->status, ThreadStatusFinished);
282 thr->clock.acquire(&tctx->sync);
283 StatInc(thr, StatSyncAcquire);
284 ThreadDead(thr, tctx);
285 }
286
ThreadDetach(ThreadState * thr,uptr pc,int tid)287 void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
288 CHECK_GT(thr->in_rtl, 0);
289 CHECK_GT(tid, 0);
290 CHECK_LT(tid, kMaxTid);
291 Context *ctx = CTX();
292 Lock l(&ctx->thread_mtx);
293 ThreadContext *tctx = ctx->threads[tid];
294 if (tctx->status == ThreadStatusInvalid) {
295 TsanPrintf("ThreadSanitizer: detach of non-existent thread\n");
296 return;
297 }
298 if (tctx->status == ThreadStatusFinished) {
299 ThreadDead(thr, tctx);
300 } else {
301 tctx->detached = true;
302 }
303 }
304
ThreadFinalizerGoroutine(ThreadState * thr)305 void ThreadFinalizerGoroutine(ThreadState *thr) {
306 thr->clock.Disable(thr->tid);
307 }
308
MemoryAccessRange(ThreadState * thr,uptr pc,uptr addr,uptr size,bool is_write)309 void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
310 uptr size, bool is_write) {
311 if (size == 0)
312 return;
313
314 u64 *shadow_mem = (u64*)MemToShadow(addr);
315 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
316 thr->tid, (void*)pc, (void*)addr,
317 (int)size, is_write);
318
319 #if TSAN_DEBUG
320 if (!IsAppMem(addr)) {
321 TsanPrintf("Access to non app mem %zx\n", addr);
322 DCHECK(IsAppMem(addr));
323 }
324 if (!IsAppMem(addr + size - 1)) {
325 TsanPrintf("Access to non app mem %zx\n", addr + size - 1);
326 DCHECK(IsAppMem(addr + size - 1));
327 }
328 if (!IsShadowMem((uptr)shadow_mem)) {
329 TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
330 DCHECK(IsShadowMem((uptr)shadow_mem));
331 }
332 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
333 TsanPrintf("Bad shadow addr %p (%zx)\n",
334 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
335 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
336 }
337 #endif
338
339 StatInc(thr, StatMopRange);
340
341 FastState fast_state = thr->fast_state;
342 if (fast_state.GetIgnoreBit())
343 return;
344
345 fast_state.IncrementEpoch();
346 thr->fast_state = fast_state;
347 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
348
349 bool unaligned = (addr % kShadowCell) != 0;
350
351 // Handle unaligned beginning, if any.
352 for (; addr % kShadowCell && size; addr++, size--) {
353 int const kAccessSizeLog = 0;
354 Shadow cur(fast_state);
355 cur.SetWrite(is_write);
356 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
357 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
358 shadow_mem, cur);
359 }
360 if (unaligned)
361 shadow_mem += kShadowCnt;
362 // Handle middle part, if any.
363 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
364 int const kAccessSizeLog = 3;
365 Shadow cur(fast_state);
366 cur.SetWrite(is_write);
367 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
368 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
369 shadow_mem, cur);
370 shadow_mem += kShadowCnt;
371 }
372 // Handle ending, if any.
373 for (; size; addr++, size--) {
374 int const kAccessSizeLog = 0;
375 Shadow cur(fast_state);
376 cur.SetWrite(is_write);
377 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
378 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
379 shadow_mem, cur);
380 }
381 }
382
MemoryRead1Byte(ThreadState * thr,uptr pc,uptr addr)383 void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr) {
384 MemoryAccess(thr, pc, addr, 0, 0);
385 }
386
MemoryWrite1Byte(ThreadState * thr,uptr pc,uptr addr)387 void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr) {
388 MemoryAccess(thr, pc, addr, 0, 1);
389 }
390
MemoryRead8Byte(ThreadState * thr,uptr pc,uptr addr)391 void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr) {
392 MemoryAccess(thr, pc, addr, 3, 0);
393 }
394
MemoryWrite8Byte(ThreadState * thr,uptr pc,uptr addr)395 void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr) {
396 MemoryAccess(thr, pc, addr, 3, 1);
397 }
398 } // namespace __tsan
399