blob: 6ca8ca7ab64b052c6539caed57a7bea3ded52261 [file] [log] [blame]
Alexey Samsonov3b2f9f42012-06-04 13:55:19 +00001//===-- tsan_rtl.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// Main file (entry points) for the TSan run-time.
13//===----------------------------------------------------------------------===//
14
Dmitry Vyukov6fa46f72012-06-29 16:58:33 +000015#include "sanitizer_common/sanitizer_atomic.h"
Alexey Samsonov58a3c582012-06-18 08:44:30 +000016#include "sanitizer_common/sanitizer_common.h"
Kostya Serebryanyc5bea202012-05-31 13:42:53 +000017#include "sanitizer_common/sanitizer_libc.h"
Dmitry Vyukov318f7772012-08-31 17:27:49 +000018#include "sanitizer_common/sanitizer_stackdepot.h"
Alexey Samsonov8bd90982012-06-07 09:50:16 +000019#include "sanitizer_common/sanitizer_placement_new.h"
Alexey Samsonovfdff4a82012-09-06 08:48:43 +000020#include "sanitizer_common/sanitizer_symbolizer.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000021#include "tsan_defs.h"
22#include "tsan_platform.h"
23#include "tsan_rtl.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000024#include "tsan_mman.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000025#include "tsan_suppressions.h"
26
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000027volatile int __tsan_resumed = 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000028
29extern "C" void __tsan_resume() {
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000030 __tsan_resumed = 1;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000031}
32
33namespace __tsan {
34
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +000035#ifndef TSAN_GO
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000036THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +000037#endif
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000038static char ctx_placeholder[sizeof(Context)] ALIGNED(64);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000039
40static Context *ctx;
41Context *CTX() {
42 return ctx;
43}
44
45Context::Context()
46 : initialized()
47 , report_mtx(MutexTypeReport, StatMtxReport)
48 , nreported()
49 , nmissed_expected()
50 , thread_mtx(MutexTypeThreads, StatMtxThreads)
51 , racy_stacks(MBlockRacyStacks)
52 , racy_addresses(MBlockRacyAddresses) {
53}
54
55// The objects are allocated in TLS, so one may rely on zero-initialization.
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000056ThreadState::ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000057 uptr stk_addr, uptr stk_size,
58 uptr tls_addr, uptr tls_size)
59 : fast_state(tid, epoch)
60 // Do not touch these, rely on zero initialization,
61 // they may be accessed before the ctor.
62 // , fast_ignore_reads()
63 // , fast_ignore_writes()
64 // , in_rtl()
65 , shadow_stack_pos(&shadow_stack[0])
66 , tid(tid)
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000067 , unique_id(unique_id)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000068 , stk_addr(stk_addr)
69 , stk_size(stk_size)
70 , tls_addr(tls_addr)
71 , tls_size(tls_size) {
72}
73
74ThreadContext::ThreadContext(int tid)
75 : tid(tid)
76 , unique_id()
77 , user_id()
78 , thr()
79 , status(ThreadStatusInvalid)
80 , detached()
81 , reuse_count()
82 , epoch0()
83 , epoch1()
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000084 , dead_info()
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000085 , dead_next() {
86}
87
Dmitry Vyukov15710c92012-05-22 11:33:03 +000088static void WriteMemoryProfile(char *buf, uptr buf_size, int num) {
89 uptr shadow = GetShadowMemoryConsumption();
90
91 int nthread = 0;
92 int nlivethread = 0;
93 uptr threadmem = 0;
94 {
95 Lock l(&ctx->thread_mtx);
96 for (unsigned i = 0; i < kMaxTid; i++) {
97 ThreadContext *tctx = ctx->threads[i];
98 if (tctx == 0)
99 continue;
100 nthread += 1;
101 threadmem += sizeof(ThreadContext);
102 if (tctx->status != ThreadStatusRunning)
103 continue;
104 nlivethread += 1;
105 threadmem += sizeof(ThreadState);
106 }
107 }
108
109 uptr nsync = 0;
110 uptr syncmem = CTX()->synctab.GetMemoryConsumption(&nsync);
111
Alexey Samsonove1cb5242012-06-19 09:21:57 +0000112 internal_snprintf(buf, buf_size, "%d: shadow=%zuMB"
113 " thread=%zuMB(total=%d/live=%d)"
114 " sync=%zuMB(cnt=%zu)\n",
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000115 num,
116 shadow >> 20,
117 threadmem >> 20, nthread, nlivethread,
118 syncmem >> 20, nsync);
119}
120
121static void MemoryProfileThread(void *arg) {
122 ScopedInRtl in_rtl;
123 fd_t fd = (fd_t)(uptr)arg;
124 for (int i = 0; ; i++) {
Alexey Samsonov75e5fc32012-08-22 07:25:52 +0000125 InternalScopedBuffer<char> buf(4096);
Alexey Samsonovceffb022012-09-05 07:23:44 +0000126 WriteMemoryProfile(buf.data(), buf.size(), i);
127 internal_write(fd, buf.data(), internal_strlen(buf.data()));
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000128 SleepForSeconds(1);
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000129 }
130}
131
132static void InitializeMemoryProfile() {
133 if (flags()->profile_memory == 0 || flags()->profile_memory[0] == 0)
134 return;
Alexey Samsonov75e5fc32012-08-22 07:25:52 +0000135 InternalScopedBuffer<char> filename(4096);
Alexey Samsonovceffb022012-09-05 07:23:44 +0000136 internal_snprintf(filename.data(), filename.size(), "%s.%d",
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000137 flags()->profile_memory, GetPid());
Alexey Samsonovceffb022012-09-05 07:23:44 +0000138 fd_t fd = internal_open(filename.data(), true);
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000139 if (fd == kInvalidFd) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000140 TsanPrintf("Failed to open memory profile file '%s'\n", &filename[0]);
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000141 Die();
142 }
143 internal_start_thread(&MemoryProfileThread, (void*)(uptr)fd);
144}
145
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000146static void MemoryFlushThread(void *arg) {
147 ScopedInRtl in_rtl;
148 for (int i = 0; ; i++) {
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000149 SleepForMillis(flags()->flush_memory_ms);
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000150 FlushShadowMemory();
151 }
152}
153
154static void InitializeMemoryFlush() {
155 if (flags()->flush_memory_ms == 0)
156 return;
157 if (flags()->flush_memory_ms < 100)
158 flags()->flush_memory_ms = 100;
159 internal_start_thread(&MemoryFlushThread, 0);
160}
161
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162void Initialize(ThreadState *thr) {
163 // Thread safe because done before all threads exist.
164 static bool is_initialized = false;
165 if (is_initialized)
166 return;
167 is_initialized = true;
168 ScopedInRtl in_rtl;
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000169#ifndef TSAN_GO
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000170 InitializeAllocator();
Dmitry Vyukov9f143c52012-08-16 19:36:45 +0000171#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000172 InitializeInterceptors();
173 const char *env = InitializePlatform();
174 InitializeMutex();
175 InitializeDynamicAnnotations();
176 ctx = new(ctx_placeholder) Context;
177 InitializeShadowMemory();
178 ctx->dead_list_size = 0;
179 ctx->dead_list_head = 0;
180 ctx->dead_list_tail = 0;
181 InitializeFlags(&ctx->flags, env);
182 InitializeSuppressions();
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000183 InitializeMemoryProfile();
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000184 InitializeMemoryFlush();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000185
Alexey Samsonovfdff4a82012-09-06 08:48:43 +0000186 const char *external_symbolizer = flags()->external_symbolizer_path;
187 if (external_symbolizer != 0 && external_symbolizer[0] != '\0') {
188 InitializeExternalSymbolizer(external_symbolizer);
189 }
190
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000191 if (ctx->flags.verbosity)
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000192 TsanPrintf("***** Running under ThreadSanitizer v2 (pid %d) *****\n",
193 GetPid());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000194
195 // Initialize thread 0.
196 ctx->thread_seq = 0;
197 int tid = ThreadCreate(thr, 0, 0, true);
198 CHECK_EQ(tid, 0);
199 ThreadStart(thr, tid);
200 CHECK_EQ(thr->in_rtl, 1);
201 ctx->initialized = true;
202
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000203 if (flags()->stop_on_start) {
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000204 TsanPrintf("ThreadSanitizer is suspended at startup (pid %d)."
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000205 " Call __tsan_resume().\n",
206 GetPid());
207 while (__tsan_resumed == 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000208 }
209}
210
211int Finalize(ThreadState *thr) {
212 ScopedInRtl in_rtl;
213 Context *ctx = __tsan::ctx;
214 bool failed = false;
215
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000216 ThreadFinalize(thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000217
218 if (ctx->nreported) {
219 failed = true;
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000220 TsanPrintf("ThreadSanitizer: reported %d warnings\n", ctx->nreported);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000221 }
222
223 if (ctx->nmissed_expected) {
224 failed = true;
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000225 TsanPrintf("ThreadSanitizer: missed %d expected races\n",
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 ctx->nmissed_expected);
227 }
228
229 StatOutput(ctx->stat);
Dmitry Vyukov19b855f2012-05-17 15:00:27 +0000230 return failed ? flags()->exitcode : 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000231}
232
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000233u32 CurrentStackId(ThreadState *thr, uptr pc) {
234 if (thr->shadow_stack_pos == 0) // May happen during bootstrap.
235 return 0;
236 if (pc) {
237 thr->shadow_stack_pos[0] = pc;
238 thr->shadow_stack_pos++;
239 }
240 u32 id = StackDepotPut(thr->shadow_stack,
241 thr->shadow_stack_pos - thr->shadow_stack);
242 if (pc)
243 thr->shadow_stack_pos--;
244 return id;
245}
246
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000247void TraceSwitch(ThreadState *thr) {
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000248 thr->nomalloc++;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000249 ScopedInRtl in_rtl;
250 Lock l(&thr->trace.mtx);
251 unsigned trace = (thr->fast_state.epoch() / kTracePartSize) % kTraceParts;
252 TraceHeader *hdr = &thr->trace.headers[trace];
253 hdr->epoch0 = thr->fast_state.epoch();
254 hdr->stack0.ObtainCurrent(thr, 0);
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000255 thr->nomalloc--;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000256}
257
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000258#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000259extern "C" void __tsan_trace_switch() {
260 TraceSwitch(cur_thread());
261}
262
263extern "C" void __tsan_report_race() {
264 ReportRace(cur_thread());
265}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000266#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000267
268ALWAYS_INLINE
269static Shadow LoadShadow(u64 *p) {
270 u64 raw = atomic_load((atomic_uint64_t*)p, memory_order_relaxed);
271 return Shadow(raw);
272}
273
274ALWAYS_INLINE
275static void StoreShadow(u64 *sp, u64 s) {
276 atomic_store((atomic_uint64_t*)sp, s, memory_order_relaxed);
277}
278
279ALWAYS_INLINE
280static void StoreIfNotYetStored(u64 *sp, u64 *s) {
281 StoreShadow(sp, *s);
282 *s = 0;
283}
284
285static inline void HandleRace(ThreadState *thr, u64 *shadow_mem,
286 Shadow cur, Shadow old) {
287 thr->racy_state[0] = cur.raw();
288 thr->racy_state[1] = old.raw();
289 thr->racy_shadow_addr = shadow_mem;
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000290#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000291 HACKY_CALL(__tsan_report_race);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000292#else
293 ReportRace(thr);
294#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295}
296
297static inline bool BothReads(Shadow s, int kAccessIsWrite) {
298 return !kAccessIsWrite && !s.is_write();
299}
300
301static inline bool OldIsRWStronger(Shadow old, int kAccessIsWrite) {
302 return old.is_write() || !kAccessIsWrite;
303}
304
305static inline bool OldIsRWWeaker(Shadow old, int kAccessIsWrite) {
306 return !old.is_write() || kAccessIsWrite;
307}
308
309static inline bool OldIsInSameSynchEpoch(Shadow old, ThreadState *thr) {
310 return old.epoch() >= thr->fast_synch_epoch;
311}
312
313static inline bool HappensBefore(Shadow old, ThreadState *thr) {
314 return thr->clock.get(old.tid()) >= old.epoch();
315}
316
317ALWAYS_INLINE
318void MemoryAccessImpl(ThreadState *thr, uptr addr,
319 int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state,
320 u64 *shadow_mem, Shadow cur) {
321 StatInc(thr, StatMop);
322 StatInc(thr, kAccessIsWrite ? StatMopWrite : StatMopRead);
323 StatInc(thr, (StatType)(StatMop1 + kAccessSizeLog));
324
325 // This potentially can live in an MMX/SSE scratch register.
326 // The required intrinsics are:
327 // __m128i _mm_move_epi64(__m128i*);
328 // _mm_storel_epi64(u64*, __m128i);
329 u64 store_word = cur.raw();
330
331 // scan all the shadow values and dispatch to 4 categories:
332 // same, replace, candidate and race (see comments below).
333 // we consider only 3 cases regarding access sizes:
334 // equal, intersect and not intersect. initially I considered
335 // larger and smaller as well, it allowed to replace some
336 // 'candidates' with 'same' or 'replace', but I think
337 // it's just not worth it (performance- and complexity-wise).
338
339 Shadow old(0);
340 if (kShadowCnt == 1) {
341 int idx = 0;
342#include "tsan_update_shadow_word_inl.h"
343 } else if (kShadowCnt == 2) {
344 int idx = 0;
345#include "tsan_update_shadow_word_inl.h"
346 idx = 1;
347#include "tsan_update_shadow_word_inl.h"
348 } else if (kShadowCnt == 4) {
349 int idx = 0;
350#include "tsan_update_shadow_word_inl.h"
351 idx = 1;
352#include "tsan_update_shadow_word_inl.h"
353 idx = 2;
354#include "tsan_update_shadow_word_inl.h"
355 idx = 3;
356#include "tsan_update_shadow_word_inl.h"
357 } else if (kShadowCnt == 8) {
358 int idx = 0;
359#include "tsan_update_shadow_word_inl.h"
360 idx = 1;
361#include "tsan_update_shadow_word_inl.h"
362 idx = 2;
363#include "tsan_update_shadow_word_inl.h"
364 idx = 3;
365#include "tsan_update_shadow_word_inl.h"
366 idx = 4;
367#include "tsan_update_shadow_word_inl.h"
368 idx = 5;
369#include "tsan_update_shadow_word_inl.h"
370 idx = 6;
371#include "tsan_update_shadow_word_inl.h"
372 idx = 7;
373#include "tsan_update_shadow_word_inl.h"
374 } else {
375 CHECK(false);
376 }
377
378 // we did not find any races and had already stored
379 // the current access info, so we are done
380 if (LIKELY(store_word == 0))
381 return;
382 // choose a random candidate slot and replace it
383 StoreShadow(shadow_mem + (cur.epoch() % kShadowCnt), store_word);
384 StatInc(thr, StatShadowReplace);
385 return;
386 RACE:
387 HandleRace(thr, shadow_mem, cur, old);
388 return;
389}
390
391ALWAYS_INLINE
392void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
393 int kAccessSizeLog, bool kAccessIsWrite) {
394 u64 *shadow_mem = (u64*)MemToShadow(addr);
395 DPrintf2("#%d: tsan::OnMemoryAccess: @%p %p size=%d"
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000396 " is_write=%d shadow_mem=%p {%zx, %zx, %zx, %zx}\n",
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000397 (int)thr->fast_state.tid(), (void*)pc, (void*)addr,
398 (int)(1 << kAccessSizeLog), kAccessIsWrite, shadow_mem,
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000399 (uptr)shadow_mem[0], (uptr)shadow_mem[1],
400 (uptr)shadow_mem[2], (uptr)shadow_mem[3]);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000401#if TSAN_DEBUG
402 if (!IsAppMem(addr)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000403 TsanPrintf("Access to non app mem %zx\n", addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000404 DCHECK(IsAppMem(addr));
405 }
406 if (!IsShadowMem((uptr)shadow_mem)) {
Alexey Samsonov51ae9832012-06-06 13:11:29 +0000407 TsanPrintf("Bad shadow addr %p (%zx)\n", shadow_mem, addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000408 DCHECK(IsShadowMem((uptr)shadow_mem));
409 }
410#endif
411
412 FastState fast_state = thr->fast_state;
413 if (fast_state.GetIgnoreBit())
414 return;
415 fast_state.IncrementEpoch();
416 thr->fast_state = fast_state;
417 Shadow cur(fast_state);
418 cur.SetAddr0AndSizeLog(addr & 7, kAccessSizeLog);
419 cur.SetWrite(kAccessIsWrite);
420
421 // We must not store to the trace if we do not store to the shadow.
422 // That is, this call must be moved somewhere below.
423 TraceAddEvent(thr, fast_state.epoch(), EventTypeMop, pc);
424
425 MemoryAccessImpl(thr, addr, kAccessSizeLog, kAccessIsWrite, fast_state,
426 shadow_mem, cur);
427}
428
429static void MemoryRangeSet(ThreadState *thr, uptr pc, uptr addr, uptr size,
430 u64 val) {
431 if (size == 0)
432 return;
433 // FIXME: fix me.
434 uptr offset = addr % kShadowCell;
435 if (offset) {
436 offset = kShadowCell - offset;
437 if (size <= offset)
438 return;
439 addr += offset;
440 size -= offset;
441 }
Dmitry Vyukov49dd68a2012-09-02 12:04:51 +0000442 DCHECK_EQ(addr % 8, 0);
443 // If a user passes some insane arguments (memset(0)),
444 // let it just crash as usual.
445 if (!IsAppMem(addr) || !IsAppMem(addr + size - 1))
446 return;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000447 (void)thr;
448 (void)pc;
449 // Some programs mmap like hundreds of GBs but actually used a small part.
450 // So, it's better to report a false positive on the memory
451 // then to hang here senselessly.
452 const uptr kMaxResetSize = 1024*1024*1024;
453 if (size > kMaxResetSize)
454 size = kMaxResetSize;
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000455 size = (size + (kShadowCell - 1)) & ~(kShadowCell - 1);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000456 u64 *p = (u64*)MemToShadow(addr);
457 CHECK(IsShadowMem((uptr)p));
458 CHECK(IsShadowMem((uptr)(p + size * kShadowCnt / kShadowCell - 1)));
459 // FIXME: may overwrite a part outside the region
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000460 for (uptr i = 0; i < size * kShadowCnt / kShadowCell;) {
461 p[i++] = val;
462 for (uptr j = 1; j < kShadowCnt; j++)
463 p[i++] = 0;
464 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000465}
466
467void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size) {
468 MemoryRangeSet(thr, pc, addr, size, 0);
469}
470
471void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size) {
472 MemoryAccessRange(thr, pc, addr, size, true);
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000473 Shadow s(thr->fast_state);
474 s.MarkAsFreed();
475 s.SetWrite(true);
476 s.SetAddr0AndSizeLog(0, 3);
477 MemoryRangeSet(thr, pc, addr, size, s.raw());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000478}
479
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000480void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size) {
481 Shadow s(thr->fast_state);
482 s.SetWrite(true);
483 s.SetAddr0AndSizeLog(0, 3);
484 MemoryRangeSet(thr, pc, addr, size, s.raw());
485}
486
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000487void FuncEntry(ThreadState *thr, uptr pc) {
488 DCHECK_EQ(thr->in_rtl, 0);
489 StatInc(thr, StatFuncEnter);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000490 DPrintf2("#%d: FuncEntry %p\n", (int)thr->fast_state.tid(), (void*)pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000491 thr->fast_state.IncrementEpoch();
492 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncEnter, pc);
493
494 // Shadow stack maintenance can be replaced with
495 // stack unwinding during trace switch (which presumably must be faster).
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000496 DCHECK_GE(thr->shadow_stack_pos, &thr->shadow_stack[0]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000497#ifndef TSAN_GO
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000498 DCHECK_LT(thr->shadow_stack_pos, &thr->shadow_stack[kShadowStackSize]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000499#else
500 if (thr->shadow_stack_pos == thr->shadow_stack_end) {
501 const int sz = thr->shadow_stack_end - thr->shadow_stack;
502 const int newsz = 2 * sz;
503 uptr *newstack = (uptr*)internal_alloc(MBlockShadowStack,
504 newsz * sizeof(uptr));
505 internal_memcpy(newstack, thr->shadow_stack, sz * sizeof(uptr));
506 internal_free(thr->shadow_stack);
507 thr->shadow_stack = newstack;
508 thr->shadow_stack_pos = newstack + sz;
509 thr->shadow_stack_end = newstack + newsz;
510 }
511#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000512 thr->shadow_stack_pos[0] = pc;
513 thr->shadow_stack_pos++;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000514}
515
516void FuncExit(ThreadState *thr) {
517 DCHECK_EQ(thr->in_rtl, 0);
518 StatInc(thr, StatFuncExit);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000519 DPrintf2("#%d: FuncExit\n", (int)thr->fast_state.tid());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000520 thr->fast_state.IncrementEpoch();
521 TraceAddEvent(thr, thr->fast_state.epoch(), EventTypeFuncExit, 0);
522
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000523 DCHECK_GT(thr->shadow_stack_pos, &thr->shadow_stack[0]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000524#ifndef TSAN_GO
Dmitry Vyukov3de9ca02012-05-28 07:45:35 +0000525 DCHECK_LT(thr->shadow_stack_pos, &thr->shadow_stack[kShadowStackSize]);
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000526#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000527 thr->shadow_stack_pos--;
528}
529
530void IgnoreCtl(ThreadState *thr, bool write, bool begin) {
531 DPrintf("#%d: IgnoreCtl(%d, %d)\n", thr->tid, write, begin);
532 thr->ignore_reads_and_writes += begin ? 1 : -1;
533 CHECK_GE(thr->ignore_reads_and_writes, 0);
534 if (thr->ignore_reads_and_writes)
535 thr->fast_state.SetIgnoreBit();
536 else
537 thr->fast_state.ClearIgnoreBit();
538}
539
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000540bool MD5Hash::operator==(const MD5Hash &other) const {
541 return hash[0] == other.hash[0] && hash[1] == other.hash[1];
542}
543
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000544#if TSAN_DEBUG
545void build_consistency_debug() {}
546#else
547void build_consistency_release() {}
548#endif
549
550#if TSAN_COLLECT_STATS
551void build_consistency_stats() {}
552#else
553void build_consistency_nostats() {}
554#endif
555
556#if TSAN_SHADOW_COUNT == 1
557void build_consistency_shadow1() {}
558#elif TSAN_SHADOW_COUNT == 2
559void build_consistency_shadow2() {}
560#elif TSAN_SHADOW_COUNT == 4
561void build_consistency_shadow4() {}
562#else
563void build_consistency_shadow8() {}
564#endif
565
566} // namespace __tsan
567
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000568#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000569// Must be included in this file to make sure everything is inlined.
570#include "tsan_interface_inl.h"
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000571#endif