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