1 //===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 // Platform-specific code.
13 //===----------------------------------------------------------------------===//
15 #ifndef TSAN_PLATFORM_H
16 #define TSAN_PLATFORM_H
18 #if !defined(__LP64__) && !defined(_WIN64)
19 # error "Only 64-bit is supported"
22 #include "tsan_defs.h"
23 #include "tsan_trace.h"
29 #if defined(__x86_64__)
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
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;
66 #define TSAN_MID_APP_RANGE 1
67 #elif defined(__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
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;
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
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
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;
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
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;
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;
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
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
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;
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
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;
275 // Indicates the runtime will define the memory regions at runtime.
276 #define TSAN_RUNTIME_VMA 1
279 #elif SANITIZER_GO && !SANITIZER_WINDOWS
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: -
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;
305 #elif SANITIZER_GO && SANITIZER_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: -
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;
331 # error "Unknown platform"
335 #ifdef TSAN_RUNTIME_VMA
345 #ifdef TSAN_MID_APP_RANGE
355 MAPPING_META_SHADOW_BEG,
356 MAPPING_META_SHADOW_END,
362 template<typename Mapping, int Type>
363 uptr MappingImpl(void) {
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;
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;
378 case MAPPING_APP_BEG: return Mapping::kAppMemBeg;
379 case MAPPING_APP_END: return Mapping::kAppMemEnd;
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;
391 uptr MappingArchImpl(void) {
394 case 39: return MappingImpl<Mapping39, Type>();
395 case 42: return MappingImpl<Mapping42, Type>();
396 case 48: return MappingImpl<Mapping48, Type>();
400 #elif defined(__powerpc64__)
402 return MappingImpl<Mapping44, Type>();
404 return MappingImpl<Mapping46, Type>();
407 return MappingImpl<Mapping, Type>();
413 uptr LoAppMemBeg(void) {
414 return MappingArchImpl<MAPPING_LO_APP_BEG>();
417 uptr LoAppMemEnd(void) {
418 return MappingArchImpl<MAPPING_LO_APP_END>();
421 #ifdef TSAN_MID_APP_RANGE
423 uptr MidAppMemBeg(void) {
424 return MappingArchImpl<MAPPING_MID_APP_BEG>();
427 uptr MidAppMemEnd(void) {
428 return MappingArchImpl<MAPPING_MID_APP_END>();
433 uptr HeapMemBeg(void) {
434 return MappingArchImpl<MAPPING_HEAP_BEG>();
437 uptr HeapMemEnd(void) {
438 return MappingArchImpl<MAPPING_HEAP_END>();
442 uptr HiAppMemBeg(void) {
443 return MappingArchImpl<MAPPING_HI_APP_BEG>();
446 uptr HiAppMemEnd(void) {
447 return MappingArchImpl<MAPPING_HI_APP_END>();
452 return MappingArchImpl<MAPPING_VDSO_BEG>();
458 uptr AppMemBeg(void) {
459 return MappingArchImpl<MAPPING_APP_BEG>();
462 uptr AppMemEnd(void) {
463 return MappingArchImpl<MAPPING_APP_END>();
469 bool GetUserRegion(int i, uptr *start, uptr *end) {
475 *start = LoAppMemBeg();
476 *end = LoAppMemEnd();
479 *start = HiAppMemBeg();
480 *end = HiAppMemEnd();
483 *start = HeapMemBeg();
486 # ifdef TSAN_MID_APP_RANGE
488 *start = MidAppMemBeg();
489 *end = MidAppMemEnd();
494 *start = AppMemBeg();
502 uptr ShadowBeg(void) {
503 return MappingArchImpl<MAPPING_SHADOW_BEG>();
506 uptr ShadowEnd(void) {
507 return MappingArchImpl<MAPPING_SHADOW_END>();
511 uptr MetaShadowBeg(void) {
512 return MappingArchImpl<MAPPING_META_SHADOW_BEG>();
515 uptr MetaShadowEnd(void) {
516 return MappingArchImpl<MAPPING_META_SHADOW_END>();
520 uptr TraceMemBeg(void) {
521 return MappingArchImpl<MAPPING_TRACE_BEG>();
524 uptr TraceMemEnd(void) {
525 return MappingArchImpl<MAPPING_TRACE_END>();
529 template<typename Mapping>
530 bool IsAppMemImpl(uptr mem) {
532 return (mem >= Mapping::kHeapMemBeg && mem < Mapping::kHeapMemEnd) ||
533 # ifdef TSAN_MID_APP_RANGE
534 (mem >= Mapping::kMidAppMemBeg && mem < Mapping::kMidAppMemEnd) ||
536 (mem >= Mapping::kLoAppMemBeg && mem < Mapping::kLoAppMemEnd) ||
537 (mem >= Mapping::kHiAppMemBeg && mem < Mapping::kHiAppMemEnd);
539 return mem >= Mapping::kAppMemBeg && mem < Mapping::kAppMemEnd;
544 bool IsAppMem(uptr mem) {
547 case 39: return IsAppMemImpl<Mapping39>(mem);
548 case 42: return IsAppMemImpl<Mapping42>(mem);
549 case 48: return IsAppMemImpl<Mapping48>(mem);
553 #elif defined(__powerpc64__)
555 return IsAppMemImpl<Mapping44>(mem);
557 return IsAppMemImpl<Mapping46>(mem);
560 return IsAppMemImpl<Mapping>(mem);
565 template<typename Mapping>
566 bool IsShadowMemImpl(uptr mem) {
567 return mem >= Mapping::kShadowBeg && mem <= Mapping::kShadowEnd;
571 bool IsShadowMem(uptr mem) {
574 case 39: return IsShadowMemImpl<Mapping39>(mem);
575 case 42: return IsShadowMemImpl<Mapping42>(mem);
576 case 48: return IsShadowMemImpl<Mapping48>(mem);
580 #elif defined(__powerpc64__)
582 return IsShadowMemImpl<Mapping44>(mem);
584 return IsShadowMemImpl<Mapping46>(mem);
587 return IsShadowMemImpl<Mapping>(mem);
592 template<typename Mapping>
593 bool IsMetaMemImpl(uptr mem) {
594 return mem >= Mapping::kMetaShadowBeg && mem <= Mapping::kMetaShadowEnd;
598 bool IsMetaMem(uptr mem) {
601 case 39: return IsMetaMemImpl<Mapping39>(mem);
602 case 42: return IsMetaMemImpl<Mapping42>(mem);
603 case 48: return IsMetaMemImpl<Mapping48>(mem);
607 #elif defined(__powerpc64__)
609 return IsMetaMemImpl<Mapping44>(mem);
611 return IsMetaMemImpl<Mapping46>(mem);
614 return IsMetaMemImpl<Mapping>(mem);
619 template<typename Mapping>
620 uptr MemToShadowImpl(uptr x) {
623 return (((x) & ~(Mapping::kAppMemMsk | (kShadowCell - 1)))
624 ^ Mapping::kAppMemXor) * kShadowCnt;
626 # ifndef SANITIZER_WINDOWS
627 return ((x & ~(kShadowCell - 1)) * kShadowCnt) | Mapping::kShadowBeg;
629 return ((x & ~(kShadowCell - 1)) * kShadowCnt) + Mapping::kShadowBeg;
635 uptr MemToShadow(uptr x) {
638 case 39: return MemToShadowImpl<Mapping39>(x);
639 case 42: return MemToShadowImpl<Mapping42>(x);
640 case 48: return MemToShadowImpl<Mapping48>(x);
644 #elif defined(__powerpc64__)
646 return MemToShadowImpl<Mapping44>(x);
648 return MemToShadowImpl<Mapping46>(x);
651 return MemToShadowImpl<Mapping>(x);
656 template<typename Mapping>
657 u32 *MemToMetaImpl(uptr x) {
660 return (u32*)(((((x) & ~(Mapping::kAppMemMsk | (kMetaShadowCell - 1)))) /
661 kMetaShadowCell * kMetaShadowSize) | Mapping::kMetaShadowBeg);
663 # ifndef SANITIZER_WINDOWS
664 return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
665 kMetaShadowCell * kMetaShadowSize) | Mapping::kMetaShadowBeg);
667 return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
668 kMetaShadowCell * kMetaShadowSize) + Mapping::kMetaShadowBeg);
674 u32 *MemToMeta(uptr x) {
677 case 39: return MemToMetaImpl<Mapping39>(x);
678 case 42: return MemToMetaImpl<Mapping42>(x);
679 case 48: return MemToMetaImpl<Mapping48>(x);
683 #elif defined(__powerpc64__)
685 return MemToMetaImpl<Mapping44>(x);
687 return MemToMetaImpl<Mapping46>(x);
690 return MemToMetaImpl<Mapping>(x);
695 template<typename Mapping>
696 uptr ShadowToMemImpl(uptr s) {
697 DCHECK(IsShadowMem(s));
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
704 uptr p = (s / kShadowCnt) ^ Mapping::kAppMemXor;
705 if (p >= Mapping::kLoAppMemBeg && p < Mapping::kLoAppMemEnd &&
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 &&
715 return ((s / kShadowCnt) ^ Mapping::kAppMemXor) | Mapping::kAppMemMsk;
716 #else // #if !SANITIZER_GO
717 # ifndef SANITIZER_WINDOWS
718 return (s & ~Mapping::kShadowBeg) / kShadowCnt;
720 return (s - Mapping::kShadowBeg) / kShadowCnt;
721 # endif // SANITIZER_WINDOWS
726 uptr ShadowToMem(uptr s) {
729 case 39: return ShadowToMemImpl<Mapping39>(s);
730 case 42: return ShadowToMemImpl<Mapping42>(s);
731 case 48: return ShadowToMemImpl<Mapping48>(s);
735 #elif defined(__powerpc64__)
737 return ShadowToMemImpl<Mapping44>(s);
739 return ShadowToMemImpl<Mapping46>(s);
742 return ShadowToMemImpl<Mapping>(s);
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);
753 template<typename Mapping>
754 uptr GetThreadTraceImpl(int tid) {
755 uptr p = Mapping::kTraceMemBeg + (uptr)tid * kTotalTraceSize;
756 DCHECK_LT(p, Mapping::kTraceMemEnd);
761 uptr GetThreadTrace(int tid) {
764 case 39: return GetThreadTraceImpl<Mapping39>(tid);
765 case 42: return GetThreadTraceImpl<Mapping42>(tid);
766 case 48: return GetThreadTraceImpl<Mapping48>(tid);
770 #elif defined(__powerpc64__)
772 return GetThreadTraceImpl<Mapping44>(tid);
774 return GetThreadTraceImpl<Mapping46>(tid);
777 return GetThreadTraceImpl<Mapping>(tid);
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);
791 uptr GetThreadTraceHeader(int tid) {
794 case 39: return GetThreadTraceHeaderImpl<Mapping39>(tid);
795 case 42: return GetThreadTraceHeaderImpl<Mapping42>(tid);
796 case 48: return GetThreadTraceHeaderImpl<Mapping48>(tid);
800 #elif defined(__powerpc64__)
802 return GetThreadTraceHeaderImpl<Mapping44>(tid);
804 return GetThreadTraceHeaderImpl<Mapping46>(tid);
807 return GetThreadTraceHeaderImpl<Mapping>(tid);
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);
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);
825 void DestroyThreadState();
827 } // namespace __tsan
829 #endif // TSAN_PLATFORM_H