blob: 8568f0af7daccd42a2ef3d73bda95bff986e6c9f [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_rtl.h ----------------------------------------------*- 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 internal TSan header file.
13//
14// Ground rules:
15// - C++ run-time should not be used (static CTORs, RTTI, exceptions, static
16// function-scope locals)
17// - All functions/classes/etc reside in namespace __tsan, except for those
18// declared in tsan_interface.h.
19// - Platform-specific files should be used instead of ifdefs (*).
20// - No system headers included in header files (*).
21// - Platform specific headres included only into platform-specific files (*).
22//
23// (*) Except when inlining is critical for performance.
24//===----------------------------------------------------------------------===//
25
26#ifndef TSAN_RTL_H
27#define TSAN_RTL_H
28
Alexey Samsonovbc3a7e32012-06-06 06:47:26 +000029#include "sanitizer_common/sanitizer_common.h"
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000030#include "sanitizer_common/sanitizer_allocator64.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000031#include "tsan_clock.h"
32#include "tsan_defs.h"
33#include "tsan_flags.h"
34#include "tsan_sync.h"
35#include "tsan_trace.h"
36#include "tsan_vector.h"
37#include "tsan_report.h"
38
39namespace __tsan {
40
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000041// Descriptor of user's memory block.
42struct MBlock {
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +000043 Mutex mtx;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000044 uptr size;
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +000045 SyncVar *head;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000046};
47
48#ifndef TSAN_GO
49#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukovf77c6ea2012-08-16 13:27:25 +000050const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000051#else
52const uptr kAllocatorSpace = 0x7d0000000000ULL;
53#endif
54const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
55
56typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
57 DefaultSizeClassMap> PrimaryAllocator;
58typedef SizeClassAllocatorLocalCache<PrimaryAllocator::kNumClasses,
59 PrimaryAllocator> AllocatorCache;
60typedef LargeMmapAllocator SecondaryAllocator;
61typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
62 SecondaryAllocator> Allocator;
63#endif
64
Alexey Samsonovd323f4e2012-06-06 13:58:39 +000065void TsanPrintf(const char *format, ...);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000066
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000067// FastState (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000068// unused : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000069// tid : kTidBits
70// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000071// unused : -
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000072// ignore_bit : 1
73class FastState {
74 public:
75 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000076 x_ = tid << kTidShift;
77 x_ |= epoch << kClkShift;
78 DCHECK(tid == this->tid());
79 DCHECK(epoch == this->epoch());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000080 }
81
82 explicit FastState(u64 x)
83 : x_(x) {
84 }
85
Dmitry Vyukov3482ec32012-08-16 15:08:49 +000086 u64 raw() const {
87 return x_;
88 }
89
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000090 u64 tid() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000091 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000092 return res;
93 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000094
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000095 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000096 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000097 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000098 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000099
100 void IncrementEpoch() {
101 u64 old_epoch = epoch();
102 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000103 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000104 (void)old_epoch;
105 }
106
107 void SetIgnoreBit() { x_ |= kIgnoreBit; }
108 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000109 bool GetIgnoreBit() const { return x_ & kIgnoreBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000110
111 private:
112 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000113 static const int kTidShift = 64 - kTidBits - 1;
114 static const int kClkShift = kTidShift - kClkBits;
115 static const u64 kIgnoreBit = 1ull;
116 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000117 u64 x_;
118};
119
120// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000121// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000122// tid : kTidBits
123// epoch : kClkBits
124// is_write : 1
125// size_log : 2
126// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000127class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000128 public:
129 explicit Shadow(u64 x) : FastState(x) { }
130
131 explicit Shadow(const FastState &s) : FastState(s.x_) { }
132
133 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
134 DCHECK_EQ(x_ & 31, 0);
135 DCHECK_LE(addr0, 7);
136 DCHECK_LE(kAccessSizeLog, 3);
137 x_ |= (kAccessSizeLog << 3) | addr0;
138 DCHECK_EQ(kAccessSizeLog, size_log());
139 DCHECK_EQ(addr0, this->addr0());
140 }
141
142 void SetWrite(unsigned kAccessIsWrite) {
143 DCHECK_EQ(x_ & 32, 0);
144 if (kAccessIsWrite)
145 x_ |= 32;
146 DCHECK_EQ(kAccessIsWrite, is_write());
147 }
148
149 bool IsZero() const { return x_ == 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000150
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000151 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000152 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000153 DCHECK_EQ(shifted_xor == 0, s1.tid() == s2.tid());
154 return shifted_xor == 0;
155 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000156
157 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000158 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
159 return masked_xor == 0;
160 }
161
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
163 unsigned kS2AccessSize) {
164 bool res = false;
165 u64 diff = s1.addr0() - s2.addr0();
166 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
167 // if (s1.addr0() + size1) > s2.addr0()) return true;
168 if (s1.size() > -diff) res = true;
169 } else {
170 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
171 if (kS2AccessSize > diff) res = true;
172 }
173 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
174 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
175 return res;
176 }
177
178 // The idea behind the offset is as follows.
179 // Consider that we have 8 bool's contained within a single 8-byte block
180 // (mapped to a single shadow "cell"). Now consider that we write to the bools
181 // from a single thread (which we consider the common case).
182 // W/o offsetting each access will have to scan 4 shadow values at average
183 // to find the corresponding shadow value for the bool.
184 // With offsetting we start scanning shadow with the offset so that
185 // each access hits necessary shadow straight off (at least in an expected
186 // optimistic case).
187 // This logic works seamlessly for any layout of user data. For example,
188 // if user data is {int, short, char, char}, then accesses to the int are
189 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
190 // from a single thread won't need to scan all 8 shadow values.
191 unsigned ComputeSearchOffset() {
192 return x_ & 7;
193 }
194 u64 addr0() const { return x_ & 7; }
195 u64 size() const { return 1ull << size_log(); }
196 bool is_write() const { return x_ & 32; }
197
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000198 // The idea behind the freed bit is as follows.
199 // When the memory is freed (or otherwise unaccessible) we write to the shadow
200 // values with tid/epoch related to the free and the freed bit set.
201 // During memory accesses processing the freed bit is considered
202 // as msb of tid. So any access races with shadow with freed bit set
203 // (it is as if write from a thread with which we never synchronized before).
204 // This allows us to detect accesses to freed memory w/o additional
205 // overheads in memory access processing and at the same time restore
206 // tid/epoch of free.
207 void MarkAsFreed() {
208 x_ |= kFreedBit;
209 }
210
211 bool GetFreedAndReset() {
212 bool res = x_ & kFreedBit;
213 x_ &= ~kFreedBit;
214 return res;
215 }
216
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000217 private:
218 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000219
220 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
221 if (s1.addr0() == s2.addr0()) return true;
222 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
223 return true;
224 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
225 return true;
226 return false;
227 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228};
229
230// Freed memory.
231// As if 8-byte write by thread 0xff..f at epoch 0xff..f, races with everything.
232const u64 kShadowFreed = 0xfffffffffffffff8ull;
233
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000234struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000235
236// This struct is stored in TLS.
237struct ThreadState {
238 FastState fast_state;
239 // Synch epoch represents the threads's epoch before the last synchronization
240 // action. It allows to reduce number of shadow state updates.
241 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
242 // if we are processing write to X from the same thread at epoch=200,
243 // we do nothing, because both writes happen in the same 'synch epoch'.
244 // That is, if another memory access does not race with the former write,
245 // it does not race with the latter as well.
246 // QUESTION: can we can squeeze this into ThreadState::Fast?
247 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
248 // taken by epoch between synchs.
249 // This way we can save one load from tls.
250 u64 fast_synch_epoch;
251 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
252 // We do not distinguish beteween ignoring reads and writes
253 // for better performance.
254 int ignore_reads_and_writes;
255 uptr *shadow_stack_pos;
256 u64 *racy_shadow_addr;
257 u64 racy_state[2];
258 Trace trace;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000259#ifndef TSAN_GO
260 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000261 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000262#else
263 // Go uses satellite shadow stack with dynamic size.
264 uptr *shadow_stack;
265 uptr *shadow_stack_end;
266#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000267 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000268#ifndef TSAN_GO
269 AllocatorCache alloc_cache;
270#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000271 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000272 const int tid;
273 int in_rtl;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000274 bool is_alive;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000275 const uptr stk_addr;
276 const uptr stk_size;
277 const uptr tls_addr;
278 const uptr tls_size;
279
280 DeadlockDetector deadlock_detector;
281
282 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000283 SignalContext *signal_ctx;
284
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000285 // Set in regions of runtime that must be signal-safe and fork-safe.
286 // If set, malloc must not be called.
287 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000288
289 explicit ThreadState(Context *ctx, int tid, u64 epoch,
290 uptr stk_addr, uptr stk_size,
291 uptr tls_addr, uptr tls_size);
292};
293
294Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000296#ifndef TSAN_GO
297extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298INLINE ThreadState *cur_thread() {
299 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
300}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000301#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000302
303enum ThreadStatus {
304 ThreadStatusInvalid, // Non-existent thread, data is invalid.
305 ThreadStatusCreated, // Created but not yet running.
306 ThreadStatusRunning, // The thread is currently running.
307 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
308 ThreadStatusDead, // Joined, but some info (trace) is still alive.
309};
310
311// An info about a thread that is hold for some time after its termination.
312struct ThreadDeadInfo {
313 Trace trace;
314};
315
316struct ThreadContext {
317 const int tid;
318 int unique_id; // Non-rolling thread id.
319 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
320 ThreadState *thr;
321 ThreadStatus status;
322 bool detached;
323 int reuse_count;
324 SyncClock sync;
325 // Epoch at which the thread had started.
326 // If we see an event from the thread stamped by an older epoch,
327 // the event is from a dead thread that shared tid with this thread.
328 u64 epoch0;
329 u64 epoch1;
330 StackTrace creation_stack;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000331 ThreadDeadInfo *dead_info;
332 ThreadContext *dead_next; // In dead thread list.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000333
334 explicit ThreadContext(int tid);
335};
336
337struct RacyStacks {
338 MD5Hash hash[2];
339 bool operator==(const RacyStacks &other) const {
340 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
341 return true;
342 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
343 return true;
344 return false;
345 }
346};
347
348struct RacyAddress {
349 uptr addr_min;
350 uptr addr_max;
351};
352
353struct Context {
354 Context();
355
356 bool initialized;
357
358 SyncTab synctab;
359
360 Mutex report_mtx;
361 int nreported;
362 int nmissed_expected;
363
364 Mutex thread_mtx;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000365 unsigned thread_seq;
366 unsigned unique_thread_seq;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000367 int alive_threads;
368 int max_alive_threads;
369 ThreadContext *threads[kMaxTid];
370 int dead_list_size;
371 ThreadContext* dead_list_head;
372 ThreadContext* dead_list_tail;
373
374 Vector<RacyStacks> racy_stacks;
375 Vector<RacyAddress> racy_addresses;
376
377 Flags flags;
378
379 u64 stat[StatCnt];
380 u64 int_alloc_cnt[MBlockTypeCount];
381 u64 int_alloc_siz[MBlockTypeCount];
382};
383
384class ScopedInRtl {
385 public:
386 ScopedInRtl();
387 ~ScopedInRtl();
388 private:
389 ThreadState*thr_;
390 int in_rtl_;
391 int errno_;
392};
393
394class ScopedReport {
395 public:
396 explicit ScopedReport(ReportType typ);
397 ~ScopedReport();
398
399 void AddStack(const StackTrace *stack);
400 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack);
401 void AddThread(const ThreadContext *tctx);
402 void AddMutex(const SyncVar *s);
403 void AddLocation(uptr addr, uptr size);
404
405 const ReportDesc *GetReport() const;
406
407 private:
408 Context *ctx_;
409 ReportDesc *rep_;
410
411 ScopedReport(const ScopedReport&);
412 void operator = (const ScopedReport&);
413};
414
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000415void RestoreStack(int tid, const u64 epoch, StackTrace *stk);
416
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000417void StatAggregate(u64 *dst, u64 *src);
418void StatOutput(u64 *stat);
419void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
420 if (kCollectStats)
421 thr->stat[typ] += n;
422}
423
424void InitializeShadowMemory();
425void InitializeInterceptors();
426void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000427
428void ReportRace(ThreadState *thr);
Dmitry Vyukov665ce2a2012-05-14 15:28:03 +0000429bool OutputReport(const ScopedReport &srep,
430 const ReportStack *suppress_stack = 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000431bool IsExpectedReport(uptr addr, uptr size);
432
433#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000434# define DPrintf TsanPrintf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000435#else
436# define DPrintf(...)
437#endif
438
439#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000440# define DPrintf2 TsanPrintf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000441#else
442# define DPrintf2(...)
443#endif
444
445void Initialize(ThreadState *thr);
446int Finalize(ThreadState *thr);
447
448void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
449 int kAccessSizeLog, bool kAccessIsWrite);
450void MemoryAccessImpl(ThreadState *thr, uptr addr,
451 int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state,
452 u64 *shadow_mem, Shadow cur);
453void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr);
454void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr);
455void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr);
456void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr);
457void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
458 uptr size, bool is_write);
459void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
460void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000461void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000462void IgnoreCtl(ThreadState *thr, bool write, bool begin);
463
464void FuncEntry(ThreadState *thr, uptr pc);
465void FuncExit(ThreadState *thr);
466
467int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
468void ThreadStart(ThreadState *thr, int tid);
469void ThreadFinish(ThreadState *thr);
470int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
471void ThreadJoin(ThreadState *thr, uptr pc, int tid);
472void ThreadDetach(ThreadState *thr, uptr pc, int tid);
473void ThreadFinalize(ThreadState *thr);
Dmitry Vyukovdfc8e522012-07-25 13:16:35 +0000474void ThreadFinalizerGoroutine(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000475
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000476void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
477 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000478void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
479void MutexLock(ThreadState *thr, uptr pc, uptr addr);
480void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
481void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
482void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
483void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
484
485void Acquire(ThreadState *thr, uptr pc, uptr addr);
486void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000487void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000488
489// The hacky call uses custom calling convention and an assembly thunk.
490// It is considerably faster that a normal call for the caller
491// if it is not executed (it is intended for slow paths from hot functions).
492// The trick is that the call preserves all registers and the compiler
493// does not treat it as a call.
494// If it does not work for you, use normal call.
495#if TSAN_DEBUG == 0
496// The caller may not create the stack frame for itself at all,
497// so we create a reserve stack frame for it (1024b must be enough).
498#define HACKY_CALL(f) \
499 __asm__ __volatile__("sub $0x400, %%rsp;" \
500 "call " #f "_thunk;" \
501 "add $0x400, %%rsp;" ::: "memory");
502#else
503#define HACKY_CALL(f) f()
504#endif
505
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000506void TraceSwitch(ThreadState *thr);
507
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000508extern "C" void __tsan_trace_switch();
509void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, u64 epoch,
510 EventType typ, uptr addr) {
511 StatInc(thr, StatEvents);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000512 if (UNLIKELY((epoch % kTracePartSize) == 0)) {
513#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000514 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000515#else
516 TraceSwitch(thr);
517#endif
518 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000519 Event *evp = &thr->trace.events[epoch % kTraceSize];
520 Event ev = (u64)addr | ((u64)typ << 61);
521 *evp = ev;
522}
523
524} // namespace __tsan
525
526#endif // TSAN_RTL_H