1 //===-- tsan_defs.h ---------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
16 #include "sanitizer_common/sanitizer_internal_defs.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "sanitizer_common/sanitizer_mutex.h"
19 #include "ubsan/ubsan_platform.h"
21 #ifndef TSAN_VECTORIZE
22 # define TSAN_VECTORIZE __SSE4_2__
26 // <emmintrin.h> transitively includes <stdlib.h>,
27 // and it's prohibited to include std headers into tsan runtime.
28 // So we do this dirty trick.
29 # define _MM_MALLOC_H_INCLUDED
30 # define __MM_MALLOC_H
31 # include <emmintrin.h>
32 # include <smmintrin.h>
33 # define VECTOR_ALIGNED ALIGNED(16)
36 # define VECTOR_ALIGNED
39 // Setup defaults for compile definitions.
40 #ifndef TSAN_NO_HISTORY
41 # define TSAN_NO_HISTORY 0
44 #ifndef TSAN_CONTAINS_UBSAN
45 # if CAN_SANITIZE_UB && !SANITIZER_GO
46 # define TSAN_CONTAINS_UBSAN 1
48 # define TSAN_CONTAINS_UBSAN 0
54 constexpr uptr kByteBits
= 8;
57 enum class Sid
: u8
{};
58 constexpr uptr kThreadSlotCount
= 256;
59 constexpr Sid kFreeSid
= static_cast<Sid
>(255);
61 // Abstract time unit, vector clock element.
62 enum class Epoch
: u16
{};
63 constexpr uptr kEpochBits
= 14;
64 constexpr Epoch kEpochZero
= static_cast<Epoch
>(0);
65 constexpr Epoch kEpochOver
= static_cast<Epoch
>(1 << kEpochBits
);
66 constexpr Epoch kEpochLast
= static_cast<Epoch
>((1 << kEpochBits
) - 1);
68 inline Epoch
EpochInc(Epoch epoch
) {
69 return static_cast<Epoch
>(static_cast<u16
>(epoch
) + 1);
72 inline bool EpochOverflow(Epoch epoch
) { return epoch
== kEpochOver
; }
74 const uptr kShadowStackSize
= 64 * 1024;
76 // Count of shadow values in a shadow cell.
77 const uptr kShadowCnt
= 4;
79 // That many user bytes are mapped onto a single shadow cell.
80 const uptr kShadowCell
= 8;
82 // Single shadow value.
83 enum class RawShadow
: u32
{};
84 const uptr kShadowSize
= sizeof(RawShadow
);
86 // Shadow memory is kShadowMultiplier times larger than user memory.
87 const uptr kShadowMultiplier
= kShadowSize
* kShadowCnt
/ kShadowCell
;
89 // That many user bytes are mapped onto a single meta shadow cell.
90 // Must be less or equal to minimal memory allocator alignment.
91 const uptr kMetaShadowCell
= 8;
93 // Size of a single meta shadow value (u32).
94 const uptr kMetaShadowSize
= 4;
96 // All addresses and PCs are assumed to be compressable to that many bits.
97 const uptr kCompressedAddrBits
= 44;
100 const bool kCollectHistory
= false;
102 const bool kCollectHistory
= true;
105 // The following "build consistency" machinery ensures that all source files
106 // are built in the same configuration. Inconsistent builds lead to
107 // hard to debug crashes.
109 void build_consistency_debug();
111 void build_consistency_release();
114 static inline void USED
build_consistency() {
116 build_consistency_debug();
118 build_consistency_release();
124 return a
< b
? a
: b
;
129 return a
> b
? a
: b
;
133 T
RoundUp(T p
, u64 align
) {
134 DCHECK_EQ(align
& (align
- 1), 0);
135 return (T
)(((u64
)p
+ align
- 1) & ~(align
- 1));
139 T
RoundDown(T p
, u64 align
) {
140 DCHECK_EQ(align
& (align
- 1), 0);
141 return (T
)((u64
)p
& ~(align
- 1));
144 // Zeroizes high part, returns 'bits' lsb bits.
146 T
GetLsb(T v
, int bits
) {
147 return (T
)((u64
)v
& ((1ull << bits
) - 1));
152 bool operator==(const MD5Hash
&other
) const;
155 MD5Hash
md5_hash(const void *data
, uptr size
);
168 typedef uptr AccessType
;
172 kAccessRead
= 1 << 0,
173 kAccessAtomic
= 1 << 1,
174 kAccessVptr
= 1 << 2, // read or write of an object virtual table pointer
175 kAccessFree
= 1 << 3, // synthetic memory access during memory freeing
176 kAccessExternalPC
= 1 << 4, // access PC can have kExternalPCBit set
177 kAccessCheckOnly
= 1 << 5, // check for races, but don't store
178 kAccessNoRodata
= 1 << 6, // don't check for .rodata marker
179 kAccessSlotLocked
= 1 << 7, // memory access with TidSlot locked
182 // Descriptor of user's memory block.
190 COMPILER_CHECK(sizeof(MBlock
) == 16);
192 enum ExternalTag
: uptr
{
193 kExternalTagNone
= 0,
194 kExternalTagSwiftModifyingAccess
= 1,
195 kExternalTagFirstUserAvailable
= 2,
196 kExternalTagMax
= 1024,
197 // Don't set kExternalTagMax over 65,536, since MBlock only stores tags
198 // as 16-bit values, see tsan_defs.h.
202 MutexTypeReport
= MutexLastCommon
,
204 MutexTypeAnnotations
,
209 MutexTypeInternalAlloc
,
215 } // namespace __tsan
217 #endif // TSAN_DEFS_H