2012-06-05 17:49:25 +08:00
|
|
|
//===-- sanitizer_posix.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 POSIX-specific functions from
|
|
|
|
// sanitizer_libc.h.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
#include "sanitizer_common.h"
|
2012-06-05 17:49:25 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2012-06-15 14:08:19 +08:00
|
|
|
#include "sanitizer_procmaps.h"
|
2012-06-05 17:49:25 +08:00
|
|
|
|
2012-08-14 23:18:40 +08:00
|
|
|
#include <errno.h>
|
2012-06-15 14:37:34 +08:00
|
|
|
#include <pthread.h>
|
2012-06-05 17:49:25 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2012-06-15 14:37:34 +08:00
|
|
|
#include <stdlib.h>
|
2012-08-14 23:18:40 +08:00
|
|
|
#include <string.h>
|
2012-06-06 17:26:25 +08:00
|
|
|
#include <sys/mman.h>
|
2012-06-15 14:08:19 +08:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
2012-06-06 17:26:25 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2012-06-05 17:49:25 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
// ------------- sanitizer_common.h
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetPageSize() {
|
|
|
|
return sysconf(_SC_PAGESIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr GetMmapGranularity() {
|
|
|
|
return GetPageSize();
|
|
|
|
}
|
2012-06-15 14:08:19 +08:00
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
int GetPid() {
|
|
|
|
return getpid();
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
uptr GetThreadSelf() {
|
|
|
|
return (uptr)pthread_self();
|
|
|
|
}
|
|
|
|
|
2012-06-07 00:15:07 +08:00
|
|
|
void *MmapOrDie(uptr size, const char *mem_type) {
|
2012-11-23 23:38:49 +08:00
|
|
|
size = RoundUpTo(size, GetPageSizeCached());
|
2012-06-06 17:26:25 +08:00
|
|
|
void *res = internal_mmap(0, size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
|
|
if (res == (void*)-1) {
|
2012-10-04 15:21:09 +08:00
|
|
|
static int recursion_count;
|
|
|
|
if (recursion_count) {
|
|
|
|
// The Report() and CHECK calls below may call mmap recursively and fail.
|
|
|
|
// If we went into recursion, just die.
|
2013-01-29 17:39:58 +08:00
|
|
|
RawWrite("ERROR: Failed to mmap\n");
|
2012-10-04 15:21:09 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
recursion_count++;
|
2013-01-31 22:11:21 +08:00
|
|
|
Report("ERROR: %s failed to allocate 0x%zx (%zd) bytes of %s: %s\n",
|
|
|
|
SanitizerToolName, size, size, mem_type, strerror(errno));
|
2012-08-14 23:18:40 +08:00
|
|
|
DumpProcessMap();
|
2012-06-07 00:15:07 +08:00
|
|
|
CHECK("unable to mmap" && 0);
|
2012-06-06 17:26:25 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnmapOrDie(void *addr, uptr size) {
|
|
|
|
if (!addr || !size) return;
|
|
|
|
int res = internal_munmap(addr, size);
|
|
|
|
if (res != 0) {
|
2013-01-31 22:11:21 +08:00
|
|
|
Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
|
|
|
|
SanitizerToolName, size, size, addr);
|
2012-06-07 00:15:07 +08:00
|
|
|
CHECK("unable to unmap" && 0);
|
2012-06-06 17:26:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:42:58 +08:00
|
|
|
void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
|
|
|
|
RoundUpTo(size, PageSize),
|
2012-11-07 00:48:46 +08:00
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
|
|
|
|
-1, 0);
|
|
|
|
if (p == (void*)-1)
|
2013-01-31 22:11:21 +08:00
|
|
|
Report("ERROR: "
|
|
|
|
"%s failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
|
|
|
|
SanitizerToolName, size, size, fixed_addr, errno);
|
2012-11-06 23:39:16 +08:00
|
|
|
return p;
|
2012-06-14 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
2012-12-13 13:36:00 +08:00
|
|
|
void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
|
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
|
|
|
|
RoundUpTo(size, PageSize),
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
|
|
|
|
-1, 0);
|
|
|
|
if (p == (void*)-1) {
|
2013-01-31 22:11:21 +08:00
|
|
|
Report("ERROR:"
|
|
|
|
" %s failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
|
|
|
|
SanitizerToolName, size, size, fixed_addr, errno);
|
2012-12-13 13:36:00 +08:00
|
|
|
CHECK("unable to mmap" && 0);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:42:58 +08:00
|
|
|
void *Mprotect(uptr fixed_addr, uptr size) {
|
|
|
|
return internal_mmap((void*)fixed_addr, size,
|
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
|
2012-06-18 17:42:39 +08:00
|
|
|
-1, 0);
|
2012-06-14 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 15:37:24 +08:00
|
|
|
void FlushUnneededShadowMemory(uptr addr, uptr size) {
|
|
|
|
madvise((void*)addr, size, MADV_DONTNEED);
|
|
|
|
}
|
|
|
|
|
2012-07-03 16:24:14 +08:00
|
|
|
void *MapFileToMemory(const char *file_name, uptr *buff_size) {
|
2013-02-01 23:58:46 +08:00
|
|
|
fd_t fd = OpenFile(file_name, false);
|
2012-07-03 16:24:14 +08:00
|
|
|
CHECK_NE(fd, kInvalidFd);
|
|
|
|
uptr fsize = internal_filesize(fd);
|
|
|
|
CHECK_NE(fsize, (uptr)-1);
|
|
|
|
CHECK_GT(fsize, 0);
|
2012-11-23 23:38:49 +08:00
|
|
|
*buff_size = RoundUpTo(fsize, GetPageSizeCached());
|
2012-07-03 16:24:14 +08:00
|
|
|
void *map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
return (map == MAP_FAILED) ? 0 : map;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-15 15:29:14 +08:00
|
|
|
static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
|
|
|
|
uptr start2, uptr end2) {
|
|
|
|
CHECK(start1 <= end1);
|
|
|
|
CHECK(start2 <= end2);
|
|
|
|
return (end1 < start2) || (end2 < start1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this is thread-unsafe, but should not cause problems most of the time.
|
|
|
|
// When the shadow is mapped only a single thread usually exists (plus maybe
|
|
|
|
// several worker threads on Mac, which aren't expected to map big chunks of
|
|
|
|
// memory).
|
|
|
|
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
|
2012-08-27 21:48:48 +08:00
|
|
|
MemoryMappingLayout procmaps;
|
2012-06-15 15:29:14 +08:00
|
|
|
uptr start, end;
|
|
|
|
while (procmaps.Next(&start, &end,
|
|
|
|
/*offset*/0, /*filename*/0, /*filename_size*/0)) {
|
|
|
|
if (!IntervalsAreSeparate(start, end, range_start, range_end))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
void DumpProcessMap() {
|
2012-08-27 21:48:48 +08:00
|
|
|
MemoryMappingLayout proc_maps;
|
2012-06-15 14:08:19 +08:00
|
|
|
uptr start, end;
|
|
|
|
const sptr kBufSize = 4095;
|
2012-06-15 15:41:23 +08:00
|
|
|
char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
|
2012-06-15 14:08:19 +08:00
|
|
|
Report("Process memory map follows:\n");
|
|
|
|
while (proc_maps.Next(&start, &end, /* file_offset */0,
|
|
|
|
filename, kBufSize)) {
|
|
|
|
Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
|
|
|
|
}
|
|
|
|
Report("End of process memory map.\n");
|
2012-06-15 15:41:23 +08:00
|
|
|
UnmapOrDie(filename, kBufSize);
|
2012-06-15 14:08:19 +08:00
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
const char *GetPwd() {
|
|
|
|
return GetEnv("PWD");
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
void DisableCoreDumper() {
|
|
|
|
struct rlimit nocore;
|
|
|
|
nocore.rlim_cur = 0;
|
|
|
|
nocore.rlim_max = 0;
|
|
|
|
setrlimit(RLIMIT_CORE, &nocore);
|
|
|
|
}
|
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
bool StackSizeIsUnlimited() {
|
|
|
|
struct rlimit rlim;
|
|
|
|
CHECK_EQ(0, getrlimit(RLIMIT_STACK, &rlim));
|
|
|
|
return (rlim.rlim_cur == (uptr)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetStackSizeLimitInBytes(uptr limit) {
|
|
|
|
struct rlimit rlim;
|
|
|
|
rlim.rlim_cur = limit;
|
|
|
|
rlim.rlim_max = limit;
|
2012-11-28 20:19:50 +08:00
|
|
|
if (setrlimit(RLIMIT_STACK, &rlim)) {
|
2013-01-31 22:11:21 +08:00
|
|
|
Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
|
2012-11-28 20:19:50 +08:00
|
|
|
Die();
|
|
|
|
}
|
2012-09-17 17:12:39 +08:00
|
|
|
CHECK(!StackSizeIsUnlimited());
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
void SleepForSeconds(int seconds) {
|
|
|
|
sleep(seconds);
|
|
|
|
}
|
|
|
|
|
2012-06-18 16:44:30 +08:00
|
|
|
void SleepForMillis(int millis) {
|
|
|
|
usleep(millis * 1000);
|
|
|
|
}
|
|
|
|
|
2012-06-15 14:37:34 +08:00
|
|
|
void Exit(int exitcode) {
|
|
|
|
_exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Abort() {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Atexit(void (*function)(void)) {
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef SANITIZER_GO
|
2012-06-15 14:37:34 +08:00
|
|
|
return atexit(function);
|
2012-07-06 00:18:28 +08:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2012-06-15 14:37:34 +08:00
|
|
|
}
|
|
|
|
|
2012-11-02 23:18:34 +08:00
|
|
|
int internal_isatty(fd_t fd) {
|
|
|
|
return isatty(fd);
|
|
|
|
}
|
|
|
|
|
2012-06-05 17:49:25 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // __linux__ || __APPLE_
|