2012-11-12 16:53:47 +01:00
|
|
|
//===-- asan_malloc_linux.cc ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Linux-specific malloc interception.
|
|
|
|
// We simply define functions like malloc, free, realloc, etc.
|
|
|
|
// They will replace the corresponding libc functions automagically.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-11-04 22:33:31 +01:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2014-05-22 09:09:21 +02:00
|
|
|
#if SANITIZER_FREEBSD || SANITIZER_LINUX
|
2012-11-12 16:53:47 +01:00
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
#include "sanitizer_common/sanitizer_tls_get_addr.h"
|
2012-11-12 16:53:47 +01:00
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_stack.h"
|
|
|
|
|
|
|
|
// ---------------------- Replacement functions ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
2016-05-17 11:17:54 +02:00
|
|
|
static uptr allocated_for_dlsym;
|
|
|
|
static const uptr kDlsymAllocPoolSize = 1024;
|
|
|
|
static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];
|
|
|
|
|
|
|
|
static bool IsInDlsymAllocPool(const void *ptr) {
|
|
|
|
uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
|
|
|
|
return off < sizeof(alloc_memory_for_dlsym);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *AllocateFromLocalPool(uptr size_in_bytes) {
|
|
|
|
uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
|
|
|
|
void *mem = (void*)&alloc_memory_for_dlsym[allocated_for_dlsym];
|
|
|
|
allocated_for_dlsym += size_in_words;
|
|
|
|
CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
INTERCEPTOR(void, free, void *ptr) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_FREE;
|
2016-05-17 11:17:54 +02:00
|
|
|
if (UNLIKELY(IsInDlsymAllocPool(ptr)))
|
|
|
|
return;
|
2013-01-10 13:44:08 +01:00
|
|
|
asan_free(ptr, &stack, FROM_MALLOC);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void, cfree, void *ptr) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_FREE;
|
2016-05-17 11:17:54 +02:00
|
|
|
if (UNLIKELY(IsInDlsymAllocPool(ptr)))
|
|
|
|
return;
|
2013-01-10 13:44:08 +01:00
|
|
|
asan_free(ptr, &stack, FROM_MALLOC);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, malloc, uptr size) {
|
2016-05-17 11:17:54 +02:00
|
|
|
if (UNLIKELY(!asan_inited))
|
|
|
|
// Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
|
|
|
|
return AllocateFromLocalPool(size);
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-11-12 16:53:47 +01:00
|
|
|
return asan_malloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
|
2016-05-17 11:17:54 +02:00
|
|
|
if (UNLIKELY(!asan_inited))
|
2012-11-12 16:53:47 +01:00
|
|
|
// Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
|
2016-05-17 11:17:54 +02:00
|
|
|
return AllocateFromLocalPool(nmemb * size);
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-11-12 16:53:47 +01:00
|
|
|
return asan_calloc(nmemb, size, &stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2016-05-17 11:17:54 +02:00
|
|
|
if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
|
|
|
|
uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
|
|
|
|
uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
|
2016-11-08 23:04:09 +01:00
|
|
|
void *new_ptr;
|
|
|
|
if (UNLIKELY(!asan_inited)) {
|
|
|
|
new_ptr = AllocateFromLocalPool(size);
|
|
|
|
} else {
|
|
|
|
copy_size = size;
|
|
|
|
new_ptr = asan_malloc(copy_size, &stack);
|
|
|
|
}
|
2016-05-17 11:17:54 +02:00
|
|
|
internal_memcpy(new_ptr, ptr, copy_size);
|
|
|
|
return new_ptr;
|
|
|
|
}
|
2012-11-12 16:53:47 +01:00
|
|
|
return asan_realloc(ptr, size, &stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
return asan_memalign(boundary, size, &stack, FROM_MALLOC);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
|
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
return asan_memalign(boundary, size, &stack, FROM_MALLOC);
|
|
|
|
}
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
|
|
|
|
GET_STACK_TRACE_MALLOC;
|
|
|
|
void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
|
2016-11-08 23:04:09 +01:00
|
|
|
DTLS_on_libc_memalign(res, size);
|
2014-05-22 09:09:21 +02:00
|
|
|
return res;
|
|
|
|
}
|
2012-11-12 16:53:47 +01:00
|
|
|
|
|
|
|
INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
|
2013-12-05 10:18:38 +01:00
|
|
|
GET_CURRENT_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
return asan_malloc_usable_size(ptr, pc, bp);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We avoid including malloc.h for portability reasons.
|
|
|
|
// man mallinfo says the fields are "long", but the implementation uses int.
|
|
|
|
// It doesn't matter much -- we just need to make sure that the libc's mallinfo
|
|
|
|
// is not called.
|
|
|
|
struct fake_mallinfo {
|
|
|
|
int x[10];
|
|
|
|
};
|
|
|
|
|
|
|
|
INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
|
|
|
|
struct fake_mallinfo res;
|
|
|
|
REAL(memset)(&res, 0, sizeof(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, mallopt, int cmd, int value) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-11-12 16:53:47 +01:00
|
|
|
// Printf("posix_memalign: %zx %zu\n", alignment, size);
|
|
|
|
return asan_posix_memalign(memptr, alignment, size, &stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, valloc, uptr size) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-11-12 16:53:47 +01:00
|
|
|
return asan_valloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(void*, pvalloc, uptr size) {
|
2013-01-10 13:44:08 +01:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-11-12 16:53:47 +01:00
|
|
|
return asan_pvalloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
2013-01-10 13:44:08 +01:00
|
|
|
INTERCEPTOR(void, malloc_stats, void) {
|
|
|
|
__asan_print_accumulated_stats();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
// Format of __libc_malloc_dispatch has changed in Android L.
|
|
|
|
// While we are moving towards a solution that does not depend on bionic
|
|
|
|
// internals, here is something to support both K* and L releases.
|
|
|
|
struct MallocDebugK {
|
|
|
|
void *(*malloc)(uptr bytes);
|
|
|
|
void (*free)(void *mem);
|
|
|
|
void *(*calloc)(uptr n_elements, uptr elem_size);
|
|
|
|
void *(*realloc)(void *oldMem, uptr bytes);
|
|
|
|
void *(*memalign)(uptr alignment, uptr bytes);
|
|
|
|
uptr (*malloc_usable_size)(void *mem);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MallocDebugL {
|
|
|
|
void *(*calloc)(uptr n_elements, uptr elem_size);
|
|
|
|
void (*free)(void *mem);
|
|
|
|
fake_mallinfo (*mallinfo)(void);
|
|
|
|
void *(*malloc)(uptr bytes);
|
|
|
|
uptr (*malloc_usable_size)(void *mem);
|
|
|
|
void *(*memalign)(uptr alignment, uptr bytes);
|
|
|
|
int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
|
|
|
|
void* (*pvalloc)(uptr size);
|
|
|
|
void *(*realloc)(void *oldMem, uptr bytes);
|
|
|
|
void* (*valloc)(uptr size);
|
|
|
|
};
|
|
|
|
|
|
|
|
ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
|
|
|
|
WRAP(malloc), WRAP(free), WRAP(calloc),
|
|
|
|
WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
|
|
|
|
|
|
|
|
ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
|
|
|
|
WRAP(calloc), WRAP(free), WRAP(mallinfo),
|
|
|
|
WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
|
|
|
|
WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
|
|
|
|
WRAP(valloc)};
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
void ReplaceSystemMalloc() {
|
|
|
|
void **__libc_malloc_dispatch_p =
|
|
|
|
(void **)AsanDlSymNext("__libc_malloc_dispatch");
|
|
|
|
if (__libc_malloc_dispatch_p) {
|
|
|
|
// Decide on K vs L dispatch format by the presence of
|
|
|
|
// __libc_malloc_default_dispatch export in libc.
|
|
|
|
void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
|
|
|
|
if (default_dispatch_p)
|
|
|
|
*__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
|
|
|
|
else
|
|
|
|
*__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#else // SANITIZER_ANDROID
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
void ReplaceSystemMalloc() {
|
|
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
#endif // SANITIZER_ANDROID
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX
|