From dc8d1f1039564940a44da00bcb755829dbf5256f Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Mon, 27 Aug 2012 09:30:58 +0000 Subject: [PATCH] [Sanitizer] move low-level (mmap-based) allocator to sanitizer_common llvm-svn: 162663 --- compiler-rt/lib/asan/asan_internal.h | 15 ----------- compiler-rt/lib/asan/asan_rtl.cc | 22 ++++++---------- compiler-rt/lib/asan/asan_thread_registry.cc | 2 +- .../sanitizer_common/sanitizer_allocator.cc | 25 +++++++++++++++++++ .../lib/sanitizer_common/sanitizer_common.h | 15 +++++++++++ .../sanitizer_internal_defs.h | 2 ++ 6 files changed, 50 insertions(+), 31 deletions(-) diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h index d72a5569549b..a283c2b8e743 100644 --- a/compiler-rt/lib/asan/asan_internal.h +++ b/compiler-rt/lib/asan/asan_internal.h @@ -141,8 +141,6 @@ extern int asan_inited; extern bool asan_init_is_running; extern void (*death_callback)(void); -enum LinkerInitialized { LINKER_INITIALIZED = 0 }; - #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) #if !defined(_WIN32) || defined(__clang__) @@ -177,19 +175,6 @@ const int kAsanInternalHeapMagic = 0xfe; static const uptr kCurrentStackFrameMagic = 0x41B58AB3; static const uptr kRetiredStackFrameMagic = 0x45E0360E; -// -------------------------- LowLevelAllocator ----- {{{1 -// A simple low-level memory allocator for internal use. -class LowLevelAllocator { - public: - explicit LowLevelAllocator(LinkerInitialized) {} - // 'size' must be a power of two. - // Requires an external lock. - void *Allocate(uptr size); - private: - char *allocated_end_; - char *allocated_current_; -}; - } // namespace __asan #endif // ASAN_INTERNAL_H diff --git a/compiler-rt/lib/asan/asan_rtl.cc b/compiler-rt/lib/asan/asan_rtl.cc index e00cc5f4384b..853a844bc986 100644 --- a/compiler-rt/lib/asan/asan_rtl.cc +++ b/compiler-rt/lib/asan/asan_rtl.cc @@ -172,21 +172,9 @@ static void ReserveShadowMemoryRange(uptr beg, uptr end) { CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed"); } -// ---------------------- LowLevelAllocator ------------- {{{1 -void *LowLevelAllocator::Allocate(uptr size) { - CHECK((size & (size - 1)) == 0 && "size must be a power of two"); - if (allocated_end_ - allocated_current_ < (sptr)size) { - uptr size_to_allocate = Max(size, kPageSize); - allocated_current_ = - (char*)MmapOrDie(size_to_allocate, __FUNCTION__); - allocated_end_ = allocated_current_ + size_to_allocate; - PoisonShadow((uptr)allocated_current_, size_to_allocate, - kAsanInternalHeapMagic); - } - CHECK(allocated_end_ - allocated_current_ >= (sptr)size); - void *res = allocated_current_; - allocated_current_ += size; - return res; +// --------------- LowLevelAllocateCallbac ---------- {{{1 +static void OnLowLevelAllocate(uptr ptr, uptr size) { + PoisonShadow(ptr, size, kAsanInternalHeapMagic); } // -------------------------- Run-time entry ------------------- {{{1 @@ -290,8 +278,12 @@ void NOINLINE __asan_set_death_callback(void (*callback)(void)) { void __asan_init() { if (asan_inited) return; + CHECK(!asan_init_is_running && "ASan init calls itself!"); asan_init_is_running = true; + // Setup internal allocator callback. + SetLowLevelAllocateCallback(OnLowLevelAllocate); + // Make sure we are not statically linked. AsanDoesNotSupportStaticLinkage(); diff --git a/compiler-rt/lib/asan/asan_thread_registry.cc b/compiler-rt/lib/asan/asan_thread_registry.cc index 4540d589c552..7c9747bd76b4 100644 --- a/compiler-rt/lib/asan/asan_thread_registry.cc +++ b/compiler-rt/lib/asan/asan_thread_registry.cc @@ -20,7 +20,7 @@ namespace __asan { -static AsanThreadRegistry asan_thread_registry(__asan::LINKER_INITIALIZED); +static AsanThreadRegistry asan_thread_registry(LINKER_INITIALIZED); AsanThreadRegistry &asanThreadRegistry() { return asan_thread_registry; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc index 816fddf1c5ad..b08434ad6680 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc @@ -56,4 +56,29 @@ void *InternalAllocBlock(void *p) { return pp + 1; } +// LowLevelAllocator +static LowLevelAllocateCallback low_level_alloc_callback; + +void *LowLevelAllocator::Allocate(uptr size) { + CHECK((size & (size - 1)) == 0 && "size must be a power of two"); + if (allocated_end_ - allocated_current_ < (sptr)size) { + uptr size_to_allocate = Max(size, kPageSize); + allocated_current_ = + (char*)MmapOrDie(size_to_allocate, __FUNCTION__); + allocated_end_ = allocated_current_ + size_to_allocate; + if (low_level_alloc_callback) { + low_level_alloc_callback((uptr)allocated_current_, + size_to_allocate); + } + } + CHECK(allocated_end_ - allocated_current_ >= (sptr)size); + void *res = allocated_current_; + allocated_current_ += size; + return res; +} + +void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) { + low_level_alloc_callback = callback; +} + } // namespace __sanitizer diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 0e9371fa23e6..d5b8f6231c0a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -78,6 +78,21 @@ class InternalScopedBuffer { void operator=(const InternalScopedBuffer&); }; +// Simple low-level (mmap-based) allocator for internal use. +class LowLevelAllocator { + public: + explicit LowLevelAllocator(LinkerInitialized) {} + // 'size' must be a power of two. Requires an external lock. + void *Allocate(uptr size); + private: + char *allocated_end_; + char *allocated_current_; +}; +typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size); +// Allows to register tool-specific callbacks for LowLevelAllocator. +// Passing NULL removes the callback. +void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback); + // IO void RawWrite(const char *buffer); void Printf(const char *format, ...); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h index b8cf61fad84a..a68cab25c141 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h @@ -160,4 +160,6 @@ void NORETURN CheckFailed(const char *file, int line, const char *cond, #undef UINT64_MAX #define UINT64_MAX (__UINT64_C(18446744073709551615)) +enum LinkerInitialized { LINKER_INITIALIZED = 0 }; + #endif // SANITIZER_DEFS_H