Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_platform_linux.cc --------------------------------------------===// |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | // Linux-specific code. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 15 | |
| 16 | #include "sanitizer_common/sanitizer_platform.h" |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 17 | #if SANITIZER_LINUX || SANITIZER_FREEBSD |
Dmitry Vyukov | 3f24d63 | 2012-07-16 13:25:47 +0000 | [diff] [blame] | 18 | |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | 48aee68 | 2012-06-05 06:19:00 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_procmaps.h" |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_stoptheworld.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 23 | #include "tsan_platform.h" |
| 24 | #include "tsan_rtl.h" |
| 25 | #include "tsan_flags.h" |
| 26 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <pthread.h> |
| 29 | #include <signal.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <sys/mman.h> |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 35 | #include <sys/syscall.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | #include <sys/socket.h> |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 37 | #include <sys/time.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/resource.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <unistd.h> |
| 42 | #include <errno.h> |
| 43 | #include <sched.h> |
| 44 | #include <dlfcn.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 45 | #if SANITIZER_LINUX |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 46 | #define __need_res_state |
| 47 | #include <resolv.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 48 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 49 | |
Dmitry Vyukov | 17b0a3c | 2013-10-15 13:03:06 +0000 | [diff] [blame] | 50 | #ifdef sa_handler |
| 51 | # undef sa_handler |
| 52 | #endif |
| 53 | |
| 54 | #ifdef sa_sigaction |
| 55 | # undef sa_sigaction |
| 56 | #endif |
| 57 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 58 | #if SANITIZER_FREEBSD |
| 59 | extern "C" void *__libc_stack_end; |
| 60 | void *__libc_stack_end = 0; |
| 61 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 62 | |
| 63 | namespace __tsan { |
| 64 | |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 65 | const uptr kPageSize = 4096; |
| 66 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 67 | enum { |
| 68 | MemTotal = 0, |
| 69 | MemShadow = 1, |
| 70 | MemMeta = 2, |
| 71 | MemFile = 3, |
| 72 | MemMmap = 4, |
| 73 | MemTrace = 5, |
| 74 | MemHeap = 6, |
| 75 | MemOther = 7, |
| 76 | MemCount = 8, |
| 77 | }; |
| 78 | |
Alexander Potapenko | 2e13ca8 | 2013-09-03 11:09:16 +0000 | [diff] [blame] | 79 | void FillProfileCallback(uptr start, uptr rss, bool file, |
| 80 | uptr *mem, uptr stats_size) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 81 | mem[MemTotal] += rss; |
Alexander Potapenko | 2e13ca8 | 2013-09-03 11:09:16 +0000 | [diff] [blame] | 82 | start >>= 40; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 83 | if (start < 0x10) |
| 84 | mem[MemShadow] += rss; |
| 85 | else if (start >= 0x20 && start < 0x30) |
| 86 | mem[file ? MemFile : MemMmap] += rss; |
| 87 | else if (start >= 0x30 && start < 0x40) |
| 88 | mem[MemMeta] += rss; |
| 89 | else if (start >= 0x7e) |
| 90 | mem[file ? MemFile : MemMmap] += rss; |
| 91 | else if (start >= 0x60 && start < 0x62) |
| 92 | mem[MemTrace] += rss; |
| 93 | else if (start >= 0x7d && start < 0x7e) |
| 94 | mem[MemHeap] += rss; |
| 95 | else |
| 96 | mem[MemOther] += rss; |
Dmitry Vyukov | 5d72fc7 | 2013-03-18 13:55:33 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 99 | void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) { |
| 100 | uptr mem[MemCount] = {}; |
Alexander Potapenko | 2e13ca8 | 2013-09-03 11:09:16 +0000 | [diff] [blame] | 101 | __sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 102 | internal_snprintf(buf, buf_size, |
| 103 | "RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd" |
| 104 | " trace:%zd heap:%zd other:%zd nthr=%zd/%zd\n", |
| 105 | mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, |
| 106 | mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20, |
| 107 | mem[MemHeap] >> 20, mem[MemOther] >> 20, |
| 108 | nlive, nthread); |
Dmitry Vyukov | 2612773 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 111 | uptr GetRSS() { |
| 112 | uptr mem[7] = {}; |
| 113 | __sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7); |
| 114 | return mem[6]; |
| 115 | } |
| 116 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 117 | #if SANITIZER_LINUX |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 118 | void FlushShadowMemoryCallback( |
| 119 | const SuspendedThreadsList &suspended_threads_list, |
| 120 | void *argument) { |
Kostya Serebryany | a54aec8 | 2012-12-27 07:37:24 +0000 | [diff] [blame] | 121 | FlushUnneededShadowMemory(kLinuxShadowBeg, kLinuxShadowEnd - kLinuxShadowBeg); |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 122 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 123 | #endif |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 124 | |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 125 | void FlushShadowMemory() { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 126 | #if SANITIZER_LINUX |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 127 | StopTheWorld(FlushShadowMemoryCallback, 0); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 128 | #endif |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 131 | #ifndef TSAN_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 132 | static void ProtectRange(uptr beg, uptr end) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 133 | CHECK_LE(beg, end); |
| 134 | if (beg == end) |
| 135 | return; |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 136 | if (beg != (uptr)Mprotect(beg, end - beg)) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 137 | Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end); |
| 138 | Printf("FATAL: Make sure you are not using unlimited stack\n"); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 139 | Die(); |
| 140 | } |
| 141 | } |
| 142 | |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 143 | // Mark shadow for .rodata sections with the special kShadowRodata marker. |
| 144 | // Accesses to .rodata can't race, so this saves time, memory and trace space. |
| 145 | static void MapRodata() { |
| 146 | // First create temp file. |
| 147 | const char *tmpdir = GetEnv("TMPDIR"); |
| 148 | if (tmpdir == 0) |
| 149 | tmpdir = GetEnv("TEST_TMPDIR"); |
| 150 | #ifdef P_tmpdir |
| 151 | if (tmpdir == 0) |
| 152 | tmpdir = P_tmpdir; |
| 153 | #endif |
| 154 | if (tmpdir == 0) |
| 155 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 156 | char name[256]; |
| 157 | internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d", |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 158 | tmpdir, (int)internal_getpid()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 159 | uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 160 | if (internal_iserror(openrv)) |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 161 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 162 | internal_unlink(name); // Unlink it now, so that we can reuse the buffer. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 163 | fd_t fd = openrv; |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 164 | // Fill the file with kShadowRodata. |
| 165 | const uptr kMarkerSize = 512 * 1024 / sizeof(u64); |
| 166 | InternalScopedBuffer<u64> marker(kMarkerSize); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 167 | // volatile to prevent insertion of memset |
| 168 | for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++) |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 169 | *p = kShadowRodata; |
| 170 | internal_write(fd, marker.data(), marker.size()); |
| 171 | // Map the file into memory. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 172 | uptr page = internal_mmap(0, kPageSize, PROT_READ | PROT_WRITE, |
| 173 | MAP_PRIVATE | MAP_ANONYMOUS, fd, 0); |
| 174 | if (internal_iserror(page)) { |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 175 | internal_close(fd); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | // Map the file into shadow of .rodata sections. |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 179 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 180 | uptr start, end, offset, prot; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 181 | // Reusing the buffer 'name'. |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 182 | while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), &prot)) { |
| 183 | if (name[0] != 0 && name[0] != '[' |
| 184 | && (prot & MemoryMappingLayout::kProtectionRead) |
| 185 | && (prot & MemoryMappingLayout::kProtectionExecute) |
| 186 | && !(prot & MemoryMappingLayout::kProtectionWrite) |
| 187 | && IsAppMem(start)) { |
| 188 | // Assume it's .rodata |
| 189 | char *shadow_start = (char*)MemToShadow(start); |
| 190 | char *shadow_end = (char*)MemToShadow(end); |
| 191 | for (char *p = shadow_start; p < shadow_end; p += marker.size()) { |
| 192 | internal_mmap(p, Min<uptr>(marker.size(), shadow_end - p), |
| 193 | PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | internal_close(fd); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 200 | void InitializeShadowMemory() { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 201 | // Map memory shadow. |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 202 | uptr shadow = (uptr)MmapFixedNoReserve(kLinuxShadowBeg, |
| 203 | kLinuxShadowEnd - kLinuxShadowBeg); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 204 | if (shadow != kLinuxShadowBeg) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 205 | Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n"); |
| 206 | Printf("FATAL: Make sure to compile with -fPIE and " |
Dmitry Vyukov | 2e87051 | 2012-08-15 15:35:15 +0000 | [diff] [blame] | 207 | "to link with -pie (%p, %p).\n", shadow, kLinuxShadowBeg); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 208 | Die(); |
| 209 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 210 | DPrintf("memory shadow: %zx-%zx (%zuGB)\n", |
| 211 | kLinuxShadowBeg, kLinuxShadowEnd, |
| 212 | (kLinuxShadowEnd - kLinuxShadowBeg) >> 30); |
| 213 | |
| 214 | // Map meta shadow. |
| 215 | if (MemToMeta(kLinuxAppMemBeg) < (u32*)kMetaShadow) { |
| 216 | Printf("ThreadSanitizer: bad meta shadow (%p -> %p < %p)\n", |
| 217 | kLinuxAppMemBeg, MemToMeta(kLinuxAppMemBeg), kMetaShadow); |
| 218 | Die(); |
| 219 | } |
| 220 | if (MemToMeta(kLinuxAppMemEnd) >= (u32*)(kMetaShadow + kMetaSize)) { |
| 221 | Printf("ThreadSanitizer: bad meta shadow (%p -> %p >= %p)\n", |
| 222 | kLinuxAppMemEnd, MemToMeta(kLinuxAppMemEnd), kMetaShadow + kMetaSize); |
| 223 | Die(); |
| 224 | } |
| 225 | uptr meta = (uptr)MmapFixedNoReserve(kMetaShadow, kMetaSize); |
| 226 | if (meta != kMetaShadow) { |
| 227 | Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n"); |
| 228 | Printf("FATAL: Make sure to compile with -fPIE and " |
| 229 | "to link with -pie (%p, %p).\n", meta, kMetaShadow); |
| 230 | Die(); |
| 231 | } |
| 232 | DPrintf("meta shadow: %zx-%zx (%zuGB)\n", |
| 233 | kMetaShadow, kMetaShadow + kMetaSize, kMetaSize >> 30); |
| 234 | |
| 235 | // Protect gaps. |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 236 | const uptr kClosedLowBeg = 0x200000; |
| 237 | const uptr kClosedLowEnd = kLinuxShadowBeg - 1; |
| 238 | const uptr kClosedMidBeg = kLinuxShadowEnd + 1; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 239 | const uptr kClosedMidEnd = min(min(kLinuxAppMemBeg, kTraceMemBegin), |
| 240 | kMetaShadow); |
| 241 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 242 | ProtectRange(kClosedLowBeg, kClosedLowEnd); |
| 243 | ProtectRange(kClosedMidBeg, kClosedMidEnd); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 244 | VPrintf(2, "kClosedLow %zx-%zx (%zuGB)\n", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 245 | kClosedLowBeg, kClosedLowEnd, (kClosedLowEnd - kClosedLowBeg) >> 30); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 246 | VPrintf(2, "kClosedMid %zx-%zx (%zuGB)\n", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 247 | kClosedMidBeg, kClosedMidEnd, (kClosedMidEnd - kClosedMidBeg) >> 30); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 248 | VPrintf(2, "app mem: %zx-%zx (%zuGB)\n", |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 249 | kLinuxAppMemBeg, kLinuxAppMemEnd, |
| 250 | (kLinuxAppMemEnd - kLinuxAppMemBeg) >> 30); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 251 | VPrintf(2, "stack: %zx\n", (uptr)&shadow); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 252 | |
| 253 | MapRodata(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 254 | } |
Dmitry Vyukov | a05fcc1 | 2012-11-06 16:00:16 +0000 | [diff] [blame] | 255 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 256 | |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 257 | static uptr g_data_start; |
| 258 | static uptr g_data_end; |
| 259 | |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 260 | #ifndef TSAN_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 261 | static void CheckPIE() { |
| 262 | // Ensure that the binary is indeed compiled with -pie. |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 263 | MemoryMappingLayout proc_maps(true); |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 264 | uptr start, end; |
| 265 | if (proc_maps.Next(&start, &end, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 266 | /*offset*/0, /*filename*/0, /*filename_size*/0, |
| 267 | /*protection*/0)) { |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 268 | if ((u64)start < kLinuxAppMemBeg) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 269 | Printf("FATAL: ThreadSanitizer can not mmap the shadow memory (" |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 270 | "something is mapped at 0x%zx < 0x%zx)\n", |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 271 | start, kLinuxAppMemBeg); |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 272 | Printf("FATAL: Make sure to compile with -fPIE" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 273 | " and to link with -pie.\n"); |
| 274 | Die(); |
| 275 | } |
| 276 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 279 | static void InitDataSeg() { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 280 | MemoryMappingLayout proc_maps(true); |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 281 | uptr start, end, offset; |
| 282 | char name[128]; |
| 283 | bool prev_is_data = false; |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 284 | while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), |
| 285 | /*protection*/ 0)) { |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 286 | DPrintf("%p-%p %p %s\n", start, end, offset, name); |
| 287 | bool is_data = offset != 0 && name[0] != 0; |
Evgeniy Stepanov | 97dac9e | 2012-09-27 13:20:40 +0000 | [diff] [blame] | 288 | // BSS may get merged with [heap] in /proc/self/maps. This is not very |
| 289 | // reliable. |
| 290 | bool is_bss = offset == 0 && |
| 291 | (name[0] == 0 || internal_strcmp(name, "[heap]") == 0) && prev_is_data; |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 292 | if (g_data_start == 0 && is_data) |
| 293 | g_data_start = start; |
| 294 | if (is_bss) |
| 295 | g_data_end = end; |
| 296 | prev_is_data = is_data; |
| 297 | } |
| 298 | DPrintf("guessed data_start=%p data_end=%p\n", g_data_start, g_data_end); |
| 299 | CHECK_LT(g_data_start, g_data_end); |
| 300 | CHECK_GE((uptr)&g_data_start, g_data_start); |
| 301 | CHECK_LT((uptr)&g_data_start, g_data_end); |
| 302 | } |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 303 | |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 304 | #endif // #ifndef TSAN_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 305 | |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 306 | static rlim_t getlim(int res) { |
| 307 | rlimit rlim; |
| 308 | CHECK_EQ(0, getrlimit(res, &rlim)); |
| 309 | return rlim.rlim_cur; |
| 310 | } |
| 311 | |
| 312 | static void setlim(int res, rlim_t lim) { |
| 313 | // The following magic is to prevent clang from replacing it with memset. |
| 314 | volatile rlimit rlim; |
| 315 | rlim.rlim_cur = lim; |
| 316 | rlim.rlim_max = lim; |
| 317 | setrlimit(res, (rlimit*)&rlim); |
| 318 | } |
| 319 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 320 | const char *InitializePlatform() { |
| 321 | void *p = 0; |
| 322 | if (sizeof(p) == 8) { |
| 323 | // Disable core dumps, dumping of 16TB usually takes a bit long. |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 324 | setlim(RLIMIT_CORE, 0); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 327 | // Go maps shadow memory lazily and works fine with limited address space. |
| 328 | // Unlimited stack is not a problem as well, because the executable |
| 329 | // is not compiled with -pie. |
| 330 | if (kCppMode) { |
| 331 | bool reexec = false; |
| 332 | // TSan doesn't play well with unlimited stack size (as stack |
| 333 | // overlaps with shadow memory). If we detect unlimited stack size, |
| 334 | // we re-exec the program with limited stack size as a best effort. |
| 335 | if (getlim(RLIMIT_STACK) == (rlim_t)-1) { |
| 336 | const uptr kMaxStackSize = 32 * 1024 * 1024; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 337 | VReport(1, "Program is run with unlimited stack size, which wouldn't " |
| 338 | "work with ThreadSanitizer.\n" |
| 339 | "Re-execing with stack size limited to %zd bytes.\n", |
| 340 | kMaxStackSize); |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 341 | SetStackSizeLimitInBytes(kMaxStackSize); |
| 342 | reexec = true; |
| 343 | } |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 344 | |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 345 | if (getlim(RLIMIT_AS) != (rlim_t)-1) { |
Dmitry Vyukov | 4dc3082 | 2012-12-21 10:47:48 +0000 | [diff] [blame] | 346 | Report("WARNING: Program is run with limited virtual address space," |
| 347 | " which wouldn't work with ThreadSanitizer.\n"); |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 348 | Report("Re-execing with unlimited virtual address space.\n"); |
| 349 | setlim(RLIMIT_AS, -1); |
| 350 | reexec = true; |
| 351 | } |
| 352 | if (reexec) |
| 353 | ReExec(); |
| 354 | } |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 355 | |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 356 | #ifndef TSAN_GO |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 357 | CheckPIE(); |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 358 | InitTlsSize(); |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 359 | InitDataSeg(); |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 360 | #endif |
Kostya Serebryany | ce44055 | 2013-02-19 08:19:13 +0000 | [diff] [blame] | 361 | return GetEnv(kTsanOptionsEnv); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 364 | bool IsGlobalVar(uptr addr) { |
| 365 | return g_data_start && addr >= g_data_start && addr < g_data_end; |
| 366 | } |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 367 | |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 368 | #ifndef TSAN_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 369 | // Extract file descriptors passed to glibc internal __res_iclose function. |
| 370 | // This is required to properly "close" the fds, because we do not see internal |
| 371 | // closes within glibc. The code is a pure hack. |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 372 | int ExtractResolvFDs(void *state, int *fds, int nfd) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 373 | #if SANITIZER_LINUX |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 374 | int cnt = 0; |
| 375 | __res_state *statp = (__res_state*)state; |
| 376 | for (int i = 0; i < MAXNS && cnt < nfd; i++) { |
| 377 | if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1) |
| 378 | fds[cnt++] = statp->_u._ext.nssocks[i]; |
| 379 | } |
| 380 | return cnt; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame^] | 381 | #else |
| 382 | return 0; |
| 383 | #endif |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 384 | } |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 385 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 386 | // Extract file descriptors passed via UNIX domain sockets. |
| 387 | // This is requried to properly handle "open" of these fds. |
| 388 | // see 'man recvmsg' and 'man 3 cmsg'. |
| 389 | int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) { |
| 390 | int res = 0; |
| 391 | msghdr *msg = (msghdr*)msgp; |
| 392 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); |
| 393 | for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 394 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) |
| 395 | continue; |
| 396 | int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]); |
| 397 | for (int i = 0; i < n; i++) { |
| 398 | fds[res++] = ((int*)CMSG_DATA(cmsg))[i]; |
| 399 | if (res == nfd) |
| 400 | return res; |
| 401 | } |
| 402 | } |
| 403 | return res; |
| 404 | } |
| 405 | |
| 406 | int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m, |
| 407 | void *abstime), void *c, void *m, void *abstime, |
| 408 | void(*cleanup)(void *arg), void *arg) { |
| 409 | // pthread_cleanup_push/pop are hardcore macros mess. |
| 410 | // We can't intercept nor call them w/o including pthread.h. |
| 411 | int res; |
| 412 | pthread_cleanup_push(cleanup, arg); |
| 413 | res = fn(c, m, abstime); |
| 414 | pthread_cleanup_pop(0); |
| 415 | return res; |
| 416 | } |
| 417 | #endif |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 418 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 419 | } // namespace __tsan |
Dmitry Vyukov | 3f24d63 | 2012-07-16 13:25:47 +0000 | [diff] [blame] | 420 | |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 421 | #endif // SANITIZER_LINUX |