[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / libsanitizer / asan / asan_malloc_linux.cc
blobd03f1bb89c8cbaac4036f4d8b9cd32bcdcaa1966
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 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX
18 #include "sanitizer_common/sanitizer_tls_get_addr.h"
19 #include "asan_allocator.h"
20 #include "asan_interceptors.h"
21 #include "asan_internal.h"
22 #include "asan_stack.h"
24 // ---------------------- Replacement functions ---------------- {{{1
25 using namespace __asan; // NOLINT
27 INTERCEPTOR(void, free, void *ptr) {
28 GET_STACK_TRACE_FREE;
29 asan_free(ptr, &stack, FROM_MALLOC);
32 INTERCEPTOR(void, cfree, void *ptr) {
33 GET_STACK_TRACE_FREE;
34 asan_free(ptr, &stack, FROM_MALLOC);
37 INTERCEPTOR(void*, malloc, uptr size) {
38 GET_STACK_TRACE_MALLOC;
39 return asan_malloc(size, &stack);
42 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
43 if (UNLIKELY(!asan_inited)) {
44 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
45 const uptr kCallocPoolSize = 1024;
46 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
47 static uptr allocated;
48 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
49 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
50 allocated += size_in_words;
51 CHECK(allocated < kCallocPoolSize);
52 return mem;
54 GET_STACK_TRACE_MALLOC;
55 return asan_calloc(nmemb, size, &stack);
58 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
59 GET_STACK_TRACE_MALLOC;
60 return asan_realloc(ptr, size, &stack);
63 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
64 GET_STACK_TRACE_MALLOC;
65 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
68 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
69 GET_STACK_TRACE_MALLOC;
70 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
73 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
74 GET_STACK_TRACE_MALLOC;
75 void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
76 DTLS_on_libc_memalign(res, size * boundary);
77 return res;
80 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
81 GET_CURRENT_PC_BP_SP;
82 (void)sp;
83 return asan_malloc_usable_size(ptr, pc, bp);
86 // We avoid including malloc.h for portability reasons.
87 // man mallinfo says the fields are "long", but the implementation uses int.
88 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
89 // is not called.
90 struct fake_mallinfo {
91 int x[10];
94 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
95 struct fake_mallinfo res;
96 REAL(memset)(&res, 0, sizeof(res));
97 return res;
100 INTERCEPTOR(int, mallopt, int cmd, int value) {
101 return -1;
104 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
105 GET_STACK_TRACE_MALLOC;
106 // Printf("posix_memalign: %zx %zu\n", alignment, size);
107 return asan_posix_memalign(memptr, alignment, size, &stack);
110 INTERCEPTOR(void*, valloc, uptr size) {
111 GET_STACK_TRACE_MALLOC;
112 return asan_valloc(size, &stack);
115 INTERCEPTOR(void*, pvalloc, uptr size) {
116 GET_STACK_TRACE_MALLOC;
117 return asan_pvalloc(size, &stack);
120 INTERCEPTOR(void, malloc_stats, void) {
121 __asan_print_accumulated_stats();
124 #if SANITIZER_ANDROID
125 // Format of __libc_malloc_dispatch has changed in Android L.
126 // While we are moving towards a solution that does not depend on bionic
127 // internals, here is something to support both K* and L releases.
128 struct MallocDebugK {
129 void *(*malloc)(uptr bytes);
130 void (*free)(void *mem);
131 void *(*calloc)(uptr n_elements, uptr elem_size);
132 void *(*realloc)(void *oldMem, uptr bytes);
133 void *(*memalign)(uptr alignment, uptr bytes);
134 uptr (*malloc_usable_size)(void *mem);
137 struct MallocDebugL {
138 void *(*calloc)(uptr n_elements, uptr elem_size);
139 void (*free)(void *mem);
140 fake_mallinfo (*mallinfo)(void);
141 void *(*malloc)(uptr bytes);
142 uptr (*malloc_usable_size)(void *mem);
143 void *(*memalign)(uptr alignment, uptr bytes);
144 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
145 void* (*pvalloc)(uptr size);
146 void *(*realloc)(void *oldMem, uptr bytes);
147 void* (*valloc)(uptr size);
150 ALIGNED(32) const MallocDebugK asan_malloc_dispatch_k = {
151 WRAP(malloc), WRAP(free), WRAP(calloc),
152 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
154 ALIGNED(32) const MallocDebugL asan_malloc_dispatch_l = {
155 WRAP(calloc), WRAP(free), WRAP(mallinfo),
156 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
157 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
158 WRAP(valloc)};
160 namespace __asan {
161 void ReplaceSystemMalloc() {
162 void **__libc_malloc_dispatch_p =
163 (void **)AsanDlSymNext("__libc_malloc_dispatch");
164 if (__libc_malloc_dispatch_p) {
165 // Decide on K vs L dispatch format by the presence of
166 // __libc_malloc_default_dispatch export in libc.
167 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
168 if (default_dispatch_p)
169 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
170 else
171 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
174 } // namespace __asan
176 #else // SANITIZER_ANDROID
178 namespace __asan {
179 void ReplaceSystemMalloc() {
181 } // namespace __asan
182 #endif // SANITIZER_ANDROID
184 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX