blob: 7ed3796b5012660ce1c2023f164c265db94231a1 [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
Kostya Serebryany83ed8892014-12-09 01:31:14 +000027#ifdef SANITIZER_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";
Dmitry Vyukov087efd22013-01-30 14:38:44 +000031// Go linker does not support weak symbols.
32#define CPP_WEAK
Dmitry Vyukov9952b672012-11-08 11:32:40 +000033#else
Dmitry Vyukoveb3d36e2012-11-28 13:01:32 +000034const bool kGoMode = false;
35const bool kCppMode = true;
Dmitry Vyukov9952b672012-11-08 11:32:40 +000036const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
Dmitry Vyukov087efd22013-01-30 14:38:44 +000037#define CPP_WEAK WEAK
Dmitry Vyukov9952b672012-11-08 11:32:40 +000038#endif
39
Dmitry Vyukovf6985e32012-05-22 14:34:43 +000040const int kTidBits = 13;
Kostya Serebryany07c48052012-05-11 14:42:24 +000041const unsigned kMaxTid = 1 << kTidBits;
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000042const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
Dmitry Vyukovba429142013-02-01 09:42:06 +000043const int kClkBits = 42;
Dmitry Vyukovb5eb8f02014-04-11 15:38:03 +000044const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
Dmitry Vyukov464ebbd2013-10-16 15:35:12 +000045const uptr kShadowStackSize = 64 * 1024;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000046
47#ifdef TSAN_SHADOW_COUNT
48# if TSAN_SHADOW_COUNT == 2 \
49 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000050const uptr kShadowCnt = TSAN_SHADOW_COUNT;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000051# else
52# error "TSAN_SHADOW_COUNT must be one of 2,4,8"
53# endif
54#else
55// Count of shadow values in a shadow cell.
Dmitry Vyukovafdcc962014-05-30 13:36:29 +000056#define TSAN_SHADOW_COUNT 4
Dmitry Vyukovf34db582012-11-15 18:44:22 +000057const uptr kShadowCnt = 4;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000058#endif
59
60// That many user bytes are mapped onto a single shadow cell.
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000061const uptr kShadowCell = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000062
63// Size of a single shadow value (u64).
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000064const uptr kShadowSize = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000065
Dmitry Vyukovc0157122012-11-06 16:00:16 +000066// Shadow memory is kShadowMultiplier times larger than user memory.
67const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
68
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +000069// That many user bytes are mapped onto a single meta shadow cell.
70// Must be less or equal to minimal memory allocator alignment.
71const uptr kMetaShadowCell = 8;
72
73// Size of a single meta shadow value (u32).
74const uptr kMetaShadowSize = 4;
75
Dmitry Vyukov547089e2014-05-15 12:51:48 +000076#if defined(TSAN_NO_HISTORY) && TSAN_NO_HISTORY
77const bool kCollectHistory = false;
78#else
79const bool kCollectHistory = true;
80#endif
81
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000082#if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
83const bool kCollectStats = true;
84#else
85const bool kCollectStats = false;
86#endif
87
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000088// The following "build consistency" machinery ensures that all source files
89// are built in the same configuration. Inconsistent builds lead to
90// hard to debug crashes.
91#if TSAN_DEBUG
92void build_consistency_debug();
93#else
94void build_consistency_release();
95#endif
96
97#if TSAN_COLLECT_STATS
98void build_consistency_stats();
99#else
100void build_consistency_nostats();
101#endif
102
103#if TSAN_SHADOW_COUNT == 1
104void build_consistency_shadow1();
105#elif TSAN_SHADOW_COUNT == 2
106void build_consistency_shadow2();
107#elif TSAN_SHADOW_COUNT == 4
108void build_consistency_shadow4();
109#else
110void build_consistency_shadow8();
111#endif
112
113static inline void USED build_consistency() {
114#if TSAN_DEBUG
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000115 build_consistency_debug();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000116#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000117 build_consistency_release();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000118#endif
119#if TSAN_COLLECT_STATS
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000120 build_consistency_stats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000121#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000122 build_consistency_nostats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000123#endif
124#if TSAN_SHADOW_COUNT == 1
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000125 build_consistency_shadow1();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000126#elif TSAN_SHADOW_COUNT == 2
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000127 build_consistency_shadow2();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000128#elif TSAN_SHADOW_COUNT == 4
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000129 build_consistency_shadow4();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000130#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000131 build_consistency_shadow8();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000132#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000133}
134
135template<typename T>
136T min(T a, T b) {
137 return a < b ? a : b;
138}
139
140template<typename T>
141T max(T a, T b) {
142 return a > b ? a : b;
143}
144
145template<typename T>
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000146T RoundUp(T p, u64 align) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000147 DCHECK_EQ(align & (align - 1), 0);
148 return (T)(((u64)p + align - 1) & ~(align - 1));
149}
150
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000151template<typename T>
152T RoundDown(T p, u64 align) {
153 DCHECK_EQ(align & (align - 1), 0);
154 return (T)((u64)p & ~(align - 1));
155}
156
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000157// Zeroizes high part, returns 'bits' lsb bits.
158template<typename T>
159T GetLsb(T v, int bits) {
160 return (T)((u64)v & ((1ull << bits) - 1));
161}
162
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000163struct MD5Hash {
164 u64 hash[2];
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000165 bool operator==(const MD5Hash &other) const;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166};
167
168MD5Hash md5_hash(const void *data, uptr size);
169
170struct ThreadState;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000171class ThreadContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000172struct Context;
173struct ReportStack;
174class ReportDesc;
175class RegionAlloc;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000176
177// Descriptor of user's memory block.
178struct MBlock {
179 u64 siz;
180 u32 stk;
181 u16 tid;
182};
183
184COMPILER_CHECK(sizeof(MBlock) == 16);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000185
186} // namespace __tsan
187
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000188#endif // TSAN_DEFS_H