1//===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of ThreadSanitizer (TSan), a race detector.
10//
11// Platform-specific code.
12//===----------------------------------------------------------------------===//
13
14#ifndef TSAN_PLATFORM_H
15#define TSAN_PLATFORM_H
16
17#if !defined(__LP64__) && !defined(_WIN64)
18# error "Only 64-bit is supported"
19#endif
20
21#include "tsan_defs.h"
22#include "tsan_trace.h"
23
24namespace __tsan {
25
26#if defined(__x86_64__)
27#define HAS_48_BIT_ADDRESS_SPACE 1
28#elif SANITIZER_IOSSIM // arm64 iOS simulators (order of #if matters)
29#define HAS_48_BIT_ADDRESS_SPACE 1
30#elif SANITIZER_IOS // arm64 iOS devices (order of #if matters)
31#define HAS_48_BIT_ADDRESS_SPACE 0
32#elif SANITIZER_MAC // arm64 macOS (order of #if matters)
33#define HAS_48_BIT_ADDRESS_SPACE 1
34#else
35#define HAS_48_BIT_ADDRESS_SPACE 0
36#endif
37
38#if !SANITIZER_GO
39
40#if HAS_48_BIT_ADDRESS_SPACE
41/*
42C/C++ on linux/x86_64 and freebsd/x86_64
430000 0000 1000 - 0080 0000 0000: main binary and/or MAP_32BIT mappings (512GB)
440040 0000 0000 - 0100 0000 0000: -
450100 0000 0000 - 2000 0000 0000: shadow
462000 0000 0000 - 3000 0000 0000: -
473000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
484000 0000 0000 - 5500 0000 0000: -
495500 0000 0000 - 5680 0000 0000: pie binaries without ASLR or on 4.1+ kernels
505680 0000 0000 - 6000 0000 0000: -
516000 0000 0000 - 6200 0000 0000: traces
526200 0000 0000 - 7d00 0000 0000: -
537b00 0000 0000 - 7c00 0000 0000: heap
547c00 0000 0000 - 7e80 0000 0000: -
557e80 0000 0000 - 8000 0000 0000: modules and main thread stack
56
57C/C++ on netbsd/amd64 can reuse the same mapping:
58 * The address space starts from 0x1000 (option with 0x0) and ends with
59 0x7f7ffffff000.
60 * LoAppMem-kHeapMemEnd can be reused as it is.
61 * No VDSO support.
62 * No MidAppMem region.
63 * No additional HeapMem region.
64 * HiAppMem contains the stack, loader, shared libraries and heap.
65 * Stack on NetBSD/amd64 has prereserved 128MB.
66 * Heap grows downwards (top-down).
67 * ASLR must be disabled per-process or globally.
68
69*/
70struct Mapping {
71 static const uptr kMetaShadowBeg = 0x300000000000ull;
72 static const uptr kMetaShadowEnd = 0x340000000000ull;
73 static const uptr kTraceMemBeg = 0x600000000000ull;
74 static const uptr kTraceMemEnd = 0x620000000000ull;
75 static const uptr kShadowBeg = 0x010000000000ull;
76 static const uptr kShadowEnd = 0x200000000000ull;
77 static const uptr kHeapMemBeg = 0x7b0000000000ull;
78 static const uptr kHeapMemEnd = 0x7c0000000000ull;
79 static const uptr kLoAppMemBeg = 0x000000001000ull;
80 static const uptr kLoAppMemEnd = 0x008000000000ull;
81 static const uptr kMidAppMemBeg = 0x550000000000ull;
82 static const uptr kMidAppMemEnd = 0x568000000000ull;
83 static const uptr kHiAppMemBeg = 0x7e8000000000ull;
84 static const uptr kHiAppMemEnd = 0x800000000000ull;
85 static const uptr kAppMemMsk = 0x780000000000ull;
86 static const uptr kAppMemXor = 0x040000000000ull;
87 static const uptr kVdsoBeg = 0xf000000000000000ull;
88};
89
90#define TSAN_MID_APP_RANGE 1
91#elif defined(__mips64)
92/*
93C/C++ on linux/mips64 (40-bit VMA)
940000 0000 00 - 0100 0000 00: - (4 GB)
950100 0000 00 - 0200 0000 00: main binary (4 GB)
960200 0000 00 - 2000 0000 00: - (120 GB)
972000 0000 00 - 4000 0000 00: shadow (128 GB)
984000 0000 00 - 5000 0000 00: metainfo (memory blocks and sync objects) (64 GB)
995000 0000 00 - aa00 0000 00: - (360 GB)
100aa00 0000 00 - ab00 0000 00: main binary (PIE) (4 GB)
101ab00 0000 00 - b000 0000 00: - (20 GB)
102b000 0000 00 - b200 0000 00: traces (8 GB)
103b200 0000 00 - fe00 0000 00: - (304 GB)
104fe00 0000 00 - ff00 0000 00: heap (4 GB)
105ff00 0000 00 - ff80 0000 00: - (2 GB)
106ff80 0000 00 - ffff ffff ff: modules and main thread stack (<2 GB)
107*/
108struct Mapping40 {
109 static const uptr kMetaShadowBeg = 0x4000000000ull;
110 static const uptr kMetaShadowEnd = 0x5000000000ull;
111 static const uptr kTraceMemBeg = 0xb000000000ull;
112 static const uptr kTraceMemEnd = 0xb200000000ull;
113 static const uptr kShadowBeg = 0x2000000000ull;
114 static const uptr kShadowEnd = 0x4000000000ull;
115 static const uptr kHeapMemBeg = 0xfe00000000ull;
116 static const uptr kHeapMemEnd = 0xff00000000ull;
117 static const uptr kLoAppMemBeg = 0x0100000000ull;
118 static const uptr kLoAppMemEnd = 0x0200000000ull;
119 static const uptr kMidAppMemBeg = 0xaa00000000ull;
120 static const uptr kMidAppMemEnd = 0xab00000000ull;
121 static const uptr kHiAppMemBeg = 0xff80000000ull;
122 static const uptr kHiAppMemEnd = 0xffffffffffull;
123 static const uptr kAppMemMsk = 0xf800000000ull;
124 static const uptr kAppMemXor = 0x0800000000ull;
125 static const uptr kVdsoBeg = 0xfffff00000ull;
126};
127
128#define TSAN_MID_APP_RANGE 1
129#define TSAN_RUNTIME_VMA 1
130#elif defined(__aarch64__) && defined(__APPLE__)
131/*
132C/C++ on Darwin/iOS/ARM64 (36-bit VMA, 64 GB VM)
1330000 0000 00 - 0100 0000 00: - (4 GB)
1340100 0000 00 - 0200 0000 00: main binary, modules, thread stacks (4 GB)
1350200 0000 00 - 0300 0000 00: heap (4 GB)
1360300 0000 00 - 0400 0000 00: - (4 GB)
1370400 0000 00 - 0c00 0000 00: shadow memory (32 GB)
1380c00 0000 00 - 0d00 0000 00: - (4 GB)
1390d00 0000 00 - 0e00 0000 00: metainfo (4 GB)
1400e00 0000 00 - 0f00 0000 00: - (4 GB)
1410f00 0000 00 - 0fc0 0000 00: traces (3 GB)
1420fc0 0000 00 - 1000 0000 00: -
143*/
144struct Mapping {
145 static const uptr kLoAppMemBeg = 0x0100000000ull;
146 static const uptr kLoAppMemEnd = 0x0200000000ull;
147 static const uptr kHeapMemBeg = 0x0200000000ull;
148 static const uptr kHeapMemEnd = 0x0300000000ull;
149 static const uptr kShadowBeg = 0x0400000000ull;
150 static const uptr kShadowEnd = 0x0c00000000ull;
151 static const uptr kMetaShadowBeg = 0x0d00000000ull;
152 static const uptr kMetaShadowEnd = 0x0e00000000ull;
153 static const uptr kTraceMemBeg = 0x0f00000000ull;
154 static const uptr kTraceMemEnd = 0x0fc0000000ull;
155 static const uptr kHiAppMemBeg = 0x0fc0000000ull;
156 static const uptr kHiAppMemEnd = 0x0fc0000000ull;
157 static const uptr kAppMemMsk = 0x0ull;
158 static const uptr kAppMemXor = 0x0ull;
159 static const uptr kVdsoBeg = 0x7000000000000000ull;
160};
161
162#elif defined(__aarch64__) && !defined(__APPLE__)
163// AArch64 supports multiple VMA which leads to multiple address transformation
164// functions. To support these multiple VMAS transformations and mappings TSAN
165// runtime for AArch64 uses an external memory read (vmaSize) to select which
166// mapping to use. Although slower, it make a same instrumented binary run on
167// multiple kernels.
168
169/*
170C/C++ on linux/aarch64 (39-bit VMA)
1710000 0010 00 - 0100 0000 00: main binary
1720100 0000 00 - 0800 0000 00: -
1730800 0000 00 - 2000 0000 00: shadow memory
1742000 0000 00 - 3100 0000 00: -
1753100 0000 00 - 3400 0000 00: metainfo
1763400 0000 00 - 5500 0000 00: -
1775500 0000 00 - 5600 0000 00: main binary (PIE)
1785600 0000 00 - 6000 0000 00: -
1796000 0000 00 - 6200 0000 00: traces
1806200 0000 00 - 7d00 0000 00: -
1817c00 0000 00 - 7d00 0000 00: heap
1827d00 0000 00 - 7fff ffff ff: modules and main thread stack
183*/
184struct Mapping39 {
185 static const uptr kLoAppMemBeg = 0x0000001000ull;
186 static const uptr kLoAppMemEnd = 0x0100000000ull;
187 static const uptr kShadowBeg = 0x0800000000ull;
188 static const uptr kShadowEnd = 0x2000000000ull;
189 static const uptr kMetaShadowBeg = 0x3100000000ull;
190 static const uptr kMetaShadowEnd = 0x3400000000ull;
191 static const uptr kMidAppMemBeg = 0x5500000000ull;
192 static const uptr kMidAppMemEnd = 0x5600000000ull;
193 static const uptr kTraceMemBeg = 0x6000000000ull;
194 static const uptr kTraceMemEnd = 0x6200000000ull;
195 static const uptr kHeapMemBeg = 0x7c00000000ull;
196 static const uptr kHeapMemEnd = 0x7d00000000ull;
197 static const uptr kHiAppMemBeg = 0x7e00000000ull;
198 static const uptr kHiAppMemEnd = 0x7fffffffffull;
199 static const uptr kAppMemMsk = 0x7800000000ull;
200 static const uptr kAppMemXor = 0x0200000000ull;
201 static const uptr kVdsoBeg = 0x7f00000000ull;
202};
203
204/*
205C/C++ on linux/aarch64 (42-bit VMA)
20600000 0010 00 - 01000 0000 00: main binary
20701000 0000 00 - 10000 0000 00: -
20810000 0000 00 - 20000 0000 00: shadow memory
20920000 0000 00 - 26000 0000 00: -
21026000 0000 00 - 28000 0000 00: metainfo
21128000 0000 00 - 2aa00 0000 00: -
2122aa00 0000 00 - 2ab00 0000 00: main binary (PIE)
2132ab00 0000 00 - 36200 0000 00: -
21436200 0000 00 - 36240 0000 00: traces
21536240 0000 00 - 3e000 0000 00: -
2163e000 0000 00 - 3f000 0000 00: heap
2173f000 0000 00 - 3ffff ffff ff: modules and main thread stack
218*/
219struct Mapping42 {
220 static const uptr kLoAppMemBeg = 0x00000001000ull;
221 static const uptr kLoAppMemEnd = 0x01000000000ull;
222 static const uptr kShadowBeg = 0x10000000000ull;
223 static const uptr kShadowEnd = 0x20000000000ull;
224 static const uptr kMetaShadowBeg = 0x26000000000ull;
225 static const uptr kMetaShadowEnd = 0x28000000000ull;
226 static const uptr kMidAppMemBeg = 0x2aa00000000ull;
227 static const uptr kMidAppMemEnd = 0x2ab00000000ull;
228 static const uptr kTraceMemBeg = 0x36200000000ull;
229 static const uptr kTraceMemEnd = 0x36400000000ull;
230 static const uptr kHeapMemBeg = 0x3e000000000ull;
231 static const uptr kHeapMemEnd = 0x3f000000000ull;
232 static const uptr kHiAppMemBeg = 0x3f000000000ull;
233 static const uptr kHiAppMemEnd = 0x3ffffffffffull;
234 static const uptr kAppMemMsk = 0x3c000000000ull;
235 static const uptr kAppMemXor = 0x04000000000ull;
236 static const uptr kVdsoBeg = 0x37f00000000ull;
237};
238
239struct Mapping48 {
240 static const uptr kLoAppMemBeg = 0x0000000001000ull;
241 static const uptr kLoAppMemEnd = 0x0000200000000ull;
242 static const uptr kShadowBeg = 0x0002000000000ull;
243 static const uptr kShadowEnd = 0x0004000000000ull;
244 static const uptr kMetaShadowBeg = 0x0005000000000ull;
245 static const uptr kMetaShadowEnd = 0x0006000000000ull;
246 static const uptr kMidAppMemBeg = 0x0aaaa00000000ull;
247 static const uptr kMidAppMemEnd = 0x0aaaf00000000ull;
248 static const uptr kTraceMemBeg = 0x0f06000000000ull;
249 static const uptr kTraceMemEnd = 0x0f06200000000ull;
250 static const uptr kHeapMemBeg = 0x0ffff00000000ull;
251 static const uptr kHeapMemEnd = 0x0ffff00000000ull;
252 static const uptr kHiAppMemBeg = 0x0ffff00000000ull;
253 static const uptr kHiAppMemEnd = 0x1000000000000ull;
254 static const uptr kAppMemMsk = 0x0fff800000000ull;
255 static const uptr kAppMemXor = 0x0000800000000ull;
256 static const uptr kVdsoBeg = 0xffff000000000ull;
257};
258
259// Indicates the runtime will define the memory regions at runtime.
260#define TSAN_RUNTIME_VMA 1
261// Indicates that mapping defines a mid range memory segment.
262#define TSAN_MID_APP_RANGE 1
263#elif defined(__powerpc64__)
264// PPC64 supports multiple VMA which leads to multiple address transformation
265// functions. To support these multiple VMAS transformations and mappings TSAN
266// runtime for PPC64 uses an external memory read (vmaSize) to select which
267// mapping to use. Although slower, it make a same instrumented binary run on
268// multiple kernels.
269
270/*
271C/C++ on linux/powerpc64 (44-bit VMA)
2720000 0000 0100 - 0001 0000 0000: main binary
2730001 0000 0000 - 0001 0000 0000: -
2740001 0000 0000 - 0b00 0000 0000: shadow
2750b00 0000 0000 - 0b00 0000 0000: -
2760b00 0000 0000 - 0d00 0000 0000: metainfo (memory blocks and sync objects)
2770d00 0000 0000 - 0d00 0000 0000: -
2780d00 0000 0000 - 0f00 0000 0000: traces
2790f00 0000 0000 - 0f00 0000 0000: -
2800f00 0000 0000 - 0f50 0000 0000: heap
2810f50 0000 0000 - 0f60 0000 0000: -
2820f60 0000 0000 - 1000 0000 0000: modules and main thread stack
283*/
284struct Mapping44 {
285 static const uptr kMetaShadowBeg = 0x0b0000000000ull;
286 static const uptr kMetaShadowEnd = 0x0d0000000000ull;
287 static const uptr kTraceMemBeg = 0x0d0000000000ull;
288 static const uptr kTraceMemEnd = 0x0f0000000000ull;
289 static const uptr kShadowBeg = 0x000100000000ull;
290 static const uptr kShadowEnd = 0x0b0000000000ull;
291 static const uptr kLoAppMemBeg = 0x000000000100ull;
292 static const uptr kLoAppMemEnd = 0x000100000000ull;
293 static const uptr kHeapMemBeg = 0x0f0000000000ull;
294 static const uptr kHeapMemEnd = 0x0f5000000000ull;
295 static const uptr kHiAppMemBeg = 0x0f6000000000ull;
296 static const uptr kHiAppMemEnd = 0x100000000000ull; // 44 bits
297 static const uptr kAppMemMsk = 0x0f0000000000ull;
298 static const uptr kAppMemXor = 0x002100000000ull;
299 static const uptr kVdsoBeg = 0x3c0000000000000ull;
300};
301
302/*
303C/C++ on linux/powerpc64 (46-bit VMA)
3040000 0000 1000 - 0100 0000 0000: main binary
3050100 0000 0000 - 0200 0000 0000: -
3060100 0000 0000 - 1000 0000 0000: shadow
3071000 0000 0000 - 1000 0000 0000: -
3081000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
3092000 0000 0000 - 2000 0000 0000: -
3102000 0000 0000 - 2200 0000 0000: traces
3112200 0000 0000 - 3d00 0000 0000: -
3123d00 0000 0000 - 3e00 0000 0000: heap
3133e00 0000 0000 - 3e80 0000 0000: -
3143e80 0000 0000 - 4000 0000 0000: modules and main thread stack
315*/
316struct Mapping46 {
317 static const uptr kMetaShadowBeg = 0x100000000000ull;
318 static const uptr kMetaShadowEnd = 0x200000000000ull;
319 static const uptr kTraceMemBeg = 0x200000000000ull;
320 static const uptr kTraceMemEnd = 0x220000000000ull;
321 static const uptr kShadowBeg = 0x010000000000ull;
322 static const uptr kShadowEnd = 0x100000000000ull;
323 static const uptr kHeapMemBeg = 0x3d0000000000ull;
324 static const uptr kHeapMemEnd = 0x3e0000000000ull;
325 static const uptr kLoAppMemBeg = 0x000000001000ull;
326 static const uptr kLoAppMemEnd = 0x010000000000ull;
327 static const uptr kHiAppMemBeg = 0x3e8000000000ull;
328 static const uptr kHiAppMemEnd = 0x400000000000ull; // 46 bits
329 static const uptr kAppMemMsk = 0x3c0000000000ull;
330 static const uptr kAppMemXor = 0x020000000000ull;
331 static const uptr kVdsoBeg = 0x7800000000000000ull;
332};
333
334/*
335C/C++ on linux/powerpc64 (47-bit VMA)
3360000 0000 1000 - 0100 0000 0000: main binary
3370100 0000 0000 - 0200 0000 0000: -
3380100 0000 0000 - 1000 0000 0000: shadow
3391000 0000 0000 - 1000 0000 0000: -
3401000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
3412000 0000 0000 - 2000 0000 0000: -
3422000 0000 0000 - 2200 0000 0000: traces
3432200 0000 0000 - 7d00 0000 0000: -
3447d00 0000 0000 - 7e00 0000 0000: heap
3457e00 0000 0000 - 7e80 0000 0000: -
3467e80 0000 0000 - 8000 0000 0000: modules and main thread stack
347*/
348struct Mapping47 {
349 static const uptr kMetaShadowBeg = 0x100000000000ull;
350 static const uptr kMetaShadowEnd = 0x200000000000ull;
351 static const uptr kTraceMemBeg = 0x200000000000ull;
352 static const uptr kTraceMemEnd = 0x220000000000ull;
353 static const uptr kShadowBeg = 0x010000000000ull;
354 static const uptr kShadowEnd = 0x100000000000ull;
355 static const uptr kHeapMemBeg = 0x7d0000000000ull;
356 static const uptr kHeapMemEnd = 0x7e0000000000ull;
357 static const uptr kLoAppMemBeg = 0x000000001000ull;
358 static const uptr kLoAppMemEnd = 0x010000000000ull;
359 static const uptr kHiAppMemBeg = 0x7e8000000000ull;
360 static const uptr kHiAppMemEnd = 0x800000000000ull; // 47 bits
361 static const uptr kAppMemMsk = 0x7c0000000000ull;
362 static const uptr kAppMemXor = 0x020000000000ull;
363 static const uptr kVdsoBeg = 0x7800000000000000ull;
364};
365
366// Indicates the runtime will define the memory regions at runtime.
367#define TSAN_RUNTIME_VMA 1
368#endif
369
370#elif SANITIZER_GO && !SANITIZER_WINDOWS && HAS_48_BIT_ADDRESS_SPACE
371
372/* Go on linux, darwin and freebsd on x86_64
3730000 0000 1000 - 0000 1000 0000: executable
3740000 1000 0000 - 00c0 0000 0000: -
37500c0 0000 0000 - 00e0 0000 0000: heap
37600e0 0000 0000 - 2000 0000 0000: -
3772000 0000 0000 - 2380 0000 0000: shadow
3782380 0000 0000 - 3000 0000 0000: -
3793000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
3804000 0000 0000 - 6000 0000 0000: -
3816000 0000 0000 - 6200 0000 0000: traces
3826200 0000 0000 - 8000 0000 0000: -
383*/
384
385struct Mapping {
386 static const uptr kMetaShadowBeg = 0x300000000000ull;
387 static const uptr kMetaShadowEnd = 0x400000000000ull;
388 static const uptr kTraceMemBeg = 0x600000000000ull;
389 static const uptr kTraceMemEnd = 0x620000000000ull;
390 static const uptr kShadowBeg = 0x200000000000ull;
391 static const uptr kShadowEnd = 0x238000000000ull;
392 static const uptr kAppMemBeg = 0x000000001000ull;
393 static const uptr kAppMemEnd = 0x00e000000000ull;
394};
395
396#elif SANITIZER_GO && SANITIZER_WINDOWS
397
398/* Go on windows
3990000 0000 1000 - 0000 1000 0000: executable
4000000 1000 0000 - 00f8 0000 0000: -
40100c0 0000 0000 - 00e0 0000 0000: heap
40200e0 0000 0000 - 0100 0000 0000: -
4030100 0000 0000 - 0500 0000 0000: shadow
4040500 0000 0000 - 0560 0000 0000: -
4050560 0000 0000 - 0760 0000 0000: traces
4060760 0000 0000 - 07d0 0000 0000: metainfo (memory blocks and sync objects)
40707d0 0000 0000 - 8000 0000 0000: -
408*/
409
410struct Mapping {
411 static const uptr kMetaShadowBeg = 0x076000000000ull;
412 static const uptr kMetaShadowEnd = 0x07d000000000ull;
413 static const uptr kTraceMemBeg = 0x056000000000ull;
414 static const uptr kTraceMemEnd = 0x076000000000ull;
415 static const uptr kShadowBeg = 0x010000000000ull;
416 static const uptr kShadowEnd = 0x050000000000ull;
417 static const uptr kAppMemBeg = 0x000000001000ull;
418 static const uptr kAppMemEnd = 0x00e000000000ull;
419};
420
421#elif SANITIZER_GO && defined(__powerpc64__)
422
423/* Only Mapping46 and Mapping47 are currently supported for powercp64 on Go. */
424
425/* Go on linux/powerpc64 (46-bit VMA)
4260000 0000 1000 - 0000 1000 0000: executable
4270000 1000 0000 - 00c0 0000 0000: -
42800c0 0000 0000 - 00e0 0000 0000: heap
42900e0 0000 0000 - 2000 0000 0000: -
4302000 0000 0000 - 2380 0000 0000: shadow
4312380 0000 0000 - 2400 0000 0000: -
4322400 0000 0000 - 3400 0000 0000: metainfo (memory blocks and sync objects)
4333400 0000 0000 - 3600 0000 0000: -
4343600 0000 0000 - 3800 0000 0000: traces
4353800 0000 0000 - 4000 0000 0000: -
436*/
437
438struct Mapping46 {
439 static const uptr kMetaShadowBeg = 0x240000000000ull;
440 static const uptr kMetaShadowEnd = 0x340000000000ull;
441 static const uptr kTraceMemBeg = 0x360000000000ull;
442 static const uptr kTraceMemEnd = 0x380000000000ull;
443 static const uptr kShadowBeg = 0x200000000000ull;
444 static const uptr kShadowEnd = 0x238000000000ull;
445 static const uptr kAppMemBeg = 0x000000001000ull;
446 static const uptr kAppMemEnd = 0x00e000000000ull;
447};
448
449/* Go on linux/powerpc64 (47-bit VMA)
4500000 0000 1000 - 0000 1000 0000: executable
4510000 1000 0000 - 00c0 0000 0000: -
45200c0 0000 0000 - 00e0 0000 0000: heap
45300e0 0000 0000 - 2000 0000 0000: -
4542000 0000 0000 - 3000 0000 0000: shadow
4553000 0000 0000 - 3000 0000 0000: -
4563000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
4574000 0000 0000 - 6000 0000 0000: -
4586000 0000 0000 - 6200 0000 0000: traces
4596200 0000 0000 - 8000 0000 0000: -
460*/
461
462struct Mapping47 {
463 static const uptr kMetaShadowBeg = 0x300000000000ull;
464 static const uptr kMetaShadowEnd = 0x400000000000ull;
465 static const uptr kTraceMemBeg = 0x600000000000ull;
466 static const uptr kTraceMemEnd = 0x620000000000ull;
467 static const uptr kShadowBeg = 0x200000000000ull;
468 static const uptr kShadowEnd = 0x300000000000ull;
469 static const uptr kAppMemBeg = 0x000000001000ull;
470 static const uptr kAppMemEnd = 0x00e000000000ull;
471};
472
473#define TSAN_RUNTIME_VMA 1
474
475#elif SANITIZER_GO && defined(__aarch64__)
476
477/* Go on linux/aarch64 (48-bit VMA) and darwin/aarch64 (47-bit VMA)
4780000 0000 1000 - 0000 1000 0000: executable
4790000 1000 0000 - 00c0 0000 0000: -
48000c0 0000 0000 - 00e0 0000 0000: heap
48100e0 0000 0000 - 2000 0000 0000: -
4822000 0000 0000 - 3000 0000 0000: shadow
4833000 0000 0000 - 3000 0000 0000: -
4843000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
4854000 0000 0000 - 6000 0000 0000: -
4866000 0000 0000 - 6200 0000 0000: traces
4876200 0000 0000 - 8000 0000 0000: -
488*/
489
490struct Mapping {
491 static const uptr kMetaShadowBeg = 0x300000000000ull;
492 static const uptr kMetaShadowEnd = 0x400000000000ull;
493 static const uptr kTraceMemBeg = 0x600000000000ull;
494 static const uptr kTraceMemEnd = 0x620000000000ull;
495 static const uptr kShadowBeg = 0x200000000000ull;
496 static const uptr kShadowEnd = 0x300000000000ull;
497 static const uptr kAppMemBeg = 0x000000001000ull;
498 static const uptr kAppMemEnd = 0x00e000000000ull;
499};
500
501// Indicates the runtime will define the memory regions at runtime.
502#define TSAN_RUNTIME_VMA 1
503
504#elif SANITIZER_GO && defined(__mips64)
505/*
506Go on linux/mips64 (47-bit VMA)
5070000 0000 1000 - 0000 1000 0000: executable
5080000 1000 0000 - 00c0 0000 0000: -
50900c0 0000 0000 - 00e0 0000 0000: heap
51000e0 0000 0000 - 2000 0000 0000: -
5112000 0000 0000 - 3000 0000 0000: shadow
5123000 0000 0000 - 3000 0000 0000: -
5133000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
5144000 0000 0000 - 6000 0000 0000: -
5156000 0000 0000 - 6200 0000 0000: traces
5166200 0000 0000 - 8000 0000 0000: -
517*/
518struct Mapping47 {
519 static const uptr kMetaShadowBeg = 0x300000000000ull;
520 static const uptr kMetaShadowEnd = 0x400000000000ull;
521 static const uptr kTraceMemBeg = 0x600000000000ull;
522 static const uptr kTraceMemEnd = 0x620000000000ull;
523 static const uptr kShadowBeg = 0x200000000000ull;
524 static const uptr kShadowEnd = 0x300000000000ull;
525 static const uptr kAppMemBeg = 0x000000001000ull;
526 static const uptr kAppMemEnd = 0x00e000000000ull;
527};
528
529#define TSAN_RUNTIME_VMA 1
530
531#else
532# error "Unknown platform"
533#endif
534
535
536#ifdef TSAN_RUNTIME_VMA
537extern uptr vmaSize;
538#endif
539
540
541enum MappingType {
542 MAPPING_LO_APP_BEG,
543 MAPPING_LO_APP_END,
544 MAPPING_HI_APP_BEG,
545 MAPPING_HI_APP_END,
546#ifdef TSAN_MID_APP_RANGE
547 MAPPING_MID_APP_BEG,
548 MAPPING_MID_APP_END,
549#endif
550 MAPPING_HEAP_BEG,
551 MAPPING_HEAP_END,
552 MAPPING_APP_BEG,
553 MAPPING_APP_END,
554 MAPPING_SHADOW_BEG,
555 MAPPING_SHADOW_END,
556 MAPPING_META_SHADOW_BEG,
557 MAPPING_META_SHADOW_END,
558 MAPPING_TRACE_BEG,
559 MAPPING_TRACE_END,
560 MAPPING_VDSO_BEG,
561};
562
563template<typename Mapping, int Type>
564uptr MappingImpl(void) {
565 switch (Type) {
566#if !SANITIZER_GO
567 case MAPPING_LO_APP_BEG: return Mapping::kLoAppMemBeg;
568 case MAPPING_LO_APP_END: return Mapping::kLoAppMemEnd;
569# ifdef TSAN_MID_APP_RANGE
570 case MAPPING_MID_APP_BEG: return Mapping::kMidAppMemBeg;
571 case MAPPING_MID_APP_END: return Mapping::kMidAppMemEnd;
572# endif
573 case MAPPING_HI_APP_BEG: return Mapping::kHiAppMemBeg;
574 case MAPPING_HI_APP_END: return Mapping::kHiAppMemEnd;
575 case MAPPING_HEAP_BEG: return Mapping::kHeapMemBeg;
576 case MAPPING_HEAP_END: return Mapping::kHeapMemEnd;
577 case MAPPING_VDSO_BEG: return Mapping::kVdsoBeg;
578#else
579 case MAPPING_APP_BEG: return Mapping::kAppMemBeg;
580 case MAPPING_APP_END: return Mapping::kAppMemEnd;
581#endif
582 case MAPPING_SHADOW_BEG: return Mapping::kShadowBeg;
583 case MAPPING_SHADOW_END: return Mapping::kShadowEnd;
584 case MAPPING_META_SHADOW_BEG: return Mapping::kMetaShadowBeg;
585 case MAPPING_META_SHADOW_END: return Mapping::kMetaShadowEnd;
586 case MAPPING_TRACE_BEG: return Mapping::kTraceMemBeg;
587 case MAPPING_TRACE_END: return Mapping::kTraceMemEnd;
588 }
589}
590
591template<int Type>
592uptr MappingArchImpl(void) {
593#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
594 switch (vmaSize) {
595 case 39: return MappingImpl<Mapping39, Type>();
596 case 42: return MappingImpl<Mapping42, Type>();
597 case 48: return MappingImpl<Mapping48, Type>();
598 }
599 DCHECK(0);
600 return 0;
601#elif defined(__powerpc64__)
602 switch (vmaSize) {
603#if !SANITIZER_GO
604 case 44: return MappingImpl<Mapping44, Type>();
605#endif
606 case 46: return MappingImpl<Mapping46, Type>();
607 case 47: return MappingImpl<Mapping47, Type>();
608 }
609 DCHECK(0);
610 return 0;
611#elif defined(__mips64)
612 switch (vmaSize) {
613#if !SANITIZER_GO
614 case 40: return MappingImpl<Mapping40, Type>();
615#else
616 case 47: return MappingImpl<Mapping47, Type>();
617#endif
618 }
619 DCHECK(0);
620 return 0;
621#else
622 return MappingImpl<Mapping, Type>();
623#endif
624}
625
626#if !SANITIZER_GO
627ALWAYS_INLINE
628uptr LoAppMemBeg(void) {
629 return MappingArchImpl<MAPPING_LO_APP_BEG>();
630}
631ALWAYS_INLINE
632uptr LoAppMemEnd(void) {
633 return MappingArchImpl<MAPPING_LO_APP_END>();
634}
635
636#ifdef TSAN_MID_APP_RANGE
637ALWAYS_INLINE
638uptr MidAppMemBeg(void) {
639 return MappingArchImpl<MAPPING_MID_APP_BEG>();
640}
641ALWAYS_INLINE
642uptr MidAppMemEnd(void) {
643 return MappingArchImpl<MAPPING_MID_APP_END>();
644}
645#endif
646
647ALWAYS_INLINE
648uptr HeapMemBeg(void) {
649 return MappingArchImpl<MAPPING_HEAP_BEG>();
650}
651ALWAYS_INLINE
652uptr HeapMemEnd(void) {
653 return MappingArchImpl<MAPPING_HEAP_END>();
654}
655
656ALWAYS_INLINE
657uptr HiAppMemBeg(void) {
658 return MappingArchImpl<MAPPING_HI_APP_BEG>();
659}
660ALWAYS_INLINE
661uptr HiAppMemEnd(void) {
662 return MappingArchImpl<MAPPING_HI_APP_END>();
663}
664
665ALWAYS_INLINE
666uptr VdsoBeg(void) {
667 return MappingArchImpl<MAPPING_VDSO_BEG>();
668}
669
670#else
671
672ALWAYS_INLINE
673uptr AppMemBeg(void) {
674 return MappingArchImpl<MAPPING_APP_BEG>();
675}
676ALWAYS_INLINE
677uptr AppMemEnd(void) {
678 return MappingArchImpl<MAPPING_APP_END>();
679}
680
681#endif
682
683static inline
684bool GetUserRegion(int i, uptr *start, uptr *end) {
685 switch (i) {
686 default:
687 return false;
688#if !SANITIZER_GO
689 case 0:
690 *start = LoAppMemBeg();
691 *end = LoAppMemEnd();
692 return true;
693 case 1:
694 *start = HiAppMemBeg();
695 *end = HiAppMemEnd();
696 return true;
697 case 2:
698 *start = HeapMemBeg();
699 *end = HeapMemEnd();
700 return true;
701# ifdef TSAN_MID_APP_RANGE
702 case 3:
703 *start = MidAppMemBeg();
704 *end = MidAppMemEnd();
705 return true;
706# endif
707#else
708 case 0:
709 *start = AppMemBeg();
710 *end = AppMemEnd();
711 return true;
712#endif
713 }
714}
715
716ALWAYS_INLINE
717uptr ShadowBeg(void) {
718 return MappingArchImpl<MAPPING_SHADOW_BEG>();
719}
720ALWAYS_INLINE
721uptr ShadowEnd(void) {
722 return MappingArchImpl<MAPPING_SHADOW_END>();
723}
724
725ALWAYS_INLINE
726uptr MetaShadowBeg(void) {
727 return MappingArchImpl<MAPPING_META_SHADOW_BEG>();
728}
729ALWAYS_INLINE
730uptr MetaShadowEnd(void) {
731 return MappingArchImpl<MAPPING_META_SHADOW_END>();
732}
733
734ALWAYS_INLINE
735uptr TraceMemBeg(void) {
736 return MappingArchImpl<MAPPING_TRACE_BEG>();
737}
738ALWAYS_INLINE
739uptr TraceMemEnd(void) {
740 return MappingArchImpl<MAPPING_TRACE_END>();
741}
742
743
744template<typename Mapping>
745bool IsAppMemImpl(uptr mem) {
746#if !SANITIZER_GO
747 return (mem >= Mapping::kHeapMemBeg && mem < Mapping::kHeapMemEnd) ||
748# ifdef TSAN_MID_APP_RANGE
749 (mem >= Mapping::kMidAppMemBeg && mem < Mapping::kMidAppMemEnd) ||
750# endif
751 (mem >= Mapping::kLoAppMemBeg && mem < Mapping::kLoAppMemEnd) ||
752 (mem >= Mapping::kHiAppMemBeg && mem < Mapping::kHiAppMemEnd);
753#else
754 return mem >= Mapping::kAppMemBeg && mem < Mapping::kAppMemEnd;
755#endif
756}
757
758ALWAYS_INLINE
759bool IsAppMem(uptr mem) {
760#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
761 switch (vmaSize) {
762 case 39: return IsAppMemImpl<Mapping39>(mem);
763 case 42: return IsAppMemImpl<Mapping42>(mem);
764 case 48: return IsAppMemImpl<Mapping48>(mem);
765 }
766 DCHECK(0);
767 return false;
768#elif defined(__powerpc64__)
769 switch (vmaSize) {
770#if !SANITIZER_GO
771 case 44: return IsAppMemImpl<Mapping44>(mem);
772#endif
773 case 46: return IsAppMemImpl<Mapping46>(mem);
774 case 47: return IsAppMemImpl<Mapping47>(mem);
775 }
776 DCHECK(0);
777 return false;
778#elif defined(__mips64)
779 switch (vmaSize) {
780#if !SANITIZER_GO
781 case 40: return IsAppMemImpl<Mapping40>(mem);
782#else
783 case 47: return IsAppMemImpl<Mapping47>(mem);
784#endif
785 }
786 DCHECK(0);
787 return false;
788#else
789 return IsAppMemImpl<Mapping>(mem);
790#endif
791}
792
793
794template<typename Mapping>
795bool IsShadowMemImpl(uptr mem) {
796 return mem >= Mapping::kShadowBeg && mem <= Mapping::kShadowEnd;
797}
798
799ALWAYS_INLINE
800bool IsShadowMem(uptr mem) {
801#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
802 switch (vmaSize) {
803 case 39: return IsShadowMemImpl<Mapping39>(mem);
804 case 42: return IsShadowMemImpl<Mapping42>(mem);
805 case 48: return IsShadowMemImpl<Mapping48>(mem);
806 }
807 DCHECK(0);
808 return false;
809#elif defined(__powerpc64__)
810 switch (vmaSize) {
811#if !SANITIZER_GO
812 case 44: return IsShadowMemImpl<Mapping44>(mem);
813#endif
814 case 46: return IsShadowMemImpl<Mapping46>(mem);
815 case 47: return IsShadowMemImpl<Mapping47>(mem);
816 }
817 DCHECK(0);
818 return false;
819#elif defined(__mips64)
820 switch (vmaSize) {
821#if !SANITIZER_GO
822 case 40: return IsShadowMemImpl<Mapping40>(mem);
823#else
824 case 47: return IsShadowMemImpl<Mapping47>(mem);
825#endif
826 }
827 DCHECK(0);
828 return false;
829#else
830 return IsShadowMemImpl<Mapping>(mem);
831#endif
832}
833
834
835template<typename Mapping>
836bool IsMetaMemImpl(uptr mem) {
837 return mem >= Mapping::kMetaShadowBeg && mem <= Mapping::kMetaShadowEnd;
838}
839
840ALWAYS_INLINE
841bool IsMetaMem(uptr mem) {
842#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
843 switch (vmaSize) {
844 case 39: return IsMetaMemImpl<Mapping39>(mem);
845 case 42: return IsMetaMemImpl<Mapping42>(mem);
846 case 48: return IsMetaMemImpl<Mapping48>(mem);
847 }
848 DCHECK(0);
849 return false;
850#elif defined(__powerpc64__)
851 switch (vmaSize) {
852#if !SANITIZER_GO
853 case 44: return IsMetaMemImpl<Mapping44>(mem);
854#endif
855 case 46: return IsMetaMemImpl<Mapping46>(mem);
856 case 47: return IsMetaMemImpl<Mapping47>(mem);
857 }
858 DCHECK(0);
859 return false;
860#elif defined(__mips64)
861 switch (vmaSize) {
862#if !SANITIZER_GO
863 case 40: return IsMetaMemImpl<Mapping40>(mem);
864#else
865 case 47: return IsMetaMemImpl<Mapping47>(mem);
866#endif
867 }
868 DCHECK(0);
869 return false;
870#else
871 return IsMetaMemImpl<Mapping>(mem);
872#endif
873}
874
875
876template<typename Mapping>
877uptr MemToShadowImpl(uptr x) {
878 DCHECK(IsAppMem(x));
879#if !SANITIZER_GO
880 return (((x) & ~(Mapping::kAppMemMsk | (kShadowCell - 1)))
881 ^ Mapping::kAppMemXor) * kShadowCnt;
882#else
883# ifndef SANITIZER_WINDOWS
884 return ((x & ~(kShadowCell - 1)) * kShadowCnt) | Mapping::kShadowBeg;
885# else
886 return ((x & ~(kShadowCell - 1)) * kShadowCnt) + Mapping::kShadowBeg;
887# endif
888#endif
889}
890
891ALWAYS_INLINE
892uptr MemToShadow(uptr x) {
893#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
894 switch (vmaSize) {
895 case 39: return MemToShadowImpl<Mapping39>(x);
896 case 42: return MemToShadowImpl<Mapping42>(x);
897 case 48: return MemToShadowImpl<Mapping48>(x);
898 }
899 DCHECK(0);
900 return 0;
901#elif defined(__powerpc64__)
902 switch (vmaSize) {
903#if !SANITIZER_GO
904 case 44: return MemToShadowImpl<Mapping44>(x);
905#endif
906 case 46: return MemToShadowImpl<Mapping46>(x);
907 case 47: return MemToShadowImpl<Mapping47>(x);
908 }
909 DCHECK(0);
910 return 0;
911#elif defined(__mips64)
912 switch (vmaSize) {
913#if !SANITIZER_GO
914 case 40: return MemToShadowImpl<Mapping40>(x);
915#else
916 case 47: return MemToShadowImpl<Mapping47>(x);
917#endif
918 }
919 DCHECK(0);
920 return 0;
921#else
922 return MemToShadowImpl<Mapping>(x);
923#endif
924}
925
926
927template<typename Mapping>
928u32 *MemToMetaImpl(uptr x) {
929 DCHECK(IsAppMem(x));
930#if !SANITIZER_GO
931 return (u32*)(((((x) & ~(Mapping::kAppMemMsk | (kMetaShadowCell - 1)))) /
932 kMetaShadowCell * kMetaShadowSize) | Mapping::kMetaShadowBeg);
933#else
934# ifndef SANITIZER_WINDOWS
935 return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
936 kMetaShadowCell * kMetaShadowSize) | Mapping::kMetaShadowBeg);
937# else
938 return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
939 kMetaShadowCell * kMetaShadowSize) + Mapping::kMetaShadowBeg);
940# endif
941#endif
942}
943
944ALWAYS_INLINE
945u32 *MemToMeta(uptr x) {
946#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
947 switch (vmaSize) {
948 case 39: return MemToMetaImpl<Mapping39>(x);
949 case 42: return MemToMetaImpl<Mapping42>(x);
950 case 48: return MemToMetaImpl<Mapping48>(x);
951 }
952 DCHECK(0);
953 return 0;
954#elif defined(__powerpc64__)
955 switch (vmaSize) {
956#if !SANITIZER_GO
957 case 44: return MemToMetaImpl<Mapping44>(x);
958#endif
959 case 46: return MemToMetaImpl<Mapping46>(x);
960 case 47: return MemToMetaImpl<Mapping47>(x);
961 }
962 DCHECK(0);
963 return 0;
964#elif defined(__mips64)
965 switch (vmaSize) {
966#if !SANITIZER_GO
967 case 40: return MemToMetaImpl<Mapping40>(x);
968#else
969 case 47: return MemToMetaImpl<Mapping47>(x);
970#endif
971 }
972 DCHECK(0);
973 return 0;
974#else
975 return MemToMetaImpl<Mapping>(x);
976#endif
977}
978
979
980template<typename Mapping>
981uptr ShadowToMemImpl(uptr s) {
982 DCHECK(IsShadowMem(s));
983#if !SANITIZER_GO
984 // The shadow mapping is non-linear and we've lost some bits, so we don't have
985 // an easy way to restore the original app address. But the mapping is a
986 // bijection, so we try to restore the address as belonging to low/mid/high
987 // range consecutively and see if shadow->app->shadow mapping gives us the
988 // same address.
989 uptr p = (s / kShadowCnt) ^ Mapping::kAppMemXor;
990 if (p >= Mapping::kLoAppMemBeg && p < Mapping::kLoAppMemEnd &&
991 MemToShadow(p) == s)
992 return p;
993# ifdef TSAN_MID_APP_RANGE
994 p = ((s / kShadowCnt) ^ Mapping::kAppMemXor) +
995 (Mapping::kMidAppMemBeg & Mapping::kAppMemMsk);
996 if (p >= Mapping::kMidAppMemBeg && p < Mapping::kMidAppMemEnd &&
997 MemToShadow(p) == s)
998 return p;
999# endif
1000 return ((s / kShadowCnt) ^ Mapping::kAppMemXor) | Mapping::kAppMemMsk;
1001#else // #if !SANITIZER_GO
1002# ifndef SANITIZER_WINDOWS
1003 return (s & ~Mapping::kShadowBeg) / kShadowCnt;
1004# else
1005 return (s - Mapping::kShadowBeg) / kShadowCnt;
1006# endif // SANITIZER_WINDOWS
1007#endif
1008}
1009
1010ALWAYS_INLINE
1011uptr ShadowToMem(uptr s) {
1012#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
1013 switch (vmaSize) {
1014 case 39: return ShadowToMemImpl<Mapping39>(s);
1015 case 42: return ShadowToMemImpl<Mapping42>(s);
1016 case 48: return ShadowToMemImpl<Mapping48>(s);
1017 }
1018 DCHECK(0);
1019 return 0;
1020#elif defined(__powerpc64__)
1021 switch (vmaSize) {
1022#if !SANITIZER_GO
1023 case 44: return ShadowToMemImpl<Mapping44>(s);
1024#endif
1025 case 46: return ShadowToMemImpl<Mapping46>(s);
1026 case 47: return ShadowToMemImpl<Mapping47>(s);
1027 }
1028 DCHECK(0);
1029 return 0;
1030#elif defined(__mips64)
1031 switch (vmaSize) {
1032#if !SANITIZER_GO
1033 case 40: return ShadowToMemImpl<Mapping40>(s);
1034#else
1035 case 47: return ShadowToMemImpl<Mapping47>(s);
1036#endif
1037 }
1038 DCHECK(0);
1039 return 0;
1040#else
1041 return ShadowToMemImpl<Mapping>(s);
1042#endif
1043}
1044
1045
1046
1047// The additional page is to catch shadow stack overflow as paging fault.
1048// Windows wants 64K alignment for mmaps.
1049const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
1050 + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
1051
1052template<typename Mapping>
1053uptr GetThreadTraceImpl(int tid) {
1054 uptr p = Mapping::kTraceMemBeg + (uptr)tid * kTotalTraceSize;
1055 DCHECK_LT(p, Mapping::kTraceMemEnd);
1056 return p;
1057}
1058
1059ALWAYS_INLINE
1060uptr GetThreadTrace(int tid) {
1061#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
1062 switch (vmaSize) {
1063 case 39: return GetThreadTraceImpl<Mapping39>(tid);
1064 case 42: return GetThreadTraceImpl<Mapping42>(tid);
1065 case 48: return GetThreadTraceImpl<Mapping48>(tid);
1066 }
1067 DCHECK(0);
1068 return 0;
1069#elif defined(__powerpc64__)
1070 switch (vmaSize) {
1071#if !SANITIZER_GO
1072 case 44: return GetThreadTraceImpl<Mapping44>(tid);
1073#endif
1074 case 46: return GetThreadTraceImpl<Mapping46>(tid);
1075 case 47: return GetThreadTraceImpl<Mapping47>(tid);
1076 }
1077 DCHECK(0);
1078 return 0;
1079#elif defined(__mips64)
1080 switch (vmaSize) {
1081#if !SANITIZER_GO
1082 case 40: return GetThreadTraceImpl<Mapping40>(tid);
1083#else
1084 case 47: return GetThreadTraceImpl<Mapping47>(tid);
1085#endif
1086 }
1087 DCHECK(0);
1088 return 0;
1089#else
1090 return GetThreadTraceImpl<Mapping>(tid);
1091#endif
1092}
1093
1094
1095template<typename Mapping>
1096uptr GetThreadTraceHeaderImpl(int tid) {
1097 uptr p = Mapping::kTraceMemBeg + (uptr)tid * kTotalTraceSize
1098 + kTraceSize * sizeof(Event);
1099 DCHECK_LT(p, Mapping::kTraceMemEnd);
1100 return p;
1101}
1102
1103ALWAYS_INLINE
1104uptr GetThreadTraceHeader(int tid) {
1105#if defined(__aarch64__) && !defined(__APPLE__) && !SANITIZER_GO
1106 switch (vmaSize) {
1107 case 39: return GetThreadTraceHeaderImpl<Mapping39>(tid);
1108 case 42: return GetThreadTraceHeaderImpl<Mapping42>(tid);
1109 case 48: return GetThreadTraceHeaderImpl<Mapping48>(tid);
1110 }
1111 DCHECK(0);
1112 return 0;
1113#elif defined(__powerpc64__)
1114 switch (vmaSize) {
1115#if !SANITIZER_GO
1116 case 44: return GetThreadTraceHeaderImpl<Mapping44>(tid);
1117#endif
1118 case 46: return GetThreadTraceHeaderImpl<Mapping46>(tid);
1119 case 47: return GetThreadTraceHeaderImpl<Mapping47>(tid);
1120 }
1121 DCHECK(0);
1122 return 0;
1123#elif defined(__mips64)
1124 switch (vmaSize) {
1125#if !SANITIZER_GO
1126 case 40: return GetThreadTraceHeaderImpl<Mapping40>(tid);
1127#else
1128 case 47: return GetThreadTraceHeaderImpl<Mapping47>(tid);
1129#endif
1130 }
1131 DCHECK(0);
1132 return 0;
1133#else
1134 return GetThreadTraceHeaderImpl<Mapping>(tid);
1135#endif
1136}
1137
1138void InitializePlatform();
1139void InitializePlatformEarly();
1140void CheckAndProtect();
1141void InitializeShadowMemoryPlatform();
1142void FlushShadowMemory();
1143void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive);
1144int ExtractResolvFDs(void *state, int *fds, int nfd);
1145int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
1146uptr ExtractLongJmpSp(uptr *env);
1147void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size);
1148
1149int call_pthread_cancel_with_cleanup(int (*fn)(void *arg),
1150 void (*cleanup)(void *arg), void *arg);
1151
1152void DestroyThreadState();
1153void PlatformCleanUpThreadState(ThreadState *thr);
1154
1155} // namespace __tsan
1156
1157#endif // TSAN_PLATFORM_H
1158