2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_malloc_linux.cc ----------------------------------------------===//
|
2011-11-30 09:07:02 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Linux-specific malloc interception.
|
|
|
|
// We simply define functions like malloc, free, realloc, etc.
|
|
|
|
// They will replace the corresponding libc functions automagically.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
|
|
#if SANITIZER_LINUX
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_stack.h"
|
|
|
|
|
2013-03-19 21:54:41 +08:00
|
|
|
#if SANITIZER_ANDROID
|
2012-09-12 17:42:23 +08:00
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size)
|
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
|
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void*, calloc, uptr nmemb, uptr size)
|
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void*, realloc, void *ptr, uptr size)
|
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void*, memalign, uptr boundary, uptr size)
|
2012-08-06 23:13:22 +08:00
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
struct MallocDebug {
|
2012-06-25 17:58:29 +08:00
|
|
|
void* (*malloc)(uptr bytes);
|
2011-11-30 09:07:02 +08:00
|
|
|
void (*free)(void* mem);
|
2012-06-25 17:58:29 +08:00
|
|
|
void* (*calloc)(uptr n_elements, uptr elem_size);
|
|
|
|
void* (*realloc)(void* oldMem, uptr bytes);
|
|
|
|
void* (*memalign)(uptr alignment, uptr bytes);
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
2012-02-03 16:37:19 +08:00
|
|
|
const MallocDebug asan_malloc_dispatch ALIGNED(32) = {
|
2012-08-06 23:13:22 +08:00
|
|
|
WRAP(malloc), WRAP(free), WRAP(calloc), WRAP(realloc), WRAP(memalign)
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" const MallocDebug* __libc_malloc_dispatch;
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
void ReplaceSystemMalloc() {
|
|
|
|
__libc_malloc_dispatch = &asan_malloc_dispatch;
|
|
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#else // ANDROID
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
void ReplaceSystemMalloc() {
|
|
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
#endif // ANDROID
|
|
|
|
|
|
|
|
// ---------------------- Replacement functions ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, free, void *ptr) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_FREE;
|
2012-12-21 16:53:59 +08:00
|
|
|
asan_free(ptr, &stack, FROM_MALLOC);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, cfree, void *ptr) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_FREE;
|
2012-12-21 16:53:59 +08:00
|
|
|
asan_free(ptr, &stack, FROM_MALLOC);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, malloc, uptr size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_malloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
if (!asan_inited) {
|
2012-02-08 21:45:31 +08:00
|
|
|
// Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
|
2012-06-25 17:58:29 +08:00
|
|
|
const uptr kCallocPoolSize = 1024;
|
2012-05-31 22:35:53 +08:00
|
|
|
static uptr calloc_memory_for_dlsym[kCallocPoolSize];
|
2012-06-25 17:58:29 +08:00
|
|
|
static uptr allocated;
|
|
|
|
uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
|
2011-11-30 09:07:02 +08:00
|
|
|
void *mem = (void*)&calloc_memory_for_dlsym[allocated];
|
|
|
|
allocated += size_in_words;
|
|
|
|
CHECK(allocated < kCallocPoolSize);
|
|
|
|
return mem;
|
|
|
|
}
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_calloc(nmemb, size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_realloc(ptr, size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-12-21 16:53:59 +08:00
|
|
|
return asan_memalign(boundary, size, &stack, FROM_MALLOC);
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, __libc_memalign, uptr align, uptr s)
|
2012-02-03 16:37:19 +08:00
|
|
|
ALIAS("memalign");
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
|
2013-11-13 22:46:58 +08:00
|
|
|
GET_CURRENT_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
return asan_malloc_usable_size(ptr, pc, bp);
|
2012-01-17 14:39:10 +08:00
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08: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;
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(memset)(&res, 0, sizeof(res));
|
2011-11-30 09:07:02 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(int, mallopt, int cmd, int value) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-03-21 19:32:46 +08:00
|
|
|
// Printf("posix_memalign: %zx %zu\n", alignment, size);
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_posix_memalign(memptr, alignment, size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, valloc, uptr size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_valloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:58:29 +08:00
|
|
|
INTERCEPTOR(void*, pvalloc, uptr size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2011-11-30 09:07:02 +08:00
|
|
|
return asan_pvalloc(size, &stack);
|
|
|
|
}
|
2011-12-02 05:40:52 +08:00
|
|
|
|
2012-12-20 19:54:21 +08:00
|
|
|
INTERCEPTOR(void, malloc_stats, void) {
|
2012-12-26 20:20:35 +08:00
|
|
|
__asan_print_accumulated_stats();
|
2012-12-20 19:54:21 +08:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:29:53 +08:00
|
|
|
#endif // SANITIZER_LINUX
|