blob: f88322c65b72ca77341b2ac5eca07a940a46d417 [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
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000021namespace __tsan {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000022
Kostya Serebryany83ed8892014-12-09 01:31:14 +000023#ifdef SANITIZER_GO
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +000024const bool kGoMode = true;
25const bool kCppMode = false;
Dmitry Vyukov9952b672012-11-08 11:32:40 +000026const char *const kTsanOptionsEnv = "GORACE";
Dmitry Vyukov087efd22013-01-30 14:38:44 +000027// Go linker does not support weak symbols.
28#define CPP_WEAK
Dmitry Vyukov9952b672012-11-08 11:32:40 +000029#else
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +000030const bool kGoMode = false;
31const bool kCppMode = true;
Dmitry Vyukov9952b672012-11-08 11:32:40 +000032const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
Dmitry Vyukov087efd22013-01-30 14:38:44 +000033#define CPP_WEAK WEAK
Dmitry Vyukov9952b672012-11-08 11:32:40 +000034#endif
35
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000036const int kTidBits = 13;
Kostya Serebryany07c48052012-05-11 14:42:24 +000037const unsigned kMaxTid = 1 << kTidBits;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000038const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
Dmitry Vyukovba429142013-02-01 09:42:06 +000039const int kClkBits = 42;
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +000040const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +000041const uptr kShadowStackSize = 64 * 1024;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000042
43#ifdef TSAN_SHADOW_COUNT
44# if TSAN_SHADOW_COUNT == 2 \
45 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000046const uptr kShadowCnt = TSAN_SHADOW_COUNT;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000047# else
48# error "TSAN_SHADOW_COUNT must be one of 2,4,8"
49# endif
50#else
51// Count of shadow values in a shadow cell.
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000052#define TSAN_SHADOW_COUNT 4
Dmitry Vyukovf34db582012-11-15 18:44:22 +000053const uptr kShadowCnt = 4;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000054#endif
55
56// That many user bytes are mapped onto a single shadow cell.
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000057const uptr kShadowCell = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000058
59// Size of a single shadow value (u64).
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000060const uptr kShadowSize = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000061
Dmitry Vyukovc0157122012-11-06 16:00:16 +000062// Shadow memory is kShadowMultiplier times larger than user memory.
63const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
64
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +000065// That many user bytes are mapped onto a single meta shadow cell.
66// Must be less or equal to minimal memory allocator alignment.
67const uptr kMetaShadowCell = 8;
68
69// Size of a single meta shadow value (u32).
70const uptr kMetaShadowSize = 4;
71
Dmitry Vyukov547089e2014-05-15 12:51:48 +000072#if defined(TSAN_NO_HISTORY) && TSAN_NO_HISTORY
73const bool kCollectHistory = false;
74#else
75const bool kCollectHistory = true;
76#endif
77
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000078#if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
79const bool kCollectStats = true;
80#else
81const bool kCollectStats = false;
82#endif
83
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000084// The following "build consistency" machinery ensures that all source files
85// are built in the same configuration. Inconsistent builds lead to
86// hard to debug crashes.
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +000087#if SANITIZER_DEBUG
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000088void build_consistency_debug();
89#else
90void build_consistency_release();
91#endif
92
93#if TSAN_COLLECT_STATS
94void build_consistency_stats();
95#else
96void build_consistency_nostats();
97#endif
98
99#if TSAN_SHADOW_COUNT == 1
100void build_consistency_shadow1();
101#elif TSAN_SHADOW_COUNT == 2
102void build_consistency_shadow2();
103#elif TSAN_SHADOW_COUNT == 4
104void build_consistency_shadow4();
105#else
106void build_consistency_shadow8();
107#endif
108
109static inline void USED build_consistency() {
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +0000110#if SANITIZER_DEBUG
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000111 build_consistency_debug();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000112#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000113 build_consistency_release();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000114#endif
115#if TSAN_COLLECT_STATS
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000116 build_consistency_stats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000117#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000118 build_consistency_nostats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000119#endif
120#if TSAN_SHADOW_COUNT == 1
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000121 build_consistency_shadow1();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000122#elif TSAN_SHADOW_COUNT == 2
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000123 build_consistency_shadow2();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000124#elif TSAN_SHADOW_COUNT == 4
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000125 build_consistency_shadow4();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000126#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000127 build_consistency_shadow8();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000128#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000129}
130
131template<typename T>
132T min(T a, T b) {
133 return a < b ? a : b;
134}
135
136template<typename T>
137T max(T a, T b) {
138 return a > b ? a : b;
139}
140
141template<typename T>
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000142T RoundUp(T p, u64 align) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000143 DCHECK_EQ(align & (align - 1), 0);
144 return (T)(((u64)p + align - 1) & ~(align - 1));
145}
146
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000147template<typename T>
148T RoundDown(T p, u64 align) {
149 DCHECK_EQ(align & (align - 1), 0);
150 return (T)((u64)p & ~(align - 1));
151}
152
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000153// Zeroizes high part, returns 'bits' lsb bits.
154template<typename T>
155T GetLsb(T v, int bits) {
156 return (T)((u64)v & ((1ull << bits) - 1));
157}
158
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000159struct MD5Hash {
160 u64 hash[2];
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000161 bool operator==(const MD5Hash &other) const;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000162};
163
164MD5Hash md5_hash(const void *data, uptr size);
165
166struct ThreadState;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000167class ThreadContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000168struct Context;
169struct ReportStack;
170class ReportDesc;
171class RegionAlloc;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000172
173// Descriptor of user's memory block.
174struct MBlock {
175 u64 siz;
176 u32 stk;
177 u16 tid;
178};
179
180COMPILER_CHECK(sizeof(MBlock) == 16);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000181
182} // namespace __tsan
183
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000184#endif // TSAN_DEFS_H