xref: /aosp_15_r20/external/compiler-rt/lib/tsan/rtl/tsan_defs.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of ThreadSanitizer (TSan), a race detector.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot #ifndef TSAN_DEFS_H
15*7c3d14c8STreehugger Robot #define TSAN_DEFS_H
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_internal_defs.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_libc.h"
19*7c3d14c8STreehugger Robot #include "tsan_stat.h"
20*7c3d14c8STreehugger Robot #include "ubsan/ubsan_platform.h"
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot // Setup defaults for compile definitions.
23*7c3d14c8STreehugger Robot #ifndef TSAN_NO_HISTORY
24*7c3d14c8STreehugger Robot # define TSAN_NO_HISTORY 0
25*7c3d14c8STreehugger Robot #endif
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot #ifndef TSAN_COLLECT_STATS
28*7c3d14c8STreehugger Robot # define TSAN_COLLECT_STATS 0
29*7c3d14c8STreehugger Robot #endif
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot #ifndef TSAN_CONTAINS_UBSAN
32*7c3d14c8STreehugger Robot # if CAN_SANITIZE_UB && !defined(SANITIZER_GO)
33*7c3d14c8STreehugger Robot #  define TSAN_CONTAINS_UBSAN 1
34*7c3d14c8STreehugger Robot # else
35*7c3d14c8STreehugger Robot #  define TSAN_CONTAINS_UBSAN 0
36*7c3d14c8STreehugger Robot # endif
37*7c3d14c8STreehugger Robot #endif
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot namespace __tsan {
40*7c3d14c8STreehugger Robot 
41*7c3d14c8STreehugger Robot #ifdef SANITIZER_GO
42*7c3d14c8STreehugger Robot const bool kGoMode = true;
43*7c3d14c8STreehugger Robot const bool kCppMode = false;
44*7c3d14c8STreehugger Robot const char *const kTsanOptionsEnv = "GORACE";
45*7c3d14c8STreehugger Robot #else
46*7c3d14c8STreehugger Robot const bool kGoMode = false;
47*7c3d14c8STreehugger Robot const bool kCppMode = true;
48*7c3d14c8STreehugger Robot const char *const kTsanOptionsEnv = "TSAN_OPTIONS";
49*7c3d14c8STreehugger Robot #endif
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot const int kTidBits = 13;
52*7c3d14c8STreehugger Robot const unsigned kMaxTid = 1 << kTidBits;
53*7c3d14c8STreehugger Robot #ifndef SANITIZER_GO
54*7c3d14c8STreehugger Robot const unsigned kMaxTidInClock = kMaxTid * 2;  // This includes msb 'freed' bit.
55*7c3d14c8STreehugger Robot #else
56*7c3d14c8STreehugger Robot const unsigned kMaxTidInClock = kMaxTid;  // Go does not track freed memory.
57*7c3d14c8STreehugger Robot #endif
58*7c3d14c8STreehugger Robot const int kClkBits = 42;
59*7c3d14c8STreehugger Robot const unsigned kMaxTidReuse = (1 << (64 - kClkBits)) - 1;
60*7c3d14c8STreehugger Robot const uptr kShadowStackSize = 64 * 1024;
61*7c3d14c8STreehugger Robot 
62*7c3d14c8STreehugger Robot // Count of shadow values in a shadow cell.
63*7c3d14c8STreehugger Robot const uptr kShadowCnt = 4;
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot // That many user bytes are mapped onto a single shadow cell.
66*7c3d14c8STreehugger Robot const uptr kShadowCell = 8;
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot // Size of a single shadow value (u64).
69*7c3d14c8STreehugger Robot const uptr kShadowSize = 8;
70*7c3d14c8STreehugger Robot 
71*7c3d14c8STreehugger Robot // Shadow memory is kShadowMultiplier times larger than user memory.
72*7c3d14c8STreehugger Robot const uptr kShadowMultiplier = kShadowSize * kShadowCnt / kShadowCell;
73*7c3d14c8STreehugger Robot 
74*7c3d14c8STreehugger Robot // That many user bytes are mapped onto a single meta shadow cell.
75*7c3d14c8STreehugger Robot // Must be less or equal to minimal memory allocator alignment.
76*7c3d14c8STreehugger Robot const uptr kMetaShadowCell = 8;
77*7c3d14c8STreehugger Robot 
78*7c3d14c8STreehugger Robot // Size of a single meta shadow value (u32).
79*7c3d14c8STreehugger Robot const uptr kMetaShadowSize = 4;
80*7c3d14c8STreehugger Robot 
81*7c3d14c8STreehugger Robot #if TSAN_NO_HISTORY
82*7c3d14c8STreehugger Robot const bool kCollectHistory = false;
83*7c3d14c8STreehugger Robot #else
84*7c3d14c8STreehugger Robot const bool kCollectHistory = true;
85*7c3d14c8STreehugger Robot #endif
86*7c3d14c8STreehugger Robot 
87*7c3d14c8STreehugger Robot const unsigned kInvalidTid = (unsigned)-1;
88*7c3d14c8STreehugger Robot 
89*7c3d14c8STreehugger Robot // The following "build consistency" machinery ensures that all source files
90*7c3d14c8STreehugger Robot // are built in the same configuration. Inconsistent builds lead to
91*7c3d14c8STreehugger Robot // hard to debug crashes.
92*7c3d14c8STreehugger Robot #if SANITIZER_DEBUG
93*7c3d14c8STreehugger Robot void build_consistency_debug();
94*7c3d14c8STreehugger Robot #else
95*7c3d14c8STreehugger Robot void build_consistency_release();
96*7c3d14c8STreehugger Robot #endif
97*7c3d14c8STreehugger Robot 
98*7c3d14c8STreehugger Robot #if TSAN_COLLECT_STATS
99*7c3d14c8STreehugger Robot void build_consistency_stats();
100*7c3d14c8STreehugger Robot #else
101*7c3d14c8STreehugger Robot void build_consistency_nostats();
102*7c3d14c8STreehugger Robot #endif
103*7c3d14c8STreehugger Robot 
build_consistency()104*7c3d14c8STreehugger Robot static inline void USED build_consistency() {
105*7c3d14c8STreehugger Robot #if SANITIZER_DEBUG
106*7c3d14c8STreehugger Robot   build_consistency_debug();
107*7c3d14c8STreehugger Robot #else
108*7c3d14c8STreehugger Robot   build_consistency_release();
109*7c3d14c8STreehugger Robot #endif
110*7c3d14c8STreehugger Robot #if TSAN_COLLECT_STATS
111*7c3d14c8STreehugger Robot   build_consistency_stats();
112*7c3d14c8STreehugger Robot #else
113*7c3d14c8STreehugger Robot   build_consistency_nostats();
114*7c3d14c8STreehugger Robot #endif
115*7c3d14c8STreehugger Robot }
116*7c3d14c8STreehugger Robot 
117*7c3d14c8STreehugger Robot template<typename T>
min(T a,T b)118*7c3d14c8STreehugger Robot T min(T a, T b) {
119*7c3d14c8STreehugger Robot   return a < b ? a : b;
120*7c3d14c8STreehugger Robot }
121*7c3d14c8STreehugger Robot 
122*7c3d14c8STreehugger Robot template<typename T>
max(T a,T b)123*7c3d14c8STreehugger Robot T max(T a, T b) {
124*7c3d14c8STreehugger Robot   return a > b ? a : b;
125*7c3d14c8STreehugger Robot }
126*7c3d14c8STreehugger Robot 
127*7c3d14c8STreehugger Robot template<typename T>
RoundUp(T p,u64 align)128*7c3d14c8STreehugger Robot T RoundUp(T p, u64 align) {
129*7c3d14c8STreehugger Robot   DCHECK_EQ(align & (align - 1), 0);
130*7c3d14c8STreehugger Robot   return (T)(((u64)p + align - 1) & ~(align - 1));
131*7c3d14c8STreehugger Robot }
132*7c3d14c8STreehugger Robot 
133*7c3d14c8STreehugger Robot template<typename T>
RoundDown(T p,u64 align)134*7c3d14c8STreehugger Robot T RoundDown(T p, u64 align) {
135*7c3d14c8STreehugger Robot   DCHECK_EQ(align & (align - 1), 0);
136*7c3d14c8STreehugger Robot   return (T)((u64)p & ~(align - 1));
137*7c3d14c8STreehugger Robot }
138*7c3d14c8STreehugger Robot 
139*7c3d14c8STreehugger Robot // Zeroizes high part, returns 'bits' lsb bits.
140*7c3d14c8STreehugger Robot template<typename T>
GetLsb(T v,int bits)141*7c3d14c8STreehugger Robot T GetLsb(T v, int bits) {
142*7c3d14c8STreehugger Robot   return (T)((u64)v & ((1ull << bits) - 1));
143*7c3d14c8STreehugger Robot }
144*7c3d14c8STreehugger Robot 
145*7c3d14c8STreehugger Robot struct MD5Hash {
146*7c3d14c8STreehugger Robot   u64 hash[2];
147*7c3d14c8STreehugger Robot   bool operator==(const MD5Hash &other) const;
148*7c3d14c8STreehugger Robot };
149*7c3d14c8STreehugger Robot 
150*7c3d14c8STreehugger Robot MD5Hash md5_hash(const void *data, uptr size);
151*7c3d14c8STreehugger Robot 
152*7c3d14c8STreehugger Robot struct Processor;
153*7c3d14c8STreehugger Robot struct ThreadState;
154*7c3d14c8STreehugger Robot class ThreadContext;
155*7c3d14c8STreehugger Robot struct Context;
156*7c3d14c8STreehugger Robot struct ReportStack;
157*7c3d14c8STreehugger Robot class ReportDesc;
158*7c3d14c8STreehugger Robot class RegionAlloc;
159*7c3d14c8STreehugger Robot 
160*7c3d14c8STreehugger Robot // Descriptor of user's memory block.
161*7c3d14c8STreehugger Robot struct MBlock {
162*7c3d14c8STreehugger Robot   u64  siz;
163*7c3d14c8STreehugger Robot   u32  stk;
164*7c3d14c8STreehugger Robot   u16  tid;
165*7c3d14c8STreehugger Robot };
166*7c3d14c8STreehugger Robot 
167*7c3d14c8STreehugger Robot COMPILER_CHECK(sizeof(MBlock) == 16);
168*7c3d14c8STreehugger Robot 
169*7c3d14c8STreehugger Robot }  // namespace __tsan
170*7c3d14c8STreehugger Robot 
171*7c3d14c8STreehugger Robot #endif  // TSAN_DEFS_H
172