blob: 4de0c1d947ed8671285999a3afc194f3da84ea3e [file] [log] [blame]
Alexey Samsonov3b2f9f42012-06-04 13:55:19 +00001//===-- tsan_rtl_thread.cc ------------------------------------------------===//
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00002//
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 Samsonov8bd90982012-06-07 09:50:16 +000014#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000015#include "tsan_rtl.h"
16#include "tsan_mman.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000017#include "tsan_platform.h"
18#include "tsan_report.h"
19#include "tsan_sync.h"
20
21namespace __tsan {
22
Dmitry Vyukovdc36d692012-07-16 16:03:16 +000023#ifndef TSAN_GO
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000024const int kThreadQuarantineSize = 16;
Dmitry Vyukovdc36d692012-07-16 16:03:16 +000025#else
26const int kThreadQuarantineSize = 64;
27#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000028
29static 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
41void 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);
Kostya Serebryany07c48052012-05-11 14:42:24 +000047 for (unsigned i = 0; i < kMaxTid; i++) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048 ThreadContext *tctx = ctx->threads[i];
49 if (tctx == 0)
50 continue;
51 MaybeReportThreadLeak(tctx);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000052 }
53}
54
55static 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);
Alexey Samsonov51ae9832012-06-06 13:11:29 +000060 DPrintf("#%d: ThreadDead uid=%zu\n", thr->tid, tctx->user_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000061 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
75int 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) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +000085 TsanPrintf("ThreadSanitizer: %d thread limit exceeded. Dying.\n",
86 kMaxTid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000087 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++;
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000100 tctx->sync.Reset();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000101 tid = tctx->tid;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000102 DestroyAndFree(tctx->dead_info);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000103 } 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);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000113 DPrintf("#%d: ThreadCreate tid=%d uid=%zu\n", thr->tid, tid, uid);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000114 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
140void 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;
Dmitry Vyukov7339eb12012-05-25 11:15:04 +0000146 GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000147
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000148 if (tid) {
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000149 if (stk_addr && stk_size) {
150 MemoryResetRange(thr, /*pc=*/ 1, stk_addr, stk_size);
151 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000152
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000153 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 }
Dmitry Vyukov2d4e3c12012-05-28 07:44:34 +0000165 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166
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->epoch0, stk_addr, stk_size,
175 tls_addr, tls_size);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000176#ifdef TSAN_GO
177 // Setup dynamic shadow stack.
178 const int kInitStackSize = 8;
179 thr->shadow_stack = (uptr*)internal_alloc(MBlockShadowStack,
180 kInitStackSize * sizeof(uptr));
181 thr->shadow_stack_pos = thr->shadow_stack;
182 thr->shadow_stack_end = thr->shadow_stack + kInitStackSize;
183#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000184 tctx->thr = thr;
185 thr->fast_synch_epoch = tctx->epoch0;
186 thr->clock.set(tid, tctx->epoch0);
187 thr->clock.acquire(&tctx->sync);
188 StatInc(thr, StatSyncAcquire);
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000189 DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
190 "tls_addr=%zx tls_size=%zx\n",
191 tid, (uptr)tctx->epoch0, stk_addr, stk_size, tls_addr, tls_size);
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000192 thr->is_alive = true;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000193}
194
195void ThreadFinish(ThreadState *thr) {
196 CHECK_GT(thr->in_rtl, 0);
197 StatInc(thr, StatThreadFinish);
198 // FIXME: Treat it as write.
199 if (thr->stk_addr && thr->stk_size)
200 MemoryResetRange(thr, /*pc=*/ 3, thr->stk_addr, thr->stk_size);
201 if (thr->tls_addr && thr->tls_size) {
202 const uptr thr_beg = (uptr)thr;
203 const uptr thr_end = (uptr)thr + sizeof(*thr);
204 // Since the thr object is huge, skip it.
205 MemoryResetRange(thr, /*pc=*/ 4, thr->tls_addr, thr_beg - thr->tls_addr);
206 MemoryResetRange(thr, /*pc=*/ 5,
207 thr_end, thr->tls_addr + thr->tls_size - thr_end);
208 }
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000209 thr->is_alive = false;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000210 Context *ctx = CTX();
211 Lock l(&ctx->thread_mtx);
212 ThreadContext *tctx = ctx->threads[thr->tid];
213 CHECK_NE(tctx, 0);
214 CHECK_EQ(tctx->status, ThreadStatusRunning);
215 CHECK_GT(ctx->alive_threads, 0);
216 ctx->alive_threads--;
217 if (tctx->detached) {
218 ThreadDead(thr, tctx);
219 } else {
220 thr->fast_state.IncrementEpoch();
221 // Can't increment epoch w/o writing to the trace as well.
222 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeMop, 0);
223 thr->clock.set(thr->tid, thr->fast_state.epoch());
224 thr->fast_synch_epoch = thr->fast_state.epoch();
225 thr->clock.release(&tctx->sync);
226 StatInc(thr, StatSyncRelease);
227 tctx->status = ThreadStatusFinished;
228 }
229
230 // Save from info about the thread.
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000231 tctx->dead_info = new(internal_alloc(MBlockDeadInfo, sizeof(ThreadDeadInfo)))
232 ThreadDeadInfo();
Dmitry Vyukov090f3452012-06-27 21:00:23 +0000233 internal_memcpy(&tctx->dead_info->trace.events[0],
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000234 &thr->trace.events[0], sizeof(thr->trace.events));
235 for (int i = 0; i < kTraceParts; i++) {
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000236 tctx->dead_info->trace.headers[i].stack0.CopyFrom(
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000237 thr->trace.headers[i].stack0);
238 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000239 tctx->epoch1 = thr->fast_state.epoch();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000240
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000241#ifndef TSAN_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000242 AlloctorThreadFinish(thr);
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000243#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000244 thr->~ThreadState();
245 StatAggregate(ctx->stat, thr->stat);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000246 tctx->thr = 0;
247}
248
249int ThreadTid(ThreadState *thr, uptr pc, uptr uid) {
250 CHECK_GT(thr->in_rtl, 0);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000251 Context *ctx = CTX();
252 Lock l(&ctx->thread_mtx);
253 int res = -1;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000254 for (unsigned tid = 0; tid < kMaxTid; tid++) {
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000255 ThreadContext *tctx = ctx->threads[tid];
256 if (tctx != 0 && tctx->user_id == uid
257 && tctx->status != ThreadStatusInvalid) {
258 tctx->user_id = 0;
259 res = tid;
260 break;
261 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000262 }
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000263 DPrintf("#%d: ThreadTid uid=%zu tid=%d\n", thr->tid, uid, res);
Dmitry Vyukov880bb662012-05-28 17:32:50 +0000264 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000265}
266
267void ThreadJoin(ThreadState *thr, uptr pc, int tid) {
268 CHECK_GT(thr->in_rtl, 0);
269 CHECK_GT(tid, 0);
270 CHECK_LT(tid, kMaxTid);
271 DPrintf("#%d: ThreadJoin tid=%d\n", thr->tid, tid);
272 Context *ctx = CTX();
273 Lock l(&ctx->thread_mtx);
274 ThreadContext *tctx = ctx->threads[tid];
275 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000276 TsanPrintf("ThreadSanitizer: join of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000277 return;
278 }
279 CHECK_EQ(tctx->detached, false);
280 CHECK_EQ(tctx->status, ThreadStatusFinished);
281 thr->clock.acquire(&tctx->sync);
282 StatInc(thr, StatSyncAcquire);
283 ThreadDead(thr, tctx);
284}
285
286void ThreadDetach(ThreadState *thr, uptr pc, int tid) {
287 CHECK_GT(thr->in_rtl, 0);
288 CHECK_GT(tid, 0);
289 CHECK_LT(tid, kMaxTid);
290 Context *ctx = CTX();
291 Lock l(&ctx->thread_mtx);
292 ThreadContext *tctx = ctx->threads[tid];
293 if (tctx->status == ThreadStatusInvalid) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000294 TsanPrintf("ThreadSanitizer: detach of non-existent thread\n");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295 return;
296 }
297 if (tctx->status == ThreadStatusFinished) {
298 ThreadDead(thr, tctx);
299 } else {
300 tctx->detached = true;
301 }
302}
303
Dmitry Vyukovdfc8e522012-07-25 13:16:35 +0000304void ThreadFinalizerGoroutine(ThreadState *thr) {
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000305 thr->clock.Disable(thr->tid);
Dmitry Vyukovdfc8e522012-07-25 13:16:35 +0000306}
307
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000308void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
309 uptr size, bool is_write) {
310 if (size == 0)
311 return;
312
313 u64 *shadow_mem = (u64*)MemToShadow(addr);
314 DPrintf2("#%d: MemoryAccessRange: @%p %p size=%d is_write=%d\n",
315 thr->tid, (void*)pc, (void*)addr,
316 (int)size, is_write);
317
318#if TSAN_DEBUG
319 if (!IsAppMem(addr)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000320 TsanPrintf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000321 DCHECK(IsAppMem(addr));
322 }
323 if (!IsAppMem(addr + size - 1)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000324 TsanPrintf("Access to non app mem %zx\n", addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000325 DCHECK(IsAppMem(addr + size - 1));
326 }
327 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000328 TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000329 DCHECK(IsShadowMem((uptr)shadow_mem));
330 }
331 if (!IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1))) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000332 TsanPrintf("Bad shadow addr %p (%zx)\n",
333 shadow_mem + size * kShadowCnt / 8 - 1, addr + size - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000334 DCHECK(IsShadowMem((uptr)(shadow_mem + size * kShadowCnt / 8 - 1)));
335 }
336#endif
337
338 StatInc(thr, StatMopRange);
339
340 FastState fast_state = thr->fast_state;
341 if (fast_state.GetIgnoreBit())
342 return;
343
344 fast_state.IncrementEpoch();
345 thr->fast_state = fast_state;
346 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
347
348 bool unaligned = (addr % kShadowCell) != 0;
349
350 // Handle unaligned beginning, if any.
351 for (; addr % kShadowCell && size; addr++, size--) {
352 int const kAccessSizeLog = 0;
353 Shadow cur(fast_state);
354 cur.SetWrite(is_write);
355 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
356 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
357 shadow_mem, cur);
358 }
359 if (unaligned)
360 shadow_mem += kShadowCnt;
361 // Handle middle part, if any.
362 for (; size >= kShadowCell; addr += kShadowCell, size -= kShadowCell) {
363 int const kAccessSizeLog = 3;
364 Shadow cur(fast_state);
365 cur.SetWrite(is_write);
366 cur.SetAddr0AndSizeLog(0, kAccessSizeLog);
367 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
368 shadow_mem, cur);
369 shadow_mem += kShadowCnt;
370 }
371 // Handle ending, if any.
372 for (; size; addr++, size--) {
373 int const kAccessSizeLog = 0;
374 Shadow cur(fast_state);
375 cur.SetWrite(is_write);
376 cur.SetAddr0AndSizeLog(addr & (kShadowCell - 1), kAccessSizeLog);
377 MemoryAccessImpl(thr, addr, kAccessSizeLog, is_write, fast_state,
378 shadow_mem, cur);
379 }
380}
381
382void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr) {
383 MemoryAccess(thr, pc, addr, 0, 0);
384}
385
386void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr) {
387 MemoryAccess(thr, pc, addr, 0, 1);
388}
389
390void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr) {
391 MemoryAccess(thr, pc, addr, 3, 0);
392}
393
394void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr) {
395 MemoryAccess(thr, pc, addr, 3, 1);
396}
397} // namespace __tsan