summaryrefslogtreecommitdiff
path: root/libsanitizer/tsan/tsan_platform.h
blob: f34f577ec030c7f1baaa1a69dea0a387a7a4e554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
// Platform-specific code.
//===----------------------------------------------------------------------===//

#ifndef TSAN_PLATFORM_H
#define TSAN_PLATFORM_H

#if !defined(__LP64__) && !defined(_WIN64)
# error "Only 64-bit is supported"
#endif

#include "tsan_defs.h"
#include "tsan_trace.h"

namespace __tsan {

#if !defined(SANITIZER_GO)

#if defined(__x86_64__)
/*
C/C++ on linux/x86_64 and freebsd/x86_64
0000 0000 1000 - 0100 0000 0000: main binary and/or MAP_32BIT mappings
0100 0000 0000 - 0200 0000 0000: -
0200 0000 0000 - 1000 0000 0000: shadow
1000 0000 0000 - 3000 0000 0000: -
3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
4000 0000 0000 - 6000 0000 0000: -
6000 0000 0000 - 6200 0000 0000: traces
6200 0000 0000 - 7d00 0000 0000: -
7d00 0000 0000 - 7e00 0000 0000: heap
7e00 0000 0000 - 7e80 0000 0000: -
7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
*/
const uptr kMetaShadowBeg = 0x300000000000ull;
const uptr kMetaShadowEnd = 0x400000000000ull;
const uptr kTraceMemBeg   = 0x600000000000ull;
const uptr kTraceMemEnd   = 0x620000000000ull;
const uptr kShadowBeg     = 0x020000000000ull;
const uptr kShadowEnd     = 0x100000000000ull;
const uptr kHeapMemBeg    = 0x7d0000000000ull;
const uptr kHeapMemEnd    = 0x7e0000000000ull;
const uptr kLoAppMemBeg   = 0x000000001000ull;
const uptr kLoAppMemEnd   = 0x010000000000ull;
const uptr kHiAppMemBeg   = 0x7e8000000000ull;
const uptr kHiAppMemEnd   = 0x800000000000ull;
const uptr kAppMemMsk     = 0x7c0000000000ull;
const uptr kAppMemXor     = 0x020000000000ull;
const uptr kVdsoBeg       = 0xf000000000000000ull;
#elif defined(__mips64)
/*
C/C++ on linux/mips64
0100 0000 00 - 0200 0000 00: main binary
0200 0000 00 - 1400 0000 00: -
1400 0000 00 - 2400 0000 00: shadow
2400 0000 00 - 3000 0000 00: -
3000 0000 00 - 4000 0000 00: metainfo (memory blocks and sync objects)
4000 0000 00 - 6000 0000 00: -
6000 0000 00 - 6200 0000 00: traces
6200 0000 00 - fe00 0000 00: -
fe00 0000 00 - ff00 0000 00: heap
ff00 0000 00 - ff80 0000 00: -
ff80 0000 00 - ffff ffff ff: modules and main thread stack
*/
const uptr kMetaShadowBeg = 0x3000000000ull;
const uptr kMetaShadowEnd = 0x4000000000ull;
const uptr kTraceMemBeg   = 0x6000000000ull;
const uptr kTraceMemEnd   = 0x6200000000ull;
const uptr kShadowBeg     = 0x1400000000ull;
const uptr kShadowEnd     = 0x2400000000ull;
const uptr kHeapMemBeg    = 0xfe00000000ull;
const uptr kHeapMemEnd    = 0xff00000000ull;
const uptr kLoAppMemBeg   = 0x0100000000ull;
const uptr kLoAppMemEnd   = 0x0200000000ull;
const uptr kHiAppMemBeg   = 0xff80000000ull;
const uptr kHiAppMemEnd   = 0xffffffffffull;
const uptr kAppMemMsk     = 0xfc00000000ull;
const uptr kAppMemXor     = 0x0400000000ull;
const uptr kVdsoBeg       = 0xfffff00000ull;
#elif defined(__aarch64__)
# if SANITIZER_AARCH64_VMA == 39
/*
C/C++ on linux/aarch64 (39-bit VMA)
0000 4000 00 - 0200 0000 00: main binary
2000 0000 00 - 4000 0000 00: shadow memory
4000 0000 00 - 5000 0000 00: metainfo
5000 0000 00 - 6000 0000 00: -
6000 0000 00 - 6200 0000 00: traces
6200 0000 00 - 7d00 0000 00: -
7d00 0000 00 - 7e00 0000 00: heap
7e00 0000 00 - 7fff ffff ff: modules and main thread stack
*/
const uptr kLoAppMemBeg   = 0x0000400000ull;
const uptr kLoAppMemEnd   = 0x0200000000ull;
const uptr kShadowBeg     = 0x2000000000ull;
const uptr kShadowEnd     = 0x4000000000ull;
const uptr kMetaShadowBeg = 0x4000000000ull;
const uptr kMetaShadowEnd = 0x5000000000ull;
const uptr kTraceMemBeg   = 0x6000000000ull;
const uptr kTraceMemEnd   = 0x6200000000ull;
const uptr kHeapMemBeg    = 0x7d00000000ull;
const uptr kHeapMemEnd    = 0x7e00000000ull;
const uptr kHiAppMemBeg   = 0x7e00000000ull;
const uptr kHiAppMemEnd   = 0x7fffffffffull;
const uptr kAppMemMsk     = 0x7800000000ull;
const uptr kAppMemXor     = 0x0800000000ull;
const uptr kVdsoBeg       = 0x7f00000000ull;
# elif SANITIZER_AARCH64_VMA == 42
/*
C/C++ on linux/aarch64 (42-bit VMA)
00000 4000 00 - 01000 0000 00: main binary
01000 0000 00 - 10000 0000 00: -
10000 0000 00 - 20000 0000 00: shadow memory
20000 0000 00 - 26000 0000 00: -
26000 0000 00 - 28000 0000 00: metainfo
28000 0000 00 - 36200 0000 00: -
36200 0000 00 - 36240 0000 00: traces
36240 0000 00 - 3e000 0000 00: -
3e000 0000 00 - 3f000 0000 00: heap
3c000 0000 00 - 3ff00 0000 00: -
3ff00 0000 00 - 3ffff f000 00: modules and main thread stack
*/
const uptr kLoAppMemBeg   = 0x00000400000ull;
const uptr kLoAppMemEnd   = 0x01000000000ull;
const uptr kShadowBeg     = 0x10000000000ull;
const uptr kShadowEnd     = 0x20000000000ull;
const uptr kMetaShadowBeg = 0x26000000000ull;
const uptr kMetaShadowEnd = 0x28000000000ull;
const uptr kTraceMemBeg   = 0x36200000000ull;
const uptr kTraceMemEnd   = 0x36400000000ull;
const uptr kHeapMemBeg    = 0x3e000000000ull;
const uptr kHeapMemEnd    = 0x3f000000000ull;
const uptr kHiAppMemBeg   = 0x3ff00000000ull;
const uptr kHiAppMemEnd   = 0x3fffff00000ull;
const uptr kAppMemMsk     = 0x3c000000000ull;
const uptr kAppMemXor     = 0x04000000000ull;
const uptr kVdsoBeg       = 0x37f00000000ull;
# endif
#endif

ALWAYS_INLINE
bool IsAppMem(uptr mem) {
  return (mem >= kHeapMemBeg && mem < kHeapMemEnd) ||
         (mem >= kLoAppMemBeg && mem < kLoAppMemEnd) ||
         (mem >= kHiAppMemBeg && mem < kHiAppMemEnd);
}

ALWAYS_INLINE
bool IsShadowMem(uptr mem) {
  return mem >= kShadowBeg && mem <= kShadowEnd;
}

ALWAYS_INLINE
bool IsMetaMem(uptr mem) {
  return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd;
}

ALWAYS_INLINE
uptr MemToShadow(uptr x) {
  DCHECK(IsAppMem(x));
  return (((x) & ~(kAppMemMsk | (kShadowCell - 1)))
      ^ kAppMemXor) * kShadowCnt;
}

ALWAYS_INLINE
u32 *MemToMeta(uptr x) {
  DCHECK(IsAppMem(x));
  return (u32*)(((((x) & ~(kAppMemMsk | (kMetaShadowCell - 1)))
      ^ kAppMemXor) / kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg);
}

ALWAYS_INLINE
uptr ShadowToMem(uptr s) {
  CHECK(IsShadowMem(s));
  if (s >= MemToShadow(kLoAppMemBeg) && s <= MemToShadow(kLoAppMemEnd - 1))
    return (s / kShadowCnt) ^ kAppMemXor;
  else
    return ((s / kShadowCnt) ^ kAppMemXor) | kAppMemMsk;
}

static USED uptr UserRegions[] = {
  kLoAppMemBeg, kLoAppMemEnd,
  kHiAppMemBeg, kHiAppMemEnd,
  kHeapMemBeg,  kHeapMemEnd,
};

#elif defined(SANITIZER_GO) && !SANITIZER_WINDOWS

/* Go on linux, darwin and freebsd
0000 0000 1000 - 0000 1000 0000: executable
0000 1000 0000 - 00c0 0000 0000: -
00c0 0000 0000 - 00e0 0000 0000: heap
00e0 0000 0000 - 2000 0000 0000: -
2000 0000 0000 - 2380 0000 0000: shadow
2380 0000 0000 - 3000 0000 0000: -
3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
4000 0000 0000 - 6000 0000 0000: -
6000 0000 0000 - 6200 0000 0000: traces
6200 0000 0000 - 8000 0000 0000: -
*/

const uptr kMetaShadowBeg = 0x300000000000ull;
const uptr kMetaShadowEnd = 0x400000000000ull;
const uptr kTraceMemBeg   = 0x600000000000ull;
const uptr kTraceMemEnd   = 0x620000000000ull;
const uptr kShadowBeg     = 0x200000000000ull;
const uptr kShadowEnd     = 0x238000000000ull;
const uptr kAppMemBeg     = 0x000000001000ull;
const uptr kAppMemEnd     = 0x00e000000000ull;

ALWAYS_INLINE
bool IsAppMem(uptr mem) {
  return mem >= kAppMemBeg && mem < kAppMemEnd;
}

ALWAYS_INLINE
bool IsShadowMem(uptr mem) {
  return mem >= kShadowBeg && mem <= kShadowEnd;
}

ALWAYS_INLINE
bool IsMetaMem(uptr mem) {
  return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd;
}

ALWAYS_INLINE
uptr MemToShadow(uptr x) {
  DCHECK(IsAppMem(x));
  return ((x & ~(kShadowCell - 1)) * kShadowCnt) | kShadowBeg;
}

ALWAYS_INLINE
u32 *MemToMeta(uptr x) {
  DCHECK(IsAppMem(x));
  return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
      kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg);
}

ALWAYS_INLINE
uptr ShadowToMem(uptr s) {
  CHECK(IsShadowMem(s));
  return (s & ~kShadowBeg) / kShadowCnt;
}

static USED uptr UserRegions[] = {
  kAppMemBeg, kAppMemEnd,
};

#elif defined(SANITIZER_GO) && SANITIZER_WINDOWS

/* Go on windows
0000 0000 1000 - 0000 1000 0000: executable
0000 1000 0000 - 00f8 0000 0000: -
00c0 0000 0000 - 00e0 0000 0000: heap
00e0 0000 0000 - 0100 0000 0000: -
0100 0000 0000 - 0500 0000 0000: shadow
0500 0000 0000 - 0560 0000 0000: -
0560 0000 0000 - 0760 0000 0000: traces
0760 0000 0000 - 07d0 0000 0000: metainfo (memory blocks and sync objects)
07d0 0000 0000 - 8000 0000 0000: -
*/

const uptr kMetaShadowBeg = 0x076000000000ull;
const uptr kMetaShadowEnd = 0x07d000000000ull;
const uptr kTraceMemBeg   = 0x056000000000ull;
const uptr kTraceMemEnd   = 0x076000000000ull;
const uptr kShadowBeg     = 0x010000000000ull;
const uptr kShadowEnd     = 0x050000000000ull;
const uptr kAppMemBeg     = 0x000000001000ull;
const uptr kAppMemEnd     = 0x00e000000000ull;

ALWAYS_INLINE
bool IsAppMem(uptr mem) {
  return mem >= kAppMemBeg && mem < kAppMemEnd;
}

ALWAYS_INLINE
bool IsShadowMem(uptr mem) {
  return mem >= kShadowBeg && mem <= kShadowEnd;
}

ALWAYS_INLINE
bool IsMetaMem(uptr mem) {
  return mem >= kMetaShadowBeg && mem <= kMetaShadowEnd;
}

ALWAYS_INLINE
uptr MemToShadow(uptr x) {
  DCHECK(IsAppMem(x));
  return ((x & ~(kShadowCell - 1)) * kShadowCnt) + kShadowBeg;
}

ALWAYS_INLINE
u32 *MemToMeta(uptr x) {
  DCHECK(IsAppMem(x));
  return (u32*)(((x & ~(kMetaShadowCell - 1)) / \
      kMetaShadowCell * kMetaShadowSize) | kMetaShadowBeg);
}

ALWAYS_INLINE
uptr ShadowToMem(uptr s) {
  CHECK(IsShadowMem(s));
  // FIXME(dvyukov): this is most likely wrong as the mapping is not bijection.
  return (s - kShadowBeg) / kShadowCnt;
}

static USED uptr UserRegions[] = {
  kAppMemBeg, kAppMemEnd,
};

#else
# error "Unknown platform"
#endif

// The additional page is to catch shadow stack overflow as paging fault.
// Windows wants 64K alignment for mmaps.
const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
    + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);

uptr ALWAYS_INLINE GetThreadTrace(int tid) {
  uptr p = kTraceMemBeg + (uptr)tid * kTotalTraceSize;
  DCHECK_LT(p, kTraceMemEnd);
  return p;
}

uptr ALWAYS_INLINE GetThreadTraceHeader(int tid) {
  uptr p = kTraceMemBeg + (uptr)tid * kTotalTraceSize
      + kTraceSize * sizeof(Event);
  DCHECK_LT(p, kTraceMemEnd);
  return p;
}

void InitializePlatform();
void CheckAndProtect();
void InitializeShadowMemoryPlatform();
void FlushShadowMemory();
void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive);

// Says whether the addr relates to a global var.
// Guesses with high probability, may yield both false positives and negatives.
bool IsGlobalVar(uptr addr);
int ExtractResolvFDs(void *state, int *fds, int nfd);
int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);

int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
    void *abstime), void *c, void *m, void *abstime,
    void(*cleanup)(void *arg), void *arg);

void DestroyThreadState();

}  // namespace __tsan

#endif  // TSAN_PLATFORM_H