2012-11-22 23:03:11 +01:00
|
|
|
//===-- tsan_platform_linux.cc --------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2014-11-13 21:41:38 +01:00
|
|
|
// Linux- and FreeBSD-specific code.
|
2012-11-22 23:03:11 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_LINUX || SANITIZER_FREEBSD
|
2012-11-22 23:03:11 +01:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
2013-12-05 10:18:38 +01:00
|
|
|
#include "sanitizer_common/sanitizer_stoptheworld.h"
|
2014-11-13 21:41:38 +01:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2012-11-22 23:03:11 +01:00
|
|
|
#include "tsan_platform.h"
|
|
|
|
#include "tsan_rtl.h"
|
|
|
|
#include "tsan_flags.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/syscall.h>
|
2013-12-05 10:18:38 +01:00
|
|
|
#include <sys/socket.h>
|
2012-11-22 23:03:11 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <dlfcn.h>
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_LINUX
|
2013-02-13 11:46:01 +01:00
|
|
|
#define __need_res_state
|
|
|
|
#include <resolv.h>
|
2014-09-23 19:59:53 +02:00
|
|
|
#endif
|
2012-11-22 23:03:11 +01:00
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
#ifdef sa_handler
|
|
|
|
# undef sa_handler
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef sa_sigaction
|
|
|
|
# undef sa_sigaction
|
|
|
|
#endif
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_FREEBSD
|
|
|
|
extern "C" void *__libc_stack_end;
|
|
|
|
void *__libc_stack_end = 0;
|
|
|
|
#endif
|
2012-11-22 23:03:11 +01:00
|
|
|
|
|
|
|
namespace __tsan {
|
|
|
|
|
2014-11-13 21:41:38 +01:00
|
|
|
static uptr g_data_start;
|
|
|
|
static uptr g_data_end;
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
const uptr kPageSize = 4096;
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
enum {
|
|
|
|
MemTotal = 0,
|
|
|
|
MemShadow = 1,
|
|
|
|
MemMeta = 2,
|
|
|
|
MemFile = 3,
|
|
|
|
MemMmap = 4,
|
|
|
|
MemTrace = 5,
|
|
|
|
MemHeap = 6,
|
|
|
|
MemOther = 7,
|
|
|
|
MemCount = 8,
|
|
|
|
};
|
|
|
|
|
2014-11-13 21:41:38 +01:00
|
|
|
void FillProfileCallback(uptr p, uptr rss, bool file,
|
2013-11-04 22:33:31 +01:00
|
|
|
uptr *mem, uptr stats_size) {
|
2014-09-23 19:59:53 +02:00
|
|
|
mem[MemTotal] += rss;
|
2014-11-13 21:41:38 +01:00
|
|
|
if (p >= kShadowBeg && p < kShadowEnd)
|
2014-09-23 19:59:53 +02:00
|
|
|
mem[MemShadow] += rss;
|
2014-11-13 21:41:38 +01:00
|
|
|
else if (p >= kMetaShadowBeg && p < kMetaShadowEnd)
|
2014-09-23 19:59:53 +02:00
|
|
|
mem[MemMeta] += rss;
|
2014-11-13 21:41:38 +01:00
|
|
|
#ifndef TSAN_GO
|
|
|
|
else if (p >= kHeapMemBeg && p < kHeapMemEnd)
|
|
|
|
mem[MemHeap] += rss;
|
|
|
|
else if (p >= kLoAppMemBeg && p < kLoAppMemEnd)
|
2014-09-23 19:59:53 +02:00
|
|
|
mem[file ? MemFile : MemMmap] += rss;
|
2014-11-13 21:41:38 +01:00
|
|
|
else if (p >= kHiAppMemBeg && p < kHiAppMemEnd)
|
|
|
|
mem[file ? MemFile : MemMmap] += rss;
|
|
|
|
#else
|
|
|
|
else if (p >= kAppMemBeg && p < kAppMemEnd)
|
|
|
|
mem[file ? MemFile : MemMmap] += rss;
|
|
|
|
#endif
|
|
|
|
else if (p >= kTraceMemBeg && p < kTraceMemEnd)
|
2014-09-23 19:59:53 +02:00
|
|
|
mem[MemTrace] += rss;
|
|
|
|
else
|
|
|
|
mem[MemOther] += rss;
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
|
|
|
|
uptr mem[MemCount] = {};
|
2013-11-04 22:33:31 +01:00
|
|
|
__sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7);
|
2014-11-13 21:41:38 +01:00
|
|
|
StackDepotStats *stacks = StackDepotGetStats();
|
2014-09-23 19:59:53 +02:00
|
|
|
internal_snprintf(buf, buf_size,
|
|
|
|
"RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd"
|
2014-11-13 21:41:38 +01:00
|
|
|
" trace:%zd heap:%zd other:%zd stacks=%zd[%zd] nthr=%zd/%zd\n",
|
2014-09-23 19:59:53 +02:00
|
|
|
mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20,
|
|
|
|
mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20,
|
|
|
|
mem[MemHeap] >> 20, mem[MemOther] >> 20,
|
2014-11-13 21:41:38 +01:00
|
|
|
stacks->allocated >> 20, stacks->n_uniq_ids,
|
2014-09-23 19:59:53 +02:00
|
|
|
nlive, nthread);
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
uptr GetRSS() {
|
2014-09-23 19:59:53 +02:00
|
|
|
uptr fd = OpenFile("/proc/self/statm", false);
|
|
|
|
if ((sptr)fd < 0)
|
|
|
|
return 0;
|
|
|
|
char buf[64];
|
|
|
|
uptr len = internal_read(fd, buf, sizeof(buf) - 1);
|
|
|
|
internal_close(fd);
|
|
|
|
if ((sptr)len <= 0)
|
|
|
|
return 0;
|
|
|
|
buf[len] = 0;
|
|
|
|
// The format of the file is:
|
|
|
|
// 1084 89 69 11 0 79 0
|
|
|
|
// We need the second number which is RSS in 4K units.
|
|
|
|
char *pos = buf;
|
|
|
|
// Skip the first number.
|
|
|
|
while (*pos >= '0' && *pos <= '9')
|
|
|
|
pos++;
|
|
|
|
// Skip whitespaces.
|
|
|
|
while (!(*pos >= '0' && *pos <= '9') && *pos != 0)
|
|
|
|
pos++;
|
|
|
|
// Read the number.
|
|
|
|
uptr rss = 0;
|
|
|
|
while (*pos >= '0' && *pos <= '9')
|
|
|
|
rss = rss * 10 + *pos++ - '0';
|
|
|
|
return rss * 4096;
|
2013-12-05 10:18:38 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_LINUX
|
2013-12-05 10:18:38 +01:00
|
|
|
void FlushShadowMemoryCallback(
|
|
|
|
const SuspendedThreadsList &suspended_threads_list,
|
|
|
|
void *argument) {
|
2014-11-13 21:41:38 +01:00
|
|
|
FlushUnneededShadowMemory(kShadowBeg, kShadowEnd - kShadowBeg);
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
#endif
|
2012-11-22 23:03:11 +01:00
|
|
|
|
2013-12-05 10:18:38 +01:00
|
|
|
void FlushShadowMemory() {
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_LINUX
|
2013-12-05 10:18:38 +01:00
|
|
|
StopTheWorld(FlushShadowMemoryCallback, 0);
|
2014-09-23 19:59:53 +02:00
|
|
|
#endif
|
2013-12-05 10:18:38 +01:00
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
#ifndef TSAN_GO
|
|
|
|
static void ProtectRange(uptr beg, uptr end) {
|
|
|
|
CHECK_LE(beg, end);
|
|
|
|
if (beg == end)
|
|
|
|
return;
|
|
|
|
if (beg != (uptr)Mprotect(beg, end - beg)) {
|
2012-11-23 15:46:25 +01:00
|
|
|
Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
|
|
|
|
Printf("FATAL: Make sure you are not using unlimited stack\n");
|
2012-11-22 23:03:11 +01:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
// Mark shadow for .rodata sections with the special kShadowRodata marker.
|
|
|
|
// Accesses to .rodata can't race, so this saves time, memory and trace space.
|
|
|
|
static void MapRodata() {
|
|
|
|
// First create temp file.
|
|
|
|
const char *tmpdir = GetEnv("TMPDIR");
|
|
|
|
if (tmpdir == 0)
|
|
|
|
tmpdir = GetEnv("TEST_TMPDIR");
|
|
|
|
#ifdef P_tmpdir
|
|
|
|
if (tmpdir == 0)
|
|
|
|
tmpdir = P_tmpdir;
|
|
|
|
#endif
|
|
|
|
if (tmpdir == 0)
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
char name[256];
|
|
|
|
internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d",
|
2013-11-04 22:33:31 +01:00
|
|
|
tmpdir, (int)internal_getpid());
|
2014-05-22 09:09:21 +02:00
|
|
|
uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
|
2013-11-04 22:33:31 +01:00
|
|
|
if (internal_iserror(openrv))
|
|
|
|
return;
|
2014-05-22 09:09:21 +02:00
|
|
|
internal_unlink(name); // Unlink it now, so that we can reuse the buffer.
|
2013-11-04 22:33:31 +01:00
|
|
|
fd_t fd = openrv;
|
|
|
|
// Fill the file with kShadowRodata.
|
|
|
|
const uptr kMarkerSize = 512 * 1024 / sizeof(u64);
|
|
|
|
InternalScopedBuffer<u64> marker(kMarkerSize);
|
2014-05-22 09:09:21 +02:00
|
|
|
// volatile to prevent insertion of memset
|
|
|
|
for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++)
|
2013-11-04 22:33:31 +01:00
|
|
|
*p = kShadowRodata;
|
|
|
|
internal_write(fd, marker.data(), marker.size());
|
|
|
|
// Map the file into memory.
|
|
|
|
uptr page = internal_mmap(0, kPageSize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
|
|
|
|
if (internal_iserror(page)) {
|
|
|
|
internal_close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Map the file into shadow of .rodata sections.
|
|
|
|
MemoryMappingLayout proc_maps(/*cache_enabled*/true);
|
|
|
|
uptr start, end, offset, prot;
|
2014-05-22 09:09:21 +02:00
|
|
|
// Reusing the buffer 'name'.
|
2013-11-04 22:33:31 +01:00
|
|
|
while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name), &prot)) {
|
|
|
|
if (name[0] != 0 && name[0] != '['
|
|
|
|
&& (prot & MemoryMappingLayout::kProtectionRead)
|
|
|
|
&& (prot & MemoryMappingLayout::kProtectionExecute)
|
|
|
|
&& !(prot & MemoryMappingLayout::kProtectionWrite)
|
|
|
|
&& IsAppMem(start)) {
|
|
|
|
// Assume it's .rodata
|
|
|
|
char *shadow_start = (char*)MemToShadow(start);
|
|
|
|
char *shadow_end = (char*)MemToShadow(end);
|
|
|
|
for (char *p = shadow_start; p < shadow_end; p += marker.size()) {
|
|
|
|
internal_mmap(p, Min<uptr>(marker.size(), shadow_end - p),
|
|
|
|
PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
internal_close(fd);
|
|
|
|
}
|
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
void InitializeShadowMemory() {
|
2014-09-23 19:59:53 +02:00
|
|
|
// Map memory shadow.
|
2014-11-13 21:41:38 +01:00
|
|
|
uptr shadow = (uptr)MmapFixedNoReserve(kShadowBeg,
|
|
|
|
kShadowEnd - kShadowBeg);
|
|
|
|
if (shadow != kShadowBeg) {
|
2012-11-23 15:46:25 +01:00
|
|
|
Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
|
|
|
|
Printf("FATAL: Make sure to compile with -fPIE and "
|
2014-11-13 21:41:38 +01:00
|
|
|
"to link with -pie (%p, %p).\n", shadow, kShadowBeg);
|
2012-11-22 23:03:11 +01:00
|
|
|
Die();
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
// This memory range is used for thread stacks and large user mmaps.
|
|
|
|
// Frequently a thread uses only a small part of stack and similarly
|
|
|
|
// a program uses a small part of large mmap. On some programs
|
|
|
|
// we see 20% memory usage reduction without huge pages for this range.
|
|
|
|
#ifdef MADV_NOHUGEPAGE
|
|
|
|
madvise((void*)MemToShadow(0x7f0000000000ULL),
|
|
|
|
0x10000000000ULL * kShadowMultiplier, MADV_NOHUGEPAGE);
|
|
|
|
#endif
|
|
|
|
DPrintf("memory shadow: %zx-%zx (%zuGB)\n",
|
2014-11-13 21:41:38 +01:00
|
|
|
kShadowBeg, kShadowEnd,
|
|
|
|
(kShadowEnd - kShadowBeg) >> 30);
|
2014-09-23 19:59:53 +02:00
|
|
|
|
|
|
|
// Map meta shadow.
|
2014-11-13 21:41:38 +01:00
|
|
|
uptr meta_size = kMetaShadowEnd - kMetaShadowBeg;
|
|
|
|
uptr meta = (uptr)MmapFixedNoReserve(kMetaShadowBeg, meta_size);
|
|
|
|
if (meta != kMetaShadowBeg) {
|
2014-09-23 19:59:53 +02:00
|
|
|
Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
|
|
|
|
Printf("FATAL: Make sure to compile with -fPIE and "
|
2014-11-13 21:41:38 +01:00
|
|
|
"to link with -pie (%p, %p).\n", meta, kMetaShadowBeg);
|
2014-09-23 19:59:53 +02:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
DPrintf("meta shadow: %zx-%zx (%zuGB)\n",
|
2014-11-13 21:41:38 +01:00
|
|
|
meta, meta + meta_size, meta_size >> 30);
|
2013-11-04 22:33:31 +01:00
|
|
|
|
|
|
|
MapRodata();
|
2012-11-22 23:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void InitDataSeg() {
|
2013-11-04 22:33:31 +01:00
|
|
|
MemoryMappingLayout proc_maps(true);
|
2012-11-22 23:03:11 +01:00
|
|
|
uptr start, end, offset;
|
|
|
|
char name[128];
|
2014-11-13 21:41:38 +01:00
|
|
|
#if SANITIZER_FREEBSD
|
|
|
|
// On FreeBSD BSS is usually the last block allocated within the
|
|
|
|
// low range and heap is the last block allocated within the range
|
|
|
|
// 0x800000000-0x8ffffffff.
|
|
|
|
while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name),
|
|
|
|
/*protection*/ 0)) {
|
|
|
|
DPrintf("%p-%p %p %s\n", start, end, offset, name);
|
|
|
|
if ((start & 0xffff00000000ULL) == 0 && (end & 0xffff00000000ULL) == 0 &&
|
|
|
|
name[0] == '\0') {
|
|
|
|
g_data_start = start;
|
|
|
|
g_data_end = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2012-11-22 23:03:11 +01:00
|
|
|
bool prev_is_data = false;
|
2013-11-04 22:33:31 +01:00
|
|
|
while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name),
|
|
|
|
/*protection*/ 0)) {
|
2012-11-22 23:03:11 +01:00
|
|
|
DPrintf("%p-%p %p %s\n", start, end, offset, name);
|
|
|
|
bool is_data = offset != 0 && name[0] != 0;
|
|
|
|
// BSS may get merged with [heap] in /proc/self/maps. This is not very
|
|
|
|
// reliable.
|
|
|
|
bool is_bss = offset == 0 &&
|
|
|
|
(name[0] == 0 || internal_strcmp(name, "[heap]") == 0) && prev_is_data;
|
|
|
|
if (g_data_start == 0 && is_data)
|
|
|
|
g_data_start = start;
|
|
|
|
if (is_bss)
|
|
|
|
g_data_end = end;
|
|
|
|
prev_is_data = is_data;
|
|
|
|
}
|
2014-11-13 21:41:38 +01:00
|
|
|
#endif
|
2012-11-22 23:03:11 +01:00
|
|
|
DPrintf("guessed data_start=%p data_end=%p\n", g_data_start, g_data_end);
|
|
|
|
CHECK_LT(g_data_start, g_data_end);
|
|
|
|
CHECK_GE((uptr)&g_data_start, g_data_start);
|
|
|
|
CHECK_LT((uptr)&g_data_start, g_data_end);
|
|
|
|
}
|
|
|
|
|
2014-11-13 21:41:38 +01:00
|
|
|
static void CheckAndProtect() {
|
|
|
|
// Ensure that the binary is indeed compiled with -pie.
|
|
|
|
MemoryMappingLayout proc_maps(true);
|
|
|
|
uptr p, end;
|
|
|
|
while (proc_maps.Next(&p, &end, 0, 0, 0, 0)) {
|
|
|
|
if (IsAppMem(p))
|
|
|
|
continue;
|
|
|
|
if (p >= kHeapMemEnd &&
|
|
|
|
p < kHeapMemEnd + PrimaryAllocator::AdditionalSize())
|
|
|
|
continue;
|
|
|
|
if (p >= 0xf000000000000000ull) // vdso
|
|
|
|
break;
|
|
|
|
Printf("FATAL: ThreadSanitizer: unexpected memory mapping %p-%p\n", p, end);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtectRange(kLoAppMemEnd, kShadowBeg);
|
|
|
|
ProtectRange(kShadowEnd, kMetaShadowBeg);
|
|
|
|
ProtectRange(kMetaShadowEnd, kTraceMemBeg);
|
|
|
|
ProtectRange(kTraceMemEnd, kHeapMemBeg);
|
|
|
|
ProtectRange(kHeapMemEnd + PrimaryAllocator::AdditionalSize(), kHiAppMemBeg);
|
|
|
|
}
|
2012-11-22 23:03:11 +01:00
|
|
|
#endif // #ifndef TSAN_GO
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
void InitializePlatform() {
|
|
|
|
DisableCoreDumperIfNecessary();
|
2012-11-22 23:03:11 +01:00
|
|
|
|
2013-01-10 13:44:08 +01:00
|
|
|
// Go maps shadow memory lazily and works fine with limited address space.
|
|
|
|
// Unlimited stack is not a problem as well, because the executable
|
|
|
|
// is not compiled with -pie.
|
|
|
|
if (kCppMode) {
|
|
|
|
bool reexec = false;
|
|
|
|
// TSan doesn't play well with unlimited stack size (as stack
|
|
|
|
// overlaps with shadow memory). If we detect unlimited stack size,
|
|
|
|
// we re-exec the program with limited stack size as a best effort.
|
2014-09-23 19:59:53 +02:00
|
|
|
if (StackSizeIsUnlimited()) {
|
2013-01-10 13:44:08 +01:00
|
|
|
const uptr kMaxStackSize = 32 * 1024 * 1024;
|
2014-05-22 09:09:21 +02:00
|
|
|
VReport(1, "Program is run with unlimited stack size, which wouldn't "
|
|
|
|
"work with ThreadSanitizer.\n"
|
|
|
|
"Re-execing with stack size limited to %zd bytes.\n",
|
|
|
|
kMaxStackSize);
|
2013-01-10 13:44:08 +01:00
|
|
|
SetStackSizeLimitInBytes(kMaxStackSize);
|
|
|
|
reexec = true;
|
|
|
|
}
|
2012-12-05 14:19:55 +01:00
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
if (!AddressSpaceIsUnlimited()) {
|
2013-01-10 13:44:08 +01:00
|
|
|
Report("WARNING: Program is run with limited virtual address space,"
|
|
|
|
" which wouldn't work with ThreadSanitizer.\n");
|
|
|
|
Report("Re-execing with unlimited virtual address space.\n");
|
2014-09-23 19:59:53 +02:00
|
|
|
SetAddressSpaceUnlimited();
|
2013-01-10 13:44:08 +01:00
|
|
|
reexec = true;
|
|
|
|
}
|
|
|
|
if (reexec)
|
|
|
|
ReExec();
|
|
|
|
}
|
2012-12-05 14:19:55 +01:00
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
#ifndef TSAN_GO
|
2014-11-13 21:41:38 +01:00
|
|
|
CheckAndProtect();
|
2013-11-04 22:33:31 +01:00
|
|
|
InitTlsSize();
|
2012-11-22 23:03:11 +01:00
|
|
|
InitDataSeg();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsGlobalVar(uptr addr) {
|
|
|
|
return g_data_start && addr >= g_data_start && addr < g_data_end;
|
|
|
|
}
|
|
|
|
|
2013-02-13 11:46:01 +01:00
|
|
|
#ifndef TSAN_GO
|
2013-12-05 10:18:38 +01:00
|
|
|
// Extract file descriptors passed to glibc internal __res_iclose function.
|
|
|
|
// This is required to properly "close" the fds, because we do not see internal
|
|
|
|
// closes within glibc. The code is a pure hack.
|
2013-02-13 11:46:01 +01:00
|
|
|
int ExtractResolvFDs(void *state, int *fds, int nfd) {
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_LINUX
|
2013-02-13 11:46:01 +01:00
|
|
|
int cnt = 0;
|
|
|
|
__res_state *statp = (__res_state*)state;
|
|
|
|
for (int i = 0; i < MAXNS && cnt < nfd; i++) {
|
|
|
|
if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1)
|
|
|
|
fds[cnt++] = statp->_u._ext.nssocks[i];
|
|
|
|
}
|
|
|
|
return cnt;
|
2014-09-23 19:59:53 +02:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2013-02-13 11:46:01 +01:00
|
|
|
}
|
2013-12-05 10:18:38 +01:00
|
|
|
|
|
|
|
// Extract file descriptors passed via UNIX domain sockets.
|
|
|
|
// This is requried to properly handle "open" of these fds.
|
|
|
|
// see 'man recvmsg' and 'man 3 cmsg'.
|
|
|
|
int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
|
|
|
|
int res = 0;
|
|
|
|
msghdr *msg = (msghdr*)msgp;
|
|
|
|
struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
|
|
|
|
for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
|
|
|
|
continue;
|
|
|
|
int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
fds[res++] = ((int*)CMSG_DATA(cmsg))[i];
|
|
|
|
if (res == nfd)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2013-02-13 11:46:01 +01:00
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
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) {
|
|
|
|
// pthread_cleanup_push/pop are hardcore macros mess.
|
|
|
|
// We can't intercept nor call them w/o including pthread.h.
|
|
|
|
int res;
|
|
|
|
pthread_cleanup_push(cleanup, arg);
|
|
|
|
res = fn(c, m, abstime);
|
|
|
|
pthread_cleanup_pop(0);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-13 11:46:01 +01:00
|
|
|
|
2012-11-22 23:03:11 +01:00
|
|
|
} // namespace __tsan
|
|
|
|
|
2014-11-13 21:41:38 +01:00
|
|
|
#endif // SANITIZER_LINUX || SANITIZER_FREEBSD
|