1//===-- asan_malloc_linux.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of AddressSanitizer, an address sanity checker.
10//
11// Linux-specific malloc interception.
12// We simply define functions like malloc, free, realloc, etc.
13// They will replace the corresponding libc functions automagically.
14//===----------------------------------------------------------------------===//
15
16#include "sanitizer_common/sanitizer_platform.h"
17#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \
18 SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU || SANITIZER_AIX
19
20# include "asan_allocator.h"
21# include "asan_interceptors.h"
22# include "asan_internal.h"
23# include "asan_stack.h"
24# include "lsan/lsan_common.h"
25# include "sanitizer_common/sanitizer_allocator_checks.h"
26# include "sanitizer_common/sanitizer_allocator_dlsym.h"
27# include "sanitizer_common/sanitizer_errno.h"
28
29// ---------------------- Replacement functions ---------------- {{{1
30using namespace __asan;
31
32struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
33 static bool UseImpl() { return !TryAsanInitFromRtl(); }
34 static void OnAllocate(const void *ptr, uptr size) {
35# if CAN_SANITIZE_LEAKS
36 // Suppress leaks from dlerror(). Previously dlsym hack on global array was
37 // used by leak sanitizer as a root region.
38 __lsan_register_root_region(p: ptr, size);
39# endif
40 }
41 static void OnFree(const void *ptr, uptr size) {
42# if CAN_SANITIZE_LEAKS
43 __lsan_unregister_root_region(p: ptr, size);
44# endif
45 }
46};
47
48INTERCEPTOR(void, free, void *ptr) {
49 if (DlsymAlloc::PointerIsMine(ptr))
50 return DlsymAlloc::Free(ptr);
51 GET_STACK_TRACE_FREE;
52 asan_free(ptr, stack: &stack);
53}
54
55#if SANITIZER_INTERCEPT_CFREE
56INTERCEPTOR(void, cfree, void *ptr) {
57 if (DlsymAlloc::PointerIsMine(ptr))
58 return DlsymAlloc::Free(ptr);
59 GET_STACK_TRACE_FREE;
60 asan_free(ptr, stack: &stack);
61}
62#endif // SANITIZER_INTERCEPT_CFREE
63
64# if SANITIZER_AIX
65// Unlike malloc, vec_malloc must return memory aligned to 16 bytes.
66INTERCEPTOR(void*, vec_malloc, uptr size) {
67 if (DlsymAlloc::Use())
68 return DlsymAlloc::Allocate(size, 16);
69 GET_STACK_TRACE_MALLOC;
70 return asan_vec_malloc(size, &stack);
71}
72
73// Unlike calloc, vec_calloc must return memory aligned to 16 bytes.
74INTERCEPTOR(void*, vec_calloc, uptr nmemb, uptr size) {
75 if (DlsymAlloc::Use())
76 return DlsymAlloc::Callocate(nmemb, size, 16);
77 GET_STACK_TRACE_MALLOC;
78 return asan_vec_calloc(nmemb, size, &stack);
79}
80# endif
81
82// TODO: Fix malloc/calloc interceptors to return 16-byte alignment with AIX on
83// PASE.
84INTERCEPTOR(void*, malloc, uptr size) {
85 if (DlsymAlloc::Use())
86 return DlsymAlloc::Allocate(size_in_bytes: size);
87 GET_STACK_TRACE_MALLOC;
88 return asan_malloc(size, stack: &stack);
89}
90
91INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
92 if (DlsymAlloc::Use())
93 return DlsymAlloc::Callocate(nmemb, size);
94 GET_STACK_TRACE_MALLOC;
95 return asan_calloc(nmemb, size, stack: &stack);
96}
97
98// TODO: AIX needs a method to ensure 16-byte alignment if the incoming
99// pointer was allocated with a 16-byte alignment requirement (or perhaps
100// merely if it happens to have 16-byte alignment).
101INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
102 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
103 return DlsymAlloc::Realloc(ptr, new_size: size);
104 GET_STACK_TRACE_MALLOC;
105 return asan_realloc(p: ptr, size, stack: &stack);
106}
107
108#if SANITIZER_INTERCEPT_REALLOCARRAY
109INTERCEPTOR(void*, reallocarray, void *ptr, uptr nmemb, uptr size) {
110 AsanInitFromRtl();
111 GET_STACK_TRACE_MALLOC;
112 return asan_reallocarray(p: ptr, nmemb, size, stack: &stack);
113}
114#endif // SANITIZER_INTERCEPT_REALLOCARRAY
115
116#if SANITIZER_INTERCEPT_MEMALIGN
117INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
118 GET_STACK_TRACE_MALLOC;
119 return asan_memalign(alignment: boundary, size, stack: &stack);
120}
121
122INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
123 GET_STACK_TRACE_MALLOC;
124 return asan_memalign(alignment: boundary, size, stack: &stack);
125}
126#endif // SANITIZER_INTERCEPT_MEMALIGN
127
128#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
129INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
130 GET_STACK_TRACE_MALLOC;
131 return asan_aligned_alloc(alignment: boundary, size, stack: &stack);
132}
133#endif // SANITIZER_INTERCEPT_ALIGNED_ALLOC
134
135INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
136 GET_CURRENT_PC_BP_SP;
137 (void)sp;
138 return asan_malloc_usable_size(ptr, pc, bp);
139}
140
141#if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
142// We avoid including malloc.h for portability reasons.
143// man mallinfo says the fields are "long", but the implementation uses int.
144// It doesn't matter much -- we just need to make sure that the libc's mallinfo
145// is not called.
146struct fake_mallinfo {
147 int x[10];
148};
149
150INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
151 struct fake_mallinfo res;
152 REAL(memset)(&res, 0, sizeof(res));
153 return res;
154}
155
156INTERCEPTOR(int, mallopt, int cmd, int value) {
157 return 0;
158}
159#endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
160
161INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
162 GET_STACK_TRACE_MALLOC;
163 return asan_posix_memalign(memptr, alignment, size, stack: &stack);
164}
165
166INTERCEPTOR(void*, valloc, uptr size) {
167 GET_STACK_TRACE_MALLOC;
168 return asan_valloc(size, stack: &stack);
169}
170
171#if SANITIZER_INTERCEPT_PVALLOC
172INTERCEPTOR(void*, pvalloc, uptr size) {
173 GET_STACK_TRACE_MALLOC;
174 return asan_pvalloc(size, stack: &stack);
175}
176#endif // SANITIZER_INTERCEPT_PVALLOC
177
178INTERCEPTOR(void, malloc_stats, void) {
179 __asan_print_accumulated_stats();
180}
181
182#if SANITIZER_ANDROID
183// Format of __libc_malloc_dispatch has changed in Android L.
184// While we are moving towards a solution that does not depend on bionic
185// internals, here is something to support both K* and L releases.
186struct MallocDebugK {
187 void *(*malloc)(uptr bytes);
188 void (*free)(void *mem);
189 void *(*calloc)(uptr n_elements, uptr elem_size);
190 void *(*realloc)(void *oldMem, uptr bytes);
191 void *(*memalign)(uptr alignment, uptr bytes);
192 uptr (*malloc_usable_size)(void *mem);
193};
194
195struct MallocDebugL {
196 void *(*calloc)(uptr n_elements, uptr elem_size);
197 void (*free)(void *mem);
198 fake_mallinfo (*mallinfo)(void);
199 void *(*malloc)(uptr bytes);
200 uptr (*malloc_usable_size)(void *mem);
201 void *(*memalign)(uptr alignment, uptr bytes);
202 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
203 void* (*pvalloc)(uptr size);
204 void *(*realloc)(void *oldMem, uptr bytes);
205 void* (*valloc)(uptr size);
206};
207
208alignas(32) const MallocDebugK asan_malloc_dispatch_k = {
209 WRAP(malloc), WRAP(free), WRAP(calloc),
210 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
211
212alignas(32) const MallocDebugL asan_malloc_dispatch_l = {
213 WRAP(calloc), WRAP(free), WRAP(mallinfo),
214 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
215 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
216 WRAP(valloc)};
217
218namespace __asan {
219void ReplaceSystemMalloc() {
220 void **__libc_malloc_dispatch_p =
221 (void **)AsanDlSymNext("__libc_malloc_dispatch");
222 if (__libc_malloc_dispatch_p) {
223 // Decide on K vs L dispatch format by the presence of
224 // __libc_malloc_default_dispatch export in libc.
225 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
226 if (default_dispatch_p)
227 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
228 else
229 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
230 }
231}
232} // namespace __asan
233
234#else // SANITIZER_ANDROID
235
236namespace __asan {
237void ReplaceSystemMalloc() {
238}
239} // namespace __asan
240#endif // SANITIZER_ANDROID
241
242#endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||
243 // SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU
244