Index: compiler-rt/lib/asan/asan_malloc_linux.cc =================================================================== --- compiler-rt/lib/asan/asan_malloc_linux.cc +++ compiler-rt/lib/asan/asan_malloc_linux.cc @@ -24,6 +24,7 @@ #include "asan_allocator.h" #include "asan_interceptors.h" #include "asan_internal.h" +#include "asan_malloc_local.h" #include "asan_stack.h" // ---------------------- Replacement functions ---------------- {{{1 @@ -67,12 +68,19 @@ return 0; } -// On RTEMS, we use the local pool to handle memory allocation before -// the ASan run-time has been initialized. -static INLINE bool EarlyMalloc() { - return SANITIZER_RTEMS && (!asan_inited || asan_init_is_running); +#if SANITIZER_RTEMS +void* MemalignFromLocalPool(uptr alignment, uptr size) { + void *ptr = nullptr; + alignment = Max(alignment, kWordSize); + PosixMemalignFromLocalPool(&ptr, alignment, size); + return ptr; } +bool FreeFromLocalPool(const void *ptr) { + return IsInDlsymAllocPool(ptr); +} +#endif + static INLINE bool MaybeInDlsym() { // Fuchsia doesn't use dlsym-based interceptors. return !SANITIZER_FUCHSIA && asan_init_is_running; Index: compiler-rt/lib/asan/asan_malloc_local.h =================================================================== --- /dev/null +++ compiler-rt/lib/asan/asan_malloc_local.h @@ -0,0 +1,48 @@ +//===-- asan_malloc_local.h -------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file is a part of AddressSanitizer, an address sanity checker. +// +// Provide interfaces to check for and handle local pool memory allocation. +//===----------------------------------------------------------------------===// + +#ifndef ASAN_MALLOC_LOCAL_H +#define ASAN_MALLOC_LOCAL_H + +#include "sanitizer_common/sanitizer_platform.h" +#include "asan_internal.h" + +// On RTEMS, we use the local pool to handle memory allocation when the ASan +// run-time is not up. +static INLINE bool EarlyMalloc() { + return SANITIZER_RTEMS && (!__asan::asan_inited || + __asan::asan_init_is_running); +} + +#if SANITIZER_RTEMS + +void* MemalignFromLocalPool(uptr alignment, uptr size); +bool FreeFromLocalPool(const void *ptr); + +#define TRY_MEMALIGN_LOCAL_POOL(alignment, size) \ + if (UNLIKELY(EarlyMalloc()))\ + return MemalignFromLocalPool(alignment, size); + +#define TRY_FREE_LOCAL_POOL(ptr) \ + if (UNLIKELY(FreeFromLocalPool(ptr)))\ + return; + +#else // SANITIZER_RTEMS + +#define TRY_MEMALIGN_LOCAL_POOL(alignment, size) +#define TRY_FREE_LOCAL_POOL(ptr) + +#endif // SANITIZER_RTEMS + +#endif // ASAN_MALLOC_LOCAL_H Index: compiler-rt/lib/asan/asan_new_delete.cc =================================================================== --- compiler-rt/lib/asan/asan_new_delete.cc +++ compiler-rt/lib/asan/asan_new_delete.cc @@ -14,6 +14,7 @@ #include "asan_allocator.h" #include "asan_internal.h" +#include "asan_malloc_local.h" #include "asan_report.h" #include "asan_stack.h" @@ -70,11 +71,13 @@ // TODO(alekseyshl): throw std::bad_alloc instead of dying on OOM. #define OPERATOR_NEW_BODY(type, nothrow) \ + TRY_MEMALIGN_LOCAL_POOL(0, size);\ GET_STACK_TRACE_MALLOC;\ void *res = asan_memalign(0, size, &stack, type);\ if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\ return res; #define OPERATOR_NEW_BODY_ALIGN(type, nothrow) \ + TRY_MEMALIGN_LOCAL_POOL((uptr)align, size);\ GET_STACK_TRACE_MALLOC;\ void *res = asan_memalign((uptr)align, size, &stack, type);\ if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\ @@ -129,18 +132,22 @@ #endif // !SANITIZER_MAC #define OPERATOR_DELETE_BODY(type) \ + TRY_FREE_LOCAL_POOL(ptr);\ GET_STACK_TRACE_FREE;\ asan_delete(ptr, 0, 0, &stack, type); #define OPERATOR_DELETE_BODY_SIZE(type) \ + TRY_FREE_LOCAL_POOL(ptr);\ GET_STACK_TRACE_FREE;\ asan_delete(ptr, size, 0, &stack, type); #define OPERATOR_DELETE_BODY_ALIGN(type) \ + TRY_FREE_LOCAL_POOL(ptr);\ GET_STACK_TRACE_FREE;\ asan_delete(ptr, 0, static_cast(align), &stack, type); #define OPERATOR_DELETE_BODY_SIZE_ALIGN(type) \ + TRY_FREE_LOCAL_POOL(ptr);\ GET_STACK_TRACE_FREE;\ asan_delete(ptr, size, static_cast(align), &stack, type);