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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-12-02 05:40:52 +08:00
|
|
|
#ifdef __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"
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
struct MallocDebug {
|
|
|
|
void* (*malloc)(size_t bytes);
|
|
|
|
void (*free)(void* mem);
|
|
|
|
void* (*calloc)(size_t n_elements, size_t elem_size);
|
|
|
|
void* (*realloc)(void* oldMem, size_t bytes);
|
|
|
|
void* (*memalign)(size_t alignment, size_t bytes);
|
|
|
|
};
|
|
|
|
|
2012-02-03 16:37:19 +08:00
|
|
|
const MallocDebug asan_malloc_dispatch ALIGNED(32) = {
|
2011-11-30 09:07:02 +08:00
|
|
|
malloc, free, calloc, realloc, memalign
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_FREE(ptr);
|
|
|
|
asan_free(ptr, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void, cfree, void *ptr) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_FREE(ptr);
|
|
|
|
asan_free(ptr, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, malloc, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_malloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, calloc, size_t nmemb, size_t 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.
|
2011-11-30 09:07:02 +08:00
|
|
|
const size_t kCallocPoolSize = 1024;
|
2012-05-31 22:35:53 +08:00
|
|
|
static uptr calloc_memory_for_dlsym[kCallocPoolSize];
|
2011-11-30 09:07:02 +08:00
|
|
|
static size_t allocated;
|
|
|
|
size_t size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
|
|
|
|
void *mem = (void*)&calloc_memory_for_dlsym[allocated];
|
|
|
|
allocated += size_in_words;
|
|
|
|
CHECK(allocated < kCallocPoolSize);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_calloc(nmemb, size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, realloc, void *ptr, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_realloc(ptr, size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, memalign, size_t boundary, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_memalign(boundary, size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, __libc_memalign, size_t align, size_t s)
|
2012-02-03 16:37:19 +08:00
|
|
|
ALIAS("memalign");
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(size_t, malloc_usable_size, void *ptr) {
|
2012-01-17 14:39:10 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_malloc_usable_size(ptr, &stack);
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:53:10 +08:00
|
|
|
INTERCEPTOR(struct mallinfo, mallinfo, void) {
|
2011-11-30 09:07:02 +08:00
|
|
|
struct 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-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_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-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, valloc, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_valloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
2012-02-02 18:39:40 +08:00
|
|
|
INTERCEPTOR(void*, pvalloc, size_t size) {
|
2011-11-30 09:07:02 +08:00
|
|
|
GET_STACK_TRACE_HERE_FOR_MALLOC;
|
|
|
|
return asan_pvalloc(size, &stack);
|
|
|
|
}
|
2011-12-02 05:40:52 +08:00
|
|
|
|
|
|
|
#endif // __linux__
|