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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
#if SANITIZER_LINUX
|
2012-06-04 22:27:50 +08:00
|
|
|
|
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"
|
2013-02-27 19:22:40 +08:00
|
|
|
#include "sanitizer_linux.h"
|
2012-12-01 10:39:45 +08:00
|
|
|
#include "sanitizer_mutex.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-12-13 17:34:23 +08:00
|
|
|
#include "sanitizer_stacktrace.h"
|
2012-06-04 22:27:50 +08:00
|
|
|
|
2013-05-20 23:57:44 +08:00
|
|
|
#include <asm/param.h>
|
2013-03-13 16:19:53 +08:00
|
|
|
#include <dlfcn.h>
|
2013-02-27 21:03:35 +08:00
|
|
|
#include <errno.h>
|
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>
|
2013-02-27 19:22:40 +08:00
|
|
|
#include <sys/ptrace.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-12-13 17:34:23 +08:00
|
|
|
#include <unwind.h>
|
2013-02-27 21:03:35 +08:00
|
|
|
|
2013-03-19 22:33:38 +08:00
|
|
|
#if !SANITIZER_ANDROID
|
2013-02-27 21:03:35 +08:00
|
|
|
#include <sys/signal.h>
|
|
|
|
#endif
|
2013-01-30 22:39:27 +08:00
|
|
|
|
2013-03-21 14:24:31 +08:00
|
|
|
// <linux/time.h>
|
|
|
|
struct kernel_timeval {
|
|
|
|
long tv_sec;
|
|
|
|
long tv_usec;
|
|
|
|
};
|
|
|
|
|
2013-01-30 22:39:27 +08:00
|
|
|
// <linux/futex.h> is broken on some linux distributions.
|
|
|
|
const int FUTEX_WAIT = 0;
|
|
|
|
const int FUTEX_WAKE = 1;
|
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 {
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
#include "sanitizer_syscall_linux_x86_64.inc"
|
|
|
|
#else
|
|
|
|
#include "sanitizer_syscall_generic.inc"
|
|
|
|
#endif
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// --------------- sanitizer_libc.h
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_mmap(void *addr, uptr length, int prot, int flags,
|
2012-06-04 22:27:50 +08:00
|
|
|
int fd, u64 offset) {
|
2012-11-19 15:53:36 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
|
2012-06-04 22:27:50 +08:00
|
|
|
#else
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
|
2012-06-04 22:27:50 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_munmap(void *addr, uptr length) {
|
|
|
|
return internal_syscall(__NR_munmap, addr, length);
|
2012-06-05 17:49:25 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_close(fd_t fd) {
|
|
|
|
return internal_syscall(__NR_close, fd);
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_open(const char *filename, int flags) {
|
|
|
|
return internal_syscall(__NR_open, filename, flags);
|
2013-02-01 23:58:46 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_open(const char *filename, int flags, u32 mode) {
|
|
|
|
return internal_syscall(__NR_open, filename, flags, mode);
|
2013-02-01 23:58:46 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr OpenFile(const char *filename, bool write) {
|
2013-02-01 23:58:46 +08:00
|
|
|
return internal_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;
|
2013-05-08 22:43:49 +08:00
|
|
|
HANDLE_EINTR(res, (sptr)internal_syscall(__NR_read, fd, buf, count));
|
2012-10-02 21:41:40 +08:00
|
|
|
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;
|
2013-05-08 22:43:49 +08:00
|
|
|
HANDLE_EINTR(res, (sptr)internal_syscall(__NR_write, fd, buf, count));
|
2012-10-02 21:41:40 +08:00
|
|
|
return res;
|
2012-06-05 16:32:53 +08:00
|
|
|
}
|
|
|
|
|
2013-05-07 20:47:04 +08:00
|
|
|
#if !SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
|
|
|
static void stat64_to_stat(struct stat64 *in, struct stat *out) {
|
|
|
|
internal_memset(out, 0, sizeof(*out));
|
|
|
|
out->st_dev = in->st_dev;
|
|
|
|
out->st_ino = in->st_ino;
|
|
|
|
out->st_mode = in->st_mode;
|
|
|
|
out->st_nlink = in->st_nlink;
|
|
|
|
out->st_uid = in->st_uid;
|
|
|
|
out->st_gid = in->st_gid;
|
|
|
|
out->st_rdev = in->st_rdev;
|
|
|
|
out->st_size = in->st_size;
|
|
|
|
out->st_blksize = in->st_blksize;
|
|
|
|
out->st_blocks = in->st_blocks;
|
|
|
|
out->st_atime = in->st_atime;
|
|
|
|
out->st_mtime = in->st_mtime;
|
|
|
|
out->st_ctime = in->st_ctime;
|
|
|
|
out->st_ino = in->st_ino;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_stat(const char *path, void *buf) {
|
2013-02-04 18:16:50 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_stat, path, buf);
|
2013-02-04 18:16:50 +08:00
|
|
|
#else
|
2013-05-07 20:47:04 +08:00
|
|
|
struct stat64 buf64;
|
2013-05-08 22:43:49 +08:00
|
|
|
int res = internal_syscall(__NR_stat64, path, &buf64);
|
2013-05-07 20:47:04 +08:00
|
|
|
stat64_to_stat(&buf64, (struct stat *)buf);
|
|
|
|
return res;
|
2013-02-04 18:16:50 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_lstat(const char *path, void *buf) {
|
2013-02-04 18:16:50 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_lstat, path, buf);
|
2013-02-04 18:16:50 +08:00
|
|
|
#else
|
2013-05-07 20:47:04 +08:00
|
|
|
struct stat64 buf64;
|
2013-05-08 22:43:49 +08:00
|
|
|
int res = internal_syscall(__NR_lstat64, path, &buf64);
|
2013-05-07 20:47:04 +08:00
|
|
|
stat64_to_stat(&buf64, (struct stat *)buf);
|
|
|
|
return res;
|
2013-02-04 18:16:50 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_fstat(fd_t fd, void *buf) {
|
2013-02-04 18:16:50 +08:00
|
|
|
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_fstat, fd, buf);
|
2013-02-04 18:16:50 +08:00
|
|
|
#else
|
2013-05-07 20:47:04 +08:00
|
|
|
struct stat64 buf64;
|
2013-05-08 22:43:49 +08:00
|
|
|
int res = internal_syscall(__NR_fstat64, fd, &buf64);
|
2013-05-07 20:47:04 +08:00
|
|
|
stat64_to_stat(&buf64, (struct stat *)buf);
|
|
|
|
return res;
|
2013-02-04 18:16:50 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-06 15:30:33 +08:00
|
|
|
uptr internal_filesize(fd_t fd) {
|
2012-07-03 16:24:14 +08:00
|
|
|
struct stat st;
|
2013-02-04 18:16:50 +08:00
|
|
|
if (internal_fstat(fd, &st))
|
|
|
|
return -1;
|
2012-06-06 15:30:33 +08:00
|
|
|
return (uptr)st.st_size;
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_dup2(int oldfd, int newfd) {
|
|
|
|
return internal_syscall(__NR_dup2, oldfd, newfd);
|
2012-06-06 15:30:33 +08:00
|
|
|
}
|
|
|
|
|
2012-09-05 22:48:24 +08:00
|
|
|
uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_readlink, path, buf, bufsize);
|
2012-09-05 22:48:24 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_unlink(const char *path) {
|
|
|
|
return internal_syscall(__NR_unlink, path);
|
2013-03-20 18:28:36 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_sched_yield() {
|
|
|
|
return internal_syscall(__NR_sched_yield);
|
2012-06-18 16:44:30 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 21:54:32 +08:00
|
|
|
void internal__exit(int exitcode) {
|
2013-05-08 22:43:49 +08:00
|
|
|
internal_syscall(__NR_exit_group, exitcode);
|
2013-02-20 21:54:32 +08:00
|
|
|
Die(); // Unreachable.
|
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_execve(const char *filename, char *const argv[],
|
|
|
|
char *const envp[]) {
|
|
|
|
return internal_syscall(__NR_execve, filename, argv, envp);
|
|
|
|
}
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// ----------------- sanitizer_common.h
|
2012-11-09 22:45:30 +08:00
|
|
|
bool FileExists(const char *filename) {
|
|
|
|
struct stat st;
|
2013-05-07 20:47:04 +08:00
|
|
|
if (internal_stat(filename, &st))
|
2012-11-09 22:45:30 +08:00
|
|
|
return false;
|
|
|
|
// Sanity check: filename is a regular file.
|
|
|
|
return S_ISREG(st.st_mode);
|
|
|
|
}
|
|
|
|
|
2012-10-02 20:58:14 +08:00
|
|
|
uptr GetTid() {
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_gettid);
|
2012-10-02 20:58:14 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 14:24:31 +08:00
|
|
|
u64 NanoTime() {
|
|
|
|
kernel_timeval tv;
|
2013-05-08 22:43:49 +08:00
|
|
|
internal_syscall(__NR_gettimeofday, &tv, 0);
|
2013-03-21 14:24:31 +08:00
|
|
|
return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2013-02-14 22:40:03 +08:00
|
|
|
extern "C" {
|
2013-05-20 22:25:32 +08:00
|
|
|
extern void *__libc_stack_end SANITIZER_WEAK_ATTRIBUTE;
|
2013-02-14 22:40:03 +08:00
|
|
|
}
|
|
|
|
|
2013-05-20 22:25:32 +08:00
|
|
|
#if !SANITIZER_GO
|
2013-01-18 03:50:42 +08:00
|
|
|
static void ReadNullSepFileToArray(const char *path, char ***arr,
|
|
|
|
int arr_size) {
|
|
|
|
char *buff;
|
2012-09-17 17:12:39 +08:00
|
|
|
uptr buff_size = 0;
|
2013-01-18 03:50:42 +08:00
|
|
|
*arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
|
|
|
|
ReadFileToBuffer(path, &buff, &buff_size, 1024 * 1024);
|
|
|
|
(*arr)[0] = buff;
|
|
|
|
int count, i;
|
|
|
|
for (count = 1, i = 1; ; i++) {
|
2012-09-17 17:12:39 +08:00
|
|
|
if (buff[i] == 0) {
|
|
|
|
if (buff[i+1] == 0) break;
|
2013-01-18 03:50:42 +08:00
|
|
|
(*arr)[count] = &buff[i+1];
|
|
|
|
CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
|
|
|
|
count++;
|
2012-09-17 17:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-18 03:50:42 +08:00
|
|
|
(*arr)[count] = 0;
|
|
|
|
}
|
2013-05-20 22:25:32 +08:00
|
|
|
#endif
|
2013-01-18 03:50:42 +08:00
|
|
|
|
2013-05-20 22:25:32 +08:00
|
|
|
static void GetArgsAndEnv(char*** argv, char*** envp) {
|
|
|
|
#if !SANITIZER_GO
|
|
|
|
if (&__libc_stack_end) {
|
|
|
|
#endif
|
|
|
|
uptr* stack_end = (uptr*)__libc_stack_end;
|
|
|
|
int argc = *stack_end;
|
|
|
|
*argv = (char**)(stack_end + 1);
|
|
|
|
*envp = (char**)(stack_end + argc + 2);
|
|
|
|
#if !SANITIZER_GO
|
|
|
|
} else {
|
|
|
|
static const int kMaxArgv = 2000, kMaxEnvp = 2000;
|
|
|
|
ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
|
|
|
|
ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-14 22:40:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReExec() {
|
2013-01-18 03:50:42 +08:00
|
|
|
char **argv, **envp;
|
2013-02-14 22:40:03 +08:00
|
|
|
GetArgsAndEnv(&argv, &envp);
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr rv = internal_execve("/proc/self/exe", argv, envp);
|
|
|
|
int rverrno;
|
|
|
|
CHECK_EQ(internal_iserror(rv, &rverrno), true);
|
|
|
|
Printf("execve failed, errno %d\n", rverrno);
|
2013-02-19 19:09:29 +08:00
|
|
|
Die();
|
2012-09-17 17:12:39 +08:00
|
|
|
}
|
|
|
|
|
2012-12-10 21:10:40 +08:00
|
|
|
void PrepareForSandboxing() {
|
|
|
|
// Some kinds of sandboxes may forbid filesystem access, so we won't be able
|
|
|
|
// to read the file mappings from /proc/self/maps. Luckily, neither the
|
|
|
|
// process will be able to load additional libraries, so it's fine to use the
|
|
|
|
// cached mappings.
|
|
|
|
MemoryMappingLayout::CacheMemoryMappings();
|
|
|
|
}
|
|
|
|
|
2012-06-07 15:13:46 +08:00
|
|
|
// ----------------- sanitizer_procmaps.h
|
2012-12-05 18:16:02 +08:00
|
|
|
// Linker initialized.
|
|
|
|
ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_;
|
2012-12-04 05:21:22 +08:00
|
|
|
StaticSpinMutex MemoryMappingLayout::cache_lock_; // Linker initialized.
|
2012-12-01 10:39:45 +08:00
|
|
|
|
2013-03-26 18:34:37 +08:00
|
|
|
MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
|
2012-12-04 05:21:22 +08:00
|
|
|
proc_self_maps_.len =
|
|
|
|
ReadFileToBuffer("/proc/self/maps", &proc_self_maps_.data,
|
|
|
|
&proc_self_maps_.mmaped_size, 1 << 26);
|
2013-03-26 18:34:37 +08:00
|
|
|
if (cache_enabled) {
|
|
|
|
if (proc_self_maps_.mmaped_size == 0) {
|
|
|
|
LoadFromCache();
|
|
|
|
CHECK_GT(proc_self_maps_.len, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK_GT(proc_self_maps_.mmaped_size, 0);
|
2012-12-01 10:39:45 +08:00
|
|
|
}
|
2012-06-07 14:15:12 +08:00
|
|
|
Reset();
|
2012-12-01 10:39:45 +08:00
|
|
|
// FIXME: in the future we may want to cache the mappings on demand only.
|
2013-03-26 18:34:37 +08:00
|
|
|
if (cache_enabled)
|
|
|
|
CacheMemoryMappings();
|
2012-06-07 14:15:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
MemoryMappingLayout::~MemoryMappingLayout() {
|
2012-12-05 07:30:00 +08:00
|
|
|
// Only unmap the buffer if it is different from the cached one. Otherwise
|
|
|
|
// it will be unmapped when the cache is refreshed.
|
|
|
|
if (proc_self_maps_.data != cached_proc_self_maps_.data) {
|
|
|
|
UnmapOrDie(proc_self_maps_.data, proc_self_maps_.mmaped_size);
|
|
|
|
}
|
2012-06-07 14:15:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
void MemoryMappingLayout::Reset() {
|
2012-12-04 05:21:22 +08:00
|
|
|
current_ = proc_self_maps_.data;
|
2012-06-07 14:15:12 +08:00
|
|
|
}
|
|
|
|
|
2012-12-01 10:39:45 +08:00
|
|
|
// static
|
|
|
|
void MemoryMappingLayout::CacheMemoryMappings() {
|
|
|
|
SpinMutexLock l(&cache_lock_);
|
|
|
|
// Don't invalidate the cache if the mappings are unavailable.
|
2012-12-04 05:21:22 +08:00
|
|
|
ProcSelfMapsBuff old_proc_self_maps;
|
|
|
|
old_proc_self_maps = cached_proc_self_maps_;
|
|
|
|
cached_proc_self_maps_.len =
|
|
|
|
ReadFileToBuffer("/proc/self/maps", &cached_proc_self_maps_.data,
|
|
|
|
&cached_proc_self_maps_.mmaped_size, 1 << 26);
|
|
|
|
if (cached_proc_self_maps_.mmaped_size == 0) {
|
|
|
|
cached_proc_self_maps_ = old_proc_self_maps;
|
2012-12-01 10:39:45 +08:00
|
|
|
} else {
|
2012-12-04 05:21:22 +08:00
|
|
|
if (old_proc_self_maps.mmaped_size) {
|
|
|
|
UnmapOrDie(old_proc_self_maps.data,
|
|
|
|
old_proc_self_maps.mmaped_size);
|
2012-12-01 10:39:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryMappingLayout::LoadFromCache() {
|
|
|
|
SpinMutexLock l(&cache_lock_);
|
2012-12-04 05:21:22 +08:00
|
|
|
if (cached_proc_self_maps_.data) {
|
2012-12-05 18:16:02 +08:00
|
|
|
proc_self_maps_ = cached_proc_self_maps_;
|
2012-12-01 10:39:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-13 14:55:02 +08:00
|
|
|
static bool IsOneOf(char c, char c1, char c2) {
|
2012-06-29 21:05:36 +08:00
|
|
|
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,
|
2013-03-13 14:51:02 +08:00
|
|
|
char filename[], uptr filename_size,
|
|
|
|
uptr *protection) {
|
2012-12-04 05:21:22 +08:00
|
|
|
char *last = proc_self_maps_.data + proc_self_maps_.len;
|
2012-06-07 14:15:12 +08:00
|
|
|
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_++, ' ');
|
2013-03-13 14:51:02 +08:00
|
|
|
uptr local_protection = 0;
|
2013-03-13 14:55:02 +08:00
|
|
|
CHECK(IsOneOf(*current_, '-', 'r'));
|
2013-03-13 14:51:02 +08:00
|
|
|
if (*current_++ == 'r')
|
|
|
|
local_protection |= kProtectionRead;
|
2013-03-13 14:55:02 +08:00
|
|
|
CHECK(IsOneOf(*current_, '-', 'w'));
|
2013-03-13 14:51:02 +08:00
|
|
|
if (*current_++ == 'w')
|
|
|
|
local_protection |= kProtectionWrite;
|
2013-03-13 14:55:02 +08:00
|
|
|
CHECK(IsOneOf(*current_, '-', 'x'));
|
2013-03-13 14:51:02 +08:00
|
|
|
if (*current_++ == 'x')
|
|
|
|
local_protection |= kProtectionExecute;
|
2013-03-13 14:55:02 +08:00
|
|
|
CHECK(IsOneOf(*current_, 's', 'p'));
|
2013-03-13 14:51:02 +08:00
|
|
|
if (*current_++ == 's')
|
|
|
|
local_protection |= kProtectionShared;
|
|
|
|
if (protection) {
|
|
|
|
*protection = local_protection;
|
|
|
|
}
|
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[],
|
2013-03-13 14:51:02 +08:00
|
|
|
uptr filename_size,
|
|
|
|
uptr *protection) {
|
|
|
|
return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
|
|
|
|
protection);
|
2012-06-07 14:15:12 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 15:51:39 +08:00
|
|
|
enum MutexState {
|
|
|
|
MtxUnlocked = 0,
|
|
|
|
MtxLocked = 1,
|
|
|
|
MtxSleeping = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
BlockingMutex::BlockingMutex(LinkerInitialized) {
|
2013-01-14 16:21:34 +08:00
|
|
|
CHECK_EQ(owner_, 0);
|
2013-01-14 15:51:39 +08:00
|
|
|
}
|
|
|
|
|
2013-03-14 21:30:56 +08:00
|
|
|
BlockingMutex::BlockingMutex() {
|
|
|
|
internal_memset(this, 0, sizeof(*this));
|
|
|
|
}
|
|
|
|
|
2013-01-14 15:51:39 +08:00
|
|
|
void BlockingMutex::Lock() {
|
|
|
|
atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
|
|
|
|
if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
|
|
|
|
return;
|
|
|
|
while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked)
|
2013-05-08 22:43:49 +08:00
|
|
|
internal_syscall(__NR_futex, m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
|
2013-01-14 15:51:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingMutex::Unlock() {
|
|
|
|
atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
|
|
|
|
u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
|
2013-01-14 16:01:58 +08:00
|
|
|
CHECK_NE(v, MtxUnlocked);
|
2013-01-14 15:51:39 +08:00
|
|
|
if (v == MtxSleeping)
|
2013-05-08 22:43:49 +08:00
|
|
|
internal_syscall(__NR_futex, m, FUTEX_WAKE, 1, 0, 0, 0);
|
2013-01-14 15:51:39 +08:00
|
|
|
}
|
|
|
|
|
2013-03-11 23:45:20 +08:00
|
|
|
void BlockingMutex::CheckLocked() {
|
|
|
|
atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
|
|
|
|
CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:22:40 +08:00
|
|
|
// ----------------- sanitizer_linux.h
|
|
|
|
// The actual size of this structure is specified by d_reclen.
|
|
|
|
// Note that getdents64 uses a different structure format. We only provide the
|
|
|
|
// 32-bit syscall here.
|
|
|
|
struct linux_dirent {
|
|
|
|
unsigned long d_ino;
|
|
|
|
unsigned long d_off;
|
|
|
|
unsigned short d_reclen;
|
|
|
|
char d_name[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Syscall wrappers.
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_ptrace(int request, int pid, void *addr, void *data) {
|
|
|
|
return internal_syscall(__NR_ptrace, request, pid, addr, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr internal_waitpid(int pid, int *status, int options) {
|
|
|
|
return internal_syscall(__NR_wait4, pid, status, options, 0 /* rusage */);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_getpid() {
|
|
|
|
return internal_syscall(__NR_getpid);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_getppid() {
|
|
|
|
return internal_syscall(__NR_getppid);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
|
|
|
|
return internal_syscall(__NR_getdents, fd, dirp, count);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
|
|
|
|
return internal_syscall(__NR_lseek, fd, offset, whence);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
|
|
|
|
return internal_syscall(__NR_prctl, option, arg2, arg3, arg4, arg5);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr internal_sigaltstack(const struct sigaltstack *ss,
|
2013-02-27 19:22:40 +08:00
|
|
|
struct sigaltstack *oss) {
|
2013-05-08 22:43:49 +08:00
|
|
|
return internal_syscall(__NR_sigaltstack, ss, oss);
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ThreadLister implementation.
|
|
|
|
ThreadLister::ThreadLister(int pid)
|
|
|
|
: pid_(pid),
|
|
|
|
descriptor_(-1),
|
2013-04-05 15:41:21 +08:00
|
|
|
buffer_(4096),
|
2013-02-27 19:22:40 +08:00
|
|
|
error_(true),
|
2013-04-05 15:41:21 +08:00
|
|
|
entry_((struct linux_dirent *)buffer_.data()),
|
2013-02-27 19:22:40 +08:00
|
|
|
bytes_read_(0) {
|
|
|
|
char task_directory_path[80];
|
|
|
|
internal_snprintf(task_directory_path, sizeof(task_directory_path),
|
|
|
|
"/proc/%d/task/", pid);
|
2013-05-08 22:43:49 +08:00
|
|
|
uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
|
|
|
|
if (internal_iserror(openrv)) {
|
2013-02-27 19:22:40 +08:00
|
|
|
error_ = true;
|
|
|
|
Report("Can't open /proc/%d/task for reading.\n", pid);
|
|
|
|
} else {
|
|
|
|
error_ = false;
|
2013-05-08 22:43:49 +08:00
|
|
|
descriptor_ = openrv;
|
2013-02-27 19:22:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ThreadLister::GetNextTID() {
|
|
|
|
int tid = -1;
|
|
|
|
do {
|
|
|
|
if (error_)
|
|
|
|
return -1;
|
|
|
|
if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
|
|
|
|
return -1;
|
|
|
|
if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
|
|
|
|
entry_->d_name[0] <= '9') {
|
|
|
|
// Found a valid tid.
|
|
|
|
tid = (int)internal_atoll(entry_->d_name);
|
|
|
|
}
|
|
|
|
entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
|
|
|
|
} while (tid < 0);
|
|
|
|
return tid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLister::Reset() {
|
|
|
|
if (error_ || descriptor_ < 0)
|
|
|
|
return;
|
|
|
|
internal_lseek(descriptor_, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLister::~ThreadLister() {
|
|
|
|
if (descriptor_ >= 0)
|
|
|
|
internal_close(descriptor_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadLister::error() { return error_; }
|
|
|
|
|
|
|
|
bool ThreadLister::GetDirectoryEntries() {
|
|
|
|
CHECK_GE(descriptor_, 0);
|
|
|
|
CHECK_NE(error_, true);
|
|
|
|
bytes_read_ = internal_getdents(descriptor_,
|
2013-04-05 15:41:21 +08:00
|
|
|
(struct linux_dirent *)buffer_.data(),
|
|
|
|
buffer_.size());
|
2013-05-08 22:43:49 +08:00
|
|
|
if (internal_iserror(bytes_read_)) {
|
2013-02-27 19:22:40 +08:00
|
|
|
Report("Can't read directory entries from /proc/%d/task.\n", pid_);
|
|
|
|
error_ = true;
|
|
|
|
return false;
|
|
|
|
} else if (bytes_read_ == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-05 15:41:21 +08:00
|
|
|
entry_ = (struct linux_dirent *)buffer_.data();
|
2013-02-27 19:22:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:24:46 +08:00
|
|
|
// Match full names of the form /path/to/base_name{-,.}*
|
|
|
|
bool LibraryNameIs(const char *full_name, const char *base_name) {
|
|
|
|
const char *name = full_name;
|
|
|
|
// Strip path.
|
|
|
|
while (*name != '\0') name++;
|
|
|
|
while (name > full_name && *name != '/') name--;
|
|
|
|
if (*name == '/') name++;
|
|
|
|
uptr base_name_length = internal_strlen(base_name);
|
|
|
|
if (internal_strncmp(name, base_name, base_name_length)) return false;
|
|
|
|
return (name[base_name_length] == '-' || name[base_name_length] == '.');
|
|
|
|
}
|
|
|
|
|
2012-06-04 22:27:50 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2013-04-03 15:24:35 +08:00
|
|
|
#endif // SANITIZER_LINUX
|