blob: af23edb1966f1782f7418ebafb7c81b754f1e33c [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_rtl.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// Main file (entry points) for the TSan run-time.
13//===----------------------------------------------------------------------===//
14
15#include "tsan_defs.h"
16#include "tsan_platform.h"
17#include "tsan_rtl.h"
18#include "tsan_interface.h"
19#include "tsan_atomic.h"
20#include "tsan_mman.h"
21#include "tsan_placement_new.h"
22#include "tsan_suppressions.h"
23
24volatile int __tsan_stop = 0;
25
26extern "C" void __tsan_resume() {
27 __tsan_stop = 0;
28}
29
30namespace __tsan {
31
32THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGN(64);
33static char ctx_placeholder[sizeof(Context)] ALIGN(64);
34
35static Context *ctx;
36Context *CTX() {
37 return ctx;
38}
39
40Context::Context()
41 : initialized()
42 , report_mtx(MutexTypeReport, StatMtxReport)
43 , nreported()
44 , nmissed_expected()
45 , thread_mtx(MutexTypeThreads, StatMtxThreads)
46 , racy_stacks(MBlockRacyStacks)
47 , racy_addresses(MBlockRacyAddresses) {
48}
49
50// The objects are allocated in TLS, so one may rely on zero-initialization.
51ThreadState::ThreadState(Context *ctx, int tid, u64 epoch,
52 uptr stk_addr, uptr stk_size,
53 uptr tls_addr, uptr tls_size)
54 : fast_state(tid, epoch)
55 // Do not touch these, rely on zero initialization,
56 // they may be accessed before the ctor.
57 // , fast_ignore_reads()
58 // , fast_ignore_writes()
59 // , in_rtl()
60 , shadow_stack_pos(&shadow_stack[0])
61 , tid(tid)
62 , func_call_count()
63 , stk_addr(stk_addr)
64 , stk_size(stk_size)
65 , tls_addr(tls_addr)
66 , tls_size(tls_size) {
67}
68
69ThreadContext::ThreadContext(int tid)
70 : tid(tid)
71 , unique_id()
72 , user_id()
73 , thr()
74 , status(ThreadStatusInvalid)
75 , detached()
76 , reuse_count()
77 , epoch0()
78 , epoch1()
79 , dead_next() {
80}
81
Dmitry Vyukov15710c92012-05-22 11:33:03 +000082static void WriteMemoryProfile(char *buf, uptr buf_size, int num) {
83 uptr shadow = GetShadowMemoryConsumption();
84
85 int nthread = 0;
86 int nlivethread = 0;
87 uptr threadmem = 0;
88 {
89 Lock l(&ctx->thread_mtx);
90 for (unsigned i = 0; i < kMaxTid; i++) {
91 ThreadContext *tctx = ctx->threads[i];
92 if (tctx == 0)
93 continue;
94 nthread += 1;
95 threadmem += sizeof(ThreadContext);
96 if (tctx->status != ThreadStatusRunning)
97 continue;
98 nlivethread += 1;
99 threadmem += sizeof(ThreadState);
100 }
101 }
102
103 uptr nsync = 0;
104 uptr syncmem = CTX()->synctab.GetMemoryConsumption(&nsync);
105
106 Snprintf(buf, buf_size, "%d: shadow=%luMB"
107 " thread=%luMB(total=%d/live=%d)"
108 " sync=%luMB(cnt=%lu)\n",
109 num,
110 shadow >> 20,
111 threadmem >> 20, nthread, nlivethread,
112 syncmem >> 20, nsync);
113}
114
115static void MemoryProfileThread(void *arg) {
116 ScopedInRtl in_rtl;
117 fd_t fd = (fd_t)(uptr)arg;
118 for (int i = 0; ; i++) {
119 InternalScopedBuf<char> buf(4096);
120 WriteMemoryProfile(buf.Ptr(), buf.Size(), i);
121 internal_write(fd, buf.Ptr(), internal_strlen(buf.Ptr()));
122 internal_sleep_ms(1000);
123 }
124}
125
126static void InitializeMemoryProfile() {
127 if (flags()->profile_memory == 0 || flags()->profile_memory[0] == 0)
128 return;
129 InternalScopedBuf<char> filename(4096);
130 Snprintf(filename.Ptr(), filename.Size(), "%s.%d",
131 flags()->profile_memory, GetPid());
132 fd_t fd = internal_open(filename.Ptr(), true);
133 if (fd == kInvalidFd) {
134 Printf("Failed to open memory profile file '%s'\n", &filename[0]);
135 Die();
136 }
137 internal_start_thread(&MemoryProfileThread, (void*)(uptr)fd);
138}
139
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000140void Initialize(ThreadState *thr) {
141 // Thread safe because done before all threads exist.
142 static bool is_initialized = false;
143 if (is_initialized)
144 return;
145 is_initialized = true;
146 ScopedInRtl in_rtl;
147 InitializeInterceptors();
148 const char *env = InitializePlatform();
149 InitializeMutex();
150 InitializeDynamicAnnotations();
151 ctx = new(ctx_placeholder) Context;
152 InitializeShadowMemory();
153 ctx->dead_list_size = 0;
154 ctx->dead_list_head = 0;
155 ctx->dead_list_tail = 0;
156 InitializeFlags(&ctx->flags, env);
157 InitializeSuppressions();
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000158 InitializeMemoryProfile();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159
160 if (ctx->flags.verbosity)
161 Printf("***** Running under ThreadSanitizer v2 (pid=%d) *****\n", GetPid());
162
163 // Initialize thread 0.
164 ctx->thread_seq = 0;
165 int tid = ThreadCreate(thr, 0, 0, true);
166 CHECK_EQ(tid, 0);
167 ThreadStart(thr, tid);
168 CHECK_EQ(thr->in_rtl, 1);
169 ctx->initialized = true;
170
171 if (__tsan_stop) {
172 Printf("ThreadSanitizer is suspended at startup.\n");
173 while (__tsan_stop);
174 }
175}
176
177int Finalize(ThreadState *thr) {
178 ScopedInRtl in_rtl;
179 Context *ctx = __tsan::ctx;
180 bool failed = false;
181
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000182 ThreadFinalize(thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000183
184 if (ctx->nreported) {
185 failed = true;
186 Printf("ThreadSanitizer: reported %d warnings\n", ctx->nreported);
187 }
188
189 if (ctx->nmissed_expected) {
190 failed = true;
191 Printf("ThreadSanitizer: missed %d expected races\n",
192 ctx->nmissed_expected);
193 }
194
195 StatOutput(ctx->stat);
Dmitry Vyukov19b855f2012-05-17 15:00:27 +0000196 return failed ? flags()->exitcode : 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000197}
198
199static void TraceSwitch(ThreadState *thr) {
200 ScopedInRtl in_rtl;
201 Lock l(&thr->trace.mtx);
202 unsigned trace = (thr->fast_state.epoch() / kTracePartSize) % kTraceParts;
203 TraceHeader *hdr = &thr->trace.headers[trace];
204 hdr->epoch0 = thr->fast_state.epoch();
205 hdr->stack0.ObtainCurrent(thr, 0);
206}
207
208extern "C" void __tsan_trace_switch() {
209 TraceSwitch(cur_thread());
210}
211
212extern "C" void __tsan_report_race() {
213 ReportRace(cur_thread());
214}
215
216ALWAYS_INLINE
217static Shadow LoadShadow(u64 *p) {
218 u64 raw = atomic_load((atomic_uint64_t*)p, memory_order_relaxed);
219 return Shadow(raw);
220}
221
222ALWAYS_INLINE
223static void StoreShadow(u64 *sp, u64 s) {
224 atomic_store((atomic_uint64_t*)sp, s, memory_order_relaxed);
225}
226
227ALWAYS_INLINE
228static void StoreIfNotYetStored(u64 *sp, u64 *s) {
229 StoreShadow(sp, *s);
230 *s = 0;
231}
232
233static inline void HandleRace(ThreadState *thr, u64 *shadow_mem,
234 Shadow cur, Shadow old) {
235 thr->racy_state[0] = cur.raw();
236 thr->racy_state[1] = old.raw();
237 thr->racy_shadow_addr = shadow_mem;
238 HACKY_CALL(__tsan_report_race);
239}
240
241static inline bool BothReads(Shadow s, int kAccessIsWrite) {
242 return !kAccessIsWrite && !s.is_write();
243}
244
245static inline bool OldIsRWStronger(Shadow old, int kAccessIsWrite) {
246 return old.is_write() || !kAccessIsWrite;
247}
248
249static inline bool OldIsRWWeaker(Shadow old, int kAccessIsWrite) {
250 return !old.is_write() || kAccessIsWrite;
251}
252
253static inline bool OldIsInSameSynchEpoch(Shadow old, ThreadState *thr) {
254 return old.epoch() >= thr->fast_synch_epoch;
255}
256
257static inline bool HappensBefore(Shadow old, ThreadState *thr) {
258 return thr->clock.get(old.tid()) >= old.epoch();
259}
260
261ALWAYS_INLINE
262void MemoryAccessImpl(ThreadState *thr, uptr addr,
263 int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state,
264 u64 *shadow_mem, Shadow cur) {
265 StatInc(thr, StatMop);
266 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
267 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
268
269 // This potentially can live in an MMX/SSE scratch register.
270 // The required intrinsics are:
271 // __m128i _mm_move_epi64(__m128i*);
272 // _mm_storel_epi64(u64*, __m128i);
273 u64 store_word = cur.raw();
274
275 // scan all the shadow values and dispatch to 4 categories:
276 // same, replace, candidate and race (see comments below).
277 // we consider only 3 cases regarding access sizes:
278 // equal, intersect and not intersect. initially I considered
279 // larger and smaller as well, it allowed to replace some
280 // 'candidates' with 'same' or 'replace', but I think
281 // it's just not worth it (performance- and complexity-wise).
282
283 Shadow old(0);
284 if (kShadowCnt == 1) {
285 int idx = 0;
286#include "tsan_update_shadow_word_inl.h"
287 } else if (kShadowCnt == 2) {
288 int idx = 0;
289#include "tsan_update_shadow_word_inl.h"
290 idx = 1;
291#include "tsan_update_shadow_word_inl.h"
292 } else if (kShadowCnt == 4) {
293 int idx = 0;
294#include "tsan_update_shadow_word_inl.h"
295 idx = 1;
296#include "tsan_update_shadow_word_inl.h"
297 idx = 2;
298#include "tsan_update_shadow_word_inl.h"
299 idx = 3;
300#include "tsan_update_shadow_word_inl.h"
301 } else if (kShadowCnt == 8) {
302 int idx = 0;
303#include "tsan_update_shadow_word_inl.h"
304 idx = 1;
305#include "tsan_update_shadow_word_inl.h"
306 idx = 2;
307#include "tsan_update_shadow_word_inl.h"
308 idx = 3;
309#include "tsan_update_shadow_word_inl.h"
310 idx = 4;
311#include "tsan_update_shadow_word_inl.h"
312 idx = 5;
313#include "tsan_update_shadow_word_inl.h"
314 idx = 6;
315#include "tsan_update_shadow_word_inl.h"
316 idx = 7;
317#include "tsan_update_shadow_word_inl.h"
318 } else {
319 CHECK(false);
320 }
321
322 // we did not find any races and had already stored
323 // the current access info, so we are done
324 if (LIKELY(store_word == 0))
325 return;
326 // choose a random candidate slot and replace it
327 StoreShadow(shadow_mem + (cur.epoch() % kShadowCnt), store_word);
328 StatInc(thr, StatShadowReplace);
329 return;
330 RACE:
331 HandleRace(thr, shadow_mem, cur, old);
332 return;
333}
334
335ALWAYS_INLINE
336void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
337 int kAccessSizeLog, bool kAccessIsWrite) {
338 u64 *shadow_mem = (u64*)MemToShadow(addr);
339 DPrintf2("#%d: tsan::OnMemoryAccess: @%p %p size=%d"
340 " is_write=%d shadow_mem=%p {%llx, %llx, %llx, %llx}\n",
341 (int)thr->fast_state.tid(), (void*)pc, (void*)addr,
342 (int)(1 << kAccessSizeLog), kAccessIsWrite, shadow_mem,
343 shadow_mem[0], shadow_mem[1], shadow_mem[2], shadow_mem[3]);
344#if TSAN_DEBUG
345 if (!IsAppMem(addr)) {
346 Printf("Access to non app mem %lx\n", addr);
347 DCHECK(IsAppMem(addr));
348 }
349 if (!IsShadowMem((uptr)shadow_mem)) {
350 Printf("Bad shadow addr %p (%lx)\n", shadow_mem, addr);
351 DCHECK(IsShadowMem((uptr)shadow_mem));
352 }
353#endif
354
355 FastState fast_state = thr->fast_state;
356 if (fast_state.GetIgnoreBit())
357 return;
358 fast_state.IncrementEpoch();
359 thr->fast_state = fast_state;
360 Shadow cur(fast_state);
361 cur.SetAddr0AndSizeLog(addr & 7, kAccessSizeLog);
362 cur.SetWrite(kAccessIsWrite);
363
364 // We must not store to the trace if we do not store to the shadow.
365 // That is, this call must be moved somewhere below.
366 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
367
368 MemoryAccessImpl(thr, addr, kAccessSizeLog, kAccessIsWrite, fast_state,
369 shadow_mem, cur);
370}
371
372static void MemoryRangeSet(ThreadState *thr, uptr pc, uptr addr, uptr size,
373 u64 val) {
374 if (size == 0)
375 return;
376 // FIXME: fix me.
377 uptr offset = addr % kShadowCell;
378 if (offset) {
379 offset = kShadowCell - offset;
380 if (size <= offset)
381 return;
382 addr += offset;
383 size -= offset;
384 }
385 CHECK_EQ(addr % 8, 0);
386 CHECK(IsAppMem(addr));
387 CHECK(IsAppMem(addr + size - 1));
388 (void)thr;
389 (void)pc;
390 // Some programs mmap like hundreds of GBs but actually used a small part.
391 // So, it's better to report a false positive on the memory
392 // then to hang here senselessly.
393 const uptr kMaxResetSize = 1024*1024*1024;
394 if (size > kMaxResetSize)
395 size = kMaxResetSize;
396 size = (size + 7) & ~7;
397 u64 *p = (u64*)MemToShadow(addr);
398 CHECK(IsShadowMem((uptr)p));
399 CHECK(IsShadowMem((uptr)(p + size * kShadowCnt / kShadowCell - 1)));
400 // FIXME: may overwrite a part outside the region
401 for (uptr i = 0; i < size * kShadowCnt / kShadowCell; i++)
402 p[i] = val;
403}
404
405void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size) {
406 MemoryRangeSet(thr, pc, addr, size, 0);
407}
408
409void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size) {
410 MemoryAccessRange(thr, pc, addr, size, true);
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000411 Shadow s(thr->fast_state);
412 s.MarkAsFreed();
413 s.SetWrite(true);
414 s.SetAddr0AndSizeLog(0, 3);
415 MemoryRangeSet(thr, pc, addr, size, s.raw());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000416}
417
418void FuncEntry(ThreadState *thr, uptr pc) {
419 DCHECK_EQ(thr->in_rtl, 0);
420 StatInc(thr, StatFuncEnter);
421 DPrintf2("#%d: tsan::FuncEntry %p\n", (int)thr->fast_state.tid(), (void*)pc);
422 thr->fast_state.IncrementEpoch();
423 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncEnter, pc);
424
425 // Shadow stack maintenance can be replaced with
426 // stack unwinding during trace switch (which presumably must be faster).
427 DCHECK(thr->shadow_stack_pos >= &thr->shadow_stack[0]);
428 DCHECK(thr->shadow_stack_pos < &thr->shadow_stack[kShadowStackSize]);
429 thr->shadow_stack_pos[0] = pc;
430 thr->shadow_stack_pos++;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000431}
432
433void FuncExit(ThreadState *thr) {
434 DCHECK_EQ(thr->in_rtl, 0);
435 StatInc(thr, StatFuncExit);
436 DPrintf2("#%d: tsan::FuncExit\n", (int)thr->fast_state.tid());
437 thr->fast_state.IncrementEpoch();
438 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncExit, 0);
439
440 DCHECK(thr->shadow_stack_pos > &thr->shadow_stack[0]);
441 DCHECK(thr->shadow_stack_pos < &thr->shadow_stack[kShadowStackSize]);
442 thr->shadow_stack_pos--;
443}
444
445void IgnoreCtl(ThreadState *thr, bool write, bool begin) {
446 DPrintf("#%d: IgnoreCtl(%d, %d)\n", thr->tid, write, begin);
447 thr->ignore_reads_and_writes += begin ? 1 : -1;
448 CHECK_GE(thr->ignore_reads_and_writes, 0);
449 if (thr->ignore_reads_and_writes)
450 thr->fast_state.SetIgnoreBit();
451 else
452 thr->fast_state.ClearIgnoreBit();
453}
454
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000455#if TSAN_DEBUG
456void build_consistency_debug() {}
457#else
458void build_consistency_release() {}
459#endif
460
461#if TSAN_COLLECT_STATS
462void build_consistency_stats() {}
463#else
464void build_consistency_nostats() {}
465#endif
466
467#if TSAN_SHADOW_COUNT == 1
468void build_consistency_shadow1() {}
469#elif TSAN_SHADOW_COUNT == 2
470void build_consistency_shadow2() {}
471#elif TSAN_SHADOW_COUNT == 4
472void build_consistency_shadow4() {}
473#else
474void build_consistency_shadow8() {}
475#endif
476
477} // namespace __tsan
478
479// Must be included in this file to make sure everything is inlined.
480#include "tsan_interface_inl.h"