* configure.ac: Change target-libasan to target-libsanitizer.
[official-gcc.git] / libsanitizer / asan / asan_malloc_linux.cc
blobb00bbe5deca13843aa68d79635c24a61ca637209
1 //===-- asan_malloc_linux.cc ----------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Linux-specific malloc interception.
11 // We simply define functions like malloc, free, realloc, etc.
12 // They will replace the corresponding libc functions automagically.
13 //===----------------------------------------------------------------------===//
14 #ifdef __linux__
16 #include "asan_allocator.h"
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_stack.h"
21 #if ASAN_ANDROID
22 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size)
23 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
24 DECLARE_REAL_AND_INTERCEPTOR(void*, calloc, uptr nmemb, uptr size)
25 DECLARE_REAL_AND_INTERCEPTOR(void*, realloc, void *ptr, uptr size)
26 DECLARE_REAL_AND_INTERCEPTOR(void*, memalign, uptr boundary, uptr size)
28 struct MallocDebug {
29 void* (*malloc)(uptr bytes);
30 void (*free)(void* mem);
31 void* (*calloc)(uptr n_elements, uptr elem_size);
32 void* (*realloc)(void* oldMem, uptr bytes);
33 void* (*memalign)(uptr alignment, uptr bytes);
36 const MallocDebug asan_malloc_dispatch ALIGNED(32) = {
37 WRAP(malloc), WRAP(free), WRAP(calloc), WRAP(realloc), WRAP(memalign)
40 extern "C" const MallocDebug* __libc_malloc_dispatch;
42 namespace __asan {
43 void ReplaceSystemMalloc() {
44 __libc_malloc_dispatch = &asan_malloc_dispatch;
46 } // namespace __asan
48 #else // ANDROID
50 namespace __asan {
51 void ReplaceSystemMalloc() {
53 } // namespace __asan
54 #endif // ANDROID
56 // ---------------------- Replacement functions ---------------- {{{1
57 using namespace __asan; // NOLINT
59 INTERCEPTOR(void, free, void *ptr) {
60 GET_STACK_TRACE_HERE_FOR_FREE(ptr);
61 asan_free(ptr, &stack);
64 INTERCEPTOR(void, cfree, void *ptr) {
65 GET_STACK_TRACE_HERE_FOR_FREE(ptr);
66 asan_free(ptr, &stack);
69 INTERCEPTOR(void*, malloc, uptr size) {
70 GET_STACK_TRACE_HERE_FOR_MALLOC;
71 return asan_malloc(size, &stack);
74 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
75 if (!asan_inited) {
76 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
77 const uptr kCallocPoolSize = 1024;
78 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
79 static uptr allocated;
80 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
81 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
82 allocated += size_in_words;
83 CHECK(allocated < kCallocPoolSize);
84 return mem;
86 GET_STACK_TRACE_HERE_FOR_MALLOC;
87 return asan_calloc(nmemb, size, &stack);
90 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
91 GET_STACK_TRACE_HERE_FOR_MALLOC;
92 return asan_realloc(ptr, size, &stack);
95 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
96 GET_STACK_TRACE_HERE_FOR_MALLOC;
97 return asan_memalign(boundary, size, &stack);
100 INTERCEPTOR(void*, __libc_memalign, uptr align, uptr s)
101 ALIAS("memalign");
103 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
104 GET_STACK_TRACE_HERE_FOR_MALLOC;
105 return asan_malloc_usable_size(ptr, &stack);
108 // We avoid including malloc.h for portability reasons.
109 // man mallinfo says the fields are "long", but the implementation uses int.
110 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
111 // is not called.
112 struct fake_mallinfo {
113 int x[10];
116 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
117 struct fake_mallinfo res;
118 REAL(memset)(&res, 0, sizeof(res));
119 return res;
122 INTERCEPTOR(int, mallopt, int cmd, int value) {
123 return -1;
126 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
127 GET_STACK_TRACE_HERE_FOR_MALLOC;
128 // Printf("posix_memalign: %zx %zu\n", alignment, size);
129 return asan_posix_memalign(memptr, alignment, size, &stack);
132 INTERCEPTOR(void*, valloc, uptr size) {
133 GET_STACK_TRACE_HERE_FOR_MALLOC;
134 return asan_valloc(size, &stack);
137 INTERCEPTOR(void*, pvalloc, uptr size) {
138 GET_STACK_TRACE_HERE_FOR_MALLOC;
139 return asan_pvalloc(size, &stack);
142 #endif // __linux__