blob: 0359f5e261b526303a723f61efa135f9826634a2 [file] [log] [blame]
Alexey Samsonov603c4be2012-06-04 13:55:19 +00001//===-- tsan_platform_linux.cc --------------------------------------------===//
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002//
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 Hines6d186232014-11-26 17:56:19 -080012// Linux- and FreeBSD-specific code.
Kostya Serebryany7ac41482012-05-10 13:48:04 +000013//===----------------------------------------------------------------------===//
14
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000015
16#include "sanitizer_common/sanitizer_platform.h"
Stephen Hines6a211c52014-07-21 00:49:56 -070017#if SANITIZER_LINUX || SANITIZER_FREEBSD
Dmitry Vyukov3f24d632012-07-16 13:25:47 +000018
Alexey Samsonov84902c72012-06-18 09:42:39 +000019#include "sanitizer_common/sanitizer_common.h"
Alexey Samsonov48aee682012-06-05 06:19:00 +000020#include "sanitizer_common/sanitizer_libc.h"
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070021#include "sanitizer_common/sanitizer_posix.h"
Alexey Samsonov84902c72012-06-18 09:42:39 +000022#include "sanitizer_common/sanitizer_procmaps.h"
Dmitry Vyukov92b54792013-10-03 17:14:35 +000023#include "sanitizer_common/sanitizer_stoptheworld.h"
Stephen Hines6d186232014-11-26 17:56:19 -080024#include "sanitizer_common/sanitizer_stackdepot.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000025#include "tsan_platform.h"
26#include "tsan_rtl.h"
27#include "tsan_flags.h"
28
Kostya Serebryany7ac41482012-05-10 13:48:04 +000029#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 Serebryany7ac41482012-05-10 13:48:04 +000037#include <sys/syscall.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070038#include <sys/socket.h>
Kostya Serebryany7ac41482012-05-10 13:48:04 +000039#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 Hines6a211c52014-07-21 00:49:56 -070047#if SANITIZER_LINUX
Dmitry Vyukov03f22482013-02-07 15:27:45 +000048#define __need_res_state
49#include <resolv.h>
Stephen Hines6a211c52014-07-21 00:49:56 -070050#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +000051
Dmitry Vyukov17b0a3c2013-10-15 13:03:06 +000052#ifdef sa_handler
53# undef sa_handler
54#endif
55
56#ifdef sa_sigaction
57# undef sa_sigaction
58#endif
59
Stephen Hines6a211c52014-07-21 00:49:56 -070060#if SANITIZER_FREEBSD
61extern "C" void *__libc_stack_end;
62void *__libc_stack_end = 0;
63#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +000064
65namespace __tsan {
66
Stephen Hines6d186232014-11-26 17:56:19 -080067static uptr g_data_start;
68static uptr g_data_end;
69
Stephen Hines6a211c52014-07-21 00:49:56 -070070enum {
71 MemTotal = 0,
72 MemShadow = 1,
73 MemMeta = 2,
74 MemFile = 3,
75 MemMmap = 4,
76 MemTrace = 5,
77 MemHeap = 6,
78 MemOther = 7,
79 MemCount = 8,
80};
81
Stephen Hines6d186232014-11-26 17:56:19 -080082void FillProfileCallback(uptr p, uptr rss, bool file,
Alexander Potapenko2e13ca82013-09-03 11:09:16 +000083 uptr *mem, uptr stats_size) {
Stephen Hines6a211c52014-07-21 00:49:56 -070084 mem[MemTotal] += rss;
Stephen Hines6d186232014-11-26 17:56:19 -080085 if (p >= kShadowBeg && p < kShadowEnd)
Stephen Hines6a211c52014-07-21 00:49:56 -070086 mem[MemShadow] += rss;
Stephen Hines6d186232014-11-26 17:56:19 -080087 else if (p >= kMetaShadowBeg && p < kMetaShadowEnd)
Stephen Hines6a211c52014-07-21 00:49:56 -070088 mem[MemMeta] += rss;
Stephen Hines86277eb2015-03-23 12:06:32 -070089#ifndef SANITIZER_GO
Stephen Hines6d186232014-11-26 17:56:19 -080090 else if (p >= kHeapMemBeg && p < kHeapMemEnd)
Stephen Hines6a211c52014-07-21 00:49:56 -070091 mem[MemHeap] += rss;
Stephen Hines6d186232014-11-26 17:56:19 -080092 else if (p >= kLoAppMemBeg && p < kLoAppMemEnd)
93 mem[file ? MemFile : MemMmap] += rss;
94 else if (p >= kHiAppMemBeg && p < kHiAppMemEnd)
95 mem[file ? MemFile : MemMmap] += rss;
96#else
97 else if (p >= kAppMemBeg && p < kAppMemEnd)
98 mem[file ? MemFile : MemMmap] += rss;
99#endif
100 else if (p >= kTraceMemBeg && p < kTraceMemEnd)
101 mem[MemTrace] += rss;
Stephen Hines6a211c52014-07-21 00:49:56 -0700102 else
103 mem[MemOther] += rss;
Dmitry Vyukov5d72fc72013-03-18 13:55:33 +0000104}
105
Stephen Hines6a211c52014-07-21 00:49:56 -0700106void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
107 uptr mem[MemCount] = {};
Alexander Potapenko2e13ca82013-09-03 11:09:16 +0000108 __sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7);
Stephen Hines6d186232014-11-26 17:56:19 -0800109 StackDepotStats *stacks = StackDepotGetStats();
Stephen Hines6a211c52014-07-21 00:49:56 -0700110 internal_snprintf(buf, buf_size,
111 "RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd"
Stephen Hines6d186232014-11-26 17:56:19 -0800112 " trace:%zd heap:%zd other:%zd stacks=%zd[%zd] nthr=%zd/%zd\n",
Stephen Hines6a211c52014-07-21 00:49:56 -0700113 mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20,
114 mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20,
115 mem[MemHeap] >> 20, mem[MemOther] >> 20,
Stephen Hines6d186232014-11-26 17:56:19 -0800116 stacks->allocated >> 20, stacks->n_uniq_ids,
Stephen Hines6a211c52014-07-21 00:49:56 -0700117 nlive, nthread);
Dmitry Vyukov26127732012-05-22 11:33:03 +0000118}
119
Stephen Hines6a211c52014-07-21 00:49:56 -0700120#if SANITIZER_LINUX
Dmitry Vyukov92b54792013-10-03 17:14:35 +0000121void FlushShadowMemoryCallback(
122 const SuspendedThreadsList &suspended_threads_list,
123 void *argument) {
Stephen Hines6d186232014-11-26 17:56:19 -0800124 FlushUnneededShadowMemory(kShadowBeg, kShadowEnd - kShadowBeg);
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000125}
Stephen Hines6a211c52014-07-21 00:49:56 -0700126#endif
Dmitry Vyukovadfb6502012-05-22 18:07:45 +0000127
Dmitry Vyukov92b54792013-10-03 17:14:35 +0000128void FlushShadowMemory() {
Stephen Hines6a211c52014-07-21 00:49:56 -0700129#if SANITIZER_LINUX
Dmitry Vyukov92b54792013-10-03 17:14:35 +0000130 StopTheWorld(FlushShadowMemoryCallback, 0);
Stephen Hines6a211c52014-07-21 00:49:56 -0700131#endif
Dmitry Vyukov92b54792013-10-03 17:14:35 +0000132}
133
Stephen Hines86277eb2015-03-23 12:06:32 -0700134#ifndef SANITIZER_GO
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000135static void ProtectRange(uptr beg, uptr end) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000136 CHECK_LE(beg, end);
137 if (beg == end)
138 return;
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700139 if (beg != (uptr)MmapNoAccess(beg, end - beg)) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000140 Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
141 Printf("FATAL: Make sure you are not using unlimited stack\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000142 Die();
143 }
144}
145
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000146// Mark shadow for .rodata sections with the special kShadowRodata marker.
147// Accesses to .rodata can't race, so this saves time, memory and trace space.
148static void MapRodata() {
149 // First create temp file.
150 const char *tmpdir = GetEnv("TMPDIR");
151 if (tmpdir == 0)
152 tmpdir = GetEnv("TEST_TMPDIR");
153#ifdef P_tmpdir
154 if (tmpdir == 0)
155 tmpdir = P_tmpdir;
156#endif
157 if (tmpdir == 0)
158 return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700159 char name[256];
160 internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d",
Peter Collingbourne0b694fc2013-05-17 16:56:53 +0000161 tmpdir, (int)internal_getpid());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700162 uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000163 if (internal_iserror(openrv))
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000164 return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700165 internal_unlink(name); // Unlink it now, so that we can reuse the buffer.
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000166 fd_t fd = openrv;
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000167 // Fill the file with kShadowRodata.
168 const uptr kMarkerSize = 512 * 1024 / sizeof(u64);
169 InternalScopedBuffer<u64> marker(kMarkerSize);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700170 // volatile to prevent insertion of memset
171 for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++)
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000172 *p = kShadowRodata;
173 internal_write(fd, marker.data(), marker.size());
174 // Map the file into memory.
Stephen Hines86277eb2015-03-23 12:06:32 -0700175 uptr page = internal_mmap(0, GetPageSizeCached(), PROT_READ | PROT_WRITE,
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000176 MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
177 if (internal_iserror(page)) {
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000178 internal_close(fd);
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000179 return;
180 }
181 // Map the file into shadow of .rodata sections.
Alexander Potapenko9ae28832013-03-26 10:34:37 +0000182 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000183 uptr start, end, offset, prot;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700184 // Reusing the buffer 'name'.
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000185 while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), &prot)) {
186 if (name[0] != 0 && name[0] != '['
187 && (prot & MemoryMappingLayout::kProtectionRead)
188 && (prot & MemoryMappingLayout::kProtectionExecute)
189 && !(prot & MemoryMappingLayout::kProtectionWrite)
190 && IsAppMem(start)) {
191 // Assume it's .rodata
192 char *shadow_start = (char*)MemToShadow(start);
193 char *shadow_end = (char*)MemToShadow(end);
194 for (char *p = shadow_start; p < shadow_end; p += marker.size()) {
195 internal_mmap(p, Min<uptr>(marker.size(), shadow_end - p),
196 PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
197 }
198 }
199 }
200 internal_close(fd);
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000201}
202
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000203void InitializeShadowMemory() {
Stephen Hines6a211c52014-07-21 00:49:56 -0700204 // Map memory shadow.
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700205 uptr shadow =
206 (uptr)MmapFixedNoReserve(kShadowBeg, kShadowEnd - kShadowBeg, "shadow");
Stephen Hines6d186232014-11-26 17:56:19 -0800207 if (shadow != kShadowBeg) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000208 Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
209 Printf("FATAL: Make sure to compile with -fPIE and "
Stephen Hines6d186232014-11-26 17:56:19 -0800210 "to link with -pie (%p, %p).\n", shadow, kShadowBeg);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000211 Die();
212 }
Stephen Hines6d186232014-11-26 17:56:19 -0800213 // This memory range is used for thread stacks and large user mmaps.
214 // Frequently a thread uses only a small part of stack and similarly
215 // a program uses a small part of large mmap. On some programs
216 // we see 20% memory usage reduction without huge pages for this range.
Stephen Hines86277eb2015-03-23 12:06:32 -0700217 // FIXME: don't use constants here.
218#if defined(__x86_64__)
219 const uptr kMadviseRangeBeg = 0x7f0000000000ull;
220 const uptr kMadviseRangeSize = 0x010000000000ull;
221#elif defined(__mips64)
222 const uptr kMadviseRangeBeg = 0xff00000000ull;
223 const uptr kMadviseRangeSize = 0x0100000000ull;
Stephen Hines6d186232014-11-26 17:56:19 -0800224#endif
Stephen Hines86277eb2015-03-23 12:06:32 -0700225 NoHugePagesInRegion(MemToShadow(kMadviseRangeBeg),
226 kMadviseRangeSize * kShadowMultiplier);
227 if (common_flags()->use_madv_dontdump)
228 DontDumpShadowMemory(kShadowBeg, kShadowEnd - kShadowBeg);
Stephen Hines6a211c52014-07-21 00:49:56 -0700229 DPrintf("memory shadow: %zx-%zx (%zuGB)\n",
Stephen Hines6d186232014-11-26 17:56:19 -0800230 kShadowBeg, kShadowEnd,
231 (kShadowEnd - kShadowBeg) >> 30);
Stephen Hines6a211c52014-07-21 00:49:56 -0700232
233 // Map meta shadow.
Stephen Hines6d186232014-11-26 17:56:19 -0800234 uptr meta_size = kMetaShadowEnd - kMetaShadowBeg;
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700235 uptr meta =
236 (uptr)MmapFixedNoReserve(kMetaShadowBeg, meta_size, "meta shadow");
Stephen Hines6d186232014-11-26 17:56:19 -0800237 if (meta != kMetaShadowBeg) {
Stephen Hines6a211c52014-07-21 00:49:56 -0700238 Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
239 Printf("FATAL: Make sure to compile with -fPIE and "
Stephen Hines6d186232014-11-26 17:56:19 -0800240 "to link with -pie (%p, %p).\n", meta, kMetaShadowBeg);
Stephen Hines6a211c52014-07-21 00:49:56 -0700241 Die();
242 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700243 if (common_flags()->use_madv_dontdump)
244 DontDumpShadowMemory(meta, meta_size);
Stephen Hines6a211c52014-07-21 00:49:56 -0700245 DPrintf("meta shadow: %zx-%zx (%zuGB)\n",
Stephen Hines6d186232014-11-26 17:56:19 -0800246 meta, meta + meta_size, meta_size >> 30);
Dmitry Vyukov82dbc512013-03-20 13:21:50 +0000247
248 MapRodata();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000249}
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000250
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000251static void InitDataSeg() {
Alexander Potapenko9ae28832013-03-26 10:34:37 +0000252 MemoryMappingLayout proc_maps(true);
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000253 uptr start, end, offset;
254 char name[128];
Stephen Hines6d186232014-11-26 17:56:19 -0800255#if SANITIZER_FREEBSD
256 // On FreeBSD BSS is usually the last block allocated within the
257 // low range and heap is the last block allocated within the range
258 // 0x800000000-0x8ffffffff.
259 while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name),
260 /*protection*/ 0)) {
261 DPrintf("%p-%p %p %s\n", start, end, offset, name);
262 if ((start & 0xffff00000000ULL) == 0 && (end & 0xffff00000000ULL) == 0 &&
263 name[0] == '\0') {
264 g_data_start = start;
265 g_data_end = end;
266 }
267 }
268#else
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000269 bool prev_is_data = false;
Alexey Samsonov45717c92013-03-13 06:51:02 +0000270 while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name),
271 /*protection*/ 0)) {
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000272 DPrintf("%p-%p %p %s\n", start, end, offset, name);
273 bool is_data = offset != 0 && name[0] != 0;
Evgeniy Stepanov97dac9e2012-09-27 13:20:40 +0000274 // BSS may get merged with [heap] in /proc/self/maps. This is not very
275 // reliable.
276 bool is_bss = offset == 0 &&
277 (name[0] == 0 || internal_strcmp(name, "[heap]") == 0) && prev_is_data;
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000278 if (g_data_start == 0 && is_data)
279 g_data_start = start;
280 if (is_bss)
281 g_data_end = end;
282 prev_is_data = is_data;
283 }
Stephen Hines6d186232014-11-26 17:56:19 -0800284#endif
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000285 DPrintf("guessed data_start=%p data_end=%p\n", g_data_start, g_data_end);
286 CHECK_LT(g_data_start, g_data_end);
287 CHECK_GE((uptr)&g_data_start, g_data_start);
288 CHECK_LT((uptr)&g_data_start, g_data_end);
289}
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000290
Stephen Hines6d186232014-11-26 17:56:19 -0800291static void CheckAndProtect() {
292 // Ensure that the binary is indeed compiled with -pie.
293 MemoryMappingLayout proc_maps(true);
294 uptr p, end;
295 while (proc_maps.Next(&p, &end, 0, 0, 0, 0)) {
296 if (IsAppMem(p))
297 continue;
298 if (p >= kHeapMemEnd &&
Stephen Hines86277eb2015-03-23 12:06:32 -0700299 p < HeapEnd())
Stephen Hines6d186232014-11-26 17:56:19 -0800300 continue;
Stephen Hines86277eb2015-03-23 12:06:32 -0700301 if (p >= kVdsoBeg) // vdso
Stephen Hines6d186232014-11-26 17:56:19 -0800302 break;
303 Printf("FATAL: ThreadSanitizer: unexpected memory mapping %p-%p\n", p, end);
304 Die();
305 }
306
307 ProtectRange(kLoAppMemEnd, kShadowBeg);
308 ProtectRange(kShadowEnd, kMetaShadowBeg);
309 ProtectRange(kMetaShadowEnd, kTraceMemBeg);
Stephen Hines86277eb2015-03-23 12:06:32 -0700310 // Memory for traces is mapped lazily in MapThreadTrace.
311 // Protect the whole range for now, so that user does not map something here.
312 ProtectRange(kTraceMemBeg, kTraceMemEnd);
Stephen Hines6d186232014-11-26 17:56:19 -0800313 ProtectRange(kTraceMemEnd, kHeapMemBeg);
Stephen Hines86277eb2015-03-23 12:06:32 -0700314 ProtectRange(HeapEnd(), kHiAppMemBeg);
Stephen Hines6d186232014-11-26 17:56:19 -0800315}
Stephen Hines86277eb2015-03-23 12:06:32 -0700316#endif // #ifndef SANITIZER_GO
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000317
Stephen Hines6d186232014-11-26 17:56:19 -0800318void InitializePlatform() {
319 DisableCoreDumperIfNecessary();
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000320
Dmitry Vyukov4de58642012-12-21 10:45:01 +0000321 // Go maps shadow memory lazily and works fine with limited address space.
322 // Unlimited stack is not a problem as well, because the executable
323 // is not compiled with -pie.
324 if (kCppMode) {
325 bool reexec = false;
326 // TSan doesn't play well with unlimited stack size (as stack
327 // overlaps with shadow memory). If we detect unlimited stack size,
328 // we re-exec the program with limited stack size as a best effort.
Stephen Hines6d186232014-11-26 17:56:19 -0800329 if (StackSizeIsUnlimited()) {
Dmitry Vyukov4de58642012-12-21 10:45:01 +0000330 const uptr kMaxStackSize = 32 * 1024 * 1024;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700331 VReport(1, "Program is run with unlimited stack size, which wouldn't "
332 "work with ThreadSanitizer.\n"
333 "Re-execing with stack size limited to %zd bytes.\n",
334 kMaxStackSize);
Dmitry Vyukov4de58642012-12-21 10:45:01 +0000335 SetStackSizeLimitInBytes(kMaxStackSize);
336 reexec = true;
337 }
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000338
Stephen Hines6d186232014-11-26 17:56:19 -0800339 if (!AddressSpaceIsUnlimited()) {
Dmitry Vyukov4dc30822012-12-21 10:47:48 +0000340 Report("WARNING: Program is run with limited virtual address space,"
341 " which wouldn't work with ThreadSanitizer.\n");
Dmitry Vyukov4de58642012-12-21 10:45:01 +0000342 Report("Re-execing with unlimited virtual address space.\n");
Stephen Hines6d186232014-11-26 17:56:19 -0800343 SetAddressSpaceUnlimited();
Dmitry Vyukov4de58642012-12-21 10:45:01 +0000344 reexec = true;
345 }
346 if (reexec)
347 ReExec();
348 }
Dmitry Vyukovd698edc2012-11-28 12:19:50 +0000349
Stephen Hines86277eb2015-03-23 12:06:32 -0700350#ifndef SANITIZER_GO
Stephen Hines6d186232014-11-26 17:56:19 -0800351 CheckAndProtect();
Evgeniy Stepanovb114ed82013-03-13 08:19:53 +0000352 InitTlsSize();
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000353 InitDataSeg();
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000354#endif
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000355}
356
Dmitry Vyukov0c2feef2012-09-07 18:08:02 +0000357bool IsGlobalVar(uptr addr) {
358 return g_data_start && addr >= g_data_start && addr < g_data_end;
359}
Dmitry Vyukovb78caa62012-07-05 16:18:28 +0000360
Stephen Hines86277eb2015-03-23 12:06:32 -0700361#ifndef SANITIZER_GO
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700362// Extract file descriptors passed to glibc internal __res_iclose function.
363// This is required to properly "close" the fds, because we do not see internal
364// closes within glibc. The code is a pure hack.
Dmitry Vyukov03f22482013-02-07 15:27:45 +0000365int ExtractResolvFDs(void *state, int *fds, int nfd) {
Stephen Hines6a211c52014-07-21 00:49:56 -0700366#if SANITIZER_LINUX
Dmitry Vyukov03f22482013-02-07 15:27:45 +0000367 int cnt = 0;
368 __res_state *statp = (__res_state*)state;
369 for (int i = 0; i < MAXNS && cnt < nfd; i++) {
370 if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1)
371 fds[cnt++] = statp->_u._ext.nssocks[i];
372 }
373 return cnt;
Stephen Hines6a211c52014-07-21 00:49:56 -0700374#else
375 return 0;
376#endif
Dmitry Vyukov03f22482013-02-07 15:27:45 +0000377}
Dmitry Vyukov03f22482013-02-07 15:27:45 +0000378
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700379// Extract file descriptors passed via UNIX domain sockets.
380// This is requried to properly handle "open" of these fds.
381// see 'man recvmsg' and 'man 3 cmsg'.
382int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
383 int res = 0;
384 msghdr *msg = (msghdr*)msgp;
385 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
386 for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
387 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
388 continue;
389 int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]);
390 for (int i = 0; i < n; i++) {
391 fds[res++] = ((int*)CMSG_DATA(cmsg))[i];
392 if (res == nfd)
393 return res;
394 }
395 }
396 return res;
397}
398
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700399// Note: this function runs with async signals enabled,
400// so it must not touch any tsan state.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700401int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
402 void *abstime), void *c, void *m, void *abstime,
403 void(*cleanup)(void *arg), void *arg) {
404 // pthread_cleanup_push/pop are hardcore macros mess.
405 // We can't intercept nor call them w/o including pthread.h.
406 int res;
407 pthread_cleanup_push(cleanup, arg);
408 res = fn(c, m, abstime);
409 pthread_cleanup_pop(0);
410 return res;
411}
412#endif
Dmitry Vyukov03f22482013-02-07 15:27:45 +0000413
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000414} // namespace __tsan
Dmitry Vyukov3f24d632012-07-16 13:25:47 +0000415
Stephen Hines6d186232014-11-26 17:56:19 -0800416#endif // SANITIZER_LINUX || SANITIZER_FREEBSD