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 | // |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 12 | // Linux- and FreeBSD-specific code. |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 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" |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_posix.h" |
Alexey Samsonov | 84902c7 | 2012-06-18 09:42:39 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_procmaps.h" |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 23 | #include "sanitizer_common/sanitizer_stoptheworld.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_stackdepot.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 25 | #include "tsan_platform.h" |
| 26 | #include "tsan_rtl.h" |
| 27 | #include "tsan_flags.h" |
| 28 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <pthread.h> |
| 31 | #include <signal.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <sys/mman.h> |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 37 | #include <sys/syscall.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 38 | #include <sys/socket.h> |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 39 | #include <sys/time.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/resource.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <unistd.h> |
| 44 | #include <errno.h> |
| 45 | #include <sched.h> |
| 46 | #include <dlfcn.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 47 | #if SANITIZER_LINUX |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 48 | #define __need_res_state |
| 49 | #include <resolv.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 50 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 51 | |
Dmitry Vyukov | 17b0a3c | 2013-10-15 13:03:06 +0000 | [diff] [blame] | 52 | #ifdef sa_handler |
| 53 | # undef sa_handler |
| 54 | #endif |
| 55 | |
| 56 | #ifdef sa_sigaction |
| 57 | # undef sa_sigaction |
| 58 | #endif |
| 59 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 60 | #if SANITIZER_FREEBSD |
| 61 | extern "C" void *__libc_stack_end; |
| 62 | void *__libc_stack_end = 0; |
| 63 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 64 | |
| 65 | namespace __tsan { |
| 66 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 67 | static uptr g_data_start; |
| 68 | static uptr g_data_end; |
| 69 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 70 | #ifdef TSAN_RUNTIME_VMA |
| 71 | // Runtime detected VMA size. |
| 72 | uptr vmaSize; |
| 73 | #endif |
| 74 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 75 | enum { |
| 76 | MemTotal = 0, |
| 77 | MemShadow = 1, |
| 78 | MemMeta = 2, |
| 79 | MemFile = 3, |
| 80 | MemMmap = 4, |
| 81 | MemTrace = 5, |
| 82 | MemHeap = 6, |
| 83 | MemOther = 7, |
| 84 | MemCount = 8, |
| 85 | }; |
| 86 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 87 | void FillProfileCallback(uptr p, uptr rss, bool file, |
Alexander Potapenko | 2e13ca8 | 2013-09-03 11:09:16 +0000 | [diff] [blame] | 88 | uptr *mem, uptr stats_size) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 89 | mem[MemTotal] += rss; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 90 | if (p >= ShadowBeg() && p < ShadowEnd()) |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 91 | mem[MemShadow] += rss; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 92 | else if (p >= MetaShadowBeg() && p < MetaShadowEnd()) |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 93 | mem[MemMeta] += rss; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 94 | #ifndef SANITIZER_GO |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 95 | else if (p >= HeapMemBeg() && p < HeapMemEnd()) |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 96 | mem[MemHeap] += rss; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 97 | else if (p >= LoAppMemBeg() && p < LoAppMemEnd()) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 98 | mem[file ? MemFile : MemMmap] += rss; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 99 | else if (p >= HiAppMemBeg() && p < HiAppMemEnd()) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 100 | mem[file ? MemFile : MemMmap] += rss; |
| 101 | #else |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 102 | else if (p >= AppMemBeg() && p < AppMemEnd()) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 103 | mem[file ? MemFile : MemMmap] += rss; |
| 104 | #endif |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 105 | else if (p >= TraceMemBeg() && p < TraceMemEnd()) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 106 | mem[MemTrace] += rss; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 107 | else |
| 108 | mem[MemOther] += rss; |
Dmitry Vyukov | 5d72fc7 | 2013-03-18 13:55:33 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 111 | void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 112 | uptr mem[MemCount]; |
| 113 | internal_memset(mem, 0, sizeof(mem[0]) * MemCount); |
Alexander Potapenko | 2e13ca8 | 2013-09-03 11:09:16 +0000 | [diff] [blame] | 114 | __sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 115 | StackDepotStats *stacks = StackDepotGetStats(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 116 | internal_snprintf(buf, buf_size, |
| 117 | "RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 118 | " trace:%zd heap:%zd other:%zd stacks=%zd[%zd] nthr=%zd/%zd\n", |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 119 | mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, |
| 120 | mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20, |
| 121 | mem[MemHeap] >> 20, mem[MemOther] >> 20, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 122 | stacks->allocated >> 20, stacks->n_uniq_ids, |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 123 | nlive, nthread); |
Dmitry Vyukov | 2612773 | 2012-05-22 11:33:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
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 | void FlushShadowMemoryCallback( |
| 128 | const SuspendedThreadsList &suspended_threads_list, |
| 129 | void *argument) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 130 | FlushUnneededShadowMemory(ShadowBeg(), ShadowEnd() - ShadowBeg()); |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 131 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 132 | #endif |
Dmitry Vyukov | adfb650 | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 133 | |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 134 | void FlushShadowMemory() { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 135 | #if SANITIZER_LINUX |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 136 | StopTheWorld(FlushShadowMemoryCallback, 0); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 137 | #endif |
Dmitry Vyukov | 92b5479 | 2013-10-03 17:14:35 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 140 | #ifndef SANITIZER_GO |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 141 | // Mark shadow for .rodata sections with the special kShadowRodata marker. |
| 142 | // Accesses to .rodata can't race, so this saves time, memory and trace space. |
| 143 | static void MapRodata() { |
| 144 | // First create temp file. |
| 145 | const char *tmpdir = GetEnv("TMPDIR"); |
| 146 | if (tmpdir == 0) |
| 147 | tmpdir = GetEnv("TEST_TMPDIR"); |
| 148 | #ifdef P_tmpdir |
| 149 | if (tmpdir == 0) |
| 150 | tmpdir = P_tmpdir; |
| 151 | #endif |
| 152 | if (tmpdir == 0) |
| 153 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 154 | char name[256]; |
| 155 | internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d", |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 156 | tmpdir, (int)internal_getpid()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 157 | uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 158 | if (internal_iserror(openrv)) |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 159 | return; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 160 | 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] | 161 | fd_t fd = openrv; |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 162 | // Fill the file with kShadowRodata. |
| 163 | const uptr kMarkerSize = 512 * 1024 / sizeof(u64); |
| 164 | InternalScopedBuffer<u64> marker(kMarkerSize); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 165 | // volatile to prevent insertion of memset |
| 166 | for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++) |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 167 | *p = kShadowRodata; |
| 168 | internal_write(fd, marker.data(), marker.size()); |
| 169 | // Map the file into memory. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 170 | uptr page = internal_mmap(0, GetPageSizeCached(), PROT_READ | PROT_WRITE, |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 171 | MAP_PRIVATE | MAP_ANONYMOUS, fd, 0); |
| 172 | if (internal_iserror(page)) { |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 173 | internal_close(fd); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | // Map the file into shadow of .rodata sections. |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 177 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 178 | uptr start, end, offset, prot; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 179 | // Reusing the buffer 'name'. |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 180 | while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), &prot)) { |
| 181 | if (name[0] != 0 && name[0] != '[' |
| 182 | && (prot & MemoryMappingLayout::kProtectionRead) |
| 183 | && (prot & MemoryMappingLayout::kProtectionExecute) |
| 184 | && !(prot & MemoryMappingLayout::kProtectionWrite) |
| 185 | && IsAppMem(start)) { |
| 186 | // Assume it's .rodata |
| 187 | char *shadow_start = (char*)MemToShadow(start); |
| 188 | char *shadow_end = (char*)MemToShadow(end); |
| 189 | for (char *p = shadow_start; p < shadow_end; p += marker.size()) { |
| 190 | internal_mmap(p, Min<uptr>(marker.size(), shadow_end - p), |
| 191 | PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | internal_close(fd); |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 198 | void InitializeShadowMemoryPlatform() { |
Dmitry Vyukov | 82dbc51 | 2013-03-20 13:21:50 +0000 | [diff] [blame] | 199 | MapRodata(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 200 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 201 | |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 202 | static void InitDataSeg() { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 203 | MemoryMappingLayout proc_maps(true); |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 204 | uptr start, end, offset; |
| 205 | char name[128]; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 206 | #if SANITIZER_FREEBSD |
| 207 | // On FreeBSD BSS is usually the last block allocated within the |
| 208 | // low range and heap is the last block allocated within the range |
| 209 | // 0x800000000-0x8ffffffff. |
| 210 | while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), |
| 211 | /*protection*/ 0)) { |
| 212 | DPrintf("%p-%p %p %s\n", start, end, offset, name); |
| 213 | if ((start & 0xffff00000000ULL) == 0 && (end & 0xffff00000000ULL) == 0 && |
| 214 | name[0] == '\0') { |
| 215 | g_data_start = start; |
| 216 | g_data_end = end; |
| 217 | } |
| 218 | } |
| 219 | #else |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 220 | bool prev_is_data = false; |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 221 | while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), |
| 222 | /*protection*/ 0)) { |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 223 | DPrintf("%p-%p %p %s\n", start, end, offset, name); |
| 224 | bool is_data = offset != 0 && name[0] != 0; |
Evgeniy Stepanov | 97dac9e | 2012-09-27 13:20:40 +0000 | [diff] [blame] | 225 | // BSS may get merged with [heap] in /proc/self/maps. This is not very |
| 226 | // reliable. |
| 227 | bool is_bss = offset == 0 && |
| 228 | (name[0] == 0 || internal_strcmp(name, "[heap]") == 0) && prev_is_data; |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 229 | if (g_data_start == 0 && is_data) |
| 230 | g_data_start = start; |
| 231 | if (is_bss) |
| 232 | g_data_end = end; |
| 233 | prev_is_data = is_data; |
| 234 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 235 | #endif |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 236 | DPrintf("guessed data_start=%p data_end=%p\n", g_data_start, g_data_end); |
| 237 | CHECK_LT(g_data_start, g_data_end); |
| 238 | CHECK_GE((uptr)&g_data_start, g_data_start); |
| 239 | CHECK_LT((uptr)&g_data_start, g_data_end); |
| 240 | } |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 241 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 242 | #endif // #ifndef SANITIZER_GO |
| 243 | |
| 244 | void InitializePlatformEarly() { |
| 245 | #ifdef TSAN_RUNTIME_VMA |
| 246 | vmaSize = |
| 247 | (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1); |
| 248 | #if defined(__aarch64__) |
| 249 | if (vmaSize != 39 && vmaSize != 42) { |
| 250 | Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); |
| 251 | Printf("FATAL: Found %d - Supported 39 and 42\n", vmaSize); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 252 | Die(); |
| 253 | } |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 254 | #elif defined(__powerpc64__) |
| 255 | if (vmaSize != 44 && vmaSize != 46) { |
| 256 | Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); |
| 257 | Printf("FATAL: Found %d - Supported 44 and 46\n", vmaSize); |
| 258 | Die(); |
| 259 | } |
| 260 | #endif |
| 261 | #endif |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 262 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 263 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 264 | void InitializePlatform() { |
| 265 | DisableCoreDumperIfNecessary(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 266 | |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 267 | // Go maps shadow memory lazily and works fine with limited address space. |
| 268 | // Unlimited stack is not a problem as well, because the executable |
| 269 | // is not compiled with -pie. |
| 270 | if (kCppMode) { |
| 271 | bool reexec = false; |
| 272 | // TSan doesn't play well with unlimited stack size (as stack |
| 273 | // overlaps with shadow memory). If we detect unlimited stack size, |
| 274 | // we re-exec the program with limited stack size as a best effort. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 275 | if (StackSizeIsUnlimited()) { |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 276 | const uptr kMaxStackSize = 32 * 1024 * 1024; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 277 | VReport(1, "Program is run with unlimited stack size, which wouldn't " |
| 278 | "work with ThreadSanitizer.\n" |
| 279 | "Re-execing with stack size limited to %zd bytes.\n", |
| 280 | kMaxStackSize); |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 281 | SetStackSizeLimitInBytes(kMaxStackSize); |
| 282 | reexec = true; |
| 283 | } |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 284 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 285 | if (!AddressSpaceIsUnlimited()) { |
Dmitry Vyukov | 4dc3082 | 2012-12-21 10:47:48 +0000 | [diff] [blame] | 286 | Report("WARNING: Program is run with limited virtual address space," |
| 287 | " which wouldn't work with ThreadSanitizer.\n"); |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 288 | Report("Re-execing with unlimited virtual address space.\n"); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 289 | SetAddressSpaceUnlimited(); |
Dmitry Vyukov | 4de5864 | 2012-12-21 10:45:01 +0000 | [diff] [blame] | 290 | reexec = true; |
| 291 | } |
| 292 | if (reexec) |
| 293 | ReExec(); |
| 294 | } |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 295 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 296 | #ifndef SANITIZER_GO |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 297 | CheckAndProtect(); |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 298 | InitTlsSize(); |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 299 | InitDataSeg(); |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 300 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Dmitry Vyukov | 0c2feef | 2012-09-07 18:08:02 +0000 | [diff] [blame] | 303 | bool IsGlobalVar(uptr addr) { |
| 304 | return g_data_start && addr >= g_data_start && addr < g_data_end; |
| 305 | } |
Dmitry Vyukov | b78caa6 | 2012-07-05 16:18:28 +0000 | [diff] [blame] | 306 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 307 | #ifndef SANITIZER_GO |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 308 | // Extract file descriptors passed to glibc internal __res_iclose function. |
| 309 | // This is required to properly "close" the fds, because we do not see internal |
| 310 | // closes within glibc. The code is a pure hack. |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 311 | int ExtractResolvFDs(void *state, int *fds, int nfd) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 312 | #if SANITIZER_LINUX && !SANITIZER_ANDROID |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 313 | int cnt = 0; |
| 314 | __res_state *statp = (__res_state*)state; |
| 315 | for (int i = 0; i < MAXNS && cnt < nfd; i++) { |
| 316 | if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1) |
| 317 | fds[cnt++] = statp->_u._ext.nssocks[i]; |
| 318 | } |
| 319 | return cnt; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 320 | #else |
| 321 | return 0; |
| 322 | #endif |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 323 | } |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 324 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 325 | // Extract file descriptors passed via UNIX domain sockets. |
| 326 | // This is requried to properly handle "open" of these fds. |
| 327 | // see 'man recvmsg' and 'man 3 cmsg'. |
| 328 | int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) { |
| 329 | int res = 0; |
| 330 | msghdr *msg = (msghdr*)msgp; |
| 331 | struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); |
| 332 | for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 333 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) |
| 334 | continue; |
| 335 | int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]); |
| 336 | for (int i = 0; i < n; i++) { |
| 337 | fds[res++] = ((int*)CMSG_DATA(cmsg))[i]; |
| 338 | if (res == nfd) |
| 339 | return res; |
| 340 | } |
| 341 | } |
| 342 | return res; |
| 343 | } |
| 344 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 345 | // Note: this function runs with async signals enabled, |
| 346 | // so it must not touch any tsan state. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 347 | int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m, |
| 348 | void *abstime), void *c, void *m, void *abstime, |
| 349 | void(*cleanup)(void *arg), void *arg) { |
| 350 | // pthread_cleanup_push/pop are hardcore macros mess. |
| 351 | // We can't intercept nor call them w/o including pthread.h. |
| 352 | int res; |
| 353 | pthread_cleanup_push(cleanup, arg); |
| 354 | res = fn(c, m, abstime); |
| 355 | pthread_cleanup_pop(0); |
| 356 | return res; |
| 357 | } |
| 358 | #endif |
Dmitry Vyukov | 03f2248 | 2013-02-07 15:27:45 +0000 | [diff] [blame] | 359 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 360 | #ifndef SANITIZER_GO |
| 361 | void ReplaceSystemMalloc() { } |
| 362 | #endif |
| 363 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 364 | } // namespace __tsan |
Dmitry Vyukov | 3f24d63 | 2012-07-16 13:25:47 +0000 | [diff] [blame] | 365 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 366 | #endif // SANITIZER_LINUX || SANITIZER_FREEBSD |