Rebase.
[official-gcc.git] / libsanitizer / tsan / tsan_platform.h
blob60eb1a8499548761fc9c3a50e366e19a75734038
1 //===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 // Platform-specific code.
11 //===----------------------------------------------------------------------===//
14 C++ linux memory layout:
15 0000 0000 0000 - 03c0 0000 0000: protected
16 03c0 0000 0000 - 1000 0000 0000: shadow
17 1000 0000 0000 - 6000 0000 0000: protected
18 6000 0000 0000 - 6200 0000 0000: traces
19 6200 0000 0000 - 7d00 0000 0000: -
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 - 6000 0000 0000: -
29 6000 0000 0000 - 6200 0000 0000: traces
30 6200 0000 0000 - 7d00 0000 0000: -
31 7d00 0000 0000 - 7e00 0000 0000: heap
32 7e00 0000 0000 - 7f00 0000 0000: -
33 7f00 0000 0000 - 7fff ffff ffff: main thread stack
35 Go linux and darwin memory layout:
36 0000 0000 0000 - 0000 1000 0000: executable
37 0000 1000 0000 - 00f8 0000 0000: -
38 00c0 0000 0000 - 00e0 0000 0000: heap
39 00e0 0000 0000 - 1000 0000 0000: -
40 1000 0000 0000 - 1380 0000 0000: shadow
41 1460 0000 0000 - 6000 0000 0000: -
42 6000 0000 0000 - 6200 0000 0000: traces
43 6200 0000 0000 - 7fff ffff ffff: -
45 Go windows memory layout:
46 0000 0000 0000 - 0000 1000 0000: executable
47 0000 1000 0000 - 00f8 0000 0000: -
48 00c0 0000 0000 - 00e0 0000 0000: heap
49 00e0 0000 0000 - 0100 0000 0000: -
50 0100 0000 0000 - 0560 0000 0000: shadow
51 0560 0000 0000 - 0760 0000 0000: traces
52 0760 0000 0000 - 07ff ffff ffff: -
55 #ifndef TSAN_PLATFORM_H
56 #define TSAN_PLATFORM_H
58 #include "tsan_defs.h"
59 #include "tsan_trace.h"
61 #if defined(__LP64__) || defined(_WIN64)
62 namespace __tsan {
64 #if defined(TSAN_GO)
65 static const uptr kLinuxAppMemBeg = 0x000000000000ULL;
66 static const uptr kLinuxAppMemEnd = 0x04dfffffffffULL;
67 # if SANITIZER_WINDOWS
68 static const uptr kLinuxShadowMsk = 0x010000000000ULL;
69 # else
70 static const uptr kLinuxShadowMsk = 0x200000000000ULL;
71 # endif
72 // TSAN_COMPAT_SHADOW is intended for COMPAT virtual memory layout,
73 // when memory addresses are of the 0x2axxxxxxxxxx form.
74 // The option is enabled with 'setarch x86_64 -L'.
75 #elif defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
76 static const uptr kLinuxAppMemBeg = 0x290000000000ULL;
77 static const uptr kLinuxAppMemEnd = 0x7fffffffffffULL;
78 static const uptr kAppMemGapBeg = 0x2c0000000000ULL;
79 static const uptr kAppMemGapEnd = 0x7d0000000000ULL;
80 #else
81 static const uptr kLinuxAppMemBeg = 0x7cf000000000ULL;
82 static const uptr kLinuxAppMemEnd = 0x7fffffffffffULL;
83 #endif
85 static const uptr kLinuxAppMemMsk = 0x7c0000000000ULL;
87 #if SANITIZER_WINDOWS
88 const uptr kTraceMemBegin = 0x056000000000ULL;
89 #else
90 const uptr kTraceMemBegin = 0x600000000000ULL;
91 #endif
92 const uptr kTraceMemSize = 0x020000000000ULL;
94 // This has to be a macro to allow constant initialization of constants below.
95 #ifndef TSAN_GO
96 #define MemToShadow(addr) \
97 (((addr) & ~(kLinuxAppMemMsk | (kShadowCell - 1))) * kShadowCnt)
98 #else
99 #define MemToShadow(addr) \
100 ((((addr) & ~(kShadowCell - 1)) * kShadowCnt) | kLinuxShadowMsk)
101 #endif
103 static const uptr kLinuxShadowBeg = MemToShadow(kLinuxAppMemBeg);
104 static const uptr kLinuxShadowEnd =
105 MemToShadow(kLinuxAppMemEnd) | 0xff;
107 static inline bool IsAppMem(uptr mem) {
108 #if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
109 return (mem >= kLinuxAppMemBeg && mem < kAppMemGapBeg) ||
110 (mem >= kAppMemGapEnd && mem <= kLinuxAppMemEnd);
111 #else
112 return mem >= kLinuxAppMemBeg && mem <= kLinuxAppMemEnd;
113 #endif
116 static inline bool IsShadowMem(uptr mem) {
117 return mem >= kLinuxShadowBeg && mem <= kLinuxShadowEnd;
120 static inline uptr ShadowToMem(uptr shadow) {
121 CHECK(IsShadowMem(shadow));
122 #ifdef TSAN_GO
123 return (shadow & ~kLinuxShadowMsk) / kShadowCnt;
124 #else
125 return (shadow / kShadowCnt) | kLinuxAppMemMsk;
126 #endif
129 // For COMPAT mapping returns an alternative address
130 // that mapped to the same shadow address.
131 // COMPAT mapping is not quite one-to-one.
132 static inline uptr AlternativeAddress(uptr addr) {
133 #if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
134 return (addr & ~kLinuxAppMemMsk) | 0x280000000000ULL;
135 #else
136 return 0;
137 #endif
140 void FlushShadowMemory();
141 void WriteMemoryProfile(char *buf, uptr buf_size);
142 uptr GetRSS();
144 const char *InitializePlatform();
145 void FinalizePlatform();
147 // The additional page is to catch shadow stack overflow as paging fault.
148 // Windows wants 64K alignment for mmaps.
149 const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
150 + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
152 uptr ALWAYS_INLINE GetThreadTrace(int tid) {
153 uptr p = kTraceMemBegin + (uptr)tid * kTotalTraceSize;
154 DCHECK_LT(p, kTraceMemBegin + kTraceMemSize);
155 return p;
158 uptr ALWAYS_INLINE GetThreadTraceHeader(int tid) {
159 uptr p = kTraceMemBegin + (uptr)tid * kTotalTraceSize
160 + kTraceSize * sizeof(Event);
161 DCHECK_LT(p, kTraceMemBegin + kTraceMemSize);
162 return p;
165 void *internal_start_thread(void(*func)(void*), void *arg);
166 void internal_join_thread(void *th);
168 // Says whether the addr relates to a global var.
169 // Guesses with high probability, may yield both false positives and negatives.
170 bool IsGlobalVar(uptr addr);
171 int ExtractResolvFDs(void *state, int *fds, int nfd);
172 int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
174 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
175 void *abstime), void *c, void *m, void *abstime,
176 void(*cleanup)(void *arg), void *arg);
178 } // namespace __tsan
180 #else // defined(__LP64__) || defined(_WIN64)
181 # error "Only 64-bit is supported"
182 #endif
184 #endif // TSAN_PLATFORM_H