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