blob: 971178d8a3e4f975a49e0647c5891524d2ee0e95 [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 Vyukovc30c5f72015-02-13 15:32:34 +000038#ifndef SANITIZER_GO
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000039const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit.
Dmitry Vyukovc30c5f72015-02-13 15:32:34 +000040#else
41const unsigned kMaxTidInClock = kMaxTid; // Go does not track freed memory.
42#endif
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
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000047// Count of shadow values in a shadow cell.
Dmitry Vyukovf34db582012-11-15 18:44:22 +000048const uptr kShadowCnt = 4;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000049
50// That many user bytes are mapped onto a single shadow cell.
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000051const uptr kShadowCell = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000052
53// Size of a single shadow value (u64).
Dmitry Vyukov1d4120b2012-11-06 13:21:06 +000054const uptr kShadowSize = 8;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000055
Dmitry Vyukovc0157122012-11-06 16:00:16 +000056// Shadow memory is kShadowMultiplier times larger than user memory.
57const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
58
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +000059// That many user bytes are mapped onto a single meta shadow cell.
60// Must be less or equal to minimal memory allocator alignment.
61const uptr kMetaShadowCell = 8;
62
63// Size of a single meta shadow value (u32).
64const uptr kMetaShadowSize = 4;
65
Dmitry Vyukov547089e2014-05-15 12:51:48 +000066#if defined(TSAN_NO_HISTORY) && TSAN_NO_HISTORY
67const bool kCollectHistory = false;
68#else
69const bool kCollectHistory = true;
70#endif
71
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000072// The following "build consistency" machinery ensures that all source files
73// are built in the same configuration. Inconsistent builds lead to
74// hard to debug crashes.
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +000075#if SANITIZER_DEBUG
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000076void build_consistency_debug();
77#else
78void build_consistency_release();
79#endif
80
81#if TSAN_COLLECT_STATS
82void build_consistency_stats();
83#else
84void build_consistency_nostats();
85#endif
86
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000087static inline void USED build_consistency() {
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +000088#if SANITIZER_DEBUG
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000089 build_consistency_debug();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000090#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000091 build_consistency_release();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000092#endif
93#if TSAN_COLLECT_STATS
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000094 build_consistency_stats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000095#else
Dmitry Vyukov30c32a82012-05-24 14:50:33 +000096 build_consistency_nostats();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000097#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000098}
99
100template<typename T>
101T min(T a, T b) {
102 return a < b ? a : b;
103}
104
105template<typename T>
106T max(T a, T b) {
107 return a > b ? a : b;
108}
109
110template<typename T>
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000111T RoundUp(T p, u64 align) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000112 DCHECK_EQ(align & (align - 1), 0);
113 return (T)(((u64)p + align - 1) & ~(align - 1));
114}
115
Dmitry Vyukov55b47ca2012-12-04 12:19:53 +0000116template<typename T>
117T RoundDown(T p, u64 align) {
118 DCHECK_EQ(align & (align - 1), 0);
119 return (T)((u64)p & ~(align - 1));
120}
121
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +0000122// Zeroizes high part, returns 'bits' lsb bits.
123template<typename T>
124T GetLsb(T v, int bits) {
125 return (T)((u64)v & ((1ull << bits) - 1));
126}
127
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000128struct MD5Hash {
129 u64 hash[2];
Dmitry Vyukov03d32ec2012-07-05 16:18:28 +0000130 bool operator==(const MD5Hash &other) const;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000131};
132
133MD5Hash md5_hash(const void *data, uptr size);
134
135struct ThreadState;
Dmitry Vyukov3238e1c2013-11-27 11:30:28 +0000136class ThreadContext;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000137struct Context;
138struct ReportStack;
139class ReportDesc;
140class RegionAlloc;
Dmitry Vyukovbde4c9c2014-05-29 13:50:54 +0000141
142// Descriptor of user's memory block.
143struct MBlock {
144 u64 siz;
145 u32 stk;
146 u16 tid;
147};
148
149COMPILER_CHECK(sizeof(MBlock) == 16);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000150
151} // namespace __tsan
152
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000153#endif // TSAN_DEFS_H