]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/tsan/rtl/tsan_platform.h
Update compiler-rt to trunk r228651. This enables using Address
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / tsan / rtl / tsan_platform.h
1 //===-- tsan_platform.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 // Platform-specific code.
13 //===----------------------------------------------------------------------===//
14
15 #ifndef TSAN_PLATFORM_H
16 #define TSAN_PLATFORM_H
17
18 #if !defined(__LP64__) && !defined(_WIN64)
19 # error "Only 64-bit is supported"
20 #endif
21
22 #include "tsan_defs.h"
23 #include "tsan_trace.h"
24
25 namespace __tsan {
26
27 #if !defined(SANITIZER_GO)
28
29 /*
30 C/C++ on linux and freebsd
31 0000 0000 1000 - 0100 0000 0000: main binary and/or MAP_32BIT mappings
32 0100 0000 0000 - 0200 0000 0000: -
33 0200 0000 0000 - 1000 0000 0000: shadow
34 1000 0000 0000 - 3000 0000 0000: -
35 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
36 4000 0000 0000 - 6000 0000 0000: -
37 6000 0000 0000 - 6200 0000 0000: traces
38 6200 0000 0000 - 7d00 0000 0000: -
39 7d00 0000 0000 - 7e00 0000 0000: heap
40 7e00 0000 0000 - 7e80 0000 0000: -
41 7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
42 */
43
44 const uptr kMetaShadowBeg = 0x300000000000ull;
45 const uptr kMetaShadowEnd = 0x400000000000ull;
46 const uptr kTraceMemBeg   = 0x600000000000ull;
47 const uptr kTraceMemEnd   = 0x620000000000ull;
48 const uptr kShadowBeg     = 0x020000000000ull;
49 const uptr kShadowEnd     = 0x100000000000ull;
50 const uptr kHeapMemBeg    = 0x7d0000000000ull;
51 const uptr kHeapMemEnd    = 0x7e0000000000ull;
52 const uptr kLoAppMemBeg   = 0x000000001000ull;
53 const uptr kLoAppMemEnd   = 0x010000000000ull;
54 const uptr kHiAppMemBeg   = 0x7e8000000000ull;
55 const uptr kHiAppMemEnd   = 0x800000000000ull;
56 const uptr kAppMemMsk     = 0x7c0000000000ull;
57 const uptr kAppMemXor     = 0x020000000000ull;
58
59 ALWAYS_INLINE
60 bool IsAppMem(uptr mem) {
61   return (mem >= kHeapMemBeg && mem < kHeapMemEnd) ||
62          (mem >= kLoAppMemBeg && mem < kLoAppMemEnd) ||
63          (mem >= kHiAppMemBeg && mem < kHiAppMemEnd);
64 }
65
66 ALWAYS_INLINE
67 bool IsShadowMem(uptr mem) {
68   return mem >= kShadowBeg && mem <= kShadowEnd;
69 }
70
71 ALWAYS_INLINE
72 bool IsMetaMem(uptr mem) {
73   return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd;
74 }
75
76 ALWAYS_INLINE
77 uptr MemToShadow(uptr x) {
78   DCHECK(IsAppMem(x));
79   return (((x) & ~(kAppMemMsk | (kShadowCell - 1)))
80       ^ kAppMemXor) * kShadowCnt;
81 }
82
83 ALWAYS_INLINE
84 u32 *MemToMeta(uptr x) {
85   DCHECK(IsAppMem(x));
86   return (u32*)(((((x) & ~(kAppMemMsk | (kMetaShadowCell - 1)))
87       ^ kAppMemXor) / kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg);
88 }
89
90 ALWAYS_INLINE
91 uptr ShadowToMem(uptr s) {
92   CHECK(IsShadowMem(s));
93   if (s >= MemToShadow(kLoAppMemBeg) && s <= MemToShadow(kLoAppMemEnd - 1))
94     return (s / kShadowCnt) ^ kAppMemXor;
95   else
96     return ((s / kShadowCnt) ^ kAppMemXor) | kAppMemMsk;
97 }
98
99 static USED uptr UserRegions[] = {
100   kLoAppMemBeg, kLoAppMemEnd,
101   kHiAppMemBeg, kHiAppMemEnd,
102   kHeapMemBeg,  kHeapMemEnd,
103 };
104
105 #elif defined(SANITIZER_GO) && !SANITIZER_WINDOWS
106
107 /* Go on linux, darwin and freebsd
108 0000 0000 1000 - 0000 1000 0000: executable
109 0000 1000 0000 - 00c0 0000 0000: -
110 00c0 0000 0000 - 00e0 0000 0000: heap
111 00e0 0000 0000 - 2000 0000 0000: -
112 2000 0000 0000 - 2380 0000 0000: shadow
113 2380 0000 0000 - 3000 0000 0000: -
114 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
115 4000 0000 0000 - 6000 0000 0000: -
116 6000 0000 0000 - 6200 0000 0000: traces
117 6200 0000 0000 - 8000 0000 0000: -
118 */
119
120 const uptr kMetaShadowBeg = 0x300000000000ull;
121 const uptr kMetaShadowEnd = 0x400000000000ull;
122 const uptr kTraceMemBeg   = 0x600000000000ull;
123 const uptr kTraceMemEnd   = 0x620000000000ull;
124 const uptr kShadowBeg     = 0x200000000000ull;
125 const uptr kShadowEnd     = 0x238000000000ull;
126 const uptr kAppMemBeg     = 0x000000001000ull;
127 const uptr kAppMemEnd     = 0x00e000000000ull;
128
129 ALWAYS_INLINE
130 bool IsAppMem(uptr mem) {
131   return mem >= kAppMemBeg && mem < kAppMemEnd;
132 }
133
134 ALWAYS_INLINE
135 bool IsShadowMem(uptr mem) {
136   return mem >= kShadowBeg && mem <= kShadowEnd;
137 }
138
139 ALWAYS_INLINE
140 bool IsMetaMem(uptr mem) {
141   return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd;
142 }
143
144 ALWAYS_INLINE
145 uptr MemToShadow(uptr x) {
146   DCHECK(IsAppMem(x));
147   return ((x & ~(kShadowCell - 1)) * kShadowCnt) | kShadowBeg;
148 }
149
150 ALWAYS_INLINE
151 u32 *MemToMeta(uptr x) {
152   DCHECK(IsAppMem(x));
153   return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
154       kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg);
155 }
156
157 ALWAYS_INLINE
158 uptr ShadowToMem(uptr s) {
159   CHECK(IsShadowMem(s));
160   return (s & ~kShadowBeg) / kShadowCnt;
161 }
162
163 static USED uptr UserRegions[] = {
164   kAppMemBeg, kAppMemEnd,
165 };
166
167 #elif defined(SANITIZER_GO) && SANITIZER_WINDOWS
168
169 /* Go on windows
170 0000 0000 1000 - 0000 1000 0000: executable
171 0000 1000 0000 - 00f8 0000 0000: -
172 00c0 0000 0000 - 00e0 0000 0000: heap
173 00e0 0000 0000 - 0100 0000 0000: -
174 0100 0000 0000 - 0380 0000 0000: shadow
175 0380 0000 0000 - 0560 0000 0000: -
176 0560 0000 0000 - 0760 0000 0000: traces
177 0760 0000 0000 - 07d0 0000 0000: metainfo (memory blocks and sync objects)
178 07d0 0000 0000 - 8000 0000 0000: -
179 */
180
181 const uptr kMetaShadowBeg = 0x076000000000ull;
182 const uptr kMetaShadowEnd = 0x07d000000000ull;
183 const uptr kTraceMemBeg   = 0x056000000000ull;
184 const uptr kTraceMemEnd   = 0x076000000000ull;
185 const uptr kShadowBeg     = 0x010000000000ull;
186 const uptr kShadowEnd     = 0x038000000000ull;
187 const uptr kAppMemBeg     = 0x000000001000ull;
188 const uptr kAppMemEnd     = 0x00e000000000ull;
189
190 ALWAYS_INLINE
191 bool IsAppMem(uptr mem) {
192   return mem >= kAppMemBeg && mem < kAppMemEnd;
193 }
194
195 ALWAYS_INLINE
196 bool IsShadowMem(uptr mem) {
197   return mem >= kShadowBeg && mem <= kShadowEnd;
198 }
199
200 ALWAYS_INLINE
201 bool IsMetaMem(uptr mem) {
202   return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd;
203 }
204
205 ALWAYS_INLINE
206 uptr MemToShadow(uptr x) {
207   DCHECK(IsAppMem(x));
208   return ((x & ~(kShadowCell - 1)) * kShadowCnt) | kShadowBeg;
209 }
210
211 ALWAYS_INLINE
212 u32 *MemToMeta(uptr x) {
213   DCHECK(IsAppMem(x));
214   return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
215       kMetaShadowCell * kMetaShadowSize) | kMetaShadowEnd);
216 }
217
218 ALWAYS_INLINE
219 uptr ShadowToMem(uptr s) {
220   CHECK(IsShadowMem(s));
221   // FIXME(dvyukov): this is most likely wrong as the mapping is not bijection.
222   return (x & ~kShadowBeg) / kShadowCnt;
223 }
224
225 static USED uptr UserRegions[] = {
226   kAppMemBeg, kAppMemEnd,
227 };
228
229 #else
230 # error "Unknown platform"
231 #endif
232
233 // The additional page is to catch shadow stack overflow as paging fault.
234 // Windows wants 64K alignment for mmaps.
235 const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
236     + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
237
238 uptr ALWAYS_INLINE GetThreadTrace(int tid) {
239   uptr p = kTraceMemBeg + (uptr)tid * kTotalTraceSize;
240   DCHECK_LT(p, kTraceMemEnd);
241   return p;
242 }
243
244 uptr ALWAYS_INLINE GetThreadTraceHeader(int tid) {
245   uptr p = kTraceMemBeg + (uptr)tid * kTotalTraceSize
246       + kTraceSize * sizeof(Event);
247   DCHECK_LT(p, kTraceMemEnd);
248   return p;
249 }
250
251 void InitializePlatform();
252 void FlushShadowMemory();
253 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive);
254
255 // Says whether the addr relates to a global var.
256 // Guesses with high probability, may yield both false positives and negatives.
257 bool IsGlobalVar(uptr addr);
258 int ExtractResolvFDs(void *state, int *fds, int nfd);
259 int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
260
261 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
262     void *abstime), void *c, void *m, void *abstime,
263     void(*cleanup)(void *arg), void *arg);
264
265 }  // namespace __tsan
266
267 #endif  // TSAN_PLATFORM_H