1 //===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 // Platform-specific code.
11 //===----------------------------------------------------------------------===//
13 #ifndef TSAN_PLATFORM_H
14 #define TSAN_PLATFORM_H
16 #if !defined(__LP64__) && !defined(_WIN64)
17 # error "Only 64-bit is supported"
20 #include "tsan_defs.h"
21 #include "tsan_trace.h"
27 #if defined(__x86_64__)
29 C/C++ on linux/x86_64 and freebsd/x86_64
30 0000 0000 1000 - 0080 0000 0000: main binary and/or MAP_32BIT mappings (512GB)
31 0040 0000 0000 - 0100 0000 0000: -
32 0100 0000 0000 - 2000 0000 0000: shadow
33 2000 0000 0000 - 3000 0000 0000: -
34 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
35 4000 0000 0000 - 5500 0000 0000: -
36 5500 0000 0000 - 5680 0000 0000: pie binaries without ASLR or on 4.1+ kernels
37 5680 0000 0000 - 6000 0000 0000: -
38 6000 0000 0000 - 6200 0000 0000: traces
39 6200 0000 0000 - 7d00 0000 0000: -
40 7b00 0000 0000 - 7c00 0000 0000: heap
41 7c00 0000 0000 - 7e80 0000 0000: -
42 7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
45 static const uptr kMetaShadowBeg
= 0x300000000000ull
;
46 static const uptr kMetaShadowEnd
= 0x340000000000ull
;
47 static const uptr kTraceMemBeg
= 0x600000000000ull
;
48 static const uptr kTraceMemEnd
= 0x620000000000ull
;
49 static const uptr kShadowBeg
= 0x010000000000ull
;
50 static const uptr kShadowEnd
= 0x200000000000ull
;
51 static const uptr kHeapMemBeg
= 0x7b0000000000ull
;
52 static const uptr kHeapMemEnd
= 0x7c0000000000ull
;
53 static const uptr kLoAppMemBeg
= 0x000000001000ull
;
54 static const uptr kLoAppMemEnd
= 0x008000000000ull
;
55 static const uptr kMidAppMemBeg
= 0x550000000000ull
;
56 static const uptr kMidAppMemEnd
= 0x568000000000ull
;
57 static const uptr kHiAppMemBeg
= 0x7e8000000000ull
;
58 static const uptr kHiAppMemEnd
= 0x800000000000ull
;
59 static const uptr kAppMemMsk
= 0x780000000000ull
;
60 static const uptr kAppMemXor
= 0x040000000000ull
;
61 static const uptr kVdsoBeg
= 0xf000000000000000ull
;
64 #define TSAN_MID_APP_RANGE 1
65 #elif defined(__mips64)
68 0100 0000 00 - 0200 0000 00: main binary
69 0200 0000 00 - 1400 0000 00: -
70 1400 0000 00 - 2400 0000 00: shadow
71 2400 0000 00 - 3000 0000 00: -
72 3000 0000 00 - 4000 0000 00: metainfo (memory blocks and sync objects)
73 4000 0000 00 - 6000 0000 00: -
74 6000 0000 00 - 6200 0000 00: traces
75 6200 0000 00 - fe00 0000 00: -
76 fe00 0000 00 - ff00 0000 00: heap
77 ff00 0000 00 - ff80 0000 00: -
78 ff80 0000 00 - ffff ffff ff: modules and main thread stack
81 static const uptr kMetaShadowBeg
= 0x4000000000ull
;
82 static const uptr kMetaShadowEnd
= 0x5000000000ull
;
83 static const uptr kTraceMemBeg
= 0xb000000000ull
;
84 static const uptr kTraceMemEnd
= 0xb200000000ull
;
85 static const uptr kShadowBeg
= 0x2400000000ull
;
86 static const uptr kShadowEnd
= 0x4000000000ull
;
87 static const uptr kHeapMemBeg
= 0xfe00000000ull
;
88 static const uptr kHeapMemEnd
= 0xff00000000ull
;
89 static const uptr kLoAppMemBeg
= 0x0100000000ull
;
90 static const uptr kLoAppMemEnd
= 0x0200000000ull
;
91 static const uptr kMidAppMemBeg
= 0xaa00000000ull
;
92 static const uptr kMidAppMemEnd
= 0xab00000000ull
;
93 static const uptr kHiAppMemBeg
= 0xff80000000ull
;
94 static const uptr kHiAppMemEnd
= 0xffffffffffull
;
95 static const uptr kAppMemMsk
= 0xf800000000ull
;
96 static const uptr kAppMemXor
= 0x0800000000ull
;
97 static const uptr kVdsoBeg
= 0xfffff00000ull
;
100 #define TSAN_MID_APP_RANGE 1
101 #elif defined(__aarch64__)
102 // AArch64 supports multiple VMA which leads to multiple address transformation
103 // functions. To support these multiple VMAS transformations and mappings TSAN
104 // runtime for AArch64 uses an external memory read (vmaSize) to select which
105 // mapping to use. Although slower, it make a same instrumented binary run on
109 C/C++ on linux/aarch64 (39-bit VMA)
110 0000 0010 00 - 0100 0000 00: main binary
111 0100 0000 00 - 0800 0000 00: -
112 0800 0000 00 - 2000 0000 00: shadow memory
113 2000 0000 00 - 3100 0000 00: -
114 3100 0000 00 - 3400 0000 00: metainfo
115 3400 0000 00 - 5500 0000 00: -
116 5500 0000 00 - 5600 0000 00: main binary (PIE)
117 5600 0000 00 - 6000 0000 00: -
118 6000 0000 00 - 6200 0000 00: traces
119 6200 0000 00 - 7d00 0000 00: -
120 7c00 0000 00 - 7d00 0000 00: heap
121 7d00 0000 00 - 7fff ffff ff: modules and main thread stack
124 static const uptr kLoAppMemBeg
= 0x0000001000ull
;
125 static const uptr kLoAppMemEnd
= 0x0100000000ull
;
126 static const uptr kShadowBeg
= 0x0800000000ull
;
127 static const uptr kShadowEnd
= 0x2000000000ull
;
128 static const uptr kMetaShadowBeg
= 0x3100000000ull
;
129 static const uptr kMetaShadowEnd
= 0x3400000000ull
;
130 static const uptr kMidAppMemBeg
= 0x5500000000ull
;
131 static const uptr kMidAppMemEnd
= 0x5600000000ull
;
132 static const uptr kTraceMemBeg
= 0x6000000000ull
;
133 static const uptr kTraceMemEnd
= 0x6200000000ull
;
134 static const uptr kHeapMemBeg
= 0x7c00000000ull
;
135 static const uptr kHeapMemEnd
= 0x7d00000000ull
;
136 static const uptr kHiAppMemBeg
= 0x7e00000000ull
;
137 static const uptr kHiAppMemEnd
= 0x7fffffffffull
;
138 static const uptr kAppMemMsk
= 0x7800000000ull
;
139 static const uptr kAppMemXor
= 0x0200000000ull
;
140 static const uptr kVdsoBeg
= 0x7f00000000ull
;
144 C/C++ on linux/aarch64 (42-bit VMA)
145 00000 0010 00 - 01000 0000 00: main binary
146 01000 0000 00 - 10000 0000 00: -
147 10000 0000 00 - 20000 0000 00: shadow memory
148 20000 0000 00 - 26000 0000 00: -
149 26000 0000 00 - 28000 0000 00: metainfo
150 28000 0000 00 - 2aa00 0000 00: -
151 2aa00 0000 00 - 2ab00 0000 00: main binary (PIE)
152 2ab00 0000 00 - 36200 0000 00: -
153 36200 0000 00 - 36240 0000 00: traces
154 36240 0000 00 - 3e000 0000 00: -
155 3e000 0000 00 - 3f000 0000 00: heap
156 3f000 0000 00 - 3ffff ffff ff: modules and main thread stack
159 static const uptr kLoAppMemBeg
= 0x00000001000ull
;
160 static const uptr kLoAppMemEnd
= 0x01000000000ull
;
161 static const uptr kShadowBeg
= 0x10000000000ull
;
162 static const uptr kShadowEnd
= 0x20000000000ull
;
163 static const uptr kMetaShadowBeg
= 0x26000000000ull
;
164 static const uptr kMetaShadowEnd
= 0x28000000000ull
;
165 static const uptr kMidAppMemBeg
= 0x2aa00000000ull
;
166 static const uptr kMidAppMemEnd
= 0x2ab00000000ull
;
167 static const uptr kTraceMemBeg
= 0x36200000000ull
;
168 static const uptr kTraceMemEnd
= 0x36400000000ull
;
169 static const uptr kHeapMemBeg
= 0x3e000000000ull
;
170 static const uptr kHeapMemEnd
= 0x3f000000000ull
;
171 static const uptr kHiAppMemBeg
= 0x3f000000000ull
;
172 static const uptr kHiAppMemEnd
= 0x3ffffffffffull
;
173 static const uptr kAppMemMsk
= 0x3c000000000ull
;
174 static const uptr kAppMemXor
= 0x04000000000ull
;
175 static const uptr kVdsoBeg
= 0x37f00000000ull
;
179 static const uptr kLoAppMemBeg
= 0x0000000001000ull
;
180 static const uptr kLoAppMemEnd
= 0x0000200000000ull
;
181 static const uptr kShadowBeg
= 0x0002000000000ull
;
182 static const uptr kShadowEnd
= 0x0004000000000ull
;
183 static const uptr kMetaShadowBeg
= 0x0005000000000ull
;
184 static const uptr kMetaShadowEnd
= 0x0006000000000ull
;
185 static const uptr kMidAppMemBeg
= 0x0aaaa00000000ull
;
186 static const uptr kMidAppMemEnd
= 0x0aaaf00000000ull
;
187 static const uptr kTraceMemBeg
= 0x0f06000000000ull
;
188 static const uptr kTraceMemEnd
= 0x0f06200000000ull
;
189 static const uptr kHeapMemBeg
= 0x0ffff00000000ull
;
190 static const uptr kHeapMemEnd
= 0x0ffff00000000ull
;
191 static const uptr kHiAppMemBeg
= 0x0ffff00000000ull
;
192 static const uptr kHiAppMemEnd
= 0x1000000000000ull
;
193 static const uptr kAppMemMsk
= 0x0fff800000000ull
;
194 static const uptr kAppMemXor
= 0x0000800000000ull
;
195 static const uptr kVdsoBeg
= 0xffff000000000ull
;
198 // Indicates the runtime will define the memory regions at runtime.
199 #define TSAN_RUNTIME_VMA 1
200 // Indicates that mapping defines a mid range memory segment.
201 #define TSAN_MID_APP_RANGE 1
202 #elif defined(__powerpc64__)
203 // PPC64 supports multiple VMA which leads to multiple address transformation
204 // functions. To support these multiple VMAS transformations and mappings TSAN
205 // runtime for PPC64 uses an external memory read (vmaSize) to select which
206 // mapping to use. Although slower, it make a same instrumented binary run on
210 C/C++ on linux/powerpc64 (44-bit VMA)
211 0000 0000 0100 - 0001 0000 0000: main binary
212 0001 0000 0000 - 0001 0000 0000: -
213 0001 0000 0000 - 0b00 0000 0000: shadow
214 0b00 0000 0000 - 0b00 0000 0000: -
215 0b00 0000 0000 - 0d00 0000 0000: metainfo (memory blocks and sync objects)
216 0d00 0000 0000 - 0d00 0000 0000: -
217 0d00 0000 0000 - 0f00 0000 0000: traces
218 0f00 0000 0000 - 0f00 0000 0000: -
219 0f00 0000 0000 - 0f50 0000 0000: heap
220 0f50 0000 0000 - 0f60 0000 0000: -
221 0f60 0000 0000 - 1000 0000 0000: modules and main thread stack
224 static const uptr kMetaShadowBeg
= 0x0b0000000000ull
;
225 static const uptr kMetaShadowEnd
= 0x0d0000000000ull
;
226 static const uptr kTraceMemBeg
= 0x0d0000000000ull
;
227 static const uptr kTraceMemEnd
= 0x0f0000000000ull
;
228 static const uptr kShadowBeg
= 0x000100000000ull
;
229 static const uptr kShadowEnd
= 0x0b0000000000ull
;
230 static const uptr kLoAppMemBeg
= 0x000000000100ull
;
231 static const uptr kLoAppMemEnd
= 0x000100000000ull
;
232 static const uptr kHeapMemBeg
= 0x0f0000000000ull
;
233 static const uptr kHeapMemEnd
= 0x0f5000000000ull
;
234 static const uptr kHiAppMemBeg
= 0x0f6000000000ull
;
235 static const uptr kHiAppMemEnd
= 0x100000000000ull
; // 44 bits
236 static const uptr kAppMemMsk
= 0x0f0000000000ull
;
237 static const uptr kAppMemXor
= 0x002100000000ull
;
238 static const uptr kVdsoBeg
= 0x3c0000000000000ull
;
242 C/C++ on linux/powerpc64 (46-bit VMA)
243 0000 0000 1000 - 0100 0000 0000: main binary
244 0100 0000 0000 - 0200 0000 0000: -
245 0100 0000 0000 - 1000 0000 0000: shadow
246 1000 0000 0000 - 1000 0000 0000: -
247 1000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
248 2000 0000 0000 - 2000 0000 0000: -
249 2000 0000 0000 - 2200 0000 0000: traces
250 2200 0000 0000 - 3d00 0000 0000: -
251 3d00 0000 0000 - 3e00 0000 0000: heap
252 3e00 0000 0000 - 3e80 0000 0000: -
253 3e80 0000 0000 - 4000 0000 0000: modules and main thread stack
256 static const uptr kMetaShadowBeg
= 0x100000000000ull
;
257 static const uptr kMetaShadowEnd
= 0x200000000000ull
;
258 static const uptr kTraceMemBeg
= 0x200000000000ull
;
259 static const uptr kTraceMemEnd
= 0x220000000000ull
;
260 static const uptr kShadowBeg
= 0x010000000000ull
;
261 static const uptr kShadowEnd
= 0x100000000000ull
;
262 static const uptr kHeapMemBeg
= 0x3d0000000000ull
;
263 static const uptr kHeapMemEnd
= 0x3e0000000000ull
;
264 static const uptr kLoAppMemBeg
= 0x000000001000ull
;
265 static const uptr kLoAppMemEnd
= 0x010000000000ull
;
266 static const uptr kHiAppMemBeg
= 0x3e8000000000ull
;
267 static const uptr kHiAppMemEnd
= 0x400000000000ull
; // 46 bits
268 static const uptr kAppMemMsk
= 0x3c0000000000ull
;
269 static const uptr kAppMemXor
= 0x020000000000ull
;
270 static const uptr kVdsoBeg
= 0x7800000000000000ull
;
273 // Indicates the runtime will define the memory regions at runtime.
274 #define TSAN_RUNTIME_VMA 1
277 #elif SANITIZER_GO && !SANITIZER_WINDOWS
279 /* Go on linux, darwin and freebsd
280 0000 0000 1000 - 0000 1000 0000: executable
281 0000 1000 0000 - 00c0 0000 0000: -
282 00c0 0000 0000 - 00e0 0000 0000: heap
283 00e0 0000 0000 - 2000 0000 0000: -
284 2000 0000 0000 - 2380 0000 0000: shadow
285 2380 0000 0000 - 3000 0000 0000: -
286 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
287 4000 0000 0000 - 6000 0000 0000: -
288 6000 0000 0000 - 6200 0000 0000: traces
289 6200 0000 0000 - 8000 0000 0000: -
293 static const uptr kMetaShadowBeg
= 0x300000000000ull
;
294 static const uptr kMetaShadowEnd
= 0x400000000000ull
;
295 static const uptr kTraceMemBeg
= 0x600000000000ull
;
296 static const uptr kTraceMemEnd
= 0x620000000000ull
;
297 static const uptr kShadowBeg
= 0x200000000000ull
;
298 static const uptr kShadowEnd
= 0x238000000000ull
;
299 static const uptr kAppMemBeg
= 0x000000001000ull
;
300 static const uptr kAppMemEnd
= 0x00e000000000ull
;
303 #elif SANITIZER_GO && SANITIZER_WINDOWS
306 0000 0000 1000 - 0000 1000 0000: executable
307 0000 1000 0000 - 00f8 0000 0000: -
308 00c0 0000 0000 - 00e0 0000 0000: heap
309 00e0 0000 0000 - 0100 0000 0000: -
310 0100 0000 0000 - 0500 0000 0000: shadow
311 0500 0000 0000 - 0560 0000 0000: -
312 0560 0000 0000 - 0760 0000 0000: traces
313 0760 0000 0000 - 07d0 0000 0000: metainfo (memory blocks and sync objects)
314 07d0 0000 0000 - 8000 0000 0000: -
318 static const uptr kMetaShadowBeg
= 0x076000000000ull
;
319 static const uptr kMetaShadowEnd
= 0x07d000000000ull
;
320 static const uptr kTraceMemBeg
= 0x056000000000ull
;
321 static const uptr kTraceMemEnd
= 0x076000000000ull
;
322 static const uptr kShadowBeg
= 0x010000000000ull
;
323 static const uptr kShadowEnd
= 0x050000000000ull
;
324 static const uptr kAppMemBeg
= 0x000000001000ull
;
325 static const uptr kAppMemEnd
= 0x00e000000000ull
;
329 # error "Unknown platform"
333 #ifdef TSAN_RUNTIME_VMA
343 #ifdef TSAN_MID_APP_RANGE
353 MAPPING_META_SHADOW_BEG
,
354 MAPPING_META_SHADOW_END
,
360 template<typename Mapping
, int Type
>
361 uptr
MappingImpl(void) {
364 case MAPPING_LO_APP_BEG
: return Mapping::kLoAppMemBeg
;
365 case MAPPING_LO_APP_END
: return Mapping::kLoAppMemEnd
;
366 # ifdef TSAN_MID_APP_RANGE
367 case MAPPING_MID_APP_BEG
: return Mapping::kMidAppMemBeg
;
368 case MAPPING_MID_APP_END
: return Mapping::kMidAppMemEnd
;
370 case MAPPING_HI_APP_BEG
: return Mapping::kHiAppMemBeg
;
371 case MAPPING_HI_APP_END
: return Mapping::kHiAppMemEnd
;
372 case MAPPING_HEAP_BEG
: return Mapping::kHeapMemBeg
;
373 case MAPPING_HEAP_END
: return Mapping::kHeapMemEnd
;
374 case MAPPING_VDSO_BEG
: return Mapping::kVdsoBeg
;
376 case MAPPING_APP_BEG
: return Mapping::kAppMemBeg
;
377 case MAPPING_APP_END
: return Mapping::kAppMemEnd
;
379 case MAPPING_SHADOW_BEG
: return Mapping::kShadowBeg
;
380 case MAPPING_SHADOW_END
: return Mapping::kShadowEnd
;
381 case MAPPING_META_SHADOW_BEG
: return Mapping::kMetaShadowBeg
;
382 case MAPPING_META_SHADOW_END
: return Mapping::kMetaShadowEnd
;
383 case MAPPING_TRACE_BEG
: return Mapping::kTraceMemBeg
;
384 case MAPPING_TRACE_END
: return Mapping::kTraceMemEnd
;
389 uptr
MappingArchImpl(void) {
392 case 39: return MappingImpl
<Mapping39
, Type
>();
393 case 42: return MappingImpl
<Mapping42
, Type
>();
394 case 48: return MappingImpl
<Mapping48
, Type
>();
398 #elif defined(__powerpc64__)
400 return MappingImpl
<Mapping44
, Type
>();
402 return MappingImpl
<Mapping46
, Type
>();
405 return MappingImpl
<Mapping
, Type
>();
411 uptr
LoAppMemBeg(void) {
412 return MappingArchImpl
<MAPPING_LO_APP_BEG
>();
415 uptr
LoAppMemEnd(void) {
416 return MappingArchImpl
<MAPPING_LO_APP_END
>();
419 #ifdef TSAN_MID_APP_RANGE
421 uptr
MidAppMemBeg(void) {
422 return MappingArchImpl
<MAPPING_MID_APP_BEG
>();
425 uptr
MidAppMemEnd(void) {
426 return MappingArchImpl
<MAPPING_MID_APP_END
>();
431 uptr
HeapMemBeg(void) {
432 return MappingArchImpl
<MAPPING_HEAP_BEG
>();
435 uptr
HeapMemEnd(void) {
436 return MappingArchImpl
<MAPPING_HEAP_END
>();
440 uptr
HiAppMemBeg(void) {
441 return MappingArchImpl
<MAPPING_HI_APP_BEG
>();
444 uptr
HiAppMemEnd(void) {
445 return MappingArchImpl
<MAPPING_HI_APP_END
>();
450 return MappingArchImpl
<MAPPING_VDSO_BEG
>();
456 uptr
AppMemBeg(void) {
457 return MappingArchImpl
<MAPPING_APP_BEG
>();
460 uptr
AppMemEnd(void) {
461 return MappingArchImpl
<MAPPING_APP_END
>();
467 bool GetUserRegion(int i
, uptr
*start
, uptr
*end
) {
473 *start
= LoAppMemBeg();
474 *end
= LoAppMemEnd();
477 *start
= HiAppMemBeg();
478 *end
= HiAppMemEnd();
481 *start
= HeapMemBeg();
484 # ifdef TSAN_MID_APP_RANGE
486 *start
= MidAppMemBeg();
487 *end
= MidAppMemEnd();
492 *start
= AppMemBeg();
500 uptr
ShadowBeg(void) {
501 return MappingArchImpl
<MAPPING_SHADOW_BEG
>();
504 uptr
ShadowEnd(void) {
505 return MappingArchImpl
<MAPPING_SHADOW_END
>();
509 uptr
MetaShadowBeg(void) {
510 return MappingArchImpl
<MAPPING_META_SHADOW_BEG
>();
513 uptr
MetaShadowEnd(void) {
514 return MappingArchImpl
<MAPPING_META_SHADOW_END
>();
518 uptr
TraceMemBeg(void) {
519 return MappingArchImpl
<MAPPING_TRACE_BEG
>();
522 uptr
TraceMemEnd(void) {
523 return MappingArchImpl
<MAPPING_TRACE_END
>();
527 template<typename Mapping
>
528 bool IsAppMemImpl(uptr mem
) {
530 return (mem
>= Mapping::kHeapMemBeg
&& mem
< Mapping::kHeapMemEnd
) ||
531 # ifdef TSAN_MID_APP_RANGE
532 (mem
>= Mapping::kMidAppMemBeg
&& mem
< Mapping::kMidAppMemEnd
) ||
534 (mem
>= Mapping::kLoAppMemBeg
&& mem
< Mapping::kLoAppMemEnd
) ||
535 (mem
>= Mapping::kHiAppMemBeg
&& mem
< Mapping::kHiAppMemEnd
);
537 return mem
>= Mapping::kAppMemBeg
&& mem
< Mapping::kAppMemEnd
;
542 bool IsAppMem(uptr mem
) {
545 case 39: return IsAppMemImpl
<Mapping39
>(mem
);
546 case 42: return IsAppMemImpl
<Mapping42
>(mem
);
547 case 48: return IsAppMemImpl
<Mapping48
>(mem
);
551 #elif defined(__powerpc64__)
553 return IsAppMemImpl
<Mapping44
>(mem
);
555 return IsAppMemImpl
<Mapping46
>(mem
);
558 return IsAppMemImpl
<Mapping
>(mem
);
563 template<typename Mapping
>
564 bool IsShadowMemImpl(uptr mem
) {
565 return mem
>= Mapping::kShadowBeg
&& mem
<= Mapping::kShadowEnd
;
569 bool IsShadowMem(uptr mem
) {
572 case 39: return IsShadowMemImpl
<Mapping39
>(mem
);
573 case 42: return IsShadowMemImpl
<Mapping42
>(mem
);
574 case 48: return IsShadowMemImpl
<Mapping48
>(mem
);
578 #elif defined(__powerpc64__)
580 return IsShadowMemImpl
<Mapping44
>(mem
);
582 return IsShadowMemImpl
<Mapping46
>(mem
);
585 return IsShadowMemImpl
<Mapping
>(mem
);
590 template<typename Mapping
>
591 bool IsMetaMemImpl(uptr mem
) {
592 return mem
>= Mapping::kMetaShadowBeg
&& mem
<= Mapping::kMetaShadowEnd
;
596 bool IsMetaMem(uptr mem
) {
599 case 39: return IsMetaMemImpl
<Mapping39
>(mem
);
600 case 42: return IsMetaMemImpl
<Mapping42
>(mem
);
601 case 48: return IsMetaMemImpl
<Mapping48
>(mem
);
605 #elif defined(__powerpc64__)
607 return IsMetaMemImpl
<Mapping44
>(mem
);
609 return IsMetaMemImpl
<Mapping46
>(mem
);
612 return IsMetaMemImpl
<Mapping
>(mem
);
617 template<typename Mapping
>
618 uptr
MemToShadowImpl(uptr x
) {
621 return (((x
) & ~(Mapping::kAppMemMsk
| (kShadowCell
- 1)))
622 ^ Mapping::kAppMemXor
) * kShadowCnt
;
624 # ifndef SANITIZER_WINDOWS
625 return ((x
& ~(kShadowCell
- 1)) * kShadowCnt
) | Mapping::kShadowBeg
;
627 return ((x
& ~(kShadowCell
- 1)) * kShadowCnt
) + Mapping::kShadowBeg
;
633 uptr
MemToShadow(uptr x
) {
636 case 39: return MemToShadowImpl
<Mapping39
>(x
);
637 case 42: return MemToShadowImpl
<Mapping42
>(x
);
638 case 48: return MemToShadowImpl
<Mapping48
>(x
);
642 #elif defined(__powerpc64__)
644 return MemToShadowImpl
<Mapping44
>(x
);
646 return MemToShadowImpl
<Mapping46
>(x
);
649 return MemToShadowImpl
<Mapping
>(x
);
654 template<typename Mapping
>
655 u32
*MemToMetaImpl(uptr x
) {
658 return (u32
*)(((((x
) & ~(Mapping::kAppMemMsk
| (kMetaShadowCell
- 1)))) /
659 kMetaShadowCell
* kMetaShadowSize
) | Mapping::kMetaShadowBeg
);
661 # ifndef SANITIZER_WINDOWS
662 return (u32
*)(((x
& ~(kMetaShadowCell
- 1)) / \
663 kMetaShadowCell
* kMetaShadowSize
) | Mapping::kMetaShadowBeg
);
665 return (u32
*)(((x
& ~(kMetaShadowCell
- 1)) / \
666 kMetaShadowCell
* kMetaShadowSize
) + Mapping::kMetaShadowBeg
);
672 u32
*MemToMeta(uptr x
) {
675 case 39: return MemToMetaImpl
<Mapping39
>(x
);
676 case 42: return MemToMetaImpl
<Mapping42
>(x
);
677 case 48: return MemToMetaImpl
<Mapping48
>(x
);
681 #elif defined(__powerpc64__)
683 return MemToMetaImpl
<Mapping44
>(x
);
685 return MemToMetaImpl
<Mapping46
>(x
);
688 return MemToMetaImpl
<Mapping
>(x
);
693 template<typename Mapping
>
694 uptr
ShadowToMemImpl(uptr s
) {
695 DCHECK(IsShadowMem(s
));
697 // The shadow mapping is non-linear and we've lost some bits, so we don't have
698 // an easy way to restore the original app address. But the mapping is a
699 // bijection, so we try to restore the address as belonging to low/mid/high
700 // range consecutively and see if shadow->app->shadow mapping gives us the
702 uptr p
= (s
/ kShadowCnt
) ^ Mapping::kAppMemXor
;
703 if (p
>= Mapping::kLoAppMemBeg
&& p
< Mapping::kLoAppMemEnd
&&
706 # ifdef TSAN_MID_APP_RANGE
707 p
= ((s
/ kShadowCnt
) ^ Mapping::kAppMemXor
) +
708 (Mapping::kMidAppMemBeg
& Mapping::kAppMemMsk
);
709 if (p
>= Mapping::kMidAppMemBeg
&& p
< Mapping::kMidAppMemEnd
&&
713 return ((s
/ kShadowCnt
) ^ Mapping::kAppMemXor
) | Mapping::kAppMemMsk
;
714 #else // #if !SANITIZER_GO
715 # ifndef SANITIZER_WINDOWS
716 return (s
& ~Mapping::kShadowBeg
) / kShadowCnt
;
718 return (s
- Mapping::kShadowBeg
) / kShadowCnt
;
719 # endif // SANITIZER_WINDOWS
724 uptr
ShadowToMem(uptr s
) {
727 case 39: return ShadowToMemImpl
<Mapping39
>(s
);
728 case 42: return ShadowToMemImpl
<Mapping42
>(s
);
729 case 48: return ShadowToMemImpl
<Mapping48
>(s
);
733 #elif defined(__powerpc64__)
735 return ShadowToMemImpl
<Mapping44
>(s
);
737 return ShadowToMemImpl
<Mapping46
>(s
);
740 return ShadowToMemImpl
<Mapping
>(s
);
746 // The additional page is to catch shadow stack overflow as paging fault.
747 // Windows wants 64K alignment for mmaps.
748 const uptr kTotalTraceSize
= (kTraceSize
* sizeof(Event
) + sizeof(Trace
)
749 + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
751 template<typename Mapping
>
752 uptr
GetThreadTraceImpl(int tid
) {
753 uptr p
= Mapping::kTraceMemBeg
+ (uptr
)tid
* kTotalTraceSize
;
754 DCHECK_LT(p
, Mapping::kTraceMemEnd
);
759 uptr
GetThreadTrace(int tid
) {
762 case 39: return GetThreadTraceImpl
<Mapping39
>(tid
);
763 case 42: return GetThreadTraceImpl
<Mapping42
>(tid
);
764 case 48: return GetThreadTraceImpl
<Mapping48
>(tid
);
768 #elif defined(__powerpc64__)
770 return GetThreadTraceImpl
<Mapping44
>(tid
);
772 return GetThreadTraceImpl
<Mapping46
>(tid
);
775 return GetThreadTraceImpl
<Mapping
>(tid
);
780 template<typename Mapping
>
781 uptr
GetThreadTraceHeaderImpl(int tid
) {
782 uptr p
= Mapping::kTraceMemBeg
+ (uptr
)tid
* kTotalTraceSize
783 + kTraceSize
* sizeof(Event
);
784 DCHECK_LT(p
, Mapping::kTraceMemEnd
);
789 uptr
GetThreadTraceHeader(int tid
) {
792 case 39: return GetThreadTraceHeaderImpl
<Mapping39
>(tid
);
793 case 42: return GetThreadTraceHeaderImpl
<Mapping42
>(tid
);
794 case 48: return GetThreadTraceHeaderImpl
<Mapping48
>(tid
);
798 #elif defined(__powerpc64__)
800 return GetThreadTraceHeaderImpl
<Mapping44
>(tid
);
802 return GetThreadTraceHeaderImpl
<Mapping46
>(tid
);
805 return GetThreadTraceHeaderImpl
<Mapping
>(tid
);
809 void InitializePlatform();
810 void InitializePlatformEarly();
811 void CheckAndProtect();
812 void InitializeShadowMemoryPlatform();
813 void FlushShadowMemory();
814 void WriteMemoryProfile(char *buf
, uptr buf_size
, uptr nthread
, uptr nlive
);
815 int ExtractResolvFDs(void *state
, int *fds
, int nfd
);
816 int ExtractRecvmsgFDs(void *msg
, int *fds
, int nfd
);
818 int call_pthread_cancel_with_cleanup(int(*fn
)(void *c
, void *m
,
819 void *abstime
), void *c
, void *m
, void *abstime
,
820 void(*cleanup
)(void *arg
), void *arg
);
822 void DestroyThreadState();
824 } // namespace __tsan
826 #endif // TSAN_PLATFORM_H