blob: 5c5ab9ead6f3e6fe309d270477342c8bc359ebf6 [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";
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 Vyukov5bfac972012-07-16 16:44:47 +000044#ifndef TSAN_GO
Dmitry Vyukov23ecb4a2012-09-06 16:11:30 +000045const int kShadowStackSize = 4 * 1024;
Dmitry Vyukovc87e7282012-09-06 15:18:14 +000046const int kTraceStackSize = 256;
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +000047#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000048
49#ifdef TSAN_SHADOW_COUNT
50# if TSAN_SHADOW_COUNT == 2 \
51 || TSAN_SHADOW_COUNT == 4 || TSAN_SHADOW_COUNT == 8
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000052const uptr kShadowCnt = TSAN_SHADOW_COUNT;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000053# else
54# error "TSAN_SHADOW_COUNT must be one of 2,4,8"
55# endif
56#else
57// Count of shadow values in a shadow cell.
Dmitry Vyukovf34db582012-11-15 18:44:22 +000058const uptr kShadowCnt = 4;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000059#endif
60
61// That many user bytes are mapped onto a single shadow cell.
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000062const uptr kShadowCell = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000063
64// Size of a single shadow value (u64).
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000065const uptr kShadowSize = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000066
Dmitry Vyukovc0157122012-11-06 16:00:16 +000067// Shadow memory is kShadowMultiplier times larger than user memory.
68const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
69
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000070#if defined(TSAN_COLLECT_STATS) && TSAN_COLLECT_STATS
71const bool kCollectStats = true;
72#else
73const bool kCollectStats = false;
74#endif
75
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000076// The following "build consistency" machinery ensures that all source files
77// are built in the same configuration. Inconsistent builds lead to
78// hard to debug crashes.
79#if TSAN_DEBUG
80void build_consistency_debug();
81#else
82void build_consistency_release();
83#endif
84
85#if TSAN_COLLECT_STATS
86void build_consistency_stats();
87#else
88void build_consistency_nostats();
89#endif
90
91#if TSAN_SHADOW_COUNT == 1
92void build_consistency_shadow1();
93#elif TSAN_SHADOW_COUNT == 2
94void build_consistency_shadow2();
95#elif TSAN_SHADOW_COUNT == 4
96void build_consistency_shadow4();
97#else
98void build_consistency_shadow8();
99#endif
100
101static inline void USED build_consistency() {
102#if TSAN_DEBUG
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000103 build_consistency_debug();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000104#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000105 build_consistency_release();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000106#endif
107#if TSAN_COLLECT_STATS
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000108 build_consistency_stats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000109#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000110 build_consistency_nostats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000111#endif
112#if TSAN_SHADOW_COUNT == 1
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000113 build_consistency_shadow1();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000114#elif TSAN_SHADOW_COUNT == 2
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000115 build_consistency_shadow2();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000116#elif TSAN_SHADOW_COUNT == 4
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000117 build_consistency_shadow4();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000118#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +0000119 build_consistency_shadow8();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000120#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000121}
122
123template<typename T>
124T min(T a, T b) {
125 return a < b ? a : b;
126}
127
128template<typename T>
129T max(T a, T b) {
130 return a > b ? a : b;
131}
132
133template<typename T>
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000134T RoundUp(T p, u64 align) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000135 DCHECK_EQ(align & (align - 1), 0);
136 return (T)(((u64)p + align - 1) & ~(align - 1));
137}
138
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000139template<typename T>
140T RoundDown(T p, u64 align) {
141 DCHECK_EQ(align & (align - 1), 0);
142 return (T)((u64)p & ~(align - 1));
143}
144
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000145// Zeroizes high part, returns 'bits' lsb bits.
146template<typename T>
147T GetLsb(T v, int bits) {
148 return (T)((u64)v & ((1ull << bits) - 1));
149}
150
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000151struct MD5Hash {
152 u64 hash[2];
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000153 bool operator==(const MD5Hash &other) const;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000154};
155
156MD5Hash md5_hash(const void *data, uptr size);
157
158struct ThreadState;
159struct ThreadContext;
160struct Context;
161struct ReportStack;
162class ReportDesc;
163class RegionAlloc;
164class StackTrace;
Dmitry Vyukov954fc8c2012-08-15 15:35:15 +0000165struct MBlock;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000166
167} // namespace __tsan
168
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000169#endif // TSAN_DEFS_H