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