Changeset View
Changeset View
Standalone View
Standalone View
compiler-rt/lib/asan/asan_malloc_linux.cc
Show All 12 Lines | |||||
// We simply define functions like malloc, free, realloc, etc. | // We simply define functions like malloc, free, realloc, etc. | ||||
// They will replace the corresponding libc functions automagically. | // They will replace the corresponding libc functions automagically. | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "sanitizer_common/sanitizer_platform.h" | #include "sanitizer_common/sanitizer_platform.h" | ||||
#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \ | #if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \ | ||||
SANITIZER_NETBSD || SANITIZER_RTEMS || SANITIZER_SOLARIS | SANITIZER_NETBSD || SANITIZER_RTEMS || SANITIZER_SOLARIS | ||||
#include "sanitizer_common/sanitizer_allocator_checks.h" | |||||
#include "sanitizer_common/sanitizer_errno.h" | |||||
#include "sanitizer_common/sanitizer_tls_get_addr.h" | #include "sanitizer_common/sanitizer_tls_get_addr.h" | ||||
#include "asan_allocator.h" | #include "asan_allocator.h" | ||||
#include "asan_interceptors.h" | #include "asan_interceptors.h" | ||||
#include "asan_internal.h" | #include "asan_internal.h" | ||||
#include "asan_stack.h" | #include "asan_stack.h" | ||||
// ---------------------- Replacement functions ---------------- {{{1 | // ---------------------- Replacement functions ---------------- {{{1 | ||||
using namespace __asan; // NOLINT | using namespace __asan; // NOLINT | ||||
Show All 10 Lines | |||||
static void *AllocateFromLocalPool(uptr size_in_bytes) { | static void *AllocateFromLocalPool(uptr size_in_bytes) { | ||||
uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize; | uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize; | ||||
void *mem = (void*)&alloc_memory_for_dlsym[allocated_for_dlsym]; | void *mem = (void*)&alloc_memory_for_dlsym[allocated_for_dlsym]; | ||||
allocated_for_dlsym += size_in_words; | allocated_for_dlsym += size_in_words; | ||||
CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize); | CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize); | ||||
return mem; | return mem; | ||||
} | } | ||||
static int PosixMemalignFromLocalPool(void **memptr, uptr alignment, | |||||
uptr size_in_bytes) { | |||||
if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) | |||||
return errno_EINVAL; | |||||
CHECK(alignment >= kWordSize); | |||||
uptr addr = (uptr)&alloc_memory_for_dlsym[allocated_for_dlsym]; | |||||
uptr aligned_addr = RoundUpTo(addr, alignment); | |||||
uptr aligned_size = RoundUpTo(size_in_bytes, kWordSize); | |||||
uptr *end_mem = (uptr*)(aligned_addr + aligned_size); | |||||
uptr allocated = end_mem - alloc_memory_for_dlsym; | |||||
if (allocated >= kDlsymAllocPoolSize) | |||||
return errno_ENOMEM; | |||||
allocated_for_dlsym = allocated; | |||||
*memptr = (void*)aligned_addr; | |||||
return 0; | |||||
} | |||||
// On RTEMS, we use the local pool to handle memory allocation before | // On RTEMS, we use the local pool to handle memory allocation before | ||||
// the ASan run-time has been initialized. | // the ASan run-time has been initialized. | ||||
static INLINE bool EarlyMalloc() { | static INLINE bool EarlyMalloc() { | ||||
return SANITIZER_RTEMS && (!asan_inited || asan_init_is_running); | return SANITIZER_RTEMS && (!asan_inited || asan_init_is_running); | ||||
} | } | ||||
static INLINE bool MaybeInDlsym() { | static INLINE bool MaybeInDlsym() { | ||||
// Fuchsia doesn't use dlsym-based interceptors. | // Fuchsia doesn't use dlsym-based interceptors. | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
INTERCEPTOR(int, mallopt, int cmd, int value) { | INTERCEPTOR(int, mallopt, int cmd, int value) { | ||||
return -1; | return -1; | ||||
} | } | ||||
#endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO | #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO | ||||
INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) { | INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) { | ||||
if (UNLIKELY(UseLocalPool())) | |||||
return PosixMemalignFromLocalPool(memptr, alignment, size); | |||||
GET_STACK_TRACE_MALLOC; | GET_STACK_TRACE_MALLOC; | ||||
// Printf("posix_memalign: %zx %zu\n", alignment, size); | |||||
return asan_posix_memalign(memptr, alignment, size, &stack); | return asan_posix_memalign(memptr, alignment, size, &stack); | ||||
alekseyshl: While you're here, please remove this printf too. | |||||
} | } | ||||
INTERCEPTOR(void*, valloc, uptr size) { | INTERCEPTOR(void*, valloc, uptr size) { | ||||
Let's do it for all platforms, for simplicity and match the malloc implementation. alekseyshl: Let's do it for all platforms, for simplicity and match the malloc implementation. | |||||
Ok. waltl: Ok.
| |||||
GET_STACK_TRACE_MALLOC; | GET_STACK_TRACE_MALLOC; | ||||
return asan_valloc(size, &stack); | return asan_valloc(size, &stack); | ||||
} | } | ||||
#if SANITIZER_INTERCEPT_PVALLOC | #if SANITIZER_INTERCEPT_PVALLOC | ||||
INTERCEPTOR(void*, pvalloc, uptr size) { | INTERCEPTOR(void*, pvalloc, uptr size) { | ||||
GET_STACK_TRACE_MALLOC; | GET_STACK_TRACE_MALLOC; | ||||
return asan_pvalloc(size, &stack); | return asan_pvalloc(size, &stack); | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |
While you're here, please remove this printf too.