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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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 <asm/prctl.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>
|
|
|
|
|
2012-06-05 21:50:57 +08:00
|
|
|
extern "C" int arch_prctl(int code, __sanitizer::uptr *addr);
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
void Die() {
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
namespace __tsan {
|
|
|
|
|
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
|
|
|
|
2012-05-22 19:33:03 +08:00
|
|
|
uptr GetShadowMemoryConsumption() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-23 02:07:45 +08:00
|
|
|
void FlushShadowMemory() {
|
|
|
|
madvise((void*)kLinuxShadowBeg,
|
|
|
|
kLinuxShadowEnd - kLinuxShadowBeg,
|
|
|
|
MADV_DONTNEED);
|
|
|
|
}
|
|
|
|
|
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-06-06 21:11:29 +08:00
|
|
|
TsanPrintf("FATAL: ThreadSanitizer can not protect [%zx,%zx]\n", beg, end);
|
2012-06-06 18:13:27 +08:00
|
|
|
TsanPrintf("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
|
|
|
|
|
|
|
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-06-06 18:13:27 +08:00
|
|
|
TsanPrintf("FATAL: ThreadSanitizer can not mmap the shadow memory\n");
|
|
|
|
TsanPrintf("FATAL: Make sure to compile with -fPIE and "
|
|
|
|
"to link with -pie.\n");
|
2012-05-10 21:48:04 +08:00
|
|
|
Die();
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
|
|
|
const uptr kClosedLowBeg = 0x200000;
|
|
|
|
const uptr kClosedLowEnd = kLinuxShadowBeg - 1;
|
|
|
|
const uptr kClosedMidBeg = kLinuxShadowEnd + 1;
|
|
|
|
const uptr kClosedMidEnd = kLinuxAppMemBeg - 1;
|
2012-05-10 21:48:04 +08:00
|
|
|
ProtectRange(kClosedLowBeg, kClosedLowEnd);
|
|
|
|
ProtectRange(kClosedMidBeg, kClosedMidEnd);
|
2012-07-06 00:18:28 +08:00
|
|
|
#endif
|
|
|
|
#ifndef TSAN_GO
|
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-07-06 00:18:28 +08:00
|
|
|
#endif
|
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-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
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-07-06 00:18:28 +08:00
|
|
|
#endif
|
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);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2012-06-18 17:42:39 +08:00
|
|
|
ProcessMaps proc_maps;
|
|
|
|
uptr start, end;
|
|
|
|
if (proc_maps.Next(&start, &end,
|
|
|
|
/*offset*/0, /*filename*/0, /*filename_size*/0)) {
|
|
|
|
if ((u64)start < kLinuxAppMemBeg) {
|
2012-06-06 18:13:27 +08:00
|
|
|
TsanPrintf("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-06-06 18:13:27 +08:00
|
|
|
TsanPrintf("FATAL: Make sure to compile with -fPIE"
|
2012-05-10 21:48:04 +08:00
|
|
|
" and to link with -pie.\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
static uptr g_tls_size;
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
#ifdef __i386__
|
|
|
|
# define INTERNAL_FUNCTION __attribute__((regparm(3), stdcall))
|
|
|
|
#else
|
|
|
|
# define INTERNAL_FUNCTION
|
|
|
|
#endif
|
|
|
|
extern "C" void _dl_get_tls_static_info(size_t*, size_t*)
|
|
|
|
__attribute__((weak)) INTERNAL_FUNCTION;
|
|
|
|
|
|
|
|
static int InitTlsSize() {
|
|
|
|
typedef void (*get_tls_func)(size_t*, size_t*) INTERNAL_FUNCTION;
|
|
|
|
get_tls_func get_tls = &_dl_get_tls_static_info;
|
|
|
|
if (get_tls == 0)
|
|
|
|
get_tls = (get_tls_func)dlsym(RTLD_NEXT, "_dl_get_tls_static_info");
|
|
|
|
CHECK_NE(get_tls, 0);
|
|
|
|
size_t tls_size = 0;
|
|
|
|
size_t tls_align = 0;
|
|
|
|
get_tls(&tls_size, &tls_align);
|
|
|
|
return tls_size;
|
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
#endif // #ifndef TSAN_GO
|
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.
|
|
|
|
// The following magic is to prevent clang from replacing it with memset.
|
|
|
|
volatile rlimit lim;
|
|
|
|
lim.rlim_cur = 0;
|
|
|
|
lim.rlim_max = 0;
|
|
|
|
setrlimit(RLIMIT_CORE, (rlimit*)&lim);
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
CheckPIE();
|
|
|
|
g_tls_size = (uptr)InitTlsSize();
|
2012-07-06 00:18:28 +08:00
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
return getenv("TSAN_OPTIONS");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinalizePlatform() {
|
|
|
|
fflush(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr GetTlsSize() {
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
return g_tls_size;
|
2012-07-06 00:18:28 +08:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-05-25 19:15:04 +08:00
|
|
|
void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
|
2012-05-10 21:48:04 +08:00
|
|
|
uptr *tls_addr, uptr *tls_size) {
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
arch_prctl(ARCH_GET_FS, tls_addr);
|
|
|
|
*tls_addr -= g_tls_size;
|
|
|
|
*tls_size = g_tls_size;
|
|
|
|
|
2012-06-18 17:42:39 +08:00
|
|
|
uptr stack_top, stack_bottom;
|
|
|
|
GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
|
|
|
|
*stk_addr = stack_bottom;
|
|
|
|
*stk_size = stack_top - stack_bottom;
|
2012-05-25 19:15:04 +08:00
|
|
|
|
2012-06-18 17:42:39 +08:00
|
|
|
if (!main) {
|
2012-05-25 19:15:04 +08:00
|
|
|
// If stack and tls intersect, make them non-intersecting.
|
|
|
|
if (*tls_addr > *stk_addr && *tls_addr < *stk_addr + *stk_size) {
|
|
|
|
CHECK_GT(*tls_addr + *tls_size, *stk_addr);
|
|
|
|
CHECK_LE(*tls_addr + *tls_size, *stk_addr + *stk_size);
|
|
|
|
*stk_size -= *tls_size;
|
|
|
|
*tls_addr = *stk_addr + *stk_size;
|
|
|
|
}
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
2012-07-06 00:18:28 +08:00
|
|
|
#else
|
|
|
|
*stk_addr = 0;
|
|
|
|
*stk_size = 0;
|
|
|
|
*tls_addr = 0;
|
|
|
|
*tls_size = 0;
|
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
} // namespace __tsan
|