2012-06-04 21:55:19 +08:00
|
|
|
//===-- tsan_platform_linux.cc --------------------------------------------===//
|
2012-05-10 21:48:04 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Linux-specific code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
|
|
#if SANITIZER_LINUX
|
2012-07-16 21:25:47 +08:00
|
|
|
|
2012-06-18 17:42:39 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2012-06-05 14:19:00 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2012-06-18 17:42:39 +08:00
|
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
2012-05-10 21:48:04 +08: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/prctl.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#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>
|
2013-02-07 23:27:45 +08:00
|
|
|
#define __need_res_state
|
|
|
|
#include <resolv.h>
|
2013-03-18 21:55:33 +08:00
|
|
|
#include <malloc.h>
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2013-03-18 21:55:33 +08:00
|
|
|
extern "C" struct mallinfo __libc_mallinfo();
|
2012-05-10 21:48:04 +08:00
|
|
|
|
|
|
|
namespace __tsan {
|
|
|
|
|
2013-03-20 21:21:50 +08:00
|
|
|
const uptr kPageSize = 4096;
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
ScopedInRtl::ScopedInRtl()
|
|
|
|
: thr_(cur_thread()) {
|
|
|
|
in_rtl_ = thr_->in_rtl;
|
|
|
|
thr_->in_rtl++;
|
|
|
|
errno_ = errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedInRtl::~ScopedInRtl() {
|
|
|
|
thr_->in_rtl--;
|
|
|
|
errno = errno_;
|
|
|
|
CHECK_EQ(in_rtl_, thr_->in_rtl);
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
#else
|
|
|
|
ScopedInRtl::ScopedInRtl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedInRtl::~ScopedInRtl() {
|
|
|
|
}
|
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2013-09-03 19:09:16 +08:00
|
|
|
void FillProfileCallback(uptr start, uptr rss, bool file,
|
|
|
|
uptr *mem, uptr stats_size) {
|
|
|
|
CHECK_EQ(7, stats_size);
|
|
|
|
mem[6] += rss; // total
|
|
|
|
start >>= 40;
|
|
|
|
if (start < 0x10) // shadow
|
|
|
|
mem[0] += rss;
|
|
|
|
else if (start >= 0x20 && start < 0x30) // compat modules
|
|
|
|
mem[file ? 1 : 2] += rss;
|
|
|
|
else if (start >= 0x7e) // modules
|
|
|
|
mem[file ? 1 : 2] += rss;
|
|
|
|
else if (start >= 0x60 && start < 0x62) // traces
|
|
|
|
mem[3] += rss;
|
|
|
|
else if (start >= 0x7d && start < 0x7e) // heap
|
|
|
|
mem[4] += rss;
|
|
|
|
else // other
|
|
|
|
mem[5] += rss;
|
2013-03-18 21:55:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteMemoryProfile(char *buf, uptr buf_size) {
|
2013-09-03 19:09:16 +08:00
|
|
|
uptr mem[7] = {};
|
|
|
|
__sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7);
|
2013-03-18 21:55:33 +08:00
|
|
|
char *buf_pos = buf;
|
|
|
|
char *buf_end = buf + buf_size;
|
|
|
|
buf_pos += internal_snprintf(buf_pos, buf_end - buf_pos,
|
|
|
|
"RSS %zd MB: shadow:%zd file:%zd mmap:%zd trace:%zd heap:%zd other:%zd\n",
|
2013-09-03 19:09:16 +08:00
|
|
|
mem[6] >> 20, mem[0] >> 20, mem[1] >> 20, mem[2] >> 20,
|
2013-03-18 21:55:33 +08:00
|
|
|
mem[3] >> 20, mem[4] >> 20, mem[5] >> 20);
|
|
|
|
struct mallinfo mi = __libc_mallinfo();
|
|
|
|
buf_pos += internal_snprintf(buf_pos, buf_end - buf_pos,
|
|
|
|
"mallinfo: arena=%d mmap=%d fordblks=%d keepcost=%d\n",
|
|
|
|
mi.arena >> 20, mi.hblkhd >> 20, mi.fordblks >> 20, mi.keepcost >> 20);
|
2012-05-22 19:33:03 +08:00
|
|
|
}
|
|
|
|
|
2012-05-23 02:07:45 +08:00
|
|
|
void FlushShadowMemory() {
|
2012-12-27 15:37:24 +08:00
|
|
|
FlushUnneededShadowMemory(kLinuxShadowBeg, kLinuxShadowEnd - kLinuxShadowBeg);
|
2012-05-23 02:07:45 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
static void ProtectRange(uptr beg, uptr end) {
|
|
|
|
ScopedInRtl in_rtl;
|
|
|
|
CHECK_LE(beg, end);
|
|
|
|
if (beg == end)
|
|
|
|
return;
|
2012-06-18 17:42:39 +08:00
|
|
|
if (beg != (uptr)Mprotect(beg, end - beg)) {
|
2012-11-02 20:17:51 +08:00
|
|
|
Printf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
|
|
|
|
Printf("FATAL: Make sure you are not using unlimited stack\n");
|
2012-05-10 21:48:04 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-11-07 00:00:16 +08:00
|
|
|
#ifndef TSAN_GO
|
2013-03-20 21:21:50 +08: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;
|
|
|
|
char filename[256];
|
2013-05-18 00:56:53 +08:00
|
|
|
internal_snprintf(filename, sizeof(filename), "%s/tsan.rodata.%d",
|
|
|
|
tmpdir, (int)internal_getpid());
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr openrv = internal_open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
|
|
if (internal_iserror(openrv))
|
2013-03-20 21:21:50 +08:00
|
|
|
return;
|
2013-05-08 22:43:49 +08:00
|
|
|
fd_t fd = openrv;
|
2013-03-20 21:21:50 +08:00
|
|
|
// Fill the file with kShadowRodata.
|
|
|
|
const uptr kMarkerSize = 512 * 1024 / sizeof(u64);
|
|
|
|
InternalScopedBuffer<u64> marker(kMarkerSize);
|
|
|
|
for (u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++)
|
|
|
|
*p = kShadowRodata;
|
|
|
|
internal_write(fd, marker.data(), marker.size());
|
|
|
|
// Map the file into memory.
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr page = internal_mmap(0, kPageSize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
|
|
|
|
if (internal_iserror(page)) {
|
2013-03-20 21:21:50 +08:00
|
|
|
internal_close(fd);
|
|
|
|
internal_unlink(filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Map the file into shadow of .rodata sections.
|
2013-03-26 18:34:37 +08:00
|
|
|
MemoryMappingLayout proc_maps(/*cache_enabled*/true);
|
2013-03-20 21:21:50 +08:00
|
|
|
uptr start, end, offset, prot;
|
|
|
|
char name[128];
|
|
|
|
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);
|
|
|
|
internal_unlink(filename);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
void InitializeShadowMemory() {
|
2012-06-18 17:42:39 +08:00
|
|
|
uptr shadow = (uptr)MmapFixedNoReserve(kLinuxShadowBeg,
|
|
|
|
kLinuxShadowEnd - kLinuxShadowBeg);
|
2012-05-10 21:48:04 +08:00
|
|
|
if (shadow != kLinuxShadowBeg) {
|
2012-11-02 20:17:51 +08:00
|
|
|
Printf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
|
|
|
|
Printf("FATAL: Make sure to compile with -fPIE and "
|
2012-08-15 23:35:15 +08:00
|
|
|
"to link with -pie (%p, %p).\n", shadow, kLinuxShadowBeg);
|
2012-05-10 21:48:04 +08:00
|
|
|
Die();
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
const uptr kClosedLowBeg = 0x200000;
|
|
|
|
const uptr kClosedLowEnd = kLinuxShadowBeg - 1;
|
|
|
|
const uptr kClosedMidBeg = kLinuxShadowEnd + 1;
|
2012-11-28 18:35:31 +08:00
|
|
|
const uptr kClosedMidEnd = min(kLinuxAppMemBeg, kTraceMemBegin);
|
2012-05-10 21:48:04 +08:00
|
|
|
ProtectRange(kClosedLowBeg, kClosedLowEnd);
|
|
|
|
ProtectRange(kClosedMidBeg, kClosedMidEnd);
|
2012-06-06 21:11:29 +08:00
|
|
|
DPrintf("kClosedLow %zx-%zx (%zuGB)\n",
|
2012-05-10 21:48:04 +08:00
|
|
|
kClosedLowBeg, kClosedLowEnd, (kClosedLowEnd - kClosedLowBeg) >> 30);
|
2012-06-06 21:11:29 +08:00
|
|
|
DPrintf("kLinuxShadow %zx-%zx (%zuGB)\n",
|
2012-05-10 21:48:04 +08:00
|
|
|
kLinuxShadowBeg, kLinuxShadowEnd,
|
|
|
|
(kLinuxShadowEnd - kLinuxShadowBeg) >> 30);
|
2012-06-06 21:11:29 +08:00
|
|
|
DPrintf("kClosedMid %zx-%zx (%zuGB)\n",
|
2012-05-10 21:48:04 +08:00
|
|
|
kClosedMidBeg, kClosedMidEnd, (kClosedMidEnd - kClosedMidBeg) >> 30);
|
2012-06-06 21:11:29 +08:00
|
|
|
DPrintf("kLinuxAppMem %zx-%zx (%zuGB)\n",
|
2012-05-10 21:48:04 +08:00
|
|
|
kLinuxAppMemBeg, kLinuxAppMemEnd,
|
|
|
|
(kLinuxAppMemEnd - kLinuxAppMemBeg) >> 30);
|
2012-06-06 21:11:29 +08:00
|
|
|
DPrintf("stack %zx\n", (uptr)&shadow);
|
2013-03-20 21:21:50 +08:00
|
|
|
|
|
|
|
MapRodata();
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
2012-11-07 00:00:16 +08:00
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-09-08 02:08:02 +08:00
|
|
|
static uptr g_data_start;
|
|
|
|
static uptr g_data_end;
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
static void CheckPIE() {
|
|
|
|
// Ensure that the binary is indeed compiled with -pie.
|
2013-03-26 18:34:37 +08:00
|
|
|
MemoryMappingLayout proc_maps(true);
|
2012-06-18 17:42:39 +08:00
|
|
|
uptr start, end;
|
|
|
|
if (proc_maps.Next(&start, &end,
|
2013-03-13 14:51:02 +08:00
|
|
|
/*offset*/0, /*filename*/0, /*filename_size*/0,
|
|
|
|
/*protection*/0)) {
|
2012-06-18 17:42:39 +08:00
|
|
|
if ((u64)start < kLinuxAppMemBeg) {
|
2012-11-02 20:17:51 +08:00
|
|
|
Printf("FATAL: ThreadSanitizer can not mmap the shadow memory ("
|
2012-06-06 21:11:29 +08:00
|
|
|
"something is mapped at 0x%zx < 0x%zx)\n",
|
2012-06-18 17:42:39 +08:00
|
|
|
start, kLinuxAppMemBeg);
|
2012-11-02 20:17:51 +08:00
|
|
|
Printf("FATAL: Make sure to compile with -fPIE"
|
2012-05-10 21:48:04 +08:00
|
|
|
" and to link with -pie.\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 02:08:02 +08:00
|
|
|
static void InitDataSeg() {
|
2013-03-26 18:34:37 +08:00
|
|
|
MemoryMappingLayout proc_maps(true);
|
2012-09-08 02:08:02 +08:00
|
|
|
uptr start, end, offset;
|
|
|
|
char name[128];
|
|
|
|
bool prev_is_data = false;
|
2013-03-13 14:51:02 +08:00
|
|
|
while (proc_maps.Next(&start, &end, &offset, name, ARRAY_SIZE(name),
|
|
|
|
/*protection*/ 0)) {
|
2012-09-08 02:08:02 +08:00
|
|
|
DPrintf("%p-%p %p %s\n", start, end, offset, name);
|
|
|
|
bool is_data = offset != 0 && name[0] != 0;
|
2012-09-27 21:20:40 +08:00
|
|
|
// 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;
|
2012-09-08 02:08:02 +08:00
|
|
|
if (g_data_start == 0 && is_data)
|
|
|
|
g_data_start = start;
|
|
|
|
if (is_bss)
|
|
|
|
g_data_end = end;
|
|
|
|
prev_is_data = is_data;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
|
|
|
|
#endif // #ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-11-28 20:19:50 +08:00
|
|
|
static rlim_t getlim(int res) {
|
|
|
|
rlimit rlim;
|
|
|
|
CHECK_EQ(0, getrlimit(res, &rlim));
|
|
|
|
return rlim.rlim_cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setlim(int res, rlim_t lim) {
|
|
|
|
// The following magic is to prevent clang from replacing it with memset.
|
|
|
|
volatile rlimit rlim;
|
|
|
|
rlim.rlim_cur = lim;
|
|
|
|
rlim.rlim_max = lim;
|
|
|
|
setrlimit(res, (rlimit*)&rlim);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
const char *InitializePlatform() {
|
|
|
|
void *p = 0;
|
|
|
|
if (sizeof(p) == 8) {
|
|
|
|
// Disable core dumps, dumping of 16TB usually takes a bit long.
|
2012-11-28 20:19:50 +08:00
|
|
|
setlim(RLIMIT_CORE, 0);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-12-21 18:45:01 +08: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.
|
|
|
|
if (getlim(RLIMIT_STACK) == (rlim_t)-1) {
|
|
|
|
const uptr kMaxStackSize = 32 * 1024 * 1024;
|
|
|
|
Report("WARNING: Program is run with unlimited stack size, which "
|
|
|
|
"wouldn't work with ThreadSanitizer.\n");
|
2012-12-21 18:47:48 +08:00
|
|
|
Report("Re-execing with stack size limited to %zd bytes.\n",
|
|
|
|
kMaxStackSize);
|
2012-12-21 18:45:01 +08:00
|
|
|
SetStackSizeLimitInBytes(kMaxStackSize);
|
|
|
|
reexec = true;
|
|
|
|
}
|
2012-11-28 20:19:50 +08:00
|
|
|
|
2012-12-21 18:45:01 +08:00
|
|
|
if (getlim(RLIMIT_AS) != (rlim_t)-1) {
|
2012-12-21 18:47:48 +08:00
|
|
|
Report("WARNING: Program is run with limited virtual address space,"
|
|
|
|
" which wouldn't work with ThreadSanitizer.\n");
|
2012-12-21 18:45:01 +08:00
|
|
|
Report("Re-execing with unlimited virtual address space.\n");
|
|
|
|
setlim(RLIMIT_AS, -1);
|
|
|
|
reexec = true;
|
|
|
|
}
|
|
|
|
if (reexec)
|
|
|
|
ReExec();
|
|
|
|
}
|
2012-11-28 20:19:50 +08:00
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
CheckPIE();
|
2013-03-13 16:19:53 +08:00
|
|
|
InitTlsSize();
|
2012-09-08 02:08:02 +08:00
|
|
|
InitDataSeg();
|
2012-07-06 00:18:28 +08:00
|
|
|
#endif
|
2013-02-19 16:19:13 +08:00
|
|
|
return GetEnv(kTsanOptionsEnv);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-09-08 02:08:02 +08:00
|
|
|
bool IsGlobalVar(uptr addr) {
|
|
|
|
return g_data_start && addr >= g_data_start && addr < g_data_end;
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
|
2013-02-07 23:27:45 +08:00
|
|
|
#ifndef TSAN_GO
|
|
|
|
int ExtractResolvFDs(void *state, int *fds, int nfd) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
} // namespace __tsan
|
2012-07-16 21:25:47 +08:00
|
|
|
|
2013-03-19 22:54:17 +08:00
|
|
|
#endif // SANITIZER_LINUX
|