blob: 336b656b7cee893522faa600376904accc4082be [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
86 u64 tid() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000087 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000088 return res;
89 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000090
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000091 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000092 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000093 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000094 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000095
96 void IncrementEpoch() {
97 u64 old_epoch = epoch();
98 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +000099 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000100 (void)old_epoch;
101 }
102
103 void SetIgnoreBit() { x_ |= kIgnoreBit; }
104 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000105 bool GetIgnoreBit() const { return x_ & kIgnoreBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000106
107 private:
108 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000109 static const int kTidShift = 64 - kTidBits - 1;
110 static const int kClkShift = kTidShift - kClkBits;
111 static const u64 kIgnoreBit = 1ull;
112 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000113 u64 x_;
114};
115
116// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000117// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000118// tid : kTidBits
119// epoch : kClkBits
120// is_write : 1
121// size_log : 2
122// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000123class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000124 public:
125 explicit Shadow(u64 x) : FastState(x) { }
126
127 explicit Shadow(const FastState &s) : FastState(s.x_) { }
128
129 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
130 DCHECK_EQ(x_ & 31, 0);
131 DCHECK_LE(addr0, 7);
132 DCHECK_LE(kAccessSizeLog, 3);
133 x_ |= (kAccessSizeLog << 3) | addr0;
134 DCHECK_EQ(kAccessSizeLog, size_log());
135 DCHECK_EQ(addr0, this->addr0());
136 }
137
138 void SetWrite(unsigned kAccessIsWrite) {
139 DCHECK_EQ(x_ & 32, 0);
140 if (kAccessIsWrite)
141 x_ |= 32;
142 DCHECK_EQ(kAccessIsWrite, is_write());
143 }
144
145 bool IsZero() const { return x_ == 0; }
146 u64 raw() const { return x_; }
147
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000148 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000149 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000150 DCHECK_EQ(shifted_xor == 0, s1.tid() == s2.tid());
151 return shifted_xor == 0;
152 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000153
154 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000155 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
156 return masked_xor == 0;
157 }
158
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
160 unsigned kS2AccessSize) {
161 bool res = false;
162 u64 diff = s1.addr0() - s2.addr0();
163 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
164 // if (s1.addr0() + size1) > s2.addr0()) return true;
165 if (s1.size() > -diff) res = true;
166 } else {
167 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
168 if (kS2AccessSize > diff) res = true;
169 }
170 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
171 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
172 return res;
173 }
174
175 // The idea behind the offset is as follows.
176 // Consider that we have 8 bool's contained within a single 8-byte block
177 // (mapped to a single shadow "cell"). Now consider that we write to the bools
178 // from a single thread (which we consider the common case).
179 // W/o offsetting each access will have to scan 4 shadow values at average
180 // to find the corresponding shadow value for the bool.
181 // With offsetting we start scanning shadow with the offset so that
182 // each access hits necessary shadow straight off (at least in an expected
183 // optimistic case).
184 // This logic works seamlessly for any layout of user data. For example,
185 // if user data is {int, short, char, char}, then accesses to the int are
186 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
187 // from a single thread won't need to scan all 8 shadow values.
188 unsigned ComputeSearchOffset() {
189 return x_ & 7;
190 }
191 u64 addr0() const { return x_ & 7; }
192 u64 size() const { return 1ull << size_log(); }
193 bool is_write() const { return x_ & 32; }
194
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000195 // The idea behind the freed bit is as follows.
196 // When the memory is freed (or otherwise unaccessible) we write to the shadow
197 // values with tid/epoch related to the free and the freed bit set.
198 // During memory accesses processing the freed bit is considered
199 // as msb of tid. So any access races with shadow with freed bit set
200 // (it is as if write from a thread with which we never synchronized before).
201 // This allows us to detect accesses to freed memory w/o additional
202 // overheads in memory access processing and at the same time restore
203 // tid/epoch of free.
204 void MarkAsFreed() {
205 x_ |= kFreedBit;
206 }
207
208 bool GetFreedAndReset() {
209 bool res = x_ & kFreedBit;
210 x_ &= ~kFreedBit;
211 return res;
212 }
213
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000214 private:
215 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000216
217 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
218 if (s1.addr0() == s2.addr0()) return true;
219 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
220 return true;
221 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
222 return true;
223 return false;
224 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000225};
226
227// Freed memory.
228// As if 8-byte write by thread 0xff..f at epoch 0xff..f, races with everything.
229const u64 kShadowFreed = 0xfffffffffffffff8ull;
230
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000231struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000232
233// This struct is stored in TLS.
234struct ThreadState {
235 FastState fast_state;
236 // Synch epoch represents the threads's epoch before the last synchronization
237 // action. It allows to reduce number of shadow state updates.
238 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
239 // if we are processing write to X from the same thread at epoch=200,
240 // we do nothing, because both writes happen in the same 'synch epoch'.
241 // That is, if another memory access does not race with the former write,
242 // it does not race with the latter as well.
243 // QUESTION: can we can squeeze this into ThreadState::Fast?
244 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
245 // taken by epoch between synchs.
246 // This way we can save one load from tls.
247 u64 fast_synch_epoch;
248 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
249 // We do not distinguish beteween ignoring reads and writes
250 // for better performance.
251 int ignore_reads_and_writes;
252 uptr *shadow_stack_pos;
253 u64 *racy_shadow_addr;
254 u64 racy_state[2];
255 Trace trace;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000256#ifndef TSAN_GO
257 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000258 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000259#else
260 // Go uses satellite shadow stack with dynamic size.
261 uptr *shadow_stack;
262 uptr *shadow_stack_end;
263#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000264 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000265#ifndef TSAN_GO
266 AllocatorCache alloc_cache;
267#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000268 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000269 const int tid;
270 int in_rtl;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000271 bool is_alive;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000272 const uptr stk_addr;
273 const uptr stk_size;
274 const uptr tls_addr;
275 const uptr tls_size;
276
277 DeadlockDetector deadlock_detector;
278
279 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000280 SignalContext *signal_ctx;
281
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000282 // Set in regions of runtime that must be signal-safe and fork-safe.
283 // If set, malloc must not be called.
284 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000285
286 explicit ThreadState(Context *ctx, int tid, u64 epoch,
287 uptr stk_addr, uptr stk_size,
288 uptr tls_addr, uptr tls_size);
289};
290
291Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000292
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000293#ifndef TSAN_GO
294extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295INLINE ThreadState *cur_thread() {
296 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
297}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000298#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000299
300enum ThreadStatus {
301 ThreadStatusInvalid, // Non-existent thread, data is invalid.
302 ThreadStatusCreated, // Created but not yet running.
303 ThreadStatusRunning, // The thread is currently running.
304 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
305 ThreadStatusDead, // Joined, but some info (trace) is still alive.
306};
307
308// An info about a thread that is hold for some time after its termination.
309struct ThreadDeadInfo {
310 Trace trace;
311};
312
313struct ThreadContext {
314 const int tid;
315 int unique_id; // Non-rolling thread id.
316 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
317 ThreadState *thr;
318 ThreadStatus status;
319 bool detached;
320 int reuse_count;
321 SyncClock sync;
322 // Epoch at which the thread had started.
323 // If we see an event from the thread stamped by an older epoch,
324 // the event is from a dead thread that shared tid with this thread.
325 u64 epoch0;
326 u64 epoch1;
327 StackTrace creation_stack;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000328 ThreadDeadInfo *dead_info;
329 ThreadContext *dead_next; // In dead thread list.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000330
331 explicit ThreadContext(int tid);
332};
333
334struct RacyStacks {
335 MD5Hash hash[2];
336 bool operator==(const RacyStacks &other) const {
337 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
338 return true;
339 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
340 return true;
341 return false;
342 }
343};
344
345struct RacyAddress {
346 uptr addr_min;
347 uptr addr_max;
348};
349
350struct Context {
351 Context();
352
353 bool initialized;
354
355 SyncTab synctab;
356
357 Mutex report_mtx;
358 int nreported;
359 int nmissed_expected;
360
361 Mutex thread_mtx;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000362 unsigned thread_seq;
363 unsigned unique_thread_seq;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000364 int alive_threads;
365 int max_alive_threads;
366 ThreadContext *threads[kMaxTid];
367 int dead_list_size;
368 ThreadContext* dead_list_head;
369 ThreadContext* dead_list_tail;
370
371 Vector<RacyStacks> racy_stacks;
372 Vector<RacyAddress> racy_addresses;
373
374 Flags flags;
375
376 u64 stat[StatCnt];
377 u64 int_alloc_cnt[MBlockTypeCount];
378 u64 int_alloc_siz[MBlockTypeCount];
379};
380
381class ScopedInRtl {
382 public:
383 ScopedInRtl();
384 ~ScopedInRtl();
385 private:
386 ThreadState*thr_;
387 int in_rtl_;
388 int errno_;
389};
390
391class ScopedReport {
392 public:
393 explicit ScopedReport(ReportType typ);
394 ~ScopedReport();
395
396 void AddStack(const StackTrace *stack);
397 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack);
398 void AddThread(const ThreadContext *tctx);
399 void AddMutex(const SyncVar *s);
400 void AddLocation(uptr addr, uptr size);
401
402 const ReportDesc *GetReport() const;
403
404 private:
405 Context *ctx_;
406 ReportDesc *rep_;
407
408 ScopedReport(const ScopedReport&);
409 void operator = (const ScopedReport&);
410};
411
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000412void StatAggregate(u64 *dst, u64 *src);
413void StatOutput(u64 *stat);
414void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
415 if (kCollectStats)
416 thr->stat[typ] += n;
417}
418
419void InitializeShadowMemory();
420void InitializeInterceptors();
421void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000422
423void ReportRace(ThreadState *thr);
Dmitry Vyukov665ce2a2012-05-14 15:28:03 +0000424bool OutputReport(const ScopedReport &srep,
425 const ReportStack *suppress_stack = 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000426bool IsExpectedReport(uptr addr, uptr size);
427
428#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000429# define DPrintf TsanPrintf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000430#else
431# define DPrintf(...)
432#endif
433
434#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovac4c2902012-06-06 10:13:27 +0000435# define DPrintf2 TsanPrintf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000436#else
437# define DPrintf2(...)
438#endif
439
440void Initialize(ThreadState *thr);
441int Finalize(ThreadState *thr);
442
443void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
444 int kAccessSizeLog, bool kAccessIsWrite);
445void MemoryAccessImpl(ThreadState *thr, uptr addr,
446 int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state,
447 u64 *shadow_mem, Shadow cur);
448void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr);
449void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr);
450void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr);
451void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr);
452void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
453 uptr size, bool is_write);
454void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
455void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000456void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000457void IgnoreCtl(ThreadState *thr, bool write, bool begin);
458
459void FuncEntry(ThreadState *thr, uptr pc);
460void FuncExit(ThreadState *thr);
461
462int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
463void ThreadStart(ThreadState *thr, int tid);
464void ThreadFinish(ThreadState *thr);
465int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
466void ThreadJoin(ThreadState *thr, uptr pc, int tid);
467void ThreadDetach(ThreadState *thr, uptr pc, int tid);
468void ThreadFinalize(ThreadState *thr);
Dmitry Vyukovdfc8e522012-07-25 13:16:35 +0000469void ThreadFinalizerGoroutine(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000470
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000471void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
472 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000473void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
474void MutexLock(ThreadState *thr, uptr pc, uptr addr);
475void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
476void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
477void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
478void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
479
480void Acquire(ThreadState *thr, uptr pc, uptr addr);
481void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000482void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000483
484// The hacky call uses custom calling convention and an assembly thunk.
485// It is considerably faster that a normal call for the caller
486// if it is not executed (it is intended for slow paths from hot functions).
487// The trick is that the call preserves all registers and the compiler
488// does not treat it as a call.
489// If it does not work for you, use normal call.
490#if TSAN_DEBUG == 0
491// The caller may not create the stack frame for itself at all,
492// so we create a reserve stack frame for it (1024b must be enough).
493#define HACKY_CALL(f) \
494 __asm__ __volatile__("sub $0x400, %%rsp;" \
495 "call " #f "_thunk;" \
496 "add $0x400, %%rsp;" ::: "memory");
497#else
498#define HACKY_CALL(f) f()
499#endif
500
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000501void TraceSwitch(ThreadState *thr);
502
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000503extern "C" void __tsan_trace_switch();
504void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, u64 epoch,
505 EventType typ, uptr addr) {
506 StatInc(thr, StatEvents);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000507 if (UNLIKELY((epoch % kTracePartSize) == 0)) {
508#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000509 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000510#else
511 TraceSwitch(thr);
512#endif
513 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000514 Event *evp = &thr->trace.events[epoch % kTraceSize];
515 Event ev = (u64)addr | ((u64)typ << 61);
516 *evp = ev;
517}
518
519} // namespace __tsan
520
521#endif // TSAN_RTL_H