1 //===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 // Platform-specific code.
13 //===----------------------------------------------------------------------===//
16 C++ linux memory layout:
17 0000 0000 0000 - 03c0 0000 0000: protected
18 03c0 0000 0000 - 1000 0000 0000: shadow
19 1000 0000 0000 - 7d00 0000 0000: protected
20 7d00 0000 0000 - 7e00 0000 0000: heap
21 7e00 0000 0000 - 7fff ffff ffff: modules and main thread stack
23 C++ COMPAT linux memory layout:
24 0000 0000 0000 - 0400 0000 0000: protected
25 0400 0000 0000 - 1000 0000 0000: shadow
26 1000 0000 0000 - 2900 0000 0000: protected
27 2900 0000 0000 - 2c00 0000 0000: modules
28 2c00 0000 0000 - 7d00 0000 0000: -
29 7d00 0000 0000 - 7e00 0000 0000: heap
30 7e00 0000 0000 - 7f00 0000 0000: -
31 7f00 0000 0000 - 7fff ffff ffff: main thread stack
33 Go linux and darwin memory layout:
34 0000 0000 0000 - 0000 1000 0000: executable
35 0000 1000 0000 - 00f8 0000 0000: -
36 00f8 0000 0000 - 0118 0000 0000: heap
37 0118 0000 0000 - 1000 0000 0000: -
38 1000 0000 0000 - 1460 0000 0000: shadow
39 1460 0000 0000 - 7fff ffff ffff: -
41 Go windows memory layout:
42 0000 0000 0000 - 0000 1000 0000: executable
43 0000 1000 0000 - 00f8 0000 0000: -
44 00f8 0000 0000 - 0118 0000 0000: heap
45 0118 0000 0000 - 0100 0000 0000: -
46 0100 0000 0000 - 0560 0000 0000: shadow
47 0560 0000 0000 - 07ff ffff ffff: -
50 #ifndef TSAN_PLATFORM_H
51 #define TSAN_PLATFORM_H
55 #if defined(__LP64__) || defined(_WIN64)
59 static const uptr kLinuxAppMemBeg
= 0x000000000000ULL
;
60 static const uptr kLinuxAppMemEnd
= 0x00fcffffffffULL
;
62 static const uptr kLinuxShadowMsk
= 0x010000000000ULL
;
64 static const uptr kLinuxShadowMsk
= 0x100000000000ULL
;
66 // TSAN_COMPAT_SHADOW is intended for COMPAT virtual memory layout,
67 // when memory addresses are of the 0x2axxxxxxxxxx form.
68 // The option is enabled with 'setarch x86_64 -L'.
69 #elif defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
70 static const uptr kLinuxAppMemBeg
= 0x290000000000ULL
;
71 static const uptr kLinuxAppMemEnd
= 0x7fffffffffffULL
;
73 static const uptr kLinuxAppMemBeg
= 0x7cf000000000ULL
;
74 static const uptr kLinuxAppMemEnd
= 0x7fffffffffffULL
;
77 static const uptr kLinuxAppMemMsk
= 0x7c0000000000ULL
;
79 // This has to be a macro to allow constant initialization of constants below.
81 #define MemToShadow(addr) \
82 (((addr) & ~(kLinuxAppMemMsk | (kShadowCell - 1))) * kShadowCnt)
84 #define MemToShadow(addr) \
85 ((((addr) & ~(kShadowCell - 1)) * kShadowCnt) | kLinuxShadowMsk)
88 static const uptr kLinuxShadowBeg
= MemToShadow(kLinuxAppMemBeg
);
89 static const uptr kLinuxShadowEnd
=
90 MemToShadow(kLinuxAppMemEnd
) | 0xff;
92 static inline bool IsAppMem(uptr mem
) {
93 return mem
>= kLinuxAppMemBeg
&& mem
<= kLinuxAppMemEnd
;
96 static inline bool IsShadowMem(uptr mem
) {
97 return mem
>= kLinuxShadowBeg
&& mem
<= kLinuxShadowEnd
;
100 static inline uptr
ShadowToMem(uptr shadow
) {
101 CHECK(IsShadowMem(shadow
));
103 return (shadow
& ~kLinuxShadowMsk
) / kShadowCnt
;
105 return (shadow
/ kShadowCnt
) | kLinuxAppMemMsk
;
109 // For COMPAT mapping returns an alternative address
110 // that mapped to the same shadow address.
111 // COMPAT mapping is not quite one-to-one.
112 static inline uptr
AlternativeAddress(uptr addr
) {
113 #if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
114 return (addr
& ~kLinuxAppMemMsk
) | 0x280000000000ULL
;
120 uptr
GetShadowMemoryConsumption();
121 void FlushShadowMemory();
123 const char *InitializePlatform();
124 void FinalizePlatform();
126 void internal_start_thread(void(*func
)(void*), void *arg
);
128 // Says whether the addr relates to a global var.
129 // Guesses with high probability, may yield both false positives and negatives.
130 bool IsGlobalVar(uptr addr
);
132 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
133 uptr
*tls_addr
, uptr
*tls_size
);
135 } // namespace __tsan
137 #else // defined(__LP64__) || defined(_WIN64)
138 # error "Only 64-bit is supported"
141 #endif // TSAN_PLATFORM_H