2012-06-04 22:27:50 +08:00
|
|
|
//===-- sanitizer_linux.cc ------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries and implements linux-specific functions from
|
|
|
|
// sanitizer_libc.h.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifdef __linux__
|
|
|
|
|
2012-06-07 14:15:12 +08:00
|
|
|
#include "sanitizer_common.h"
|
2012-06-05 22:25:27 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
2012-06-04 22:27:50 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2012-07-03 16:24:14 +08:00
|
|
|
#include "sanitizer_placement_new.h"
|
2012-06-07 14:15:12 +08:00
|
|
|
#include "sanitizer_procmaps.h"
|
2012-06-04 22:27:50 +08:00
|
|
|
|
2012-06-05 15:05:10 +08:00
|
|
|
#include <fcntl.h>
|
2012-06-07 15:13:46 +08:00
|
|
|
#include <pthread.h>
|
2012-06-18 16:44:30 +08:00
|
|
|
#include <sched.h>
|
2012-06-04 22:27:50 +08:00
|
|
|
#include <sys/mman.h>
|
2012-06-07 15:13:46 +08:00
|
|
|
#include <sys/resource.h>
|
2012-06-05 15:05:10 +08:00
|
|
|
#include <sys/stat.h>
|
2012-06-04 22:27:50 +08:00
|
|
|
#include <sys/syscall.h>
|
2012-06-07 15:13:46 +08:00
|
|
|
#include <sys/time.h>
|
2012-06-04 22:27:50 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-10-02 21:41:40 +08:00
|
|
|
#include <errno.h>
|
2012-06-04 22:27:50 +08:00
|
|
|
|
2012-11-19 15:53:36 +08:00
|
|
|
// Are we using 32-bit or 64-bit syscalls?
|
2012-11-21 20:38:58 +08:00
|
|
|
// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
|
2012-11-20 16:57:26 +08:00
|
|
|
// but it still needs to use 64-bit syscalls.
|
2012-11-21 20:38:58 +08:00
|
|
|
#if defined(__x86_64__) || SANITIZER_WORDSIZE == 64
|
2012-11-19 15:53:36 +08:00
|
|
|
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
|
|
|
|
#else
|
|
|
|
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
|
|
|
|
#endif
|
|
|
|
|
2012-06-04 22:27:50 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// --------------- sanitizer_libc.h
|
2012-06-04 22:27:50 +08:00
|
|
|
void *internal_mmap(void *addr, uptr length, int prot, int flags,
|
|
|
|
int fd, u64 offset) {
|
2012-11-19 15:53:36 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2012-06-04 22:27:50 +08:00
|
|
|
return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
|
|
|
|
#else
|
|
|
|
return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-05 17:49:25 +08:00
|
|
|
int internal_munmap(void *addr, uptr length) {
|
|
|
|
return syscall(__NR_munmap, addr, length);
|
|
|
|
}
|
|
|
|
|
2012-06-05 16:32:53 +08:00
|
|
|
int internal_close(fd_t fd) {
|
|
|
|
return syscall(__NR_close, fd);
|
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:10 +08:00
|
|
|
fd_t internal_open(const char *filename, bool write) {
|
|
|
|
return syscall(__NR_open, filename,
|
2012-06-06 22:11:31 +08:00
|
|
|
write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660);
|
2012-06-05 15:05:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-05 16:32:53 +08:00
|
|
|
uptr internal_read(fd_t fd, void *buf, uptr count) {
|
2012-10-02 21:41:40 +08:00
|
|
|
sptr res;
|
|
|
|
HANDLE_EINTR(res, (sptr)syscall(__NR_read, fd, buf, count));
|
|
|
|
return res;
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr internal_write(fd_t fd, const void *buf, uptr count) {
|
2012-10-02 21:41:40 +08:00
|
|
|
sptr res;
|
|
|
|
HANDLE_EINTR(res, (sptr)syscall(__NR_write, fd, buf, count));
|
|
|
|
return res;
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 15:30:33 +08:00
|
|
|
uptr internal_filesize(fd_t fd) {
|
2012-11-19 15:53:36 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2012-07-03 16:24:14 +08:00
|
|
|
struct stat st;
|
2012-06-06 15:30:33 +08:00
|
|
|
if (syscall(__NR_fstat, fd, &st))
|
|
|
|
return -1;
|
2012-07-03 16:24:14 +08:00
|
|
|
#else
|
|
|
|
struct stat64 st;
|
|
|
|
if (syscall(__NR_fstat64, fd, &st))
|
|
|
|
return -1;
|
|
|
|
#endif
|
2012-06-06 15:30:33 +08:00
|
|
|
return (uptr)st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int internal_dup2(int oldfd, int newfd) {
|
|
|
|
return syscall(__NR_dup2, oldfd, newfd);
|
|
|
|
}
|
|
|
|
|
2012-09-05 22:48:24 +08:00
|
|
|
uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
|
|
|
|
return (uptr)syscall(__NR_readlink, path, buf, bufsize);
|
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
int internal_sched_yield() {
|
|
|
|
return syscall(__NR_sched_yield);
|
|
|
|
}
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// ----------------- sanitizer_common.h
|
2012-11-09 22:45:30 +08:00
|
|
|
bool FileExists(const char *filename) {
|
2012-11-19 15:53:36 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2012-11-09 22:45:30 +08:00
|
|
|
struct stat st;
|
|
|
|
if (syscall(__NR_stat, filename, &st))
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
struct stat64 st;
|
|
|
|
if (syscall(__NR_stat64, filename, &st))
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
// Sanity check: filename is a regular file.
|
|
|
|
return S_ISREG(st.st_mode);
|
|
|
|
}
|
|
|
|
|
2012-10-02 20:58:14 +08:00
|
|
|
uptr GetTid() {
|
|
|
|
return syscall(__NR_gettid);
|
|
|
|
}
|
|
|
|
|
2012-06-07 15:32:00 +08:00
|
|
|
void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
|
2012-06-07 15:13:46 +08:00
|
|
|
uptr *stack_bottom) {
|
|
|
|
static const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M
|
|
|
|
CHECK(stack_top);
|
|
|
|
CHECK(stack_bottom);
|
2012-06-07 15:32:00 +08:00
|
|
|
if (at_initialization) {
|
2012-06-07 15:13:46 +08:00
|
|
|
// This is the main thread. Libpthread may not be initialized yet.
|
|
|
|
struct rlimit rl;
|
2012-06-20 23:19:17 +08:00
|
|
|
CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
|
2012-06-07 15:13:46 +08:00
|
|
|
|
|
|
|
// Find the mapping that contains a stack variable.
|
2012-08-27 21:48:48 +08:00
|
|
|
MemoryMappingLayout proc_maps;
|
2012-06-07 15:13:46 +08:00
|
|
|
uptr start, end, offset;
|
|
|
|
uptr prev_end = 0;
|
|
|
|
while (proc_maps.Next(&start, &end, &offset, 0, 0)) {
|
|
|
|
if ((uptr)&rl < end)
|
|
|
|
break;
|
|
|
|
prev_end = end;
|
|
|
|
}
|
|
|
|
CHECK((uptr)&rl >= start && (uptr)&rl < end);
|
|
|
|
|
|
|
|
// Get stacksize from rlimit, but clip it so that it does not overlap
|
|
|
|
// with other mappings.
|
|
|
|
uptr stacksize = rl.rlim_cur;
|
|
|
|
if (stacksize > end - prev_end)
|
|
|
|
stacksize = end - prev_end;
|
|
|
|
// When running with unlimited stack size, we still want to set some limit.
|
|
|
|
// The unlimited stack size is caused by 'ulimit -s unlimited'.
|
|
|
|
// Also, for some reason, GNU make spawns subprocesses with unlimited stack.
|
|
|
|
if (stacksize > kMaxThreadStackSize)
|
|
|
|
stacksize = kMaxThreadStackSize;
|
|
|
|
*stack_top = end;
|
|
|
|
*stack_bottom = end - stacksize;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pthread_attr_t attr;
|
2012-06-20 23:19:17 +08:00
|
|
|
CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
|
2012-06-07 15:13:46 +08:00
|
|
|
uptr stacksize = 0;
|
|
|
|
void *stackaddr = 0;
|
|
|
|
pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize);
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
|
|
|
*stack_top = (uptr)stackaddr + stacksize;
|
|
|
|
*stack_bottom = (uptr)stackaddr;
|
|
|
|
CHECK(stacksize < kMaxThreadStackSize); // Sanity check.
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:07:21 +08:00
|
|
|
// Like getenv, but reads env directly from /proc and does not use libc.
|
|
|
|
// This function should be called first inside __asan_init.
|
|
|
|
const char *GetEnv(const char *name) {
|
|
|
|
static char *environ;
|
|
|
|
static uptr len;
|
|
|
|
static bool inited;
|
|
|
|
if (!inited) {
|
|
|
|
inited = true;
|
|
|
|
uptr environ_size;
|
|
|
|
len = ReadFileToBuffer("/proc/self/environ",
|
|
|
|
&environ, &environ_size, 1 << 26);
|
|
|
|
}
|
|
|
|
if (!environ || len == 0) return 0;
|
|
|
|
uptr namelen = internal_strlen(name);
|
|
|
|
const char *p = environ;
|
|
|
|
while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
|
|
|
|
// proc file has the format NAME=value\0NAME=value\0NAME=value\0...
|
|
|
|
const char* endp =
|
|
|
|
(char*)internal_memchr(p, '\0', len - (p - environ));
|
|
|
|
if (endp == 0) // this entry isn't NUL terminated
|
|
|
|
return 0;
|
|
|
|
else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
|
|
|
|
return p + namelen + 1; // point after =
|
|
|
|
p = endp + 1;
|
|
|
|
}
|
|
|
|
return 0; // Not found.
|
|
|
|
}
|
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
void ReExec() {
|
|
|
|
static const int kMaxArgv = 100;
|
|
|
|
InternalScopedBuffer<char*> argv(kMaxArgv + 1);
|
|
|
|
static char *buff;
|
|
|
|
uptr buff_size = 0;
|
|
|
|
ReadFileToBuffer("/proc/self/cmdline", &buff, &buff_size, 1024 * 1024);
|
|
|
|
argv[0] = buff;
|
|
|
|
int argc, i;
|
|
|
|
for (argc = 1, i = 1; ; i++) {
|
|
|
|
if (buff[i] == 0) {
|
|
|
|
if (buff[i+1] == 0) break;
|
|
|
|
argv[argc] = &buff[i+1];
|
|
|
|
CHECK_LE(argc, kMaxArgv); // FIXME: make this more flexible.
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argv[argc] = 0;
|
|
|
|
execv(argv[0], argv.data());
|
|
|
|
}
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// ----------------- sanitizer_procmaps.h
|
2012-08-27 21:48:48 +08:00
|
|
|
MemoryMappingLayout::MemoryMappingLayout() {
|
2012-06-07 14:15:12 +08:00
|
|
|
proc_self_maps_buff_len_ =
|
|
|
|
ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
|
|
|
|
&proc_self_maps_buff_mmaped_size_, 1 << 26);
|
2012-06-20 23:19:17 +08:00
|
|
|
CHECK_GT(proc_self_maps_buff_len_, 0);
|
2012-06-07 14:15:12 +08:00
|
|
|
// internal_write(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
MemoryMappingLayout::~MemoryMappingLayout() {
|
2012-06-07 14:15:12 +08:00
|
|
|
UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
void MemoryMappingLayout::Reset() {
|
2012-06-07 14:15:12 +08:00
|
|
|
current_ = proc_self_maps_buff_;
|
|
|
|
}
|
|
|
|
|
2012-06-29 21:05:36 +08:00
|
|
|
// Parse a hex value in str and update str.
|
|
|
|
static uptr ParseHex(char **str) {
|
|
|
|
uptr x = 0;
|
|
|
|
char *s;
|
|
|
|
for (s = *str; ; s++) {
|
|
|
|
char c = *s;
|
|
|
|
uptr v = 0;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
v = c - '0';
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
v = c - 'a' + 10;
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
v = c - 'A' + 10;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
x = x * 16 + v;
|
|
|
|
}
|
|
|
|
*str = s;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsOnOf(char c, char c1, char c2) {
|
|
|
|
return c == c1 || c == c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsDecimal(char c) {
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
|
|
|
|
char filename[], uptr filename_size) {
|
2012-06-07 14:15:12 +08:00
|
|
|
char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
|
|
|
|
if (current_ >= last) return false;
|
|
|
|
uptr dummy;
|
|
|
|
if (!start) start = &dummy;
|
|
|
|
if (!end) end = &dummy;
|
|
|
|
if (!offset) offset = &dummy;
|
|
|
|
char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
|
|
|
|
if (next_line == 0)
|
|
|
|
next_line = last;
|
2012-06-29 21:05:36 +08:00
|
|
|
// Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
|
|
|
|
*start = ParseHex(¤t_);
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, '-');
|
2012-06-29 21:05:36 +08:00
|
|
|
*end = ParseHex(¤t_);
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, ' ');
|
2012-06-29 21:05:36 +08:00
|
|
|
CHECK(IsOnOf(*current_++, '-', 'r'));
|
|
|
|
CHECK(IsOnOf(*current_++, '-', 'w'));
|
|
|
|
CHECK(IsOnOf(*current_++, '-', 'x'));
|
|
|
|
CHECK(IsOnOf(*current_++, 's', 'p'));
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, ' ');
|
2012-06-29 21:05:36 +08:00
|
|
|
*offset = ParseHex(¤t_);
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, ' ');
|
2012-06-29 21:05:36 +08:00
|
|
|
ParseHex(¤t_);
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, ':');
|
2012-06-29 21:05:36 +08:00
|
|
|
ParseHex(¤t_);
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, ' ');
|
2012-06-29 21:05:36 +08:00
|
|
|
while (IsDecimal(*current_))
|
|
|
|
current_++;
|
2012-06-29 22:14:32 +08:00
|
|
|
CHECK_EQ(*current_++, ' ');
|
2012-06-07 14:15:12 +08:00
|
|
|
// Skip spaces.
|
|
|
|
while (current_ < next_line && *current_ == ' ')
|
|
|
|
current_++;
|
|
|
|
// Fill in the filename.
|
|
|
|
uptr i = 0;
|
|
|
|
while (current_ < next_line) {
|
|
|
|
if (filename && i < filename_size - 1)
|
|
|
|
filename[i++] = *current_;
|
|
|
|
current_++;
|
|
|
|
}
|
|
|
|
if (filename && i < filename_size)
|
|
|
|
filename[i] = 0;
|
|
|
|
current_ = next_line + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
// Gets the object name and the offset by walking MemoryMappingLayout.
|
|
|
|
bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
|
|
|
|
char filename[],
|
|
|
|
uptr filename_size) {
|
2012-06-07 14:15:12 +08:00
|
|
|
return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:27:50 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // __linux__
|