blob: e0c04733f0a39d935c2c77d38568d93496e4f7f3 [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 Vyukov9952b672012-11-08 11:32:40 +000027#ifdef TSAN_GO
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +000028const bool kGoMode = true;
29const bool kCppMode = false;
Dmitry Vyukov9952b672012-11-08 11:32:40 +000030const char *const kTsanOptionsEnv = "GORACE";
31#else
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +000032const bool kGoMode = false;
33const bool kCppMode = true;
Dmitry Vyukov9952b672012-11-08 11:32:40 +000034const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
35#endif
36
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000037const int kTidBits = 13;
Kostya Serebryany07c48052012-05-11 14:42:24 +000038const unsigned kMaxTid = 1 << kTidBits;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000039const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
Dmitry Vyukov302cebb2012-05-22 18:07:45 +000040const int kClkBits = 43;
Dmitry Vyukov5bfac972012-07-16 16:44:47 +000041#ifndef TSAN_GO
Dmitry Vyukov23ecb4a2012-09-06 16:11:30 +000042const int kShadowStackSize = 4 * 1024;
Dmitry Vyukovc87e7282012-09-06 15:18:14 +000043const int kTraceStackSize = 256;
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +000044#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000045
46#ifdef TSAN_SHADOW_COUNT
47# if TSAN_SHADOW_COUNT == 2 \
48 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000049const uptr kShadowCnt = TSAN_SHADOW_COUNT;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000050# else
51# error "TSAN_SHADOW_COUNT must be one of 2,4,8"
52# endif
53#else
54// Count of shadow values in a shadow cell.
Dmitry Vyukovf34db582012-11-15 18:44:22 +000055const uptr kShadowCnt = 4;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000056#endif
57
58// That many user bytes are mapped onto a single shadow cell.
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000059const uptr kShadowCell = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000060
61// Size of a single shadow value (u64).
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000062const uptr kShadowSize = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000063
Dmitry Vyukovc0157122012-11-06 16:00:16 +000064// Shadow memory is kShadowMultiplier times larger than user memory.
65const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
66
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000067#if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
68const bool kCollectStats = true;
69#else
70const bool kCollectStats = false;
71#endif
72
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000073// The following "build consistency" machinery ensures that all source files
74// are built in the same configuration. Inconsistent builds lead to
75// hard to debug crashes.
76#if TSAN_DEBUG
77void build_consistency_debug();
78#else
79void build_consistency_release();
80#endif
81
82#if TSAN_COLLECT_STATS
83void build_consistency_stats();
84#else
85void build_consistency_nostats();
86#endif
87
88#if TSAN_SHADOW_COUNT == 1
89void build_consistency_shadow1();
90#elif TSAN_SHADOW_COUNT == 2
91void build_consistency_shadow2();
92#elif TSAN_SHADOW_COUNT == 4
93void build_consistency_shadow4();
94#else
95void build_consistency_shadow8();
96#endif
97
98static inline void USED build_consistency() {
99#if TSAN_DEBUG
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000100 build_consistency_debug();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000101#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000102 build_consistency_release();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000103#endif
104#if TSAN_COLLECT_STATS
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000105 build_consistency_stats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000106#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000107 build_consistency_nostats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000108#endif
109#if TSAN_SHADOW_COUNT == 1
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000110 build_consistency_shadow1();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000111#elif TSAN_SHADOW_COUNT == 2
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000112 build_consistency_shadow2();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000113#elif TSAN_SHADOW_COUNT == 4
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000114 build_consistency_shadow4();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000115#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000116 build_consistency_shadow8();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000117#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000118}
119
120template<typename T>
121T min(T a, T b) {
122 return a < b ? a : b;
123}
124
125template<typename T>
126T max(T a, T b) {
127 return a > b ? a : b;
128}
129
130template<typename T>
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000131T RoundUp(T p, u64 align) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000132 DCHECK_EQ(align & (align - 1), 0);
133 return (T)(((u64)p + align - 1) & ~(align - 1));
134}
135
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000136template<typename T>
137T RoundDown(T p, u64 align) {
138 DCHECK_EQ(align & (align - 1), 0);
139 return (T)((u64)p & ~(align - 1));
140}
141
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000142// Zeroizes high part, returns 'bits' lsb bits.
143template<typename T>
144T GetLsb(T v, int bits) {
145 return (T)((u64)v & ((1ull << bits) - 1));
146}
147
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000148struct MD5Hash {
149 u64 hash[2];
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000150 bool operator==(const MD5Hash &other) const;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000151};
152
153MD5Hash md5_hash(const void *data, uptr size);
154
155struct ThreadState;
156struct ThreadContext;
157struct Context;
158struct ReportStack;
159class ReportDesc;
160class RegionAlloc;
161class StackTrace;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000162struct MBlock;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000163
164} // namespace __tsan
165
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166#endif // TSAN_DEFS_H