2017-01-22 Matthias Klose <doko@ubuntu.com>
[official-gcc.git] / libsanitizer / tsan / tsan_platform_posix.cc
blob0a3b61a08dc4a0c19e19f89fc424658e21c55c7f
1 //===-- tsan_platform_posix.cc --------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 // POSIX-specific code.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_POSIX
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "sanitizer_common/sanitizer_procmaps.h"
19 #include "tsan_platform.h"
20 #include "tsan_rtl.h"
22 namespace __tsan {
24 #if !SANITIZER_GO
25 void InitializeShadowMemory() {
26 // Map memory shadow.
27 uptr shadow =
28 (uptr)MmapFixedNoReserve(ShadowBeg(), ShadowEnd() - ShadowBeg(),
29 "shadow");
30 if (shadow != ShadowBeg()) {
31 Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
32 Printf("FATAL: Make sure to compile with -fPIE and "
33 "to link with -pie (%p, %p).\n", shadow, ShadowBeg());
34 Die();
36 // This memory range is used for thread stacks and large user mmaps.
37 // Frequently a thread uses only a small part of stack and similarly
38 // a program uses a small part of large mmap. On some programs
39 // we see 20% memory usage reduction without huge pages for this range.
40 // FIXME: don't use constants here.
41 #if defined(__x86_64__)
42 const uptr kMadviseRangeBeg = 0x7f0000000000ull;
43 const uptr kMadviseRangeSize = 0x010000000000ull;
44 #elif defined(__mips64)
45 const uptr kMadviseRangeBeg = 0xff00000000ull;
46 const uptr kMadviseRangeSize = 0x0100000000ull;
47 #elif defined(__aarch64__)
48 uptr kMadviseRangeBeg = 0;
49 uptr kMadviseRangeSize = 0;
50 if (vmaSize == 39) {
51 kMadviseRangeBeg = 0x7d00000000ull;
52 kMadviseRangeSize = 0x0300000000ull;
53 } else if (vmaSize == 42) {
54 kMadviseRangeBeg = 0x3f000000000ull;
55 kMadviseRangeSize = 0x01000000000ull;
56 } else {
57 DCHECK(0);
59 #elif defined(__powerpc64__)
60 uptr kMadviseRangeBeg = 0;
61 uptr kMadviseRangeSize = 0;
62 if (vmaSize == 44) {
63 kMadviseRangeBeg = 0x0f60000000ull;
64 kMadviseRangeSize = 0x0010000000ull;
65 } else if (vmaSize == 46) {
66 kMadviseRangeBeg = 0x3f0000000000ull;
67 kMadviseRangeSize = 0x010000000000ull;
68 } else {
69 DCHECK(0);
71 #endif
72 NoHugePagesInRegion(MemToShadow(kMadviseRangeBeg),
73 kMadviseRangeSize * kShadowMultiplier);
74 // Meta shadow is compressing and we don't flush it,
75 // so it makes sense to mark it as NOHUGEPAGE to not over-allocate memory.
76 // On one program it reduces memory consumption from 5GB to 2.5GB.
77 NoHugePagesInRegion(MetaShadowBeg(), MetaShadowEnd() - MetaShadowBeg());
78 if (common_flags()->use_madv_dontdump)
79 DontDumpShadowMemory(ShadowBeg(), ShadowEnd() - ShadowBeg());
80 DPrintf("memory shadow: %zx-%zx (%zuGB)\n",
81 ShadowBeg(), ShadowEnd(),
82 (ShadowEnd() - ShadowBeg()) >> 30);
84 // Map meta shadow.
85 uptr meta_size = MetaShadowEnd() - MetaShadowBeg();
86 uptr meta =
87 (uptr)MmapFixedNoReserve(MetaShadowBeg(), meta_size, "meta shadow");
88 if (meta != MetaShadowBeg()) {
89 Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
90 Printf("FATAL: Make sure to compile with -fPIE and "
91 "to link with -pie (%p, %p).\n", meta, MetaShadowBeg());
92 Die();
94 if (common_flags()->use_madv_dontdump)
95 DontDumpShadowMemory(meta, meta_size);
96 DPrintf("meta shadow: %zx-%zx (%zuGB)\n",
97 meta, meta + meta_size, meta_size >> 30);
99 InitializeShadowMemoryPlatform();
102 static void ProtectRange(uptr beg, uptr end) {
103 CHECK_LE(beg, end);
104 if (beg == end)
105 return;
106 if (beg != (uptr)MmapFixedNoAccess(beg, end - beg)) {
107 Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
108 Printf("FATAL: Make sure you are not using unlimited stack\n");
109 Die();
113 void CheckAndProtect() {
114 // Ensure that the binary is indeed compiled with -pie.
115 MemoryMappingLayout proc_maps(true);
116 uptr p, end, prot;
117 while (proc_maps.Next(&p, &end, 0, 0, 0, &prot)) {
118 if (IsAppMem(p))
119 continue;
120 if (p >= HeapMemEnd() &&
121 p < HeapEnd())
122 continue;
123 if (prot == 0) // Zero page or mprotected.
124 continue;
125 if (p >= VdsoBeg()) // vdso
126 break;
127 Printf("FATAL: ThreadSanitizer: unexpected memory mapping %p-%p\n", p, end);
128 Die();
131 ProtectRange(LoAppMemEnd(), ShadowBeg());
132 ProtectRange(ShadowEnd(), MetaShadowBeg());
133 #ifdef TSAN_MID_APP_RANGE
134 ProtectRange(MetaShadowEnd(), MidAppMemBeg());
135 ProtectRange(MidAppMemEnd(), TraceMemBeg());
136 #else
137 ProtectRange(MetaShadowEnd(), TraceMemBeg());
138 #endif
139 // Memory for traces is mapped lazily in MapThreadTrace.
140 // Protect the whole range for now, so that user does not map something here.
141 ProtectRange(TraceMemBeg(), TraceMemEnd());
142 ProtectRange(TraceMemEnd(), HeapMemBeg());
143 ProtectRange(HeapEnd(), HiAppMemBeg());
145 #endif
147 } // namespace __tsan
149 #endif // SANITIZER_POSIX