]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/tsan/rtl/tsan_platform.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[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 !SANITIZER_GO
28
29 #if defined(__x86_64__)
30 /*
31 C/C++ on linux/x86_64 and freebsd/x86_64
32 0000 0000 1000 - 0080 0000 0000: main binary and/or MAP_32BIT mappings (512GB)
33 0040 0000 0000 - 0100 0000 0000: -
34 0100 0000 0000 - 2000 0000 0000: shadow
35 2000 0000 0000 - 3000 0000 0000: -
36 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
37 4000 0000 0000 - 5500 0000 0000: -
38 5500 0000 0000 - 5680 0000 0000: pie binaries without ASLR or on 4.1+ kernels
39 5680 0000 0000 - 6000 0000 0000: -
40 6000 0000 0000 - 6200 0000 0000: traces
41 6200 0000 0000 - 7d00 0000 0000: -
42 7b00 0000 0000 - 7c00 0000 0000: heap
43 7c00 0000 0000 - 7e80 0000 0000: -
44 7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
45 */
46 struct Mapping {
47   static const uptr kMetaShadowBeg = 0x300000000000ull;
48   static const uptr kMetaShadowEnd = 0x340000000000ull;
49   static const uptr kTraceMemBeg   = 0x600000000000ull;
50   static const uptr kTraceMemEnd   = 0x620000000000ull;
51   static const uptr kShadowBeg     = 0x010000000000ull;
52   static const uptr kShadowEnd     = 0x200000000000ull;
53   static const uptr kHeapMemBeg    = 0x7b0000000000ull;
54   static const uptr kHeapMemEnd    = 0x7c0000000000ull;
55   static const uptr kLoAppMemBeg   = 0x000000001000ull;
56   static const uptr kLoAppMemEnd   = 0x008000000000ull;
57   static const uptr kMidAppMemBeg  = 0x550000000000ull;
58   static const uptr kMidAppMemEnd  = 0x568000000000ull;
59   static const uptr kHiAppMemBeg   = 0x7e8000000000ull;
60   static const uptr kHiAppMemEnd   = 0x800000000000ull;
61   static const uptr kAppMemMsk     = 0x780000000000ull;
62   static const uptr kAppMemXor     = 0x040000000000ull;
63   static const uptr kVdsoBeg       = 0xf000000000000000ull;
64 };
65
66 #define TSAN_MID_APP_RANGE 1
67 #elif defined(__mips64)
68 /*
69 C/C++ on linux/mips64
70 0100 0000 00 - 0200 0000 00: main binary
71 0200 0000 00 - 1400 0000 00: -
72 1400 0000 00 - 2400 0000 00: shadow
73 2400 0000 00 - 3000 0000 00: -
74 3000 0000 00 - 4000 0000 00: metainfo (memory blocks and sync objects)
75 4000 0000 00 - 6000 0000 00: -
76 6000 0000 00 - 6200 0000 00: traces
77 6200 0000 00 - fe00 0000 00: -
78 fe00 0000 00 - ff00 0000 00: heap
79 ff00 0000 00 - ff80 0000 00: -
80 ff80 0000 00 - ffff ffff ff: modules and main thread stack
81 */
82 struct Mapping {
83   static const uptr kMetaShadowBeg = 0x4000000000ull;
84   static const uptr kMetaShadowEnd = 0x5000000000ull;
85   static const uptr kTraceMemBeg   = 0xb000000000ull;
86   static const uptr kTraceMemEnd   = 0xb200000000ull;
87   static const uptr kShadowBeg     = 0x2400000000ull;
88   static const uptr kShadowEnd     = 0x4000000000ull;
89   static const uptr kHeapMemBeg    = 0xfe00000000ull;
90   static const uptr kHeapMemEnd    = 0xff00000000ull;
91   static const uptr kLoAppMemBeg   = 0x0100000000ull;
92   static const uptr kLoAppMemEnd   = 0x0200000000ull;
93   static const uptr kMidAppMemBeg  = 0xaa00000000ull;
94   static const uptr kMidAppMemEnd  = 0xab00000000ull;
95   static const uptr kHiAppMemBeg   = 0xff80000000ull;
96   static const uptr kHiAppMemEnd   = 0xffffffffffull;
97   static const uptr kAppMemMsk     = 0xf800000000ull;
98   static const uptr kAppMemXor     = 0x0800000000ull;
99   static const uptr kVdsoBeg       = 0xfffff00000ull;
100 };
101
102 #define TSAN_MID_APP_RANGE 1
103 #elif defined(__aarch64__)
104 // AArch64 supports multiple VMA which leads to multiple address transformation
105 // functions.  To support these multiple VMAS transformations and mappings TSAN
106 // runtime for AArch64 uses an external memory read (vmaSize) to select which
107 // mapping to use.  Although slower, it make a same instrumented binary run on
108 // multiple kernels.
109
110 /*
111 C/C++ on linux/aarch64 (39-bit VMA)
112 0000 0010 00 - 0100 0000 00: main binary
113 0100 0000 00 - 0800 0000 00: -
114 0800 0000 00 - 2000 0000 00: shadow memory
115 2000 0000 00 - 3100 0000 00: -
116 3100 0000 00 - 3400 0000 00: metainfo
117 3400 0000 00 - 5500 0000 00: -
118 5500 0000 00 - 5600 0000 00: main binary (PIE)
119 5600 0000 00 - 6000 0000 00: -
120 6000 0000 00 - 6200 0000 00: traces
121 6200 0000 00 - 7d00 0000 00: -
122 7c00 0000 00 - 7d00 0000 00: heap
123 7d00 0000 00 - 7fff ffff ff: modules and main thread stack
124 */
125 struct Mapping39 {
126   static const uptr kLoAppMemBeg   = 0x0000001000ull;
127   static const uptr kLoAppMemEnd   = 0x0100000000ull;
128   static const uptr kShadowBeg     = 0x0800000000ull;
129   static const uptr kShadowEnd     = 0x2000000000ull;
130   static const uptr kMetaShadowBeg = 0x3100000000ull;
131   static const uptr kMetaShadowEnd = 0x3400000000ull;
132   static const uptr kMidAppMemBeg  = 0x5500000000ull;
133   static const uptr kMidAppMemEnd  = 0x5600000000ull;
134   static const uptr kTraceMemBeg   = 0x6000000000ull;
135   static const uptr kTraceMemEnd   = 0x6200000000ull;
136   static const uptr kHeapMemBeg    = 0x7c00000000ull;
137   static const uptr kHeapMemEnd    = 0x7d00000000ull;
138   static const uptr kHiAppMemBeg   = 0x7e00000000ull;
139   static const uptr kHiAppMemEnd   = 0x7fffffffffull;
140   static const uptr kAppMemMsk     = 0x7800000000ull;
141   static const uptr kAppMemXor     = 0x0200000000ull;
142   static const uptr kVdsoBeg       = 0x7f00000000ull;
143 };
144
145 /*
146 C/C++ on linux/aarch64 (42-bit VMA)
147 00000 0010 00 - 01000 0000 00: main binary
148 01000 0000 00 - 10000 0000 00: -
149 10000 0000 00 - 20000 0000 00: shadow memory
150 20000 0000 00 - 26000 0000 00: -
151 26000 0000 00 - 28000 0000 00: metainfo
152 28000 0000 00 - 2aa00 0000 00: -
153 2aa00 0000 00 - 2ab00 0000 00: main binary (PIE)
154 2ab00 0000 00 - 36200 0000 00: -
155 36200 0000 00 - 36240 0000 00: traces
156 36240 0000 00 - 3e000 0000 00: -
157 3e000 0000 00 - 3f000 0000 00: heap
158 3f000 0000 00 - 3ffff ffff ff: modules and main thread stack
159 */
160 struct Mapping42 {
161   static const uptr kLoAppMemBeg   = 0x00000001000ull;
162   static const uptr kLoAppMemEnd   = 0x01000000000ull;
163   static const uptr kShadowBeg     = 0x10000000000ull;
164   static const uptr kShadowEnd     = 0x20000000000ull;
165   static const uptr kMetaShadowBeg = 0x26000000000ull;
166   static const uptr kMetaShadowEnd = 0x28000000000ull;
167   static const uptr kMidAppMemBeg  = 0x2aa00000000ull;
168   static const uptr kMidAppMemEnd  = 0x2ab00000000ull;
169   static const uptr kTraceMemBeg   = 0x36200000000ull;
170   static const uptr kTraceMemEnd   = 0x36400000000ull;
171   static const uptr kHeapMemBeg    = 0x3e000000000ull;
172   static const uptr kHeapMemEnd    = 0x3f000000000ull;
173   static const uptr kHiAppMemBeg   = 0x3f000000000ull;
174   static const uptr kHiAppMemEnd   = 0x3ffffffffffull;
175   static const uptr kAppMemMsk     = 0x3c000000000ull;
176   static const uptr kAppMemXor     = 0x04000000000ull;
177   static const uptr kVdsoBeg       = 0x37f00000000ull;
178 };
179
180 struct Mapping48 {
181   static const uptr kLoAppMemBeg   = 0x0000000001000ull;
182   static const uptr kLoAppMemEnd   = 0x0000200000000ull;
183   static const uptr kShadowBeg     = 0x0002000000000ull;
184   static const uptr kShadowEnd     = 0x0004000000000ull;
185   static const uptr kMetaShadowBeg = 0x0005000000000ull;
186   static const uptr kMetaShadowEnd = 0x0006000000000ull;
187   static const uptr kMidAppMemBeg  = 0x0aaaa00000000ull;
188   static const uptr kMidAppMemEnd  = 0x0aaaf00000000ull;
189   static const uptr kTraceMemBeg   = 0x0f06000000000ull;
190   static const uptr kTraceMemEnd   = 0x0f06200000000ull;
191   static const uptr kHeapMemBeg    = 0x0ffff00000000ull;
192   static const uptr kHeapMemEnd    = 0x0ffff00000000ull;
193   static const uptr kHiAppMemBeg   = 0x0ffff00000000ull;
194   static const uptr kHiAppMemEnd   = 0x1000000000000ull;
195   static const uptr kAppMemMsk     = 0x0fff800000000ull;
196   static const uptr kAppMemXor     = 0x0000800000000ull;
197   static const uptr kVdsoBeg       = 0xffff000000000ull;
198 };
199
200 // Indicates the runtime will define the memory regions at runtime.
201 #define TSAN_RUNTIME_VMA 1
202 // Indicates that mapping defines a mid range memory segment.
203 #define TSAN_MID_APP_RANGE 1
204 #elif defined(__powerpc64__)
205 // PPC64 supports multiple VMA which leads to multiple address transformation
206 // functions.  To support these multiple VMAS transformations and mappings TSAN
207 // runtime for PPC64 uses an external memory read (vmaSize) to select which
208 // mapping to use.  Although slower, it make a same instrumented binary run on
209 // multiple kernels.
210
211 /*
212 C/C++ on linux/powerpc64 (44-bit VMA)
213 0000 0000 0100 - 0001 0000 0000: main binary
214 0001 0000 0000 - 0001 0000 0000: -
215 0001 0000 0000 - 0b00 0000 0000: shadow
216 0b00 0000 0000 - 0b00 0000 0000: -
217 0b00 0000 0000 - 0d00 0000 0000: metainfo (memory blocks and sync objects)
218 0d00 0000 0000 - 0d00 0000 0000: -
219 0d00 0000 0000 - 0f00 0000 0000: traces
220 0f00 0000 0000 - 0f00 0000 0000: -
221 0f00 0000 0000 - 0f50 0000 0000: heap
222 0f50 0000 0000 - 0f60 0000 0000: -
223 0f60 0000 0000 - 1000 0000 0000: modules and main thread stack
224 */
225 struct Mapping44 {
226   static const uptr kMetaShadowBeg = 0x0b0000000000ull;
227   static const uptr kMetaShadowEnd = 0x0d0000000000ull;
228   static const uptr kTraceMemBeg   = 0x0d0000000000ull;
229   static const uptr kTraceMemEnd   = 0x0f0000000000ull;
230   static const uptr kShadowBeg     = 0x000100000000ull;
231   static const uptr kShadowEnd     = 0x0b0000000000ull;
232   static const uptr kLoAppMemBeg   = 0x000000000100ull;
233   static const uptr kLoAppMemEnd   = 0x000100000000ull;
234   static const uptr kHeapMemBeg    = 0x0f0000000000ull;
235   static const uptr kHeapMemEnd    = 0x0f5000000000ull;
236   static const uptr kHiAppMemBeg   = 0x0f6000000000ull;
237   static const uptr kHiAppMemEnd   = 0x100000000000ull; // 44 bits
238   static const uptr kAppMemMsk     = 0x0f0000000000ull;
239   static const uptr kAppMemXor     = 0x002100000000ull;
240   static const uptr kVdsoBeg       = 0x3c0000000000000ull;
241 };
242
243 /*
244 C/C++ on linux/powerpc64 (46-bit VMA)
245 0000 0000 1000 - 0100 0000 0000: main binary
246 0100 0000 0000 - 0200 0000 0000: -
247 0100 0000 0000 - 1000 0000 0000: shadow
248 1000 0000 0000 - 1000 0000 0000: -
249 1000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
250 2000 0000 0000 - 2000 0000 0000: -
251 2000 0000 0000 - 2200 0000 0000: traces
252 2200 0000 0000 - 3d00 0000 0000: -
253 3d00 0000 0000 - 3e00 0000 0000: heap
254 3e00 0000 0000 - 3e80 0000 0000: -
255 3e80 0000 0000 - 4000 0000 0000: modules and main thread stack
256 */
257 struct Mapping46 {
258   static const uptr kMetaShadowBeg = 0x100000000000ull;
259   static const uptr kMetaShadowEnd = 0x200000000000ull;
260   static const uptr kTraceMemBeg   = 0x200000000000ull;
261   static const uptr kTraceMemEnd   = 0x220000000000ull;
262   static const uptr kShadowBeg     = 0x010000000000ull;
263   static const uptr kShadowEnd     = 0x100000000000ull;
264   static const uptr kHeapMemBeg    = 0x3d0000000000ull;
265   static const uptr kHeapMemEnd    = 0x3e0000000000ull;
266   static const uptr kLoAppMemBeg   = 0x000000001000ull;
267   static const uptr kLoAppMemEnd   = 0x010000000000ull;
268   static const uptr kHiAppMemBeg   = 0x3e8000000000ull;
269   static const uptr kHiAppMemEnd   = 0x400000000000ull; // 46 bits
270   static const uptr kAppMemMsk     = 0x3c0000000000ull;
271   static const uptr kAppMemXor     = 0x020000000000ull;
272   static const uptr kVdsoBeg       = 0x7800000000000000ull;
273 };
274
275 // Indicates the runtime will define the memory regions at runtime.
276 #define TSAN_RUNTIME_VMA 1
277 #endif
278
279 #elif SANITIZER_GO && !SANITIZER_WINDOWS
280
281 /* Go on linux, darwin and freebsd
282 0000 0000 1000 - 0000 1000 0000: executable
283 0000 1000 0000 - 00c0 0000 0000: -
284 00c0 0000 0000 - 00e0 0000 0000: heap
285 00e0 0000 0000 - 2000 0000 0000: -
286 2000 0000 0000 - 2380 0000 0000: shadow
287 2380 0000 0000 - 3000 0000 0000: -
288 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
289 4000 0000 0000 - 6000 0000 0000: -
290 6000 0000 0000 - 6200 0000 0000: traces
291 6200 0000 0000 - 8000 0000 0000: -
292 */
293
294 struct Mapping {
295   static const uptr kMetaShadowBeg = 0x300000000000ull;
296   static const uptr kMetaShadowEnd = 0x400000000000ull;
297   static const uptr kTraceMemBeg   = 0x600000000000ull;
298   static const uptr kTraceMemEnd   = 0x620000000000ull;
299   static const uptr kShadowBeg     = 0x200000000000ull;
300   static const uptr kShadowEnd     = 0x238000000000ull;
301   static const uptr kAppMemBeg     = 0x000000001000ull;
302   static const uptr kAppMemEnd     = 0x00e000000000ull;
303 };
304
305 #elif SANITIZER_GO && SANITIZER_WINDOWS
306
307 /* Go on windows
308 0000 0000 1000 - 0000 1000 0000: executable
309 0000 1000 0000 - 00f8 0000 0000: -
310 00c0 0000 0000 - 00e0 0000 0000: heap
311 00e0 0000 0000 - 0100 0000 0000: -
312 0100 0000 0000 - 0500 0000 0000: shadow
313 0500 0000 0000 - 0560 0000 0000: -
314 0560 0000 0000 - 0760 0000 0000: traces
315 0760 0000 0000 - 07d0 0000 0000: metainfo (memory blocks and sync objects)
316 07d0 0000 0000 - 8000 0000 0000: -
317 */
318
319 struct Mapping {
320   static const uptr kMetaShadowBeg = 0x076000000000ull;
321   static const uptr kMetaShadowEnd = 0x07d000000000ull;
322   static const uptr kTraceMemBeg   = 0x056000000000ull;
323   static const uptr kTraceMemEnd   = 0x076000000000ull;
324   static const uptr kShadowBeg     = 0x010000000000ull;
325   static const uptr kShadowEnd     = 0x050000000000ull;
326   static const uptr kAppMemBeg     = 0x000000001000ull;
327   static const uptr kAppMemEnd     = 0x00e000000000ull;
328 };
329
330 #else
331 # error "Unknown platform"
332 #endif
333
334
335 #ifdef TSAN_RUNTIME_VMA
336 extern uptr vmaSize;
337 #endif
338
339
340 enum MappingType {
341   MAPPING_LO_APP_BEG,
342   MAPPING_LO_APP_END,
343   MAPPING_HI_APP_BEG,
344   MAPPING_HI_APP_END,
345 #ifdef TSAN_MID_APP_RANGE
346   MAPPING_MID_APP_BEG,
347   MAPPING_MID_APP_END,
348 #endif
349   MAPPING_HEAP_BEG,
350   MAPPING_HEAP_END,
351   MAPPING_APP_BEG,
352   MAPPING_APP_END,
353   MAPPING_SHADOW_BEG,
354   MAPPING_SHADOW_END,
355   MAPPING_META_SHADOW_BEG,
356   MAPPING_META_SHADOW_END,
357   MAPPING_TRACE_BEG,
358   MAPPING_TRACE_END,
359   MAPPING_VDSO_BEG,
360 };
361
362 template<typename Mapping, int Type>
363 uptr MappingImpl(void) {
364   switch (Type) {
365 #if !SANITIZER_GO
366     case MAPPING_LO_APP_BEG: return Mapping::kLoAppMemBeg;
367     case MAPPING_LO_APP_END: return Mapping::kLoAppMemEnd;
368 # ifdef TSAN_MID_APP_RANGE
369     case MAPPING_MID_APP_BEG: return Mapping::kMidAppMemBeg;
370     case MAPPING_MID_APP_END: return Mapping::kMidAppMemEnd;
371 # endif
372     case MAPPING_HI_APP_BEG: return Mapping::kHiAppMemBeg;
373     case MAPPING_HI_APP_END: return Mapping::kHiAppMemEnd;
374     case MAPPING_HEAP_BEG: return Mapping::kHeapMemBeg;
375     case MAPPING_HEAP_END: return Mapping::kHeapMemEnd;
376     case MAPPING_VDSO_BEG: return Mapping::kVdsoBeg;
377 #else
378     case MAPPING_APP_BEG: return Mapping::kAppMemBeg;
379     case MAPPING_APP_END: return Mapping::kAppMemEnd;
380 #endif
381     case MAPPING_SHADOW_BEG: return Mapping::kShadowBeg;
382     case MAPPING_SHADOW_END: return Mapping::kShadowEnd;
383     case MAPPING_META_SHADOW_BEG: return Mapping::kMetaShadowBeg;
384     case MAPPING_META_SHADOW_END: return Mapping::kMetaShadowEnd;
385     case MAPPING_TRACE_BEG: return Mapping::kTraceMemBeg;
386     case MAPPING_TRACE_END: return Mapping::kTraceMemEnd;
387   }
388 }
389
390 template<int Type>
391 uptr MappingArchImpl(void) {
392 #ifdef __aarch64__
393   switch (vmaSize) {
394     case 39: return MappingImpl<Mapping39, Type>();
395     case 42: return MappingImpl<Mapping42, Type>();
396     case 48: return MappingImpl<Mapping48, Type>();
397   }
398   DCHECK(0);
399   return 0;
400 #elif defined(__powerpc64__)
401   if (vmaSize == 44)
402     return MappingImpl<Mapping44, Type>();
403   else
404     return MappingImpl<Mapping46, Type>();
405   DCHECK(0);
406 #else
407   return MappingImpl<Mapping, Type>();
408 #endif
409 }
410
411 #if !SANITIZER_GO
412 ALWAYS_INLINE
413 uptr LoAppMemBeg(void) {
414   return MappingArchImpl<MAPPING_LO_APP_BEG>();
415 }
416 ALWAYS_INLINE
417 uptr LoAppMemEnd(void) {
418   return MappingArchImpl<MAPPING_LO_APP_END>();
419 }
420
421 #ifdef TSAN_MID_APP_RANGE
422 ALWAYS_INLINE
423 uptr MidAppMemBeg(void) {
424   return MappingArchImpl<MAPPING_MID_APP_BEG>();
425 }
426 ALWAYS_INLINE
427 uptr MidAppMemEnd(void) {
428   return MappingArchImpl<MAPPING_MID_APP_END>();
429 }
430 #endif
431
432 ALWAYS_INLINE
433 uptr HeapMemBeg(void) {
434   return MappingArchImpl<MAPPING_HEAP_BEG>();
435 }
436 ALWAYS_INLINE
437 uptr HeapMemEnd(void) {
438   return MappingArchImpl<MAPPING_HEAP_END>();
439 }
440
441 ALWAYS_INLINE
442 uptr HiAppMemBeg(void) {
443   return MappingArchImpl<MAPPING_HI_APP_BEG>();
444 }
445 ALWAYS_INLINE
446 uptr HiAppMemEnd(void) {
447   return MappingArchImpl<MAPPING_HI_APP_END>();
448 }
449
450 ALWAYS_INLINE
451 uptr VdsoBeg(void) {
452   return MappingArchImpl<MAPPING_VDSO_BEG>();
453 }
454
455 #else
456
457 ALWAYS_INLINE
458 uptr AppMemBeg(void) {
459   return MappingArchImpl<MAPPING_APP_BEG>();
460 }
461 ALWAYS_INLINE
462 uptr AppMemEnd(void) {
463   return MappingArchImpl<MAPPING_APP_END>();
464 }
465
466 #endif
467
468 static inline
469 bool GetUserRegion(int i, uptr *start, uptr *end) {
470   switch (i) {
471   default:
472     return false;
473 #if !SANITIZER_GO
474   case 0:
475     *start = LoAppMemBeg();
476     *end = LoAppMemEnd();
477     return true;
478   case 1:
479     *start = HiAppMemBeg();
480     *end = HiAppMemEnd();
481     return true;
482   case 2:
483     *start = HeapMemBeg();
484     *end = HeapMemEnd();
485     return true;
486 # ifdef TSAN_MID_APP_RANGE
487   case 3:
488     *start = MidAppMemBeg();
489     *end = MidAppMemEnd();
490     return true;
491 # endif
492 #else
493   case 0:
494     *start = AppMemBeg();
495     *end = AppMemEnd();
496     return true;
497 #endif
498   }
499 }
500
501 ALWAYS_INLINE
502 uptr ShadowBeg(void) {
503   return MappingArchImpl<MAPPING_SHADOW_BEG>();
504 }
505 ALWAYS_INLINE
506 uptr ShadowEnd(void) {
507   return MappingArchImpl<MAPPING_SHADOW_END>();
508 }
509
510 ALWAYS_INLINE
511 uptr MetaShadowBeg(void) {
512   return MappingArchImpl<MAPPING_META_SHADOW_BEG>();
513 }
514 ALWAYS_INLINE
515 uptr MetaShadowEnd(void) {
516   return MappingArchImpl<MAPPING_META_SHADOW_END>();
517 }
518
519 ALWAYS_INLINE
520 uptr TraceMemBeg(void) {
521   return MappingArchImpl<MAPPING_TRACE_BEG>();
522 }
523 ALWAYS_INLINE
524 uptr TraceMemEnd(void) {
525   return MappingArchImpl<MAPPING_TRACE_END>();
526 }
527
528
529 template<typename Mapping>
530 bool IsAppMemImpl(uptr mem) {
531 #if !SANITIZER_GO
532   return (mem >= Mapping::kHeapMemBeg && mem < Mapping::kHeapMemEnd) ||
533 # ifdef TSAN_MID_APP_RANGE
534          (mem >= Mapping::kMidAppMemBeg && mem < Mapping::kMidAppMemEnd) ||
535 # endif
536          (mem >= Mapping::kLoAppMemBeg && mem < Mapping::kLoAppMemEnd) ||
537          (mem >= Mapping::kHiAppMemBeg && mem < Mapping::kHiAppMemEnd);
538 #else
539   return mem >= Mapping::kAppMemBeg && mem < Mapping::kAppMemEnd;
540 #endif
541 }
542
543 ALWAYS_INLINE
544 bool IsAppMem(uptr mem) {
545 #ifdef __aarch64__
546   switch (vmaSize) {
547     case 39: return IsAppMemImpl<Mapping39>(mem);
548     case 42: return IsAppMemImpl<Mapping42>(mem);
549     case 48: return IsAppMemImpl<Mapping48>(mem);
550   }
551   DCHECK(0);
552   return false;
553 #elif defined(__powerpc64__)
554   if (vmaSize == 44)
555     return IsAppMemImpl<Mapping44>(mem);
556   else
557     return IsAppMemImpl<Mapping46>(mem);
558   DCHECK(0);
559 #else
560   return IsAppMemImpl<Mapping>(mem);
561 #endif
562 }
563
564
565 template<typename Mapping>
566 bool IsShadowMemImpl(uptr mem) {
567   return mem >= Mapping::kShadowBeg && mem <= Mapping::kShadowEnd;
568 }
569
570 ALWAYS_INLINE
571 bool IsShadowMem(uptr mem) {
572 #ifdef __aarch64__
573   switch (vmaSize) {
574     case 39: return IsShadowMemImpl<Mapping39>(mem);
575     case 42: return IsShadowMemImpl<Mapping42>(mem);
576     case 48: return IsShadowMemImpl<Mapping48>(mem);
577   }
578   DCHECK(0);
579   return false;
580 #elif defined(__powerpc64__)
581   if (vmaSize == 44)
582     return IsShadowMemImpl<Mapping44>(mem);
583   else
584     return IsShadowMemImpl<Mapping46>(mem);
585   DCHECK(0);
586 #else
587   return IsShadowMemImpl<Mapping>(mem);
588 #endif
589 }
590
591
592 template<typename Mapping>
593 bool IsMetaMemImpl(uptr mem) {
594   return mem >= Mapping::kMetaShadowBeg && mem <= Mapping::kMetaShadowEnd;
595 }
596
597 ALWAYS_INLINE
598 bool IsMetaMem(uptr mem) {
599 #ifdef __aarch64__
600   switch (vmaSize) {
601     case 39: return IsMetaMemImpl<Mapping39>(mem);
602     case 42: return IsMetaMemImpl<Mapping42>(mem);
603     case 48: return IsMetaMemImpl<Mapping48>(mem);
604   }
605   DCHECK(0);
606   return false;
607 #elif defined(__powerpc64__)
608   if (vmaSize == 44)
609     return IsMetaMemImpl<Mapping44>(mem);
610   else
611     return IsMetaMemImpl<Mapping46>(mem);
612   DCHECK(0);
613 #else
614   return IsMetaMemImpl<Mapping>(mem);
615 #endif
616 }
617
618
619 template<typename Mapping>
620 uptr MemToShadowImpl(uptr x) {
621   DCHECK(IsAppMem(x));
622 #if !SANITIZER_GO
623   return (((x) & ~(Mapping::kAppMemMsk | (kShadowCell - 1)))
624       ^ Mapping::kAppMemXor) * kShadowCnt;
625 #else
626 # ifndef SANITIZER_WINDOWS
627   return ((x & ~(kShadowCell - 1)) * kShadowCnt) | Mapping::kShadowBeg;
628 # else
629   return ((x & ~(kShadowCell - 1)) * kShadowCnt) + Mapping::kShadowBeg;
630 # endif
631 #endif
632 }
633
634 ALWAYS_INLINE
635 uptr MemToShadow(uptr x) {
636 #ifdef __aarch64__
637   switch (vmaSize) {
638     case 39: return MemToShadowImpl<Mapping39>(x);
639     case 42: return MemToShadowImpl<Mapping42>(x);
640     case 48: return MemToShadowImpl<Mapping48>(x);
641   }
642   DCHECK(0);
643   return 0;
644 #elif defined(__powerpc64__)
645   if (vmaSize == 44)
646     return MemToShadowImpl<Mapping44>(x);
647   else
648     return MemToShadowImpl<Mapping46>(x);
649   DCHECK(0);
650 #else
651   return MemToShadowImpl<Mapping>(x);
652 #endif
653 }
654
655
656 template<typename Mapping>
657 u32 *MemToMetaImpl(uptr x) {
658   DCHECK(IsAppMem(x));
659 #if !SANITIZER_GO
660   return (u32*)(((((x) & ~(Mapping::kAppMemMsk | (kMetaShadowCell - 1)))) /
661       kMetaShadowCell * kMetaShadowSize) | Mapping::kMetaShadowBeg);
662 #else
663 # ifndef SANITIZER_WINDOWS
664   return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
665       kMetaShadowCell * kMetaShadowSize) | Mapping::kMetaShadowBeg);
666 # else
667   return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
668       kMetaShadowCell * kMetaShadowSize) + Mapping::kMetaShadowBeg);
669 # endif
670 #endif
671 }
672
673 ALWAYS_INLINE
674 u32 *MemToMeta(uptr x) {
675 #ifdef __aarch64__
676   switch (vmaSize) {
677     case 39: return MemToMetaImpl<Mapping39>(x);
678     case 42: return MemToMetaImpl<Mapping42>(x);
679     case 48: return MemToMetaImpl<Mapping48>(x);
680   }
681   DCHECK(0);
682   return 0;
683 #elif defined(__powerpc64__)
684   if (vmaSize == 44)
685     return MemToMetaImpl<Mapping44>(x);
686   else
687     return MemToMetaImpl<Mapping46>(x);
688   DCHECK(0);
689 #else
690   return MemToMetaImpl<Mapping>(x);
691 #endif
692 }
693
694
695 template<typename Mapping>
696 uptr ShadowToMemImpl(uptr s) {
697   DCHECK(IsShadowMem(s));
698 #if !SANITIZER_GO
699   // The shadow mapping is non-linear and we've lost some bits, so we don't have
700   // an easy way to restore the original app address. But the mapping is a
701   // bijection, so we try to restore the address as belonging to low/mid/high
702   // range consecutively and see if shadow->app->shadow mapping gives us the
703   // same address.
704   uptr p = (s / kShadowCnt) ^ Mapping::kAppMemXor;
705   if (p >= Mapping::kLoAppMemBeg && p < Mapping::kLoAppMemEnd &&
706       MemToShadow(p) == s)
707     return p;
708 # ifdef TSAN_MID_APP_RANGE
709   p = ((s / kShadowCnt) ^ Mapping::kAppMemXor) +
710       (Mapping::kMidAppMemBeg & Mapping::kAppMemMsk);
711   if (p >= Mapping::kMidAppMemBeg && p < Mapping::kMidAppMemEnd &&
712       MemToShadow(p) == s)
713     return p;
714 # endif
715   return ((s / kShadowCnt) ^ Mapping::kAppMemXor) | Mapping::kAppMemMsk;
716 #else  // #if !SANITIZER_GO
717 # ifndef SANITIZER_WINDOWS
718   return (s & ~Mapping::kShadowBeg) / kShadowCnt;
719 # else
720   return (s - Mapping::kShadowBeg) / kShadowCnt;
721 # endif // SANITIZER_WINDOWS
722 #endif
723 }
724
725 ALWAYS_INLINE
726 uptr ShadowToMem(uptr s) {
727 #ifdef __aarch64__
728   switch (vmaSize) {
729     case 39: return ShadowToMemImpl<Mapping39>(s);
730     case 42: return ShadowToMemImpl<Mapping42>(s);
731     case 48: return ShadowToMemImpl<Mapping48>(s);
732   }
733   DCHECK(0);
734   return 0;
735 #elif defined(__powerpc64__)
736   if (vmaSize == 44)
737     return ShadowToMemImpl<Mapping44>(s);
738   else
739     return ShadowToMemImpl<Mapping46>(s);
740   DCHECK(0);
741 #else
742   return ShadowToMemImpl<Mapping>(s);
743 #endif
744 }
745
746
747
748 // The additional page is to catch shadow stack overflow as paging fault.
749 // Windows wants 64K alignment for mmaps.
750 const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
751     + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
752
753 template<typename Mapping>
754 uptr GetThreadTraceImpl(int tid) {
755   uptr p = Mapping::kTraceMemBeg + (uptr)tid * kTotalTraceSize;
756   DCHECK_LT(p, Mapping::kTraceMemEnd);
757   return p;
758 }
759
760 ALWAYS_INLINE
761 uptr GetThreadTrace(int tid) {
762 #ifdef __aarch64__
763   switch (vmaSize) {
764     case 39: return GetThreadTraceImpl<Mapping39>(tid);
765     case 42: return GetThreadTraceImpl<Mapping42>(tid);
766     case 48: return GetThreadTraceImpl<Mapping48>(tid);
767   }
768   DCHECK(0);
769   return 0;
770 #elif defined(__powerpc64__)
771   if (vmaSize == 44)
772     return GetThreadTraceImpl<Mapping44>(tid);
773   else
774     return GetThreadTraceImpl<Mapping46>(tid);
775   DCHECK(0);
776 #else
777   return GetThreadTraceImpl<Mapping>(tid);
778 #endif
779 }
780
781
782 template<typename Mapping>
783 uptr GetThreadTraceHeaderImpl(int tid) {
784   uptr p = Mapping::kTraceMemBeg + (uptr)tid * kTotalTraceSize
785       + kTraceSize * sizeof(Event);
786   DCHECK_LT(p, Mapping::kTraceMemEnd);
787   return p;
788 }
789
790 ALWAYS_INLINE
791 uptr GetThreadTraceHeader(int tid) {
792 #ifdef __aarch64__
793   switch (vmaSize) {
794     case 39: return GetThreadTraceHeaderImpl<Mapping39>(tid);
795     case 42: return GetThreadTraceHeaderImpl<Mapping42>(tid);
796     case 48: return GetThreadTraceHeaderImpl<Mapping48>(tid);
797   }
798   DCHECK(0);
799   return 0;
800 #elif defined(__powerpc64__)
801   if (vmaSize == 44)
802     return GetThreadTraceHeaderImpl<Mapping44>(tid);
803   else
804     return GetThreadTraceHeaderImpl<Mapping46>(tid);
805   DCHECK(0);
806 #else
807   return GetThreadTraceHeaderImpl<Mapping>(tid);
808 #endif
809 }
810
811 void InitializePlatform();
812 void InitializePlatformEarly();
813 void CheckAndProtect();
814 void InitializeShadowMemoryPlatform();
815 void FlushShadowMemory();
816 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive);
817 int ExtractResolvFDs(void *state, int *fds, int nfd);
818 int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
819 void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size);
820
821 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
822     void *abstime), void *c, void *m, void *abstime,
823     void(*cleanup)(void *arg), void *arg);
824
825 void DestroyThreadState();
826
827 }  // namespace __tsan
828
829 #endif  // TSAN_PLATFORM_H