blob: 79075f094194744d07068930e0aa54ea33582579 [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"
Dmitry Vyukov2429b022012-11-28 10:35:31 +000038#include "tsan_platform.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000039
40namespace __tsan {
41
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000042// Descriptor of user's memory block.
43struct MBlock {
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +000044 Mutex mtx;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000045 uptr size;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000046 u32 alloc_tid;
47 u32 alloc_stack_id;
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +000048 SyncVar *head;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000049};
50
51#ifndef TSAN_GO
52#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
Dmitry Vyukovf77c6ea2012-08-16 13:27:25 +000053const uptr kAllocatorSpace = 0x7d0000000000ULL;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000054#else
55const uptr kAllocatorSpace = 0x7d0000000000ULL;
56#endif
57const uptr kAllocatorSize = 0x10000000000ULL; // 1T.
58
59typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
60 DefaultSizeClassMap> PrimaryAllocator;
61typedef SizeClassAllocatorLocalCache<PrimaryAllocator::kNumClasses,
62 PrimaryAllocator> AllocatorCache;
63typedef LargeMmapAllocator SecondaryAllocator;
64typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
65 SecondaryAllocator> Allocator;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000066Allocator *allocator();
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +000067#endif
68
Alexey Samsonov5c6b93b2012-09-11 09:44:48 +000069void TsanCheckFailed(const char *file, int line, const char *cond,
70 u64 v1, u64 v2);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000071
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000072// FastState (from most significant bit):
Dmitry Vyukov00e46042012-11-28 10:49:27 +000073// ignore : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000074// tid : kTidBits
75// epoch : kClkBits
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000076// unused : -
Dmitry Vyukove1a7f332012-11-28 12:19:50 +000077// history_size : 3
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000078class FastState {
79 public:
80 FastState(u64 tid, u64 epoch) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000081 x_ = tid << kTidShift;
82 x_ |= epoch << kClkShift;
Dmitry Vyukov00e46042012-11-28 10:49:27 +000083 DCHECK_EQ(tid, this->tid());
84 DCHECK_EQ(epoch, this->epoch());
85 DCHECK_EQ(GetIgnoreBit(), false);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000086 }
87
88 explicit FastState(u64 x)
89 : x_(x) {
90 }
91
Dmitry Vyukov3482ec32012-08-16 15:08:49 +000092 u64 raw() const {
93 return x_;
94 }
95
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000096 u64 tid() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000097 u64 res = x_ >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000098 return res;
99 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000100
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000101 u64 epoch() const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000102 u64 res = (x_ << (kTidBits + 1)) >> (64 - kClkBits);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000103 return res;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000104 }
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000105
106 void IncrementEpoch() {
107 u64 old_epoch = epoch();
108 x_ += 1 << kClkShift;
Dmitry Vyukov163a83382012-05-21 10:20:53 +0000109 DCHECK_EQ(old_epoch + 1, epoch());
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000110 (void)old_epoch;
111 }
112
113 void SetIgnoreBit() { x_ |= kIgnoreBit; }
114 void ClearIgnoreBit() { x_ &= ~kIgnoreBit; }
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000115 bool GetIgnoreBit() const { return (s64)x_ < 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000116
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000117 void SetHistorySize(int hs) {
118 CHECK_GE(hs, 0);
119 CHECK_LE(hs, 7);
120 x_ = (x_ & ~7) | hs;
121 }
122
123 int GetHistorySize() const {
124 return (int)(x_ & 7);
125 }
126
127 void ClearHistorySize() {
128 x_ &= ~7;
129 }
130
131 u64 GetTracePos() const {
132 const int hs = GetHistorySize();
133 // When hs == 0, the trace consists of 2 parts.
134 const u64 mask = (1ull << (kTracePartSizeBits + hs + 1)) - 1;
135 return epoch() & mask;
136 }
137
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000138 private:
139 friend class Shadow;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000140 static const int kTidShift = 64 - kTidBits - 1;
141 static const int kClkShift = kTidShift - kClkBits;
Dmitry Vyukov00e46042012-11-28 10:49:27 +0000142 static const u64 kIgnoreBit = 1ull << 63;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000143 static const u64 kFreedBit = 1ull << 63;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000144 u64 x_;
145};
146
147// Shadow (from most significant bit):
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000148// freed : 1
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000149// tid : kTidBits
150// epoch : kClkBits
151// is_write : 1
152// size_log : 2
153// addr0 : 3
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000154class Shadow : public FastState {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000155 public:
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000156 explicit Shadow(u64 x)
157 : FastState(x) {
158 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000160 explicit Shadow(const FastState &s)
161 : FastState(s.x_) {
162 ClearHistorySize();
163 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000164
165 void SetAddr0AndSizeLog(u64 addr0, unsigned kAccessSizeLog) {
166 DCHECK_EQ(x_ & 31, 0);
167 DCHECK_LE(addr0, 7);
168 DCHECK_LE(kAccessSizeLog, 3);
169 x_ |= (kAccessSizeLog << 3) | addr0;
170 DCHECK_EQ(kAccessSizeLog, size_log());
171 DCHECK_EQ(addr0, this->addr0());
172 }
173
174 void SetWrite(unsigned kAccessIsWrite) {
175 DCHECK_EQ(x_ & 32, 0);
176 if (kAccessIsWrite)
177 x_ |= 32;
178 DCHECK_EQ(kAccessIsWrite, is_write());
179 }
180
181 bool IsZero() const { return x_ == 0; }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000182
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000183 static inline bool TidsAreEqual(const Shadow s1, const Shadow s2) {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000184 u64 shifted_xor = (s1.x_ ^ s2.x_) >> kTidShift;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000185 DCHECK_EQ(shifted_xor == 0, s1.tid() == s2.tid());
186 return shifted_xor == 0;
187 }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000188
189 static inline bool Addr0AndSizeAreEqual(const Shadow s1, const Shadow s2) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000190 u64 masked_xor = (s1.x_ ^ s2.x_) & 31;
191 return masked_xor == 0;
192 }
193
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000194 static inline bool TwoRangesIntersect(Shadow s1, Shadow s2,
195 unsigned kS2AccessSize) {
196 bool res = false;
197 u64 diff = s1.addr0() - s2.addr0();
198 if ((s64)diff < 0) { // s1.addr0 < s2.addr0 // NOLINT
199 // if (s1.addr0() + size1) > s2.addr0()) return true;
200 if (s1.size() > -diff) res = true;
201 } else {
202 // if (s2.addr0() + kS2AccessSize > s1.addr0()) return true;
203 if (kS2AccessSize > diff) res = true;
204 }
205 DCHECK_EQ(res, TwoRangesIntersectSLOW(s1, s2));
206 DCHECK_EQ(res, TwoRangesIntersectSLOW(s2, s1));
207 return res;
208 }
209
210 // The idea behind the offset is as follows.
211 // Consider that we have 8 bool's contained within a single 8-byte block
212 // (mapped to a single shadow "cell"). Now consider that we write to the bools
213 // from a single thread (which we consider the common case).
214 // W/o offsetting each access will have to scan 4 shadow values at average
215 // to find the corresponding shadow value for the bool.
216 // With offsetting we start scanning shadow with the offset so that
217 // each access hits necessary shadow straight off (at least in an expected
218 // optimistic case).
219 // This logic works seamlessly for any layout of user data. For example,
220 // if user data is {int, short, char, char}, then accesses to the int are
221 // offsetted to 0, short - 4, 1st char - 6, 2nd char - 7. Hopefully, accesses
222 // from a single thread won't need to scan all 8 shadow values.
223 unsigned ComputeSearchOffset() {
224 return x_ & 7;
225 }
226 u64 addr0() const { return x_ & 7; }
227 u64 size() const { return 1ull << size_log(); }
228 bool is_write() const { return x_ & 32; }
229
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +0000230 // The idea behind the freed bit is as follows.
231 // When the memory is freed (or otherwise unaccessible) we write to the shadow
232 // values with tid/epoch related to the free and the freed bit set.
233 // During memory accesses processing the freed bit is considered
234 // as msb of tid. So any access races with shadow with freed bit set
235 // (it is as if write from a thread with which we never synchronized before).
236 // This allows us to detect accesses to freed memory w/o additional
237 // overheads in memory access processing and at the same time restore
238 // tid/epoch of free.
239 void MarkAsFreed() {
240 x_ |= kFreedBit;
241 }
242
243 bool GetFreedAndReset() {
244 bool res = x_ & kFreedBit;
245 x_ &= ~kFreedBit;
246 return res;
247 }
248
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000249 private:
250 u64 size_log() const { return (x_ >> 3) & 3; }
Dmitry Vyukov302cebb2012-05-22 18:07:45 +0000251
252 static bool TwoRangesIntersectSLOW(const Shadow s1, const Shadow s2) {
253 if (s1.addr0() == s2.addr0()) return true;
254 if (s1.addr0() < s2.addr0() && s1.addr0() + s1.size() > s2.addr0())
255 return true;
256 if (s2.addr0() < s1.addr0() && s2.addr0() + s2.size() > s1.addr0())
257 return true;
258 return false;
259 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000260};
261
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000262struct SignalContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000263
264// This struct is stored in TLS.
265struct ThreadState {
266 FastState fast_state;
267 // Synch epoch represents the threads's epoch before the last synchronization
268 // action. It allows to reduce number of shadow state updates.
269 // For example, fast_synch_epoch=100, last write to addr X was at epoch=150,
270 // if we are processing write to X from the same thread at epoch=200,
271 // we do nothing, because both writes happen in the same 'synch epoch'.
272 // That is, if another memory access does not race with the former write,
273 // it does not race with the latter as well.
274 // QUESTION: can we can squeeze this into ThreadState::Fast?
275 // E.g. ThreadState::Fast is a 44-bit, 32 are taken by synch_epoch and 12 are
276 // taken by epoch between synchs.
277 // This way we can save one load from tls.
278 u64 fast_synch_epoch;
279 // This is a slow path flag. On fast path, fast_state.GetIgnoreBit() is read.
280 // We do not distinguish beteween ignoring reads and writes
281 // for better performance.
282 int ignore_reads_and_writes;
283 uptr *shadow_stack_pos;
284 u64 *racy_shadow_addr;
285 u64 racy_state[2];
286 Trace trace;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000287#ifndef TSAN_GO
288 // C/C++ uses embed shadow stack of fixed size.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000289 uptr shadow_stack[kShadowStackSize];
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000290#else
291 // Go uses satellite shadow stack with dynamic size.
292 uptr *shadow_stack;
293 uptr *shadow_stack_end;
294#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000295 ThreadClock clock;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000296#ifndef TSAN_GO
297 AllocatorCache alloc_cache;
298#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000299 u64 stat[StatCnt];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000300 const int tid;
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000301 const int unique_id;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000302 int in_rtl;
Dmitry Vyukovfa985a02012-06-28 18:07:46 +0000303 bool is_alive;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000304 const uptr stk_addr;
305 const uptr stk_size;
306 const uptr tls_addr;
307 const uptr tls_size;
308
309 DeadlockDetector deadlock_detector;
310
311 bool in_signal_handler;
Dmitry Vyukov97c26bd2012-06-27 16:05:06 +0000312 SignalContext *signal_ctx;
313
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000314#ifndef TSAN_GO
315 u32 last_sleep_stack_id;
316 ThreadClock last_sleep_clock;
317#endif
318
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000319 // Set in regions of runtime that must be signal-safe and fork-safe.
320 // If set, malloc must not be called.
321 int nomalloc;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000322
Dmitry Vyukov191f2f72012-08-30 13:02:30 +0000323 explicit ThreadState(Context *ctx, int tid, int unique_id, u64 epoch,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000324 uptr stk_addr, uptr stk_size,
325 uptr tls_addr, uptr tls_size);
326};
327
328Context *CTX();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000329
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000330#ifndef TSAN_GO
331extern THREADLOCAL char cur_thread_placeholder[];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000332INLINE ThreadState *cur_thread() {
333 return reinterpret_cast<ThreadState *>(&cur_thread_placeholder);
334}
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000335#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000336
337enum ThreadStatus {
338 ThreadStatusInvalid, // Non-existent thread, data is invalid.
339 ThreadStatusCreated, // Created but not yet running.
340 ThreadStatusRunning, // The thread is currently running.
341 ThreadStatusFinished, // Joinable thread is finished but not yet joined.
Alexey Samsonov046248c2012-09-13 11:54:41 +0000342 ThreadStatusDead // Joined, but some info (trace) is still alive.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000343};
344
345// An info about a thread that is hold for some time after its termination.
346struct ThreadDeadInfo {
347 Trace trace;
348};
349
350struct ThreadContext {
351 const int tid;
352 int unique_id; // Non-rolling thread id.
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000353 uptr os_id; // pid
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000354 uptr user_id; // Some opaque user thread id (e.g. pthread_t).
355 ThreadState *thr;
356 ThreadStatus status;
357 bool detached;
358 int reuse_count;
359 SyncClock sync;
360 // Epoch at which the thread had started.
361 // If we see an event from the thread stamped by an older epoch,
362 // the event is from a dead thread that shared tid with this thread.
363 u64 epoch0;
364 u64 epoch1;
365 StackTrace creation_stack;
Dmitry Vyukovf6985e32012-05-22 14:34:43 +0000366 ThreadDeadInfo *dead_info;
367 ThreadContext *dead_next; // In dead thread list.
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000368
369 explicit ThreadContext(int tid);
370};
371
372struct RacyStacks {
373 MD5Hash hash[2];
374 bool operator==(const RacyStacks &other) const {
375 if (hash[0] == other.hash[0] && hash[1] == other.hash[1])
376 return true;
377 if (hash[0] == other.hash[1] && hash[1] == other.hash[0])
378 return true;
379 return false;
380 }
381};
382
383struct RacyAddress {
384 uptr addr_min;
385 uptr addr_max;
386};
387
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000388struct FiredSuppression {
389 ReportType type;
390 uptr pc;
391};
392
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000393struct Context {
394 Context();
395
396 bool initialized;
397
398 SyncTab synctab;
399
400 Mutex report_mtx;
401 int nreported;
402 int nmissed_expected;
403
404 Mutex thread_mtx;
Kostya Serebryany07c48052012-05-11 14:42:24 +0000405 unsigned thread_seq;
406 unsigned unique_thread_seq;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000407 int alive_threads;
408 int max_alive_threads;
409 ThreadContext *threads[kMaxTid];
410 int dead_list_size;
411 ThreadContext* dead_list_head;
412 ThreadContext* dead_list_tail;
413
414 Vector<RacyStacks> racy_stacks;
415 Vector<RacyAddress> racy_addresses;
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000416 Vector<FiredSuppression> fired_suppressions;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000417
418 Flags flags;
419
420 u64 stat[StatCnt];
421 u64 int_alloc_cnt[MBlockTypeCount];
422 u64 int_alloc_siz[MBlockTypeCount];
423};
424
425class ScopedInRtl {
426 public:
427 ScopedInRtl();
428 ~ScopedInRtl();
429 private:
430 ThreadState*thr_;
431 int in_rtl_;
432 int errno_;
433};
434
435class ScopedReport {
436 public:
437 explicit ScopedReport(ReportType typ);
438 ~ScopedReport();
439
440 void AddStack(const StackTrace *stack);
441 void AddMemoryAccess(uptr addr, Shadow s, const StackTrace *stack);
442 void AddThread(const ThreadContext *tctx);
443 void AddMutex(const SyncVar *s);
444 void AddLocation(uptr addr, uptr size);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000445 void AddSleep(u32 stack_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000446
447 const ReportDesc *GetReport() const;
448
449 private:
450 Context *ctx_;
451 ReportDesc *rep_;
452
453 ScopedReport(const ScopedReport&);
454 void operator = (const ScopedReport&);
455};
456
Dmitry Vyukov3482ec32012-08-16 15:08:49 +0000457void RestoreStack(int tid, const u64 epoch, StackTrace *stk);
458
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000459void StatAggregate(u64 *dst, u64 *src);
460void StatOutput(u64 *stat);
461void ALWAYS_INLINE INLINE StatInc(ThreadState *thr, StatType typ, u64 n = 1) {
462 if (kCollectStats)
463 thr->stat[typ] += n;
464}
465
Dmitry Vyukovc0157122012-11-06 16:00:16 +0000466void MapShadow(uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000467void InitializeShadowMemory();
468void InitializeInterceptors();
469void InitializeDynamicAnnotations();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000470
471void ReportRace(ThreadState *thr);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000472bool OutputReport(Context *ctx,
473 const ScopedReport &srep,
Dmitry Vyukov665ce2a2012-05-14 15:28:03 +0000474 const ReportStack *suppress_stack = 0);
Dmitry Vyukov90c9cbf2012-10-05 15:51:32 +0000475bool IsFiredSuppression(Context *ctx,
476 const ScopedReport &srep,
477 const StackTrace &trace);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000478bool IsExpectedReport(uptr addr, uptr size);
479
480#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 1
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000481# define DPrintf Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000482#else
483# define DPrintf(...)
484#endif
485
486#if defined(TSAN_DEBUG_OUTPUT) && TSAN_DEBUG_OUTPUT >= 2
Alexey Samsonovad9d65f2012-11-02 12:17:51 +0000487# define DPrintf2 Printf
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000488#else
489# define DPrintf2(...)
490#endif
491
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000492u32 CurrentStackId(ThreadState *thr, uptr pc);
Dmitry Vyukov46ca1fb2012-09-01 12:13:18 +0000493void PrintCurrentStack(ThreadState *thr, uptr pc);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000494
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000495void Initialize(ThreadState *thr);
496int Finalize(ThreadState *thr);
497
498void MemoryAccess(ThreadState *thr, uptr pc, uptr addr,
499 int kAccessSizeLog, bool kAccessIsWrite);
500void MemoryAccessImpl(ThreadState *thr, uptr addr,
Dmitry Vyukov933c9882012-11-15 18:49:08 +0000501 int kAccessSizeLog, bool kAccessIsWrite,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000502 u64 *shadow_mem, Shadow cur);
503void MemoryRead1Byte(ThreadState *thr, uptr pc, uptr addr);
504void MemoryWrite1Byte(ThreadState *thr, uptr pc, uptr addr);
505void MemoryRead8Byte(ThreadState *thr, uptr pc, uptr addr);
506void MemoryWrite8Byte(ThreadState *thr, uptr pc, uptr addr);
507void MemoryAccessRange(ThreadState *thr, uptr pc, uptr addr,
508 uptr size, bool is_write);
509void MemoryResetRange(ThreadState *thr, uptr pc, uptr addr, uptr size);
510void MemoryRangeFreed(ThreadState *thr, uptr pc, uptr addr, uptr size);
Dmitry Vyukov9f1509f2012-08-15 16:52:19 +0000511void MemoryRangeImitateWrite(ThreadState *thr, uptr pc, uptr addr, uptr size);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000512void IgnoreCtl(ThreadState *thr, bool write, bool begin);
513
514void FuncEntry(ThreadState *thr, uptr pc);
515void FuncExit(ThreadState *thr);
516
517int ThreadCreate(ThreadState *thr, uptr pc, uptr uid, bool detached);
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000518void ThreadStart(ThreadState *thr, int tid, uptr os_id);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000519void ThreadFinish(ThreadState *thr);
520int ThreadTid(ThreadState *thr, uptr pc, uptr uid);
521void ThreadJoin(ThreadState *thr, uptr pc, int tid);
522void ThreadDetach(ThreadState *thr, uptr pc, int tid);
523void ThreadFinalize(ThreadState *thr);
Dmitry Vyukov67dc5702012-11-07 16:41:57 +0000524int ThreadCount(ThreadState *thr);
Dmitry Vyukov262465c2012-11-15 17:40:49 +0000525void ProcessPendingSignals(ThreadState *thr);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000526
Dmitry Vyukov4723e6b2012-08-16 13:29:41 +0000527void MutexCreate(ThreadState *thr, uptr pc, uptr addr,
528 bool rw, bool recursive, bool linker_init);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000529void MutexDestroy(ThreadState *thr, uptr pc, uptr addr);
530void MutexLock(ThreadState *thr, uptr pc, uptr addr);
531void MutexUnlock(ThreadState *thr, uptr pc, uptr addr);
532void MutexReadLock(ThreadState *thr, uptr pc, uptr addr);
533void MutexReadUnlock(ThreadState *thr, uptr pc, uptr addr);
534void MutexReadOrWriteUnlock(ThreadState *thr, uptr pc, uptr addr);
535
536void Acquire(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukove11f2922012-11-07 15:08:20 +0000537void AcquireGlobal(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000538void Release(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov904d3f92012-07-28 15:27:41 +0000539void ReleaseStore(ThreadState *thr, uptr pc, uptr addr);
Dmitry Vyukov318f7772012-08-31 17:27:49 +0000540void AfterSleep(ThreadState *thr, uptr pc);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000541
542// The hacky call uses custom calling convention and an assembly thunk.
543// It is considerably faster that a normal call for the caller
544// if it is not executed (it is intended for slow paths from hot functions).
545// The trick is that the call preserves all registers and the compiler
546// does not treat it as a call.
547// If it does not work for you, use normal call.
548#if TSAN_DEBUG == 0
549// The caller may not create the stack frame for itself at all,
550// so we create a reserve stack frame for it (1024b must be enough).
551#define HACKY_CALL(f) \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000552 __asm__ __volatile__("sub $1024, %%rsp;" \
553 "/*.cfi_adjust_cfa_offset 1024;*/" \
Dmitry Vyukov20678e22012-11-26 14:20:26 +0000554 ".hidden " #f "_thunk;" \
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000555 "call " #f "_thunk;" \
Dmitry Vyukovb7f18522012-09-02 11:24:07 +0000556 "add $1024, %%rsp;" \
557 "/*.cfi_adjust_cfa_offset -1024;*/" \
558 ::: "memory", "cc");
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000559#else
560#define HACKY_CALL(f) f()
561#endif
562
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000563void TraceSwitch(ThreadState *thr);
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000564uptr TraceTopPC(ThreadState *thr);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000565uptr TraceSize();
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000566
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000567extern "C" void __tsan_trace_switch();
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000568void ALWAYS_INLINE INLINE TraceAddEvent(ThreadState *thr, FastState fs,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000569 EventType typ, uptr addr) {
570 StatInc(thr, StatEvents);
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000571 u64 epoch = fs.GetTracePos();
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000572 if (UNLIKELY((epoch % kTracePartSize) == 0)) {
573#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000574 HACKY_CALL(__tsan_trace_switch);
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000575#else
576 TraceSwitch(thr);
577#endif
578 }
Dmitry Vyukov2429b022012-11-28 10:35:31 +0000579 Event *trace = (Event*)GetThreadTrace(fs.tid());
Dmitry Vyukove1a7f332012-11-28 12:19:50 +0000580 Event *evp = &trace[epoch];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000581 Event ev = (u64)addr | ((u64)typ << 61);
582 *evp = ev;
583}
584
585} // namespace __tsan
586
587#endif // TSAN_RTL_H