[llvm-commits] [compiler-rt] r158078 - in /compiler-rt/trunk/lib: asan/asan_allocator.cc asan/asan_internal.h asan/asan_linux.cc asan/asan_mac.cc asan/asan_posix.cc asan/asan_rtl.cc asan/asan_thread.cc asan/asan_win.cc sanitizer_common/sanitizer_common.h sanitizer_common/sanitizer_posix.cc sanitizer_common/sanitizer_printf.cc sanitizer_common/sanitizer_win.cc
Alexey Samsonov
samsonov at google.com
Wed Jun 6 09:15:07 PDT 2012
Author: samsonov
Date: Wed Jun 6 11:15:07 2012
New Revision: 158078
URL: http://llvm.org/viewvc/llvm-project?rev=158078&view=rev
Log:
[Sanitizer] Switch to common mmap/munmap routines in ASan run-time.
Modified:
compiler-rt/trunk/lib/asan/asan_allocator.cc
compiler-rt/trunk/lib/asan/asan_internal.h
compiler-rt/trunk/lib/asan/asan_linux.cc
compiler-rt/trunk/lib/asan/asan_mac.cc
compiler-rt/trunk/lib/asan/asan_posix.cc
compiler-rt/trunk/lib/asan/asan_rtl.cc
compiler-rt/trunk/lib/asan/asan_thread.cc
compiler-rt/trunk/lib/asan/asan_win.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
Modified: compiler-rt/trunk/lib/asan/asan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator.cc Wed Jun 6 11:15:07 2012
@@ -131,7 +131,7 @@
static u8 *MmapNewPagesAndPoisonShadow(uptr size) {
CHECK(IsAligned(size, kPageSize));
- u8 *res = (u8*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
+ u8 *res = (u8*)MmapOrDie(size, __FUNCTION__);
PoisonShadow((uptr)res, size, kAsanHeapLeftRedzoneMagic);
if (FLAG_debug) {
Printf("ASAN_MMAP: [%p, %p)\n", res, res + size);
@@ -966,7 +966,7 @@
if (mem) {
PoisonShadow(mem, ClassMmapSize(i), 0);
allocated_size_classes_[i] = 0;
- AsanUnmapOrDie((void*)mem, ClassMmapSize(i));
+ UnmapOrDie((void*)mem, ClassMmapSize(i));
}
}
}
@@ -977,7 +977,7 @@
void FakeStack::AllocateOneSizeClass(uptr size_class) {
CHECK(ClassMmapSize(size_class) >= kPageSize);
- uptr new_mem = (uptr)AsanMmapSomewhereOrDie(
+ uptr new_mem = (uptr)MmapOrDie(
ClassMmapSize(size_class), __FUNCTION__);
// Printf("T%d new_mem[%zu]: %p-%p mmap %zu\n",
// asanThreadRegistry().GetCurrent()->tid(),
Modified: compiler-rt/trunk/lib/asan/asan_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_internal.h Wed Jun 6 11:15:07 2012
@@ -119,8 +119,6 @@
// asan_malloc_linux.cc / asan_malloc_mac.cc
void ReplaceSystemMalloc();
-void OutOfMemoryMessageAndDie(const char *mem_type, uptr size);
-
// asan_linux.cc / asan_mac.cc / asan_win.cc
void *AsanDoesNotSupportStaticLinkage();
bool AsanShadowRangeIsAvailable();
@@ -130,8 +128,6 @@
void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size);
void *AsanMmapFixedReserve(uptr fixed_addr, uptr size);
void *AsanMprotect(uptr fixed_addr, uptr size);
-void *AsanMmapSomewhereOrDie(uptr size, const char *where);
-void AsanUnmapOrDie(void *ptr, uptr size);
void AsanDisableCoreDumper();
void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
Modified: compiler-rt/trunk/lib/asan/asan_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_linux.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_linux.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_linux.cc Wed Jun 6 11:15:07 2012
@@ -74,17 +74,6 @@
return signum == SIGSEGV && FLAG_handle_segv;
}
-void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) {
- size = RoundUpTo(size, kPageSize);
- void *res = internal_mmap(0, size,
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON, -1, 0);
- if (res == (void*)-1) {
- OutOfMemoryMessageAndDie(mem_type, size);
- }
- return res;
-}
-
void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size) {
return internal_mmap((void*)fixed_addr, size,
PROT_READ | PROT_WRITE,
@@ -99,15 +88,6 @@
0, 0);
}
-void AsanUnmapOrDie(void *addr, uptr size) {
- if (!addr || !size) return;
- int res = internal_munmap(addr, size);
- if (res != 0) {
- Report("Failed to unmap\n");
- Die();
- }
-}
-
// Like getenv, but reads env directly from /proc and does not use libc.
// This function should be called first inside __asan_init.
const char* AsanGetEnv(const char* name) {
@@ -146,7 +126,7 @@
}
AsanProcMaps::~AsanProcMaps() {
- AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
+ UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
}
void AsanProcMaps::Reset() {
Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Wed Jun 6 11:15:07 2012
@@ -98,17 +98,6 @@
return (signum == SIGSEGV || signum == SIGBUS) && FLAG_handle_segv;
}
-void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) {
- size = RoundUpTo(size, kPageSize);
- void *res = internal_mmap(0, size,
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON, -1, 0);
- if (res == (void*)-1) {
- OutOfMemoryMessageAndDie(mem_type, size);
- }
- return res;
-}
-
void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size) {
return internal_mmap((void*)fixed_addr, size,
PROT_READ | PROT_WRITE,
@@ -123,15 +112,6 @@
0, 0);
}
-void AsanUnmapOrDie(void *addr, uptr size) {
- if (!addr || !size) return;
- int res = internal_munmap(addr, size);
- if (res != 0) {
- Report("Failed to unmap\n");
- Die();
- }
-}
-
const char *AsanGetEnv(const char *name) {
char ***env_ptr = _NSGetEnviron();
CHECK(env_ptr);
Modified: compiler-rt/trunk/lib/asan/asan_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_posix.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_posix.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_posix.cc Wed Jun 6 11:15:07 2012
@@ -104,7 +104,7 @@
// TODO(glider): the mapped stack should have the MAP_STACK flag in the
// future. It is not required by man 2 sigaltstack now (they're using
// malloc()).
- void* base = AsanMmapSomewhereOrDie(kAltStackSize, __FUNCTION__);
+ void* base = MmapOrDie(kAltStackSize, __FUNCTION__);
altstack.ss_sp = base;
altstack.ss_flags = 0;
altstack.ss_size = kAltStackSize;
@@ -122,7 +122,7 @@
altstack.ss_flags = SS_DISABLE;
altstack.ss_size = 0;
CHECK(0 == sigaltstack(&altstack, &oldstack));
- AsanUnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
+ UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
}
void InstallSignalHandlers() {
Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Wed Jun 6 11:15:07 2012
@@ -37,7 +37,7 @@
SleepForSeconds(FLAG_sleep_before_dying);
}
if (FLAG_unmap_shadow_on_exit)
- AsanUnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
+ UnmapOrDie((void*)kLowShadowBeg, kHighShadowEnd - kLowShadowBeg);
if (death_callback)
death_callback();
if (FLAG_abort_on_error)
@@ -119,8 +119,8 @@
for (uptr size = kMinFileLen; size <= max_len; size *= 2) {
fd_t fd = internal_open(file_name, /*write*/ false);
if (fd < 0) return 0;
- AsanUnmapOrDie(*buff, *buff_size);
- *buff = (char*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
+ UnmapOrDie(*buff, *buff_size);
+ *buff = (char*)MmapOrDie(size, __FUNCTION__);
*buff_size = size;
// Read up to one page at a time.
read_len = 0;
@@ -153,14 +153,6 @@
}
// ---------------------- mmap -------------------- {{{1
-void OutOfMemoryMessageAndDie(const char *mem_type, uptr size) {
- AsanReport("ERROR: AddressSanitizer failed to allocate "
- "0x%zx (%zd) bytes of %s\n",
- size, size, mem_type);
- PRINT_CURRENT_STACK();
- ShowStatsAndAbort();
-}
-
// Reserve memory range [beg, end].
static void ReserveShadowMemoryRange(uptr beg, uptr end) {
CHECK((beg % kPageSize) == 0);
@@ -176,7 +168,7 @@
if (allocated_end_ - allocated_current_ < size) {
uptr size_to_allocate = Max(size, kPageSize);
allocated_current_ =
- (char*)AsanMmapSomewhereOrDie(size_to_allocate, __FUNCTION__);
+ (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
allocated_end_ = allocated_current_ + size_to_allocate;
PoisonShadow((uptr)allocated_current_, size_to_allocate,
kAsanInternalHeapMagic);
@@ -372,7 +364,7 @@
if (callback) {
error_message_buffer_size = 1 << 16;
error_message_buffer =
- (char*)AsanMmapSomewhereOrDie(error_message_buffer_size, __FUNCTION__);
+ (char*)MmapOrDie(error_message_buffer_size, __FUNCTION__);
error_message_buffer_pos = 0;
}
}
Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Wed Jun 6 11:15:07 2012
@@ -29,7 +29,7 @@
AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine,
void *arg, AsanStackTrace *stack) {
uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
- AsanThread *thread = (AsanThread*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
+ AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
thread->start_routine_ = start_routine;
thread->arg_ = arg;
@@ -63,7 +63,7 @@
ClearShadowForThreadStack();
fake_stack().Cleanup();
uptr size = RoundUpTo(sizeof(AsanThread), kPageSize);
- AsanUnmapOrDie(this, size);
+ UnmapOrDie(this, size);
}
void AsanThread::Init() {
Modified: compiler-rt/trunk/lib/asan/asan_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win.cc Wed Jun 6 11:15:07 2012
@@ -37,22 +37,11 @@
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
}
-void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) {
- void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
- if (rv == 0)
- OutOfMemoryMessageAndDie(mem_type, size);
- return rv;
-}
-
void *AsanMprotect(uptr fixed_addr, uptr size) {
return VirtualAlloc((LPVOID)fixed_addr, size,
MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
}
-void AsanUnmapOrDie(void *addr, uptr size) {
- CHECK(VirtualFree(addr, size, MEM_DECOMMIT));
-}
-
// ---------------------- Stacktraces, symbols, etc. ---------------- {{{1
static AsanLock dbghelp_lock(LINKER_INITIALIZED);
static bool dbghelp_initialized = false;
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Wed Jun 6 11:15:07 2012
@@ -28,7 +28,9 @@
int GetPid();
void RawWrite(const char *buffer);
-void *MmapOrDie(uptr size);
+
+// Memory management
+void *MmapOrDie(uptr size, const char *mem_type);
void UnmapOrDie(void *addr, uptr size);
void Printf(const char *format, ...);
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Wed Jun 6 11:15:07 2012
@@ -28,14 +28,15 @@
return getpid();
}
-void *MmapOrDie(uptr size) {
+void *MmapOrDie(uptr size, const char *mem_type) {
size = RoundUpTo(size, kPageSize);
void *res = internal_mmap(0, size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (res == (void*)-1) {
- RawWrite("Failed to map!\n");
- Die();
+ Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
+ size, size, mem_type);
+ CHECK("unable to mmap" && 0);
}
return res;
}
@@ -44,8 +45,9 @@
if (!addr || !size) return;
int res = internal_munmap(addr, size);
if (res != 0) {
- RawWrite("Failed to unmap!\n");
- Die();
+ Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
+ size, size, addr);
+ CHECK("unable to unmap" && 0);
}
}
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc Wed Jun 6 11:15:07 2012
@@ -144,7 +144,7 @@
void Printf(const char *format, ...) {
const int kLen = 1024 * 4;
- char *buffer = (char*)MmapOrDie(kLen);
+ char *buffer = (char*)MmapOrDie(kLen, __FUNCTION__);
va_list args;
va_start(args, format);
int needed_length = VSNPrintf(buffer, kLen, format, args);
@@ -169,7 +169,7 @@
// Like Printf, but prints the current PID before the output string.
void Report(const char *format, ...) {
const int kLen = 1024 * 4;
- char *buffer = (char*)MmapOrDie(kLen);
+ char *buffer = (char*)MmapOrDie(kLen, __FUNCTION__);
int needed_length = SNPrintf(buffer, kLen, "==%d== ", GetPid());
RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
va_list args;
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=158078&r1=158077&r2=158078&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Wed Jun 6 11:15:07 2012
@@ -23,18 +23,21 @@
return GetProcessId(GetCurrentProcess());
}
-void *MmapOrDie(uptr size) {
+void *MmapOrDie(uptr size, const char *mem_type) {
void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
- if (rv == 0)
- RawWrite("Failed to map!\n");
- Die();
+ if (rv == 0) {
+ Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s\n",
+ size, size, mem_type);
+ CHECK("unable to mmap" && 0);
+ }
return rv;
}
void UnmapOrDie(void *addr, uptr size) {
if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
- RawWrite("Failed to unmap!\n");
- Die();
+ Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
+ size, size, addr);
+ CHECK("unable to unmap" && 0);
}
}
More information about the llvm-commits
mailing list