blob: 3d9cd54b5ead32aafb72e08bad3b40a3ca0da109 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_defs.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//===----------------------------------------------------------------------===//
13
14#ifndef TSAN_DEFS_H
15#define TSAN_DEFS_H
16
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000017#include "sanitizer_common/sanitizer_internal_defs.h"
Alexey Samsonov91e1a7e2012-06-07 11:54:08 +000018#include "sanitizer_common/sanitizer_libc.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000019#include "tsan_stat.h"
20
21#ifndef TSAN_DEBUG
22#define TSAN_DEBUG 0
23#endif // TSAN_DEBUG
24
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000025namespace __tsan {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000026
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000027const int kTidBits = 13;
Kostya Serebryany07c48052012-05-11 14:42:24 +000028const unsigned kMaxTid = 1 << kTidBits;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000029const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000030const int kClkBits = 43;
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +000031const int kShadowStackSize = 1024;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000032
33#ifdef TSAN_SHADOW_COUNT
34# if TSAN_SHADOW_COUNT == 2 \
35 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
36const unsigned kShadowCnt = TSAN_SHADOW_COUNT;
37# else
38# error "TSAN_SHADOW_COUNT must be one of 2,4,8"
39# endif
40#else
41// Count of shadow values in a shadow cell.
42const unsigned kShadowCnt = 8;
43#endif
44
45// That many user bytes are mapped onto a single shadow cell.
46const unsigned kShadowCell = 8;
47
48// Size of a single shadow value (u64).
49const unsigned kShadowSize = 8;
50
51#if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
52const bool kCollectStats = true;
53#else
54const bool kCollectStats = false;
55#endif
56
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000057// The following "build consistency" machinery ensures that all source files
58// are built in the same configuration. Inconsistent builds lead to
59// hard to debug crashes.
60#if TSAN_DEBUG
61void build_consistency_debug();
62#else
63void build_consistency_release();
64#endif
65
66#if TSAN_COLLECT_STATS
67void build_consistency_stats();
68#else
69void build_consistency_nostats();
70#endif
71
72#if TSAN_SHADOW_COUNT == 1
73void build_consistency_shadow1();
74#elif TSAN_SHADOW_COUNT == 2
75void build_consistency_shadow2();
76#elif TSAN_SHADOW_COUNT == 4
77void build_consistency_shadow4();
78#else
79void build_consistency_shadow8();
80#endif
81
82static inline void USED build_consistency() {
83#if TSAN_DEBUG
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000084 build_consistency_debug();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000085#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000086 build_consistency_release();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000087#endif
88#if TSAN_COLLECT_STATS
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000089 build_consistency_stats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000090#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000091 build_consistency_nostats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000092#endif
93#if TSAN_SHADOW_COUNT == 1
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000094 build_consistency_shadow1();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000095#elif TSAN_SHADOW_COUNT == 2
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000096 build_consistency_shadow2();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000097#elif TSAN_SHADOW_COUNT == 4
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000098 build_consistency_shadow4();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000099#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000100 build_consistency_shadow8();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000101#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000102}
103
104template<typename T>
105T min(T a, T b) {
106 return a < b ? a : b;
107}
108
109template<typename T>
110T max(T a, T b) {
111 return a > b ? a : b;
112}
113
114template<typename T>
115T RoundUp(T p, int align) {
116 DCHECK_EQ(align & (align - 1), 0);
117 return (T)(((u64)p + align - 1) & ~(align - 1));
118}
119
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000120struct MD5Hash {
121 u64 hash[2];
122 bool operator==(const MD5Hash &other) const {
123 return hash[0] == other.hash[0] && hash[1] == other.hash[1];
124 }
125};
126
127MD5Hash md5_hash(const void *data, uptr size);
128
129struct ThreadState;
130struct ThreadContext;
131struct Context;
132struct ReportStack;
133class ReportDesc;
134class RegionAlloc;
135class StackTrace;
136
137} // namespace __tsan
138
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000139#endif // TSAN_DEFS_H