blob: 49388aa53fdd46fb5c8bb8c882fb3baebd63ba33 [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 Vyukov191f2f72012-08-30 13:02:30 +000045 u32 alloc_tid;
46 u32 alloc_stack_id;
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +000047 SyncVar *head;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000048};
49
50#ifndef TSAN_GO
51#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukovf77c6ea2012-08-16 13:27:25 +000052const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000053#else
54const uptr kAllocatorSpace = 0x7d0000000000ULL;
55#endif
56const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
57
58typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
59 DefaultSizeClassMap> PrimaryAllocator;
60typedef SizeClassAllocatorLocalCache<PrimaryAllocator::kNumClasses,
61 PrimaryAllocator> AllocatorCache;
62typedef LargeMmapAllocator SecondaryAllocator;
63typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
64 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000065Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000066#endif
67
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000068void TsanCheckFailed(const char *file, int line, const char *cond,
69 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000070
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000071// FastState (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000072// unused : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000073// tid : kTidBits
74// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000075// unused : -
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000076// ignore_bit : 1
77class FastState {
78 public:
79 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000080 x_ = tid << kTidShift;
81 x_ |= epoch << kClkShift;
82 DCHECK(tid == this->tid());
83 DCHECK(epoch == this->epoch());
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000084 }
85
86 explicit FastState(u64 x)
87 : x_(x) {
88 }
89
Dmitry Vyukov3482ec32012-08-16 15:08:49 +000090 u64 raw() const {
91 return x_;
92 }
93
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000094 u64 tid() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000095 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000096 return res;
97 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000098
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000099 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000100 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000101 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000102 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000103
104 void IncrementEpoch() {
105 u64 old_epoch = epoch();
106 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000107 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000108 (void)old_epoch;
109 }
110
111 void SetIgnoreBit() { x_ |= kIgnoreBit; }
112 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000113 bool GetIgnoreBit() const { return x_ & kIgnoreBit; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000114
115 private:
116 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000117 static const int kTidShift = 64 - kTidBits - 1;
118 static const int kClkShift = kTidShift - kClkBits;
119 static const u64 kIgnoreBit = 1ull;
120 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000121 u64 x_;
122};
123
124// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000125// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000126// tid : kTidBits
127// epoch : kClkBits
128// is_write : 1
129// size_log : 2
130// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000131class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000132 public:
133 explicit Shadow(u64 x) : FastState(x) { }
134
135 explicit Shadow(const FastState &s) : FastState(s.x_) { }
136
137 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
138 DCHECK_EQ(x_ & 31, 0);
139 DCHECK_LE(addr0, 7);
140 DCHECK_LE(kAccessSizeLog, 3);
141 x_ |= (kAccessSizeLog << 3) | addr0;
142 DCHECK_EQ(kAccessSizeLog, size_log());
143 DCHECK_EQ(addr0, this->addr0());
144 }
145
146 void SetWrite(unsigned kAccessIsWrite) {
147 DCHECK_EQ(x_ & 32, 0);
148 if (kAccessIsWrite)
149 x_ |= 32;
150 DCHECK_EQ(kAccessIsWrite, is_write());
151 }
152
153 bool IsZero() const { return x_ == 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000154
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000155 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000156 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000157 DCHECK_EQ(shifted_xor == 0, s1.tid() == s2.tid());
158 return shifted_xor == 0;
159 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000160
161 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
163 return masked_xor == 0;
164 }
165
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
167 unsigned kS2AccessSize) {
168 bool res = false;
169 u64 diff = s1.addr0() - s2.addr0();
170 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
171 // if (s1.addr0() + size1) > s2.addr0()) return true;
172 if (s1.size() > -diff) res = true;
173 } else {
174 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
175 if (kS2AccessSize > diff) res = true;
176 }
177 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
178 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
179 return res;
180 }
181
182 // The idea behind the offset is as follows.
183 // Consider that we have 8 bool's contained within a single 8-byte block
184 // (mapped to a single shadow "cell"). Now consider that we write to the bools
185 // from a single thread (which we consider the common case).
186 // W/o offsetting each access will have to scan 4 shadow values at average
187 // to find the corresponding shadow value for the bool.
188 // With offsetting we start scanning shadow with the offset so that
189 // each access hits necessary shadow straight off (at least in an expected
190 // optimistic case).
191 // This logic works seamlessly for any layout of user data. For example,
192 // if user data is {int, short, char, char}, then accesses to the int are
193 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
194 // from a single thread won't need to scan all 8 shadow values.
195 unsigned ComputeSearchOffset() {
196 return x_ & 7;
197 }
198 u64 addr0() const { return x_ & 7; }
199 u64 size() const { return 1ull << size_log(); }
200 bool is_write() const { return x_ & 32; }
201
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000202 // The idea behind the freed bit is as follows.
203 // When the memory is freed (or otherwise unaccessible) we write to the shadow
204 // values with tid/epoch related to the free and the freed bit set.
205 // During memory accesses processing the freed bit is considered
206 // as msb of tid. So any access races with shadow with freed bit set
207 // (it is as if write from a thread with which we never synchronized before).
208 // This allows us to detect accesses to freed memory w/o additional
209 // overheads in memory access processing and at the same time restore
210 // tid/epoch of free.
211 void MarkAsFreed() {
212 x_ |= kFreedBit;
213 }
214
215 bool GetFreedAndReset() {
216 bool res = x_ & kFreedBit;
217 x_ &= ~kFreedBit;
218 return res;
219 }
220
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000221 private:
222 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000223
224 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
225 if (s1.addr0() == s2.addr0()) return true;
226 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
227 return true;
228 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
229 return true;
230 return false;
231 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000232};
233
234// Freed memory.
235// As if 8-byte write by thread 0xff..f at epoch 0xff..f, races with everything.
236const u64 kShadowFreed = 0xfffffffffffffff8ull;
237
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000238struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000239
240// This struct is stored in TLS.
241struct ThreadState {
242 FastState fast_state;
243 // Synch epoch represents the threads's epoch before the last synchronization
244 // action. It allows to reduce number of shadow state updates.
245 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
246 // if we are processing write to X from the same thread at epoch=200,
247 // we do nothing, because both writes happen in the same 'synch epoch'.
248 // That is, if another memory access does not race with the former write,
249 // it does not race with the latter as well.
250 // QUESTION: can we can squeeze this into ThreadState::Fast?
251 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
252 // taken by epoch between synchs.
253 // This way we can save one load from tls.
254 u64 fast_synch_epoch;
255 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
256 // We do not distinguish beteween ignoring reads and writes
257 // for better performance.
258 int ignore_reads_and_writes;
259 uptr *shadow_stack_pos;
260 u64 *racy_shadow_addr;
261 u64 racy_state[2];
262 Trace trace;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000263#ifndef TSAN_GO
264 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000265 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000266#else
267 // Go uses satellite shadow stack with dynamic size.
268 uptr *shadow_stack;
269 uptr *shadow_stack_end;
270#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000271 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000272#ifndef TSAN_GO
273 AllocatorCache alloc_cache;
274#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000275 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000276 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000277 const int unique_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000278 int in_rtl;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000279 bool is_alive;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000280 const uptr stk_addr;
281 const uptr stk_size;
282 const uptr tls_addr;
283 const uptr tls_size;
284
285 DeadlockDetector deadlock_detector;
286
287 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000288 SignalContext *signal_ctx;
289
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000290#ifndef TSAN_GO
291 u32 last_sleep_stack_id;
292 ThreadClock last_sleep_clock;
293#endif
294
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000295 // Set in regions of runtime that must be signal-safe and fork-safe.
296 // If set, malloc must not be called.
297 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000298
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000299 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000300 uptr stk_addr, uptr stk_size,
301 uptr tls_addr, uptr tls_size);
302};
303
304Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000305
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000306#ifndef TSAN_GO
307extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000308INLINE ThreadState *cur_thread() {
309 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
310}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000311#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000312
313enum ThreadStatus {
314 ThreadStatusInvalid, // Non-existent thread, data is invalid.
315 ThreadStatusCreated, // Created but not yet running.
316 ThreadStatusRunning, // The thread is currently running.
317 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
Alexey Samsonov046248c2012-09-13 11:54:41 +0000318 ThreadStatusDead // Joined, but some info (trace) is still alive.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000319};
320
321// An info about a thread that is hold for some time after its termination.
322struct ThreadDeadInfo {
323 Trace trace;
324};
325
326struct ThreadContext {
327 const int tid;
328 int unique_id; // Non-rolling thread id.
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000329 uptr os_id; // pid
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000330 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
331 ThreadState *thr;
332 ThreadStatus status;
333 bool detached;
334 int reuse_count;
335 SyncClock sync;
336 // Epoch at which the thread had started.
337 // If we see an event from the thread stamped by an older epoch,
338 // the event is from a dead thread that shared tid with this thread.
339 u64 epoch0;
340 u64 epoch1;
341 StackTrace creation_stack;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000342 ThreadDeadInfo *dead_info;
343 ThreadContext *dead_next; // In dead thread list.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000344
345 explicit ThreadContext(int tid);
346};
347
348struct RacyStacks {
349 MD5Hash hash[2];
350 bool operator==(const RacyStacks &other) const {
351 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
352 return true;
353 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
354 return true;
355 return false;
356 }
357};
358
359struct RacyAddress {
360 uptr addr_min;
361 uptr addr_max;
362};
363
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000364struct FiredSuppression {
365 ReportType type;
366 uptr pc;
367};
368
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000369struct Context {
370 Context();
371
372 bool initialized;
373
374 SyncTab synctab;
375
376 Mutex report_mtx;
377 int nreported;
378 int nmissed_expected;
379
380 Mutex thread_mtx;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000381 unsigned thread_seq;
382 unsigned unique_thread_seq;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000383 int alive_threads;
384 int max_alive_threads;
385 ThreadContext *threads[kMaxTid];
386 int dead_list_size;
387 ThreadContext* dead_list_head;
388 ThreadContext* dead_list_tail;
389
390 Vector<RacyStacks> racy_stacks;
391 Vector<RacyAddress> racy_addresses;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000392 Vector<FiredSuppression> fired_suppressions;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000393
394 Flags flags;
395
396 u64 stat[StatCnt];
397 u64 int_alloc_cnt[MBlockTypeCount];
398 u64 int_alloc_siz[MBlockTypeCount];
399};
400
401class ScopedInRtl {
402 public:
403 ScopedInRtl();
404 ~ScopedInRtl();
405 private:
406 ThreadState*thr_;
407 int in_rtl_;
408 int errno_;
409};
410
411class ScopedReport {
412 public:
413 explicit ScopedReport(ReportType typ);
414 ~ScopedReport();
415
416 void AddStack(const StackTrace *stack);
417 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack);
418 void AddThread(const ThreadContext *tctx);
419 void AddMutex(const SyncVar *s);
420 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000421 void AddSleep(u32 stack_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000422
423 const ReportDesc *GetReport() const;
424
425 private:
426 Context *ctx_;
427 ReportDesc *rep_;
428
429 ScopedReport(const ScopedReport&);
430 void operator = (const ScopedReport&);
431};
432
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000433void RestoreStack(int tid, const u64 epoch, StackTrace *stk);
434
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000435void StatAggregate(u64 *dst, u64 *src);
436void StatOutput(u64 *stat);
437void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
438 if (kCollectStats)
439 thr->stat[typ] += n;
440}
441
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000442void MapShadow(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000443void InitializeShadowMemory();
444void InitializeInterceptors();
445void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000446
447void ReportRace(ThreadState *thr);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000448bool OutputReport(Context *ctx,
449 const ScopedReport &srep,
Dmitry Vyukov665ce2a2012-05-14 15:28:03 +0000450 const ReportStack *suppress_stack = 0);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000451bool IsFiredSuppression(Context *ctx,
452 const ScopedReport &srep,
453 const StackTrace &trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000454bool IsExpectedReport(uptr addr, uptr size);
455
456#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000457# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000458#else
459# define DPrintf(...)
460#endif
461
462#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000463# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000464#else
465# define DPrintf2(...)
466#endif
467
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000468u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000469void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000470
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000471void Initialize(ThreadState *thr);
472int Finalize(ThreadState *thr);
473
474void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
475 int kAccessSizeLog, bool kAccessIsWrite);
476void MemoryAccessImpl(ThreadState *thr, uptr addr,
477 int kAccessSizeLog, bool kAccessIsWrite, FastState fast_state,
478 u64 *shadow_mem, Shadow cur);
479void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr);
480void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr);
481void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr);
482void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr);
483void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
484 uptr size, bool is_write);
485void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
486void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000487void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000488void IgnoreCtl(ThreadState *thr, bool write, bool begin);
489
490void FuncEntry(ThreadState *thr, uptr pc);
491void FuncExit(ThreadState *thr);
492
493int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000494void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000495void ThreadFinish(ThreadState *thr);
496int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
497void ThreadJoin(ThreadState *thr, uptr pc, int tid);
498void ThreadDetach(ThreadState *thr, uptr pc, int tid);
499void ThreadFinalize(ThreadState *thr);
Dmitry Vyukovdfc8e522012-07-25 13:16:35 +0000500void ThreadFinalizerGoroutine(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000501
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000502void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
503 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000504void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
505void MutexLock(ThreadState *thr, uptr pc, uptr addr);
506void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
507void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
508void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
509void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
510
511void Acquire(ThreadState *thr, uptr pc, uptr addr);
512void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000513void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000514void AfterSleep(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000515
516// The hacky call uses custom calling convention and an assembly thunk.
517// It is considerably faster that a normal call for the caller
518// if it is not executed (it is intended for slow paths from hot functions).
519// The trick is that the call preserves all registers and the compiler
520// does not treat it as a call.
521// If it does not work for you, use normal call.
522#if TSAN_DEBUG == 0
523// The caller may not create the stack frame for itself at all,
524// so we create a reserve stack frame for it (1024b must be enough).
525#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000526 __asm__ __volatile__("sub $1024, %%rsp;" \
527 "/*.cfi_adjust_cfa_offset 1024;*/" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000528 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000529 "add $1024, %%rsp;" \
530 "/*.cfi_adjust_cfa_offset -1024;*/" \
531 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000532#else
533#define HACKY_CALL(f) f()
534#endif
535
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000536void TraceSwitch(ThreadState *thr);
537
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000538extern "C" void __tsan_trace_switch();
539void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, u64 epoch,
540 EventType typ, uptr addr) {
541 StatInc(thr, StatEvents);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000542 if (UNLIKELY((epoch % kTracePartSize) == 0)) {
543#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000544 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000545#else
546 TraceSwitch(thr);
547#endif
548 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000549 Event *evp = &thr->trace.events[epoch % kTraceSize];
550 Event ev = (u64)addr | ((u64)typ << 61);
551 *evp = ev;
552}
553
554} // namespace __tsan
555
556#endif // TSAN_RTL_H